CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_4/src/CondCore/DBCommon/plugins/RelationalAuthenticationService.cc

Go to the documentation of this file.
00001 #include "CondCore/DBCommon/interface/Auth.h"
00002 #include "CondCore/DBCommon/interface/Exception.h"
00003 #include "RelationalAccess/AuthenticationCredentials.h"
00004 #include "RelationalAccess/AuthenticationServiceException.h"
00005 #include "CondCore/DBCommon/interface/CoralServiceMacros.h"
00006 #include "RelationalAuthenticationService.h"
00007 //
00008 #include "RelationalAccess/AuthenticationServiceException.h"
00009 #include "CoralKernel/IPropertyManager.h"
00010 #include "CoralKernel/Property.h"
00011 #include "CoralKernel/Context.h"
00012 //
00013 #include <memory>
00014 #include <cstdlib>
00015 #include <fstream>
00016 #include <sys/stat.h>
00017 #include <fcntl.h>
00018 #include <boost/filesystem.hpp>
00019 #include <boost/version.hpp>
00020 #include <boost/bind.hpp>
00021 //#include <iostream>
00022 #include "CoralBase/MessageStream.h"
00023 
00024 cond::RelationalAuthenticationService::RelationalAuthenticationService::RelationalAuthenticationService( const std::string& key )
00025   : coral::Service( key ),
00026     m_authenticationPath(""),
00027     m_db(),
00028     m_cache(),
00029     m_callbackID(0)
00030 {
00031   boost::function1<void, std::string> cb(boost::bind(&cond::RelationalAuthenticationService::RelationalAuthenticationService::setAuthenticationPath, this, _1));
00032        
00033   coral::Property* pm = dynamic_cast<coral::Property*>(coral::Context::instance().PropertyManager().property(Auth::COND_AUTH_PATH_PROPERTY));
00034   if(pm){
00035     setAuthenticationPath( pm->get() );
00036     m_callbackID = pm->registerCallback(cb);
00037   } 
00038 }
00039 
00040 cond::RelationalAuthenticationService::RelationalAuthenticationService::~RelationalAuthenticationService()
00041 {
00042 }
00043 
00044 void
00045 cond::RelationalAuthenticationService::RelationalAuthenticationService::setAuthenticationPath(  const std::string& inputPath )
00046 {
00047   m_authenticationPath = inputPath;
00048   m_cache.reset();
00049 }
00050 
00051 const coral::IAuthenticationCredentials&
00052 cond::RelationalAuthenticationService::RelationalAuthenticationService::credentials( const std::string& connectionString ) const
00053 {
00054   const coral::IAuthenticationCredentials* creds = m_cache.get( connectionString );
00055   if( !creds ){
00056     std::string credsStoreConn = m_db.setUpForConnectionString( connectionString, m_authenticationPath );
00057     coral::MessageStream log("cond::RelationalAuthenticationService::credentials");
00058     log << coral::Debug << "Connecting to the credential repository in \"" << credsStoreConn << "\" with principal \""<<m_db.keyPrincipalName()<<"\"."<<coral::MessageStream::endmsg;
00059     m_db.selectForUser( m_cache );
00060   } 
00061   creds = m_cache.get( connectionString );
00062   if( ! creds ){
00063     std::string msg("No Authentication available for connection=\"");
00064     msg += connectionString + "\".";
00065     throw coral::AuthenticationServiceException( msg, "cond::RelationalAuthenticationService::RelationalAuthenticationService::credentials", "");
00066   }
00067   return *creds;
00068 }
00069 
00070 const coral::IAuthenticationCredentials&
00071 cond::RelationalAuthenticationService::RelationalAuthenticationService::credentials( const std::string& connectionString,
00072                                                                                      const std::string& role ) const
00073 {
00074   const coral::IAuthenticationCredentials* creds = m_cache.get( connectionString, role );
00075   if( !creds ){
00076     std::string credsStoreConn = m_db.setUpForConnectionString( connectionString, m_authenticationPath );
00077     coral::MessageStream log("cond::RelationalAuthenticationService::credentials");
00078     log << coral::Debug << "Connecting to the credential repository in \"" << credsStoreConn << "\" with principal \""<<m_db.keyPrincipalName()<<"\"."<<coral::MessageStream::endmsg;
00079     m_db.selectForUser( m_cache );
00080   } 
00081   creds = m_cache.get( connectionString, role );
00082   if( ! creds ){
00083     std::string msg("No Authentication available for connection=\"");
00084     msg += connectionString + "\".";
00085     msg += " and role=\"" + role + "\".";
00086     throw coral::AuthenticationServiceException( msg, "cond::RelationalAuthenticationService::RelationalAuthenticationService::credentials","");
00087   }
00088   return *creds;
00089 }
00090 
00091 DEFINE_CORALSERVICE(cond::RelationalAuthenticationService::RelationalAuthenticationService,"COND/Services/RelationalAuthenticationService");