Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:25

0001 #include "CondCore/CondDB/interface/Auth.h"
0002 #include "CondCore/CondDB/interface/Exception.h"
0003 #include "RelationalAccess/AuthenticationCredentials.h"
0004 #include "RelationalAccess/AuthenticationServiceException.h"
0005 #include "CondCore/CondDB/interface/CoralServiceMacros.h"
0006 #include "RelationalAuthenticationService.h"
0007 //
0008 #include "RelationalAccess/AuthenticationServiceException.h"
0009 #include "CoralKernel/IPropertyManager.h"
0010 #include "CoralKernel/Property.h"
0011 #include "CoralKernel/Context.h"
0012 //
0013 #include <cstdlib>
0014 #include <fcntl.h>
0015 #include <fstream>
0016 #include <memory>
0017 #include <sys/stat.h>
0018 
0019 #include "CoralBase/MessageStream.h"
0020 
0021 cond::RelationalAuthenticationService::RelationalAuthenticationService::RelationalAuthenticationService(
0022     const std::string& key)
0023     : coral::Service(key), m_authenticationPath(""), m_db(), m_cache(), m_callbackID(0) {
0024   boost::function1<void, std::string> cb(
0025       std::bind(&cond::RelationalAuthenticationService::RelationalAuthenticationService::setAuthenticationPath,
0026                 this,
0027                 std::placeholders::_1));
0028 
0029   coral::Property* pm = dynamic_cast<coral::Property*>(
0030       coral::Context::instance().PropertyManager().property(auth::COND_AUTH_PATH_PROPERTY));
0031   if (pm) {
0032     setAuthenticationPath(pm->get());
0033     m_callbackID = pm->registerCallback(cb);
0034   }
0035 }
0036 
0037 cond::RelationalAuthenticationService::RelationalAuthenticationService::~RelationalAuthenticationService() {}
0038 
0039 void cond::RelationalAuthenticationService::RelationalAuthenticationService::setAuthenticationPath(
0040     const std::string& inputPath) {
0041   m_authenticationPath = inputPath;
0042   m_cache.reset();
0043 }
0044 
0045 const coral::IAuthenticationCredentials&
0046 cond::RelationalAuthenticationService::RelationalAuthenticationService::credentials(
0047     const std::string& connectionStr) const {
0048   std::string connectionString = to_lower(connectionStr);
0049   const coral::IAuthenticationCredentials* creds = m_cache.get(connectionString);
0050   if (!creds) {
0051     std::string credsStoreConn = m_db.setUpForConnectionString(connectionString, m_authenticationPath);
0052     coral::MessageStream log("cond::RelationalAuthenticationService::credentials");
0053     log << coral::Debug << "Connecting to the credential repository in \"" << credsStoreConn << "\" with principal \""
0054         << m_db.keyPrincipalName() << "\"." << coral::MessageStream::endmsg;
0055     m_db.selectForUser(m_cache);
0056   }
0057   creds = m_cache.get(connectionString);
0058   if (!creds) {
0059     std::string msg("Connection to \"");
0060     msg += connectionString + "\"";
0061     msg += " with role \"COND_DEFAULT_ROLE\" is not available for ";
0062     msg += m_db.keyPrincipalName();
0063     cond::throwException(msg, "cond::RelationalAuthenticationService::RelationalAuthenticationService::credentials");
0064   }
0065   return *creds;
0066 }
0067 
0068 const coral::IAuthenticationCredentials&
0069 cond::RelationalAuthenticationService::RelationalAuthenticationService::credentials(const std::string& connectionStr,
0070                                                                                     const std::string& role) const {
0071   std::string connectionString = to_lower(connectionStr);
0072   const coral::IAuthenticationCredentials* creds = m_cache.get(connectionString, role);
0073   if (!creds) {
0074     std::string credsStoreConn = m_db.setUpForConnectionString(connectionString, m_authenticationPath);
0075     coral::MessageStream log("cond::RelationalAuthenticationService::credentials");
0076     log << coral::Debug << "Connecting to the credential repository in \"" << credsStoreConn << "\" with principal \""
0077         << m_db.keyPrincipalName() << "\"." << coral::MessageStream::endmsg;
0078     m_db.selectForUser(m_cache);
0079   }
0080   creds = m_cache.get(connectionString, role);
0081   if (!creds) {
0082     std::string msg("Connection to \"");
0083     msg += connectionString + "\"";
0084     msg += " with role \"" + role + "\" is not available for ";
0085     msg += m_db.keyPrincipalName();
0086     cond::throwException(msg, "cond::RelationalAuthenticationService::RelationalAuthenticationService::credentials");
0087   }
0088   return *creds;
0089 }
0090 
0091 std::string cond::RelationalAuthenticationService::RelationalAuthenticationService::principalName() {
0092   return m_db.keyPrincipalName();
0093 }
0094 
0095 DEFINE_CORALSERVICE(cond::RelationalAuthenticationService::RelationalAuthenticationService,
0096                     "COND/Services/RelationalAuthenticationService");