CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CredentialStore.cc
Go to the documentation of this file.
6 //
7 #include "CoralBase/AttributeList.h"
8 #include "CoralBase/Attribute.h"
9 #include "CoralBase/MessageStream.h"
10 #include "CoralKernel/Context.h"
11 #include "CoralCommon/URIParser.h"
12 #include "RelationalAccess/AuthenticationCredentials.h"
13 #include "RelationalAccess/IBulkOperation.h"
14 #include "RelationalAccess/IConnection.h"
15 #include "RelationalAccess/ISession.h"
16 #include "RelationalAccess/IRelationalService.h"
17 #include "RelationalAccess/IRelationalDomain.h"
18 #include "RelationalAccess/ITransaction.h"
19 #include "RelationalAccess/ISchema.h"
20 #include "RelationalAccess/ITable.h"
21 #include "RelationalAccess/TableDescription.h"
22 #include "RelationalAccess/ITableDataEditor.h"
23 #include "RelationalAccess/IQuery.h"
24 #include "RelationalAccess/ICursor.h"
25 //
26 #include "RelationalAccess/AuthenticationCredentials.h"
27 //
28 #include <sstream>
29 #include <fstream>
30 #include <boost/filesystem.hpp>
31 
32 static const std::string serviceName = "CondAuthenticationService";
33 
35  m_data(){
36 }
37 
39  reset();
40 }
41 
43  for ( std::map< std::pair<std::string,std::string>, coral::AuthenticationCredentials* >::iterator iData = m_data.begin();
44  iData != m_data.end(); ++iData )
45  delete iData->second;
46  m_data.clear();
47 }
48 
50  const std::string& itemName,
51  const std::string& itemValue ){
52  registerItem( connectionString, cond::Auth::COND_DEFAULT_ROLE, itemName, itemValue );
53 }
54 
55 
57  const std::string& role,
58  const std::string& itemName,
59  const std::string& itemValue ){
60  std::pair<std::string,std::string> connKey( connectionString, role );
61  std::map< std::pair<std::string,std::string>, coral::AuthenticationCredentials* >::iterator iData = m_data.find( connKey );
62  if( iData == m_data.end() ){
63  iData = m_data.insert( std::make_pair( connKey, new coral::AuthenticationCredentials( serviceName ) ) ).first;
64  }
65  iData = m_data.insert( std::make_pair( connKey, new coral::AuthenticationCredentials( serviceName ) ) ).first;
66  iData->second->registerItem( itemName, itemValue );
67 }
68 
69 void
71  const std::string& userName,
72  const std::string& password ){
73  registerCredentials( connectionString, cond::Auth::COND_DEFAULT_ROLE, userName, password );
74 }
75 
76 void
78  const std::string& role,
79  const std::string& userName,
80  const std::string& password ){
81  std::pair<std::string,std::string> connKey( connectionString, role );
82  std::map< std::pair<std::string,std::string>, coral::AuthenticationCredentials* >::iterator iData = m_data.find( connKey );
83  if( iData != m_data.end() ){
84  delete iData->second;
85  m_data.erase( connKey );
86  }
87  iData = m_data.insert( std::make_pair( connKey, new coral::AuthenticationCredentials( serviceName ) ) ).first;
88  iData->second->registerItem( coral::IAuthenticationCredentials::userItem(), userName );
89  iData->second->registerItem( coral::IAuthenticationCredentials::passwordItem(), password );
90 }
91 
93  for ( std::map< std::pair<std::string,std::string>, coral::AuthenticationCredentials* >::const_iterator iData = data.m_data.begin();
94  iData != data.m_data.end(); ++iData ){
95  registerCredentials( iData->first.first, iData->first.second, iData->second->valueForItem( coral::IAuthenticationCredentials::userItem() ),
96  iData->second->valueForItem( coral::IAuthenticationCredentials::passwordItem() ) );
97  }
98 }
99 
100 const coral::IAuthenticationCredentials*
102 {
104 }
105 
106 const coral::IAuthenticationCredentials*
108 {
109  const coral::IAuthenticationCredentials* ret = 0;
110  std::pair<std::string,std::string> connKey( connectionString, role );
111  std::map< std::pair<std::string,std::string>, coral::AuthenticationCredentials* >::const_iterator iData = m_data.find( connKey );
112  if ( iData != m_data.end() ){
113  ret = iData->second;
114  }
115  return ret;
116 }
117 
118 const std::map< std::pair<std::string,std::string>, coral::AuthenticationCredentials* >& coral_bridge::AuthenticationCredentialSet::data() const {
119  return m_data;
120 }
121 
122 static const std::string SEQUENCE_TABLE_NAME("COND_CREDENTIAL_SEQUENCE");
123 
124 static const std::string COND_AUTHENTICATION_TABLE("COND_AUTHENTICATION");
125 static const std::string PRINCIPAL_ID_COL("P_ID");
126 static const std::string PRINCIPAL_NAME_COL("P_NAME");
127 static const std::string VERIFICATION_COL("CRED0");
128 static const std::string PRINCIPAL_KEY_COL("CRED1");
129 static const std::string ADMIN_KEY_COL("CRED2");
130 
131 static const std::string COND_AUTHORIZATION_TABLE("COND_AUTHORIZATION");
132 static const std::string AUTH_ID_COL("AUTH_ID");
133 static const std::string P_ID_COL("P_ID");
134 static const std::string ROLE_COL("C_ROLE");
135 static const std::string SCHEMA_COL("C_SCHEMA");
136 static const std::string AUTH_KEY_COL("CRED3");
137 static const std::string C_ID_COL("C_ID");
138 
139 static const std::string COND_CREDENTIAL_TABLE("COND_CREDENTIAL");
140 static const std::string CONNECTION_ID_COL("CONN_ID");
141 static const std::string CONNECTION_LABEL_COL("CONN_LABEL");
142 static const std::string USERNAME_COL("CRED4");
143 static const std::string PASSWORD_COL("CRED5");
144 static const std::string VERIFICATION_KEY_COL("CRED6");
145 static const std::string CONNECTION_KEY_COL("CRED7");
146 
147 const std::string DEFAULT_DATA_SOURCE("Cond_Default_Authentication");
148 
149 namespace cond {
150 
165  const std::string& userName ){
166  std::string ret = userName;
167  if( !serviceName.empty() ){
168  ret += "@"+serviceName;
169  }
170  return ret;
171  }
173  coral::URIParser parser;
174  parser.setURI( connectionString );
175  std::string serviceName = parser.hostName();
176  std::string schemaName = parser.databaseOrSchemaName();
177  return schemaLabel( serviceName, schemaName );
178  }
179 
181  public:
183  m_store( store ){}
185  m_store.closeSession( false );
186  }
187  void startSuper( const std::string& connectionString, const std::string& userName, const std::string& password ){
188  m_store.startSuperSession( connectionString, userName, password );
189  }
190  void start( bool readOnly=true ){
191  m_store.startSession( readOnly );
192  }
193  void close(){
194  m_store.closeSession();
195  }
196 
197  private:
199  };
200 
201  struct PrincipalData {
202  int id;
207  id(-1),
208  verifKey(""),
209  principalKey(""),
210  adminKey(""){}
211  };
212 
214  std::auto_ptr<coral::IQuery> query(schema.tableHandle(COND_AUTHENTICATION_TABLE).newQuery());
215  coral::AttributeList readBuff;
216  readBuff.extend<int>(PRINCIPAL_ID_COL);
217  readBuff.extend<std::string>(VERIFICATION_COL);
218  readBuff.extend<std::string>(PRINCIPAL_KEY_COL);
219  readBuff.extend<std::string>(ADMIN_KEY_COL);
220  coral::AttributeList whereData;
221  whereData.extend<std::string>(PRINCIPAL_NAME_COL);
222  whereData[ PRINCIPAL_NAME_COL ].data<std::string>() = principal;
223  std::string whereClause = PRINCIPAL_NAME_COL + " = :" + PRINCIPAL_NAME_COL;
224  query->defineOutput(readBuff);
225  query->addToOutputList( PRINCIPAL_ID_COL );
226  query->addToOutputList( VERIFICATION_COL );
227  query->addToOutputList( PRINCIPAL_KEY_COL );
228  query->addToOutputList( ADMIN_KEY_COL );
229  query->setCondition( whereClause, whereData );
230  coral::ICursor& cursor = query->execute();
231  bool found = false;
232  if ( cursor.next() ) {
233  found = true;
234  const coral::AttributeList& row = cursor.currentRow();
235  destination.id = row[ PRINCIPAL_ID_COL ].data<int>();
236  destination.verifKey = row[ VERIFICATION_COL ].data<std::string>();
237  destination.principalKey = row[ PRINCIPAL_KEY_COL ].data<std::string>();
238  destination.adminKey = row[ ADMIN_KEY_COL ].data<std::string>();
239  }
240  return found;
241  }
242 
243  struct CredentialData {
244  int id;
250  id(-1),
251  userName(""),
252  password(""),
253  connectionKey(""){
254  }
255  };
256 
257  bool selectConnection( coral::ISchema& schema, const std::string& connectionLabel, CredentialData& destination ){
258 
259  std::auto_ptr<coral::IQuery> query(schema.tableHandle(COND_CREDENTIAL_TABLE).newQuery());
260  coral::AttributeList readBuff;
261  readBuff.extend<int>( CONNECTION_ID_COL );
262  readBuff.extend<std::string>( USERNAME_COL );
263  readBuff.extend<std::string>( PASSWORD_COL );
264  readBuff.extend<std::string>( VERIFICATION_KEY_COL );
265  readBuff.extend<std::string>( CONNECTION_KEY_COL );
266  coral::AttributeList whereData;
267  whereData.extend<std::string>( CONNECTION_LABEL_COL );
268  whereData[ CONNECTION_LABEL_COL ].data<std::string>() = connectionLabel;
269  std::string whereClause = CONNECTION_LABEL_COL + " = :" + CONNECTION_LABEL_COL;
270  query->defineOutput(readBuff);
271  query->addToOutputList( CONNECTION_ID_COL );
272  query->addToOutputList( USERNAME_COL );
273  query->addToOutputList( PASSWORD_COL );
274  query->addToOutputList( VERIFICATION_KEY_COL );
275  query->addToOutputList( CONNECTION_KEY_COL );
276  query->setCondition( whereClause, whereData );
277  coral::ICursor& cursor = query->execute();
278  bool found = false;
279  if ( cursor.next() ) {
280  const coral::AttributeList& row = cursor.currentRow();
281  destination.id = row[ CONNECTION_ID_COL].data<int>();
282  destination.userName = row[ USERNAME_COL].data<std::string>();
283  destination.password = row[ PASSWORD_COL].data<std::string>();
284  destination.verificationKey = row[ VERIFICATION_KEY_COL].data<std::string>();
285  destination.connectionKey = row[ CONNECTION_KEY_COL].data<std::string>();
286  found = true;
287  }
288  return found;
289  }
290 
292  int id;
296  id(-1),
297  connectionId(-1),
298  key(""){}
299  };
300 
301  bool selectAuthorization( coral::ISchema& schema, int principalId, const std::string& role, const std::string& connectionString, AuthorizationData& destination ){
302  std::auto_ptr<coral::IQuery> query(schema.tableHandle(COND_AUTHORIZATION_TABLE).newQuery());
303  coral::AttributeList readBuff;
304  readBuff.extend<int>(AUTH_ID_COL);
305  readBuff.extend<int>(C_ID_COL);
306  readBuff.extend<std::string>(AUTH_KEY_COL);
307  coral::AttributeList whereData;
308  whereData.extend<int>(P_ID_COL);
309  whereData.extend<std::string>(ROLE_COL);
310  whereData.extend<std::string>(SCHEMA_COL);
311  whereData[ P_ID_COL ].data<int>() = principalId;
312  whereData[ ROLE_COL ].data<std::string>() = role;
313  whereData[ SCHEMA_COL ].data<std::string>() = connectionString;
314  std::stringstream whereClause;
315  whereClause << P_ID_COL << " = :"<<P_ID_COL;
316  whereClause << " AND " << ROLE_COL << " = :"<<ROLE_COL;
317  whereClause << " AND " << SCHEMA_COL << " = :"<<SCHEMA_COL;
318  query->defineOutput(readBuff);
319  query->addToOutputList( AUTH_ID_COL );
320  query->addToOutputList( C_ID_COL );
321  query->addToOutputList( AUTH_KEY_COL );
322  query->setCondition( whereClause.str(), whereData );
323  coral::ICursor& cursor = query->execute();
324  bool found = false;
325  if ( cursor.next() ) {
326  const coral::AttributeList& row = cursor.currentRow();
327  destination.id = row[AUTH_ID_COL].data<int>();
328  destination.connectionId = row[C_ID_COL].data<int>();
329  destination.key = row[AUTH_KEY_COL].data<std::string>();
330  found = true;
331  }
332  return found;
333  }
334 
335 }
336 
338 
339  if( m_session.get() ){
340  if(m_session->transaction().isActive()){
341  if( commit ){
342  m_session->transaction().commit();
343  } else {
344  m_session->transaction().rollback();
345  }
346  }
347  m_session->endUserSession();
348  }
349  m_session.reset();
350  if( m_connection.get() ){
351  m_connection->disconnect();
352  }
353  m_connection.reset();
354 }
355 
356 int cond::CredentialStore::addUser( const std::string& principalName, const std::string& authenticationKey, const std::string& principalKey, const std::string& adminKey ){
357 
358  coral::ISchema& schema = m_session->nominalSchema();
359  coral::ITableDataEditor& editor0 = schema.tableHandle(COND_AUTHENTICATION_TABLE).dataEditor();
360 
361  ora::SequenceManager sequenceMgr( SEQUENCE_TABLE_NAME,schema );
362  int principalId = sequenceMgr.getNextId( COND_AUTHENTICATION_TABLE, true );
363 
364  Cipher cipher0( authenticationKey );
365  Cipher cipher1( adminKey );
366 
367  coral::AttributeList authData;
368  editor0.rowBuffer(authData);
369  authData[ PRINCIPAL_ID_COL ].data<int>() = principalId;
370  authData[ PRINCIPAL_NAME_COL ].data<std::string>() = principalName;
371  authData[ VERIFICATION_COL ].data<std::string>() = cipher0.b64encrypt( principalName );
372  authData[ PRINCIPAL_KEY_COL ].data<std::string>() = cipher0.b64encrypt( principalKey );
373  authData[ ADMIN_KEY_COL ].data<std::string>() = cipher1.b64encrypt( principalKey );
374  editor0.insertRow( authData );
375  return principalId;
376 }
377 
378 std::pair<std::string,std::string> cond::CredentialStore::openConnection( const std::string& connectionString ){
379  coral::IHandle<coral::IRelationalService> relationalService = coral::Context::instance().query<coral::IRelationalService>();
380  if ( ! relationalService.isValid() ){
381  coral::Context::instance().loadComponent("CORAL/Services/RelationalService");
382  relationalService = coral::Context::instance().query<coral::IRelationalService>();
383  }
384  coral::IRelationalDomain& domain = relationalService->domainForConnection( connectionString );
385  std::pair<std::string,std::string> connTokens = domain.decodeUserConnectionString( connectionString );
386  m_connection.reset( domain.newConnection( connTokens.first ) );
387  m_connection->connect();
388  return connTokens;
389 }
390 
391 void cond::CredentialStore::openSession( const std::string& schemaName, const std::string& userName, const std::string& password, bool readMode ){
392  coral::AccessMode accessMode = coral::ReadOnly;
393  if( !readMode ) accessMode = coral::Update;
394  m_session.reset( m_connection->newSession( schemaName, accessMode) );
395  m_session->startUserSession( userName, password );
396  // open read-only transaction
397  m_session->transaction().start( readMode );
398 }
399 
401  std::pair<std::string,std::string> connTokens = openConnection( connectionString );
402  openSession( connTokens.second, userName, password, false );
403 }
404 
405 // open session on the storage
407  if(!m_serviceData){
408  throwException( "The credential store has not been initialized.","cond::CredentialStore::openConnection" );
409  }
410  const std::string& storeConnectionString = m_serviceData->connectionString;
411 
412  std::pair<std::string,std::string> connTokens = openConnection( storeConnectionString );
413 
414  const std::string& userName = m_serviceData->userName;
415  const std::string& password = m_serviceData->password;
416 
417  openSession( connTokens.second, userName, password, true );
418 
419  coral::ISchema& schema = m_session->nominalSchema();
420  if(!schema.existsTable(COND_AUTHENTICATION_TABLE) ||
421  !schema.existsTable(COND_AUTHORIZATION_TABLE) ||
422  !schema.existsTable(COND_CREDENTIAL_TABLE) ){
423  throwException("Credential database does not exists in \""+storeConnectionString+"\"","CredentialStore::startSession");
424  }
425 
426  const std::string& principalName = m_key.principalName();
427  // now authenticate...
428  PrincipalData princData;
429  if( !selectPrincipal( m_session->nominalSchema(), principalName, princData ) ){
430  throwException( "Invalid credentials provided.(0)",
431  "CredentialStore::openSession");
432  }
433  Cipher cipher0( m_key.principalKey() );
434  std::string verifStr = cipher0.b64decrypt( princData.verifKey );
435  if( verifStr != principalName ){
436  throwException( "Invalid credentials provided (1)",
437  "CredentialStore::openSession");
438  }
439  // ok, authenticated!
440  m_principalId = princData.id;
441  m_principalKey = cipher0.b64decrypt( princData.principalKey );
442 
443  if(!readMode ) {
444 
445  Cipher cipher0( m_principalKey );
446  std::string adminKey = cipher0.b64decrypt( princData.adminKey );
447  if( adminKey != m_principalKey ){
448  // not admin user!
449  throwException( "Provided credentials does not allow admin operation.",
450  "CredentialStore::openSession");
451  }
452 
453  // first find the credentials for WRITING in the security tables
454  std::auto_ptr<coral::IQuery> query(schema.newQuery());
455  query->addToTableList(COND_AUTHORIZATION_TABLE, "AUTHO");
456  query->addToTableList(COND_CREDENTIAL_TABLE, "CREDS");
457  coral::AttributeList readBuff;
458  readBuff.extend<std::string>("CREDS."+CONNECTION_LABEL_COL);
459  readBuff.extend<std::string>("CREDS."+CONNECTION_KEY_COL);
460  readBuff.extend<std::string>("CREDS."+USERNAME_COL);
461  readBuff.extend<std::string>("CREDS."+PASSWORD_COL);
462  readBuff.extend<std::string>("CREDS."+VERIFICATION_KEY_COL);
463  coral::AttributeList whereData;
464  whereData.extend<int>(P_ID_COL);
465  whereData.extend<std::string>(ROLE_COL);
466  whereData.extend<std::string>(SCHEMA_COL);
467  whereData[ P_ID_COL ].data<int>() = m_principalId;
468  whereData[ ROLE_COL ].data<std::string>() = Auth::COND_ADMIN_ROLE;
469  whereData[ SCHEMA_COL ].data<std::string>() = storeConnectionString;
470  std::stringstream whereClause;
471  whereClause << "AUTHO."<< C_ID_COL << " = CREDS."<<CONNECTION_ID_COL;
472  whereClause << " AND AUTHO."<< P_ID_COL << " = :"<<P_ID_COL;
473  whereClause << " AND AUTHO."<< ROLE_COL << " = :"<<ROLE_COL;
474  whereClause << " AND AUTHO."<< SCHEMA_COL << " = :"<<SCHEMA_COL;
475  query->defineOutput(readBuff);
476  query->addToOutputList( "CREDS."+CONNECTION_LABEL_COL );
477  query->addToOutputList( "CREDS."+CONNECTION_KEY_COL );
478  query->addToOutputList( "CREDS."+USERNAME_COL );
479  query->addToOutputList( "CREDS."+PASSWORD_COL );
480  query->addToOutputList( "CREDS."+VERIFICATION_KEY_COL );
481  query->setCondition( whereClause.str(), whereData );
482  coral::ICursor& cursor = query->execute();
483  bool found = false;
484  std::string writeUserName("");
485  std::string writePassword("");
486  if ( cursor.next() ) {
487  const coral::AttributeList& row = cursor.currentRow();
488  const std::string& connLabel = row[ "CREDS."+CONNECTION_LABEL_COL ].data<std::string>();
489  const std::string& encryptedConnectionKey = row[ "CREDS."+CONNECTION_KEY_COL ].data<std::string>();
490  std::string connectionKey = cipher0.b64decrypt( encryptedConnectionKey );
491  Cipher cipher1( connectionKey );
492  const std::string& encryptedUserName = row[ "CREDS."+USERNAME_COL ].data<std::string>();
493  const std::string& encryptedPassword = row[ "CREDS."+PASSWORD_COL ].data<std::string>();
494  const std::string& verificationKey = row[ "CREDS."+VERIFICATION_KEY_COL ].data<std::string>();
495  if( cipher1.b64decrypt( verificationKey ) != connLabel ){
496  throwException( "Could not decrypt credentials.Provided key is invalid.",
497  "CredentialStore::startSession");
498  }
499  writeUserName = cipher1.b64decrypt( encryptedUserName );
500  writePassword = cipher1.b64decrypt( encryptedPassword );
501  found = true;
502  }
503  if( ! found ){
504  throwException( "Provided credentials are invalid for write access.",
505  "CredentialStore::openSession");
506  }
507  m_session->transaction().commit();
508  m_session->endUserSession();
509  openSession( connTokens.second, writeUserName, writePassword, false );
510 
511  }
512 }
513 
514 bool cond::CredentialStore::setPermission( int principalId, const std::string& principalKey, const std::string& role, const std::string& connectionString, int connectionId, const std::string& connectionKey ){
515  coral::ISchema& schema = m_session->nominalSchema();
516  Cipher cipher( principalKey );
517  std::string encryptedConnectionKey = cipher.b64encrypt( connectionKey );
518 
519  AuthorizationData authData;
520  bool found = selectAuthorization( schema, principalId, role, connectionString, authData );
521 
522  coral::ITableDataEditor& editor = schema.tableHandle(COND_AUTHORIZATION_TABLE).dataEditor();
523  if( found ) {
524  coral::AttributeList updateData;
525  updateData.extend<int>( AUTH_ID_COL );
526  updateData.extend<int>( C_ID_COL );
527  updateData.extend<std::string>( AUTH_KEY_COL );
528  updateData[ AUTH_ID_COL ].data<int>() = authData.id;
529  updateData[ C_ID_COL ].data<int>() = connectionId;
530  updateData[ AUTH_KEY_COL ].data<std::string>() = encryptedConnectionKey;
531  std::string setCl = C_ID_COL+" = :"+C_ID_COL + ", "+AUTH_KEY_COL+" = :"+AUTH_KEY_COL;
532  std::string whereCl = AUTH_ID_COL+" = :"+AUTH_ID_COL;
533  editor.updateRows( setCl,whereCl, updateData );
534  } else {
535 
536  ora::SequenceManager sequenceMgr( SEQUENCE_TABLE_NAME,schema );
537  int next = sequenceMgr.getNextId( COND_AUTHORIZATION_TABLE, true );
538 
539  coral::AttributeList insertData;
540  insertData.extend<int>( AUTH_ID_COL );
541  insertData.extend<int>( P_ID_COL );
542  insertData.extend<std::string>( ROLE_COL );
543  insertData.extend<std::string>( SCHEMA_COL );
544  insertData.extend<std::string>( AUTH_KEY_COL );
545  insertData.extend<int>( C_ID_COL );
546  insertData[ AUTH_ID_COL ].data<int>() = next;
547  insertData[ P_ID_COL ].data<int>() = principalId;
548  insertData[ ROLE_COL ].data<std::string>() = role;
549  insertData[ SCHEMA_COL ].data<std::string>() = connectionString;
550  insertData[ AUTH_KEY_COL ].data<std::string>() = encryptedConnectionKey;
551  insertData[ C_ID_COL ].data<int>() = connectionId;
552  editor.insertRow( insertData );
553  }
554  return true;
555 }
556 
557 std::pair<int,std::string> cond::CredentialStore::updateConnection( const std::string& connectionLabel,
558  const std::string& userName,
559  const std::string& password,
560  bool forceUpdate ){
561  coral::ISchema& schema = m_session->nominalSchema();
562  CredentialData credsData;
563  bool found = selectConnection( schema, connectionLabel, credsData );
564  int connId = credsData.id;
565 
566  Cipher adminCipher( m_principalKey );
567  std::string connectionKey("");
568  coral::ITableDataEditor& editor = schema.tableHandle(COND_CREDENTIAL_TABLE).dataEditor();
569  if( found ){
570 
571  connectionKey = adminCipher.b64decrypt( credsData.connectionKey );
572  Cipher cipher( connectionKey );
573  std::string verificationKey = cipher.b64decrypt( credsData.verificationKey );
574  if( verificationKey != connectionLabel ){
575  throwException("Decoding of connection key failed.","CredentialStore::updateConnection");
576  }
577  if( forceUpdate ){
578  std::string encryptedUserName = cipher.b64encrypt( userName );
579  std::string encryptedPassword = cipher.b64encrypt( password );
580 
581  coral::AttributeList updateData;
582  updateData.extend<int>( CONNECTION_ID_COL );
583  updateData.extend<std::string>( USERNAME_COL );
584  updateData.extend<std::string>( PASSWORD_COL );
585  updateData[ CONNECTION_ID_COL ].data<int>() = connId;
586  updateData[ USERNAME_COL ].data<std::string>() = encryptedUserName;
587  updateData[ PASSWORD_COL ].data<std::string>() = encryptedPassword;
588  std::stringstream setCl;
589  setCl << USERNAME_COL << " = :" << USERNAME_COL;
590  setCl <<", " << PASSWORD_COL << " = :" << PASSWORD_COL;
592  editor.updateRows( setCl.str(),whereCl, updateData );
593  }
594  }
595 
596  if(!found){
597 
599  connectionKey = gen.make( Auth::COND_DB_KEY_SIZE );
600  Cipher cipher( connectionKey );
601  std::string encryptedUserName = cipher.b64encrypt( userName );
602  std::string encryptedPassword = cipher.b64encrypt( password );
603  std::string encryptedLabel = cipher.b64encrypt( connectionLabel );
604 
605  ora::SequenceManager sequenceMgr( SEQUENCE_TABLE_NAME,schema );
606  connId = sequenceMgr.getNextId( COND_CREDENTIAL_TABLE, true );
607 
608  coral::AttributeList insertData;
609  insertData.extend<int>( CONNECTION_ID_COL );
610  insertData.extend<std::string>( CONNECTION_LABEL_COL );
611  insertData.extend<std::string>( USERNAME_COL );
612  insertData.extend<std::string>( PASSWORD_COL );
613  insertData.extend<std::string>( VERIFICATION_KEY_COL );
614  insertData.extend<std::string>( CONNECTION_KEY_COL );
615  insertData[ CONNECTION_ID_COL ].data<int>() = connId;
616  insertData[ CONNECTION_LABEL_COL ].data<std::string>() = connectionLabel;
617  insertData[ USERNAME_COL ].data<std::string>() = encryptedUserName;
618  insertData[ PASSWORD_COL ].data<std::string>() = encryptedPassword;
619  insertData[ VERIFICATION_KEY_COL ].data<std::string>() = encryptedLabel;
620  insertData[ CONNECTION_KEY_COL ].data<std::string>() = adminCipher.b64encrypt( connectionKey ) ;;
621  editor.insertRow( insertData );
622 
623  /***
624  // then set the admin permission on the new connection
625  ora::SequenceManager sequenceMgr2( SEQUENCE_TABLE_NAME,schema );
626  int authId = sequenceMgr2.getNextId( COND_AUTHORIZATION_TABLE, true );
627 
628  coral::ITableDataEditor& authEditor = schema.tableHandle(COND_AUTHORIZATION_TABLE).dataEditor();
629  coral::AttributeList authData;
630  authData.extend<int>( AUTH_ID_COL );
631  authData.extend<int>( P_ID_COL );
632  authData.extend<std::string>( ROLE_COL );
633  authData.extend<std::string>( SCHEMA_COL );
634  authData.extend<std::string>( AUTH_KEY_COL );
635  authData.extend<int>( C_ID_COL );
636  authData[ AUTH_ID_COL ].data<int>() = authId;
637  authData[ P_ID_COL ].data<int>() = m_principalId;
638  authData[ ROLE_COL ].data<std::string>() = Auth::COND_ADMIN_ROLE;
639  authData[ SCHEMA_COL ].data<std::string>() = defaultConnectionString( m_serviceData->connectionString, m_serviceName, userName );
640  authData[ AUTH_KEY_COL ].data<std::string>() = adminCipher.b64encrypt( connectionKey ) ;
641  authData[ C_ID_COL ].data<int>() = connId;
642  authEditor.insertRow( authData );
643  **/
644  }
645  return std::make_pair( connId, connectionKey );
646 }
647 
649  m_connection(),
650  m_session(),
651  m_principalId(-1),
652  m_principalKey(""),
653  m_serviceName(""),
654  m_serviceData(0),
655  m_key(){
656 }
657 
659 }
660 
663  const std::string& authPath ){
664  if( serviceName.empty() ){
665  throwException( "Service name has not been provided.","cond::CredentialStore::setUpConnection" );
666  }
667  m_serviceName.clear();
668  m_serviceData = 0;
669 
670  if( authPath.empty() ){
671  throwException( "The authentication Path has not been provided.","cond::CredentialStore::setUpForService" );
672  }
673  boost::filesystem::path fullPath( authPath );
674  if(!boost::filesystem::exists(authPath) || !boost::filesystem::is_directory( authPath )){
675  throwException( "Authentication Path is invalid.","cond::CredentialStore::setUpForService" );
676  }
678  fullPath /= file;
679 
680  m_key.init( fullPath.string(), Auth::COND_KEY );
681 
682  std::map< std::string, ServiceCredentials >::const_iterator iK = m_key.services().find( serviceName );
683  if( iK == m_key.services().end() ){
684  std::string msg("");
685  msg += "Service \""+serviceName+"\" can't be open with the current key.";
686  throwException( msg,"cond::CredentialStore::setUpConnection" );
687  }
688  m_serviceName = serviceName;
689  m_serviceData = &iK->second;
690  return m_serviceData->connectionString;
691 }
692 
695  const std::string& authPath ){
696  coral::IHandle<coral::IRelationalService> relationalService = coral::Context::instance().query<coral::IRelationalService>();
697  if ( ! relationalService.isValid() ){
698  coral::Context::instance().loadComponent("CORAL/Services/RelationalService");
699  relationalService = coral::Context::instance().query<coral::IRelationalService>();
700  }
701  coral::IRelationalDomain& domain = relationalService->domainForConnection( connectionString );
702  std::pair<std::string,std::string> connTokens = domain.decodeUserConnectionString( connectionString );
703  std::string& serviceName = connTokens.first;
704  return setUpForService( serviceName, authPath );
705 }
706 
707 
708 bool
710  CSScopedSession session( *this );
711  session.startSuper( connectionString, userName, password );
712 
713  coral::ISchema& schema = m_session->nominalSchema();
714  if(schema.existsTable(COND_AUTHENTICATION_TABLE)) {
715  throwException("Credential database, already exists.","CredentialStore::create");
716  }
717  ora::SequenceManager sequenceMgr( SEQUENCE_TABLE_NAME, schema );
718  int columnSize = 2000;
719 
720  // authentication table
721  sequenceMgr.create( COND_AUTHENTICATION_TABLE );
722  coral::TableDescription descr0;
723  descr0.setName( COND_AUTHENTICATION_TABLE );
724  descr0.insertColumn( PRINCIPAL_ID_COL, coral::AttributeSpecification::typeNameForType<int>());
725  descr0.insertColumn( PRINCIPAL_NAME_COL, coral::AttributeSpecification::typeNameForType<std::string>(),columnSize,false);
726  descr0.insertColumn( VERIFICATION_COL, coral::AttributeSpecification::typeNameForType<std::string>(),columnSize,false);
727  descr0.insertColumn( PRINCIPAL_KEY_COL, coral::AttributeSpecification::typeNameForType<std::string>(),columnSize,false);
728  descr0.insertColumn( ADMIN_KEY_COL, coral::AttributeSpecification::typeNameForType<std::string>(),columnSize,false);
729  descr0.setNotNullConstraint( PRINCIPAL_ID_COL );
730  descr0.setNotNullConstraint( PRINCIPAL_NAME_COL );
731  descr0.setNotNullConstraint( VERIFICATION_COL );
732  descr0.setNotNullConstraint( PRINCIPAL_KEY_COL );
733  descr0.setNotNullConstraint( ADMIN_KEY_COL );
734  std::vector<std::string> columnsUnique;
735  columnsUnique.push_back( PRINCIPAL_NAME_COL);
736  descr0.setUniqueConstraint( columnsUnique );
737  std::vector<std::string> columnsForIndex;
738  columnsForIndex.push_back(PRINCIPAL_ID_COL);
739  descr0.setPrimaryKey( columnsForIndex );
740  schema.createTable( descr0 );
741 
742  // authorization table
743  sequenceMgr.create( COND_AUTHORIZATION_TABLE );
744  coral::TableDescription descr1;
745  descr1.setName( COND_AUTHORIZATION_TABLE );
746  descr1.insertColumn( AUTH_ID_COL, coral::AttributeSpecification::typeNameForType<int>());
747  descr1.insertColumn( P_ID_COL, coral::AttributeSpecification::typeNameForType<int>());
748  descr1.insertColumn( ROLE_COL, coral::AttributeSpecification::typeNameForType<std::string>(),columnSize,false);
749  descr1.insertColumn( SCHEMA_COL, coral::AttributeSpecification::typeNameForType<std::string>(),columnSize,false);
750  descr1.insertColumn( AUTH_KEY_COL, coral::AttributeSpecification::typeNameForType<std::string>(),columnSize,false);
751  descr1.insertColumn( C_ID_COL, coral::AttributeSpecification::typeNameForType<int>());
752  descr1.setNotNullConstraint( AUTH_ID_COL );
753  descr1.setNotNullConstraint( P_ID_COL );
754  descr1.setNotNullConstraint( ROLE_COL );
755  descr1.setNotNullConstraint( SCHEMA_COL );
756  descr1.setNotNullConstraint( AUTH_KEY_COL );
757  descr1.setNotNullConstraint( C_ID_COL );
758  columnsUnique.clear();
759  columnsUnique.push_back( P_ID_COL);
760  columnsUnique.push_back( ROLE_COL);
761  columnsUnique.push_back( SCHEMA_COL);
762  descr1.setUniqueConstraint( columnsUnique );
763  columnsForIndex.clear();
764  columnsForIndex.push_back(AUTH_ID_COL);
765  descr1.setPrimaryKey( columnsForIndex );
766  schema.createTable( descr1 );
767 
768  // credential table
769  sequenceMgr.create( COND_CREDENTIAL_TABLE );
770  coral::TableDescription descr2;
771  descr2.setName( COND_CREDENTIAL_TABLE );
772  descr2.insertColumn( CONNECTION_ID_COL, coral::AttributeSpecification::typeNameForType<int>());
773  descr2.insertColumn( CONNECTION_LABEL_COL, coral::AttributeSpecification::typeNameForType<std::string>(),columnSize,false);
774  descr2.insertColumn( USERNAME_COL, coral::AttributeSpecification::typeNameForType<std::string>(),columnSize,false);
775  descr2.insertColumn( PASSWORD_COL, coral::AttributeSpecification::typeNameForType<std::string>(),columnSize,false);
776  descr2.insertColumn( VERIFICATION_KEY_COL, coral::AttributeSpecification::typeNameForType<std::string>(),columnSize,false);
777  descr2.insertColumn( CONNECTION_KEY_COL, coral::AttributeSpecification::typeNameForType<std::string>(),columnSize,false);
778  descr2.setNotNullConstraint( CONNECTION_ID_COL );
779  descr2.setNotNullConstraint( CONNECTION_LABEL_COL );
780  descr2.setNotNullConstraint( USERNAME_COL );
781  descr2.setNotNullConstraint( PASSWORD_COL );
782  descr2.setNotNullConstraint( VERIFICATION_KEY_COL );
783  descr2.setNotNullConstraint( CONNECTION_KEY_COL );
784  columnsUnique.clear();
785  columnsUnique.push_back( CONNECTION_LABEL_COL);
786  descr2.setUniqueConstraint( columnsUnique );
787  columnsForIndex.clear();
788  columnsForIndex.push_back(CONNECTION_ID_COL);
789  descr2.setPrimaryKey( columnsForIndex );
790  schema.createTable( descr2 );
791 
792  session.close();
793  return true;
794 }
795 
796 bool
798  CSScopedSession session( *this );
799  session.startSuper( connectionString, userName, password );
800 
801  coral::ISchema& schema = m_session->nominalSchema();
802  schema.dropIfExistsTable( COND_AUTHORIZATION_TABLE );
803  schema.dropIfExistsTable( COND_CREDENTIAL_TABLE );
804  schema.dropIfExistsTable( COND_AUTHENTICATION_TABLE );
805  schema.dropIfExistsTable(SEQUENCE_TABLE_NAME);
806  session.close();
807  return true;
808 }
809 
810 bool cond::CredentialStore::installAdmin( const std::string& userName, const std::string& password ){
811  if(!m_serviceData){
812  throwException( "The credential store has not been initialized.","cond::CredentialStore::installAdmin" );
813  }
814  const std::string& connectionString = m_serviceData->connectionString;
815  const std::string& principalName = m_key.principalName();
816 
817  CSScopedSession session( *this );
818  session.startSuper( connectionString, userName, password );
819 
820  coral::ISchema& schema = m_session->nominalSchema();
821 
822  PrincipalData princData;
823  bool found = selectPrincipal( schema, principalName, princData );
824 
825  if( found ){
826  std::string msg("Principal \"");
827  msg += principalName + "\" has been installed already.";
828  throwException(msg,"CredentialStore::installAdmin");
829  }
830 
832  m_principalKey = gen.make( Auth::COND_DB_KEY_SIZE );
833 
834  coral::ITableDataEditor& editor0 = schema.tableHandle(COND_AUTHENTICATION_TABLE).dataEditor();
835 
836  ora::SequenceManager sequenceMgr( SEQUENCE_TABLE_NAME,schema );
837  int principalId = sequenceMgr.getNextId( COND_AUTHENTICATION_TABLE, true );
838 
839  Cipher cipher0( m_key.principalKey() );
840  Cipher cipher1( m_principalKey );
841 
842  coral::AttributeList authData;
843  editor0.rowBuffer(authData);
844  authData[ PRINCIPAL_ID_COL ].data<int>() = principalId;
845  authData[ PRINCIPAL_NAME_COL ].data<std::string>() = principalName;
846  authData[ VERIFICATION_COL ].data<std::string>() = cipher0.b64encrypt( principalName );
847  authData[ PRINCIPAL_KEY_COL ].data<std::string>() = cipher0.b64encrypt( m_principalKey );
848  authData[ ADMIN_KEY_COL ].data<std::string>() = cipher1.b64encrypt( m_principalKey );
849  editor0.insertRow( authData );
850 
851  std::string connLabel = schemaLabelForCredentialStore( connectionString );
852  DecodingKey tmpKey;
853  std::string connectionKey = gen.make( Auth::COND_DB_KEY_SIZE );
854  std::string encryptedConnectionKey = cipher1.b64encrypt( connectionKey );
855 
856  Cipher cipher2( connectionKey );
857  std::string encryptedUserName = cipher2.b64encrypt( userName );
858  std::string encryptedPassword = cipher2.b64encrypt( password );
859  std::string encryptedLabel = cipher2.b64encrypt( connLabel );
860 
861  int connId = sequenceMgr.getNextId( COND_CREDENTIAL_TABLE, true );
862 
863  coral::ITableDataEditor& editor1 = schema.tableHandle(COND_CREDENTIAL_TABLE).dataEditor();
864  coral::AttributeList connectionData;
865  editor1.rowBuffer(connectionData);
866  connectionData[ CONNECTION_ID_COL ].data<int>() = connId;
867  connectionData[ CONNECTION_LABEL_COL ].data<std::string>() = connLabel;
868  connectionData[ USERNAME_COL ].data<std::string>() = encryptedUserName;
869  connectionData[ PASSWORD_COL ].data<std::string>() = encryptedPassword;
870  connectionData[ VERIFICATION_KEY_COL ].data<std::string>() = encryptedLabel;
871  connectionData[ CONNECTION_KEY_COL ].data<std::string>() = encryptedConnectionKey;
872  editor1.insertRow( connectionData );
873 
874  int authId = sequenceMgr.getNextId( COND_AUTHORIZATION_TABLE, true );
875 
876  coral::ITableDataEditor& editor2 = schema.tableHandle(COND_AUTHORIZATION_TABLE).dataEditor();
877  coral::AttributeList permissionData;
878  editor2.rowBuffer(permissionData);
879  permissionData[ AUTH_ID_COL ].data<int>() = authId;
880  permissionData[ P_ID_COL ].data<int>() = principalId;
881  permissionData[ ROLE_COL ].data<std::string>() = Auth::COND_ADMIN_ROLE;
882  permissionData[ SCHEMA_COL ].data<std::string>() = connectionString;
883  permissionData[ AUTH_KEY_COL ].data<std::string>() = encryptedConnectionKey;
884  permissionData[ C_ID_COL ].data<int>() = connId;
885  editor2.insertRow( permissionData );
886 
887  session.close();
888  return true;
889 }
890 
892  const std::string& authenticationKey,
893  bool setAdmin ){
894  CSScopedSession session( *this );
895  session.start( false );
896 
897  coral::ISchema& schema = m_session->nominalSchema();
898 
899  PrincipalData princData;
900  bool found = selectPrincipal( schema, principalName, princData );
901 
902  Cipher adminCipher( m_principalKey );
903  Cipher cipher( authenticationKey );
904  std::string verifStr = cipher.b64encrypt( principalName );
905  std::string principalKey("");
906  if( setAdmin ) principalKey = m_principalKey;
907  int principalId = princData.id;
908 
909  coral::ITableDataEditor& editor = schema.tableHandle(COND_AUTHENTICATION_TABLE).dataEditor();
910  if( found ){
911  if( principalKey.empty() ) principalKey = adminCipher.b64decrypt( princData.adminKey );
912  coral::AttributeList updateData;
913  updateData.extend<int>( PRINCIPAL_ID_COL );
914  updateData.extend<std::string>( VERIFICATION_COL );
915  updateData.extend<std::string>( PRINCIPAL_KEY_COL );
916  updateData.extend<std::string>( ADMIN_KEY_COL );
917  updateData[ PRINCIPAL_ID_COL ].data<int>() = principalId;
918  updateData[ VERIFICATION_COL ].data<std::string>() = verifStr;
919  updateData[ PRINCIPAL_KEY_COL ].data<std::string>() = cipher.b64encrypt( principalKey );
920  updateData[ ADMIN_KEY_COL ].data<std::string>() = adminCipher.b64encrypt( principalKey );
921  std::stringstream setClause;
922  setClause << VERIFICATION_COL <<" = :" <<VERIFICATION_COL <<", ";
923  setClause << PRINCIPAL_KEY_COL << " = :" << PRINCIPAL_KEY_COL <<", ";
924  setClause << ADMIN_KEY_COL << " = :" << ADMIN_KEY_COL;
925  std::string whereClause = PRINCIPAL_ID_COL+" = :"+PRINCIPAL_ID_COL;
926  editor.updateRows( setClause.str(),whereClause, updateData );
927  } else {
928  if( principalKey.empty() ) {
930  principalKey = gen.make( Auth::COND_DB_KEY_SIZE );
931  }
932 
933  coral::ITableDataEditor& editor0 = schema.tableHandle(COND_AUTHENTICATION_TABLE).dataEditor();
934 
935  ora::SequenceManager sequenceMgr( SEQUENCE_TABLE_NAME,schema );
936  principalId = sequenceMgr.getNextId( COND_AUTHENTICATION_TABLE, true );
937 
938  coral::AttributeList authData;
939  editor0.rowBuffer(authData);
940  authData[ PRINCIPAL_ID_COL ].data<int>() = principalId;
941  authData[ PRINCIPAL_NAME_COL ].data<std::string>() = principalName;
942  authData[ VERIFICATION_COL ].data<std::string>() = cipher.b64encrypt( principalName );
943  authData[ PRINCIPAL_KEY_COL ].data<std::string>() = cipher.b64encrypt( principalKey );
944  authData[ ADMIN_KEY_COL ].data<std::string>() = adminCipher.b64encrypt( principalKey );
945  editor0.insertRow( authData );
946  }
947 
948  if(setAdmin){
949  std::string connString = m_serviceData->connectionString;
950  std::string connLabel = schemaLabelForCredentialStore( connString );
951  CredentialData credsData;
952  bool found = selectConnection( schema, connLabel, credsData );
953  if(!found){
954  throwException("Credential Store connection has not been defined.","CredentialStore::updatePrincipal");
955  }
956  setPermission( principalId, principalKey, Auth::COND_ADMIN_ROLE, connString, credsData.id, adminCipher.b64decrypt( credsData.connectionKey ) );
957  }
958 
959  session.close();
960  return true;
961 }
962 
963 
965  const std::string& role,
967  const std::string& connectionLabel ){
968  CSScopedSession session( *this );
969  session.start( false );
970 
971  coral::ISchema& schema = m_session->nominalSchema();
972 
973  PrincipalData princData;
974  bool found = selectPrincipal( schema, principal, princData );
975 
976  if( ! found ){
977  std::string msg = "Principal \"" + principal + "\" does not exist in the database.";
978  throwException( msg, "CredentialStore::setPermission");
979  }
980 
981  CredentialData credsData;
982  found = selectConnection( schema, connectionLabel, credsData );
983 
984  if( ! found ){
985  std::string msg = "Connection named \"" + connectionLabel + "\" does not exist in the database.";
986  throwException( msg, "CredentialStore::setPermission");
987  }
988 
989  Cipher cipher( m_principalKey );
990  bool ret = setPermission( princData.id, cipher.b64decrypt( princData.adminKey), role, connectionString, credsData.id, cipher.b64decrypt( credsData.connectionKey ) );
991  session.close();
992  return ret;
993 }
994 
996  const std::string& role,
997  const std::string& connectionString ){
998  CSScopedSession session( *this );
999  session.start( false );
1000  coral::ISchema& schema = m_session->nominalSchema();
1001 
1002  PrincipalData princData;
1003  bool found = selectPrincipal( schema, principal, princData );
1004 
1005  if( ! found ){
1006  std::string msg = "Principal \"" + principal + "\" does not exist in the database.";
1007  throwException( msg, "CredentialStore::unsetPermission");
1008  }
1009 
1010  coral::ITableDataEditor& editor = schema.tableHandle(COND_AUTHORIZATION_TABLE).dataEditor();
1011  coral::AttributeList deleteData;
1012  deleteData.extend<int>( P_ID_COL );
1013  deleteData.extend<std::string>( ROLE_COL );
1014  deleteData.extend<std::string>( SCHEMA_COL );
1015  deleteData[ P_ID_COL ].data<int>() = princData.id;
1016  deleteData[ ROLE_COL ].data<std::string>() = role;
1017  deleteData[ SCHEMA_COL ].data<std::string>() = connectionString;
1018  std::stringstream whereClause;
1019  whereClause << P_ID_COL+" = :"+P_ID_COL;
1020  whereClause << " AND "<< ROLE_COL <<" = :"<<ROLE_COL;
1021  whereClause << " AND "<< SCHEMA_COL <<" = :"<<SCHEMA_COL;
1022  editor.deleteRows( whereClause.str(), deleteData );
1023  session.close();
1024  return true;
1025 }
1026 
1028  const std::string& userName,
1029  const std::string& password ){
1030  CSScopedSession session( *this );
1031  session.start( false );
1032 
1033  m_session->transaction().start();
1034 
1035  updateConnection( connectionLabel,userName, password, true );
1036 
1037  session.close();
1038  return true;
1039 }
1040 
1042  CSScopedSession session( *this );
1043  session.start( false );
1044  coral::ISchema& schema = m_session->nominalSchema();
1045 
1046  PrincipalData princData;
1047  bool found = selectPrincipal( schema, principal, princData );
1048 
1049  if( ! found ){
1050  std::string msg = "Principal \"" + principal + "\" does not exist in the database.";
1051  throwException( msg, "CredentialStore::removePrincipal");
1052  }
1053 
1054  coral::ITableDataEditor& editor0 = schema.tableHandle(COND_AUTHORIZATION_TABLE).dataEditor();
1055 
1056  coral::AttributeList deleteData0;
1057  deleteData0.extend<int>( P_ID_COL );
1058  deleteData0[ P_ID_COL ].data<int>() = princData.id;
1059  std::string whereClause0 = P_ID_COL+" = :"+P_ID_COL;
1060  editor0.deleteRows( whereClause0 , deleteData0 );
1061 
1062  coral::ITableDataEditor& editor1 = schema.tableHandle(COND_AUTHENTICATION_TABLE).dataEditor();
1063 
1064  coral::AttributeList deleteData1;
1065  deleteData1.extend<int>( PRINCIPAL_ID_COL );
1066  deleteData1[ PRINCIPAL_ID_COL ].data<int>() = princData.id;
1067  std::string whereClause1 = PRINCIPAL_ID_COL+" = :"+PRINCIPAL_ID_COL;
1068  editor1.deleteRows( whereClause1 , deleteData1 );
1069 
1070  session.close();
1071 
1072  return true;
1073 }
1074 
1076  CSScopedSession session( *this );
1077  session.start( false );
1078  coral::ISchema& schema = m_session->nominalSchema();
1079 
1080  CredentialData credsData;
1081  bool found = selectConnection( schema, connectionLabel, credsData );
1082 
1083  if( ! found ){
1084  std::string msg = "Connection named \"" + connectionLabel + "\" does not exist in the database.";
1085  throwException( msg, "CredentialStore::removeConnection");
1086  }
1087 
1088  coral::ITableDataEditor& editor0 = schema.tableHandle(COND_AUTHORIZATION_TABLE).dataEditor();
1089 
1090  coral::AttributeList deleteData0;
1091  deleteData0.extend<int>( C_ID_COL );
1092  deleteData0[ C_ID_COL ].data<int>() = credsData.id;
1093  std::string whereClause0 = C_ID_COL+" = :"+C_ID_COL;
1094  editor0.deleteRows( whereClause0 , deleteData0 );
1095 
1096  coral::ITableDataEditor& editor1 = schema.tableHandle(COND_CREDENTIAL_TABLE).dataEditor();
1097 
1098  coral::AttributeList deleteData1;
1099  deleteData1.extend<int>( CONNECTION_ID_COL );
1100  deleteData1[ CONNECTION_ID_COL ].data<int>() = credsData.id;
1101  std::string whereClause1 = CONNECTION_ID_COL+" = :"+CONNECTION_ID_COL;
1102  editor1.deleteRows( whereClause1 , deleteData1 );
1103 
1104  session.close();
1105 
1106  return true;
1107 }
1108 
1110  CSScopedSession session( *this );
1111  session.start( true );
1112  coral::ISchema& schema = m_session->nominalSchema();
1113 
1114  Cipher cipher( m_principalKey );
1115 
1116  std::auto_ptr<coral::IQuery> query(schema.newQuery());
1117  query->addToTableList(COND_AUTHORIZATION_TABLE, "AUTHO");
1118  query->addToTableList(COND_CREDENTIAL_TABLE, "CREDS");
1119  coral::AttributeList readBuff;
1120  readBuff.extend<std::string>("AUTHO."+ROLE_COL);
1121  readBuff.extend<std::string>("AUTHO."+SCHEMA_COL);
1122  readBuff.extend<std::string>("AUTHO."+AUTH_KEY_COL);
1123  readBuff.extend<std::string>("CREDS."+CONNECTION_LABEL_COL);
1124  readBuff.extend<std::string>("CREDS."+USERNAME_COL);
1125  readBuff.extend<std::string>("CREDS."+PASSWORD_COL);
1126  readBuff.extend<std::string>("CREDS."+VERIFICATION_KEY_COL);
1127  coral::AttributeList whereData;
1128  whereData.extend<int>(P_ID_COL);
1129  whereData[ P_ID_COL ].data<int>() = m_principalId;
1130  std::stringstream whereClause;
1131  whereClause << "AUTHO."<< C_ID_COL << "="<<"CREDS."<<CONNECTION_ID_COL;
1132  whereClause << " AND " << "AUTHO."<< P_ID_COL << " = :"<<P_ID_COL;
1133  query->defineOutput(readBuff);
1134  query->addToOutputList( "AUTHO."+ROLE_COL );
1135  query->addToOutputList( "AUTHO."+SCHEMA_COL );
1136  query->addToOutputList( "AUTHO."+AUTH_KEY_COL );
1137  query->addToOutputList( "CREDS."+CONNECTION_LABEL_COL );
1138  query->addToOutputList( "CREDS."+USERNAME_COL );
1139  query->addToOutputList( "CREDS."+PASSWORD_COL );
1140  query->addToOutputList( "CREDS."+VERIFICATION_KEY_COL );
1141  query->setCondition( whereClause.str(), whereData );
1142  coral::ICursor& cursor = query->execute();
1143  while ( cursor.next() ) {
1144  const coral::AttributeList& row = cursor.currentRow();
1145  const std::string& role = row[ "AUTHO."+ROLE_COL ].data<std::string>();
1146  const std::string& connectionString = row[ "AUTHO."+SCHEMA_COL ].data<std::string>();
1147  const std::string& encryptedAuthKey = row[ "AUTHO."+AUTH_KEY_COL ].data<std::string>();
1148  const std::string& connectionLabel = row[ "CREDS."+CONNECTION_LABEL_COL ].data<std::string>();
1149  const std::string& encryptedUserName = row[ "CREDS."+USERNAME_COL ].data<std::string>();
1150  const std::string& encryptedPassword = row[ "CREDS."+PASSWORD_COL ].data<std::string>();
1151  const std::string& encryptedLabel = row[ "CREDS."+VERIFICATION_KEY_COL ].data<std::string>();
1152  std::string authKey = cipher.b64decrypt( encryptedAuthKey );
1153  Cipher connCipher( authKey );
1154  if( connCipher.b64decrypt( encryptedLabel ) == connectionLabel ){
1155  destinationData.registerCredentials( connectionString, role, connCipher.b64decrypt( encryptedUserName ), connCipher.b64decrypt( encryptedPassword ) );
1156  }
1157  }
1158  session.close();
1159  return true;
1160 }
1161 
1164  bool forceUpdateConnection ){
1165  CSScopedSession session( *this );
1166  session.start( false );
1167  coral::ISchema& schema = m_session->nominalSchema();
1168 
1169  PrincipalData princData;
1170  bool found = selectPrincipal( schema, principal, princData );
1171 
1172  if( ! found ){
1173  std::string msg = "Principal \"" + principal + "\" does not exist in the database.";
1174  throwException( msg, "CredentialStore::importForPrincipal");
1175  }
1176 
1177  bool imported = false;
1178  Cipher cipher( m_principalKey );
1179  std::string princKey = cipher.b64decrypt( princData.adminKey);
1180 
1181  const std::map< std::pair<std::string,std::string>, coral::AuthenticationCredentials* >& creds = dataSource.data();
1182  for( std::map< std::pair<std::string,std::string>, coral::AuthenticationCredentials* >::const_iterator iConn = creds.begin(); iConn != creds.end(); ++iConn ){
1183  const std::string& connectionString = iConn->first.first;
1184  coral::URIParser parser;
1185  parser.setURI( connectionString );
1186  std::string serviceName = parser.hostName();
1187  const std::string& role = iConn->first.second;
1188  std::string userName = iConn->second->valueForItem( coral::IAuthenticationCredentials::userItem() );
1189  std::string password = iConn->second->valueForItem( coral::IAuthenticationCredentials::passwordItem());
1190  // first import the connections
1191  std::pair<int,std::string> conn = updateConnection( schemaLabel( serviceName, userName ), userName, password, forceUpdateConnection );
1192  Cipher cipher( m_principalKey );
1193  // than set the permission for the specific role
1194  setPermission( princData.id, princKey, role, connectionString, conn.first, conn.second );
1195  imported = true;
1196  }
1197  session.close();
1198  return imported;
1199 }
1200 
1201 bool cond::CredentialStore::listPrincipals( std::vector<std::string>& destination ){
1202 
1203  CSScopedSession session( *this );
1204  session.start( true );
1205  coral::ISchema& schema = m_session->nominalSchema();
1206 
1207  std::auto_ptr<coral::IQuery> query(schema.tableHandle(COND_AUTHENTICATION_TABLE).newQuery());
1208  coral::AttributeList readBuff;
1209  readBuff.extend<std::string>(PRINCIPAL_NAME_COL);
1210  query->defineOutput(readBuff);
1211  query->addToOutputList( PRINCIPAL_NAME_COL );
1212  coral::ICursor& cursor = query->execute();
1213  bool found = false;
1214  while ( cursor.next() ) {
1215  found = true;
1216  const coral::AttributeList& row = cursor.currentRow();
1217  destination.push_back( row[ PRINCIPAL_NAME_COL ].data<std::string>() );
1218  }
1219  session.close();
1220  return found;
1221 }
1222 
1223 
1224 bool cond::CredentialStore::listConnections( std::map<std::string,std::pair<std::string,std::string> >& destination ){
1225  CSScopedSession session( *this );
1226  session.start( true );
1227  coral::ISchema& schema = m_session->nominalSchema();
1228 
1229  std::auto_ptr<coral::IQuery> query(schema.tableHandle(COND_CREDENTIAL_TABLE).newQuery());
1230  coral::AttributeList readBuff;
1231  readBuff.extend<std::string>( CONNECTION_LABEL_COL );
1232  readBuff.extend<std::string>( USERNAME_COL );
1233  readBuff.extend<std::string>( PASSWORD_COL );
1234  readBuff.extend<std::string>( VERIFICATION_KEY_COL );
1235  readBuff.extend<std::string>( CONNECTION_KEY_COL );
1236  query->defineOutput(readBuff);
1237  query->addToOutputList( CONNECTION_LABEL_COL );
1238  query->addToOutputList( USERNAME_COL );
1239  query->addToOutputList( PASSWORD_COL );
1240  query->addToOutputList( VERIFICATION_KEY_COL );
1241  query->addToOutputList( CONNECTION_KEY_COL );
1242  coral::ICursor& cursor = query->execute();
1243  bool found = false;
1244  Cipher cipher0(m_principalKey );
1245  while ( cursor.next() ) {
1246  std::string userName("");
1247  std::string password("");
1248  const coral::AttributeList& row = cursor.currentRow();
1249  const std::string& connLabel = row[ CONNECTION_LABEL_COL].data<std::string>();
1250  const std::string& encryptedKey = row[ CONNECTION_KEY_COL].data<std::string>();
1251  const std::string& encryptedVerif = row[ VERIFICATION_KEY_COL].data<std::string>();
1252  std::string connKey = cipher0.b64decrypt( encryptedKey );
1253  Cipher cipher1( connKey );
1254  std::string verif = cipher1.b64decrypt( encryptedVerif );
1255  if( verif == connLabel ){
1256  const std::string& encryptedUserName = row[ USERNAME_COL].data<std::string>();
1257  const std::string& encryptedPassword = row[ PASSWORD_COL].data<std::string>();
1258  userName = cipher1.b64decrypt( encryptedUserName );
1259  password = cipher1.b64decrypt( encryptedPassword );
1260  }
1261  destination.insert( std::make_pair( connLabel, std::make_pair( userName, password ) ) );
1262  found = true;
1263  }
1264  session.close();
1265  return found;
1266 }
1267 
1269  const std::string& role,
1270  const std::string& connectionString,
1271  std::vector<Permission>& destination ){
1272  CSScopedSession session( *this );
1273  session.start( true );
1274  coral::ISchema& schema = m_session->nominalSchema();
1275  std::auto_ptr<coral::IQuery> query(schema.newQuery());
1276  query->addToTableList(COND_AUTHENTICATION_TABLE, "AUTHE");
1277  query->addToTableList(COND_AUTHORIZATION_TABLE, "AUTHO");
1278  query->addToTableList(COND_CREDENTIAL_TABLE, "CREDS");
1279  coral::AttributeList readBuff;
1280  readBuff.extend<std::string>("AUTHE."+PRINCIPAL_NAME_COL);
1281  readBuff.extend<std::string>("AUTHO."+ROLE_COL);
1282  readBuff.extend<std::string>("AUTHO."+SCHEMA_COL);
1283  readBuff.extend<std::string>("CREDS."+CONNECTION_LABEL_COL);
1284  coral::AttributeList whereData;
1285  std::stringstream whereClause;
1286  whereClause << "AUTHE."<< PRINCIPAL_ID_COL << "= AUTHO."<< P_ID_COL;
1287  whereClause << " AND AUTHO."<< C_ID_COL << "="<<"CREDS."<<CONNECTION_ID_COL;
1288  if( !principalName.empty() ){
1289  whereData.extend<std::string>(PRINCIPAL_NAME_COL);
1290  whereData[ PRINCIPAL_NAME_COL ].data<std::string>() = principalName;
1291  whereClause << " AND AUTHE."<< PRINCIPAL_NAME_COL <<" = :"<<PRINCIPAL_NAME_COL;
1292  }
1293  if( !role.empty() ){
1294  whereData.extend<std::string>(ROLE_COL);
1295  whereData[ ROLE_COL ].data<std::string>() = role;
1296  whereClause << " AND AUTHO."<< ROLE_COL <<" = :"<<ROLE_COL;
1297  }
1298  if( !connectionString.empty() ){
1299  whereData.extend<std::string>(SCHEMA_COL);
1300  whereData[ SCHEMA_COL ].data<std::string>() = connectionString;
1301  whereClause << " AND AUTHO."<< SCHEMA_COL <<" = :"<<SCHEMA_COL;
1302  }
1303 
1304  query->defineOutput(readBuff);
1305  query->addToOutputList( "AUTHE."+PRINCIPAL_NAME_COL );
1306  query->addToOutputList( "AUTHO."+ROLE_COL );
1307  query->addToOutputList( "AUTHO."+SCHEMA_COL );
1308  query->addToOutputList( "CREDS."+CONNECTION_LABEL_COL );
1309  query->setCondition( whereClause.str(), whereData );
1310  query->addToOrderList( "AUTHO."+SCHEMA_COL );
1311  query->addToOrderList( "AUTHE."+PRINCIPAL_NAME_COL );
1312  query->addToOrderList( "AUTHO."+ROLE_COL );
1313  coral::ICursor& cursor = query->execute();
1314  bool found = false;
1315  while ( cursor.next() ) {
1316  const coral::AttributeList& row = cursor.currentRow();
1317  destination.resize( destination.size()+1 );
1318  Permission& perm = destination.back();
1319  perm.principalName = row[ "AUTHE."+PRINCIPAL_NAME_COL ].data<std::string>();
1320  perm.role = row[ "AUTHO."+ROLE_COL ].data<std::string>();
1321  perm.connectionString = row[ "AUTHO."+SCHEMA_COL ].data<std::string>();
1322  perm.connectionLabel = row[ "CREDS."+CONNECTION_LABEL_COL ].data<std::string>();
1323  found = true;
1324  }
1325  session.close();
1326  return found;
1327 }
1328 
1330  CSScopedSession session( *this );
1331  session.start( true );
1332  coral::ISchema& schema = m_session->nominalSchema();
1333  std::auto_ptr<coral::IQuery> query(schema.newQuery());
1334  query->addToTableList(COND_AUTHORIZATION_TABLE, "AUTHO");
1335  query->addToTableList(COND_CREDENTIAL_TABLE, "CREDS");
1336  coral::AttributeList readBuff;
1337  readBuff.extend<std::string>("AUTHO."+ROLE_COL);
1338  readBuff.extend<std::string>("AUTHO."+SCHEMA_COL);
1339  readBuff.extend<std::string>("CREDS."+CONNECTION_LABEL_COL);
1340  readBuff.extend<std::string>("CREDS."+VERIFICATION_KEY_COL);
1341  readBuff.extend<std::string>("CREDS."+CONNECTION_KEY_COL);
1342  readBuff.extend<std::string>("CREDS."+USERNAME_COL);
1343  readBuff.extend<std::string>("CREDS."+PASSWORD_COL);
1344  coral::AttributeList whereData;
1345  std::stringstream whereClause;
1346  whereClause << "AUTHO."<< C_ID_COL << "="<<"CREDS."<<CONNECTION_ID_COL;
1347 
1348  query->defineOutput(readBuff);
1349  query->addToOutputList( "AUTHO."+ROLE_COL );
1350  query->addToOutputList( "AUTHO."+SCHEMA_COL );
1351  query->addToOutputList( "CREDS."+CONNECTION_LABEL_COL );
1352  query->addToOutputList( "CREDS."+VERIFICATION_KEY_COL );
1353  query->addToOutputList( "CREDS."+CONNECTION_KEY_COL );
1354  query->addToOutputList( "CREDS."+USERNAME_COL );
1355  query->addToOutputList( "CREDS."+PASSWORD_COL );
1356  query->setCondition( whereClause.str(), whereData );
1357  coral::ICursor& cursor = query->execute();
1358  bool found = false;
1359  Cipher cipher0( m_principalKey );
1360  while ( cursor.next() ) {
1361  const coral::AttributeList& row = cursor.currentRow();
1362  const std::string& role = row[ "AUTHO."+ROLE_COL ].data<std::string>();
1363  const std::string& connectionString = row[ "AUTHO."+SCHEMA_COL ].data<std::string>();
1364  const std::string& connectionLabel = row[ "CREDS."+CONNECTION_LABEL_COL ].data<std::string>();
1365  const std::string& encryptedVerifKey = row[ "CREDS."+VERIFICATION_KEY_COL ].data<std::string>();
1366  const std::string& encryptedConnection = row[ "CREDS."+CONNECTION_KEY_COL ].data<std::string>();
1367  std::string userName("");
1368  std::string password("");
1369  std::string connectionKey = cipher0.b64decrypt( encryptedConnection );
1370  Cipher cipher1( connectionKey );
1371  std::string verifKey = cipher1.b64decrypt( encryptedVerifKey );
1372  if( verifKey == connectionLabel ){
1373  const std::string& encryptedUserName = row[ "CREDS."+USERNAME_COL].data<std::string>();
1374  const std::string& encryptedPassword = row[ "CREDS."+PASSWORD_COL].data<std::string>();
1375  userName = cipher1.b64decrypt( encryptedUserName );
1376  password = cipher1.b64decrypt( encryptedPassword );
1377  }
1378  data.registerCredentials( connectionString, role, userName, password );
1379  found = true;
1380  }
1381  session.close();
1382  return found;
1383 }
1384 
1386  return m_key.principalName();
1387 }
1388 
1389 
static const std::string FILE_PATH
Definition: DecodingKey.h:37
CSScopedSession(CredentialStore &store)
const std::string & keyPrincipalName()
std::map< std::pair< std::string, std::string >, coral::AuthenticationCredentials * > m_data
credentials for the specific roles
void import(const AuthenticationCredentialSet &data)
static const std::string COND_KEY
Definition: Auth.h:23
bool updatePrincipal(const std::string &principal, const std::string &principalKey, bool setAdmin=false)
static const std::string COND_ADMIN_ROLE
Definition: Auth.h:20
CredentialStore()
Standard Constructor.
static const std::string ROLE_COL("C_ROLE")
virtual ~AuthenticationCredentialSet()
Destructor.
static PFTauRenderPlugin instance
bool removeConnection(const std::string &connectionLabel)
void openSession(const std::string &schemaName, const std::string &userName, const std::string &password, bool readMode)
static const std::string SCHEMA_COL("C_SCHEMA")
bool listConnections(std::map< std::string, std::pair< std::string, std::string > > &destination)
void start(bool readOnly=true)
static const std::string COND_AUTHENTICATION_TABLE("COND_AUTHENTICATION")
static const std::string SEQUENCE_TABLE_NAME("COND_CREDENTIAL_SEQUENCE")
static const std::string COND_AUTHORIZATION_TABLE("COND_AUTHORIZATION")
void startSuper(const std::string &connectionString, const std::string &userName, const std::string &password)
static const std::string serviceName
CredentialStore & m_store
static const std::string PASSWORD_COL("CRED5")
static const std::string COND_CREDENTIAL_TABLE("COND_CREDENTIAL")
void closeSession(bool commit=true)
static const std::string CONNECTION_KEY_COL("CRED7")
void registerItem(const std::string &connectionString, const std::string &itemName, const std::string &itemValue)
const std::map< std::pair< std::string, std::string >, coral::AuthenticationCredentials * > & data() const
static const std::string ADMIN_KEY_COL("CRED2")
static const std::string VERIFICATION_KEY_COL("CRED6")
void startSession(bool readMode)
bool updateConnection(const std::string &connectionLabel, const std::string &userName, const std::string &password)
bool createSchema(const std::string &connectionString, const std::string &userName, const std::string &password)
bool exportAll(coral_bridge::AuthenticationCredentialSet &data)
std::string schemaLabel(const std::string &serviceName, const std::string &userName)
bool listPrincipals(std::vector< std::string > &destination)
bool selectConnection(coral::ISchema &schema, const std::string &connectionLabel, CredentialData &destination)
std::string setUpForConnectionString(const std::string &connectionString, const std::string &authPath)
static const std::string VERIFICATION_COL("CRED0")
int addUser(const std::string &principalName, const std::string &authenticationKey, const std::string &principalKey, const std::string &adminKey)
std::string setUpForService(const std::string &serviceName, const std::string &authPath)
Sets the initialization parameters.
bool importForPrincipal(const std::string &principal, const coral_bridge::AuthenticationCredentialSet &data, bool forceUpdateConnection=false)
import data
static const std::string USERNAME_COL("CRED4")
string key
FastSim: produces sample of signal events, overlayed with premixed minbias events.
bool selectPrincipal(coral::ISchema &schema, const std::string &principal, PrincipalData &destination)
bool setPermission(const std::string &principal, const std::string &role, const std::string &connectionString, const std::string &connectionLabel)
virtual ~CredentialStore()
Standard Destructor.
std::string schemaLabelForCredentialStore(const std::string &connectionString)
std::string b64encrypt(const std::string &input)
Definition: Cipher.cc:105
bool selectForUser(coral_bridge::AuthenticationCredentialSet &destinationData)
static const std::string CONNECTION_ID_COL("CONN_ID")
bool selectAuthorization(coral::ISchema &schema, int principalId, const std::string &role, const std::string &connectionString, AuthorizationData &destination)
int getNextId(const std::string &sequenceName, bool sinchronize=false)
void create(const std::string &sequenceName)
string connectionString
Definition: autoCondHLT.py:4
bool removePrincipal(const std::string &principal)
void startSuperSession(const std::string &connectionString, const std::string &userName, const std::string &password)
void throwException(std::string const &message, std::string const &methodName)
Definition: Exception.cc:17
static const std::string PRINCIPAL_ID_COL("P_ID")
bool installAdmin(const std::string &userName, const std::string &password)
static const unsigned int COND_DB_KEY_SIZE
Definition: Auth.h:25
tuple editor
Definition: idDealer.py:73
std::string b64decrypt(const std::string &input)
Definition: Cipher.cc:117
std::string make(size_t keySize)
Definition: DecodingKey.cc:74
bool selectPermissions(const std::string &principalName, const std::string &role, const std::string &connectionString, std::vector< Permission > &destination)
static const std::string AUTH_ID_COL("AUTH_ID")
void registerCredentials(const std::string &connectionString, const std::string &userName, const std::string &password)
const std::string DEFAULT_DATA_SOURCE("Cond_Default_Authentication")
std::pair< std::string, std::string > openConnection(const std::string &connectionString)
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
const coral::IAuthenticationCredentials * get(const std::string &connectionString) const
bool drop(const std::string &connectionString, const std::string &userName, const std::string &password)
static const std::string PRINCIPAL_KEY_COL("CRED1")
tuple query
Definition: o2o.py:269
static const std::string COND_DEFAULT_ROLE
Definition: Auth.h:17
static const std::string P_ID_COL("P_ID")
static const std::string CONNECTION_LABEL_COL("CONN_LABEL")
void reset(double vett[256])
Definition: TPedValues.cc:11
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:11
tuple conn
Definition: results_mgr.py:53
bool unsetPermission(const std::string &principal, const std::string &role, const std::string &connectionString)
static const std::string C_ID_COL("C_ID")
static const std::string AUTH_KEY_COL("CRED3")
static const std::string PRINCIPAL_NAME_COL("P_NAME")