CMS 3D CMS Logo

Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Attributes

cond::DecodingKey Class Reference

#include <DecodingKey.h>

List of all members.

Public Member Functions

void addDefaultService (const std::string &connectionString)
void addService (const std::string &serviceName, const std::string &connectionString, const std::string &userName, const std::string &password)
size_t createFromInputFile (const std::string &inputFileName, size_t generatedKeySize=0)
 DecodingKey ()
void flush ()
size_t init (const std::string &keyFileName, const std::string &password, bool readMode=true)
bool isNominal () const
void list (std::ostream &out)
const std::string & ownerName () const
const std::string & principalKey () const
const std::string & principalName () const
const std::map< std::string,
ServiceCredentials > & 
services () const
virtual ~DecodingKey ()

Static Public Member Functions

static std::string templateFile ()

Static Public Attributes

static const size_t DEFAULT_KEY_SIZE = 100
static const std::string FILE_NAME
static const std::string FILE_PATH

Private Attributes

std::string m_fileName
bool m_mode
std::string m_owner
std::string m_principalKey
std::string m_principalName
std::string m_pwd
std::map< std::string,
ServiceCredentials
m_services

Detailed Description

Definition at line 32 of file DecodingKey.h.


Constructor & Destructor Documentation

cond::DecodingKey::DecodingKey ( ) [inline]

Definition at line 98 of file DecodingKey.h.

virtual cond::DecodingKey::~DecodingKey ( ) [inline, virtual]

Definition at line 45 of file DecodingKey.h.

{}

Member Function Documentation

void cond::DecodingKey::addDefaultService ( const std::string &  connectionString)

Definition at line 286 of file DecodingKey.cc.

References DEFAULT_SERVICE().

                                                                          {
  addService( DEFAULT_SERVICE, connectionString, "", "" );  
}
void cond::DecodingKey::addService ( const std::string &  serviceName,
const std::string &  connectionString,
const std::string &  userName,
const std::string &  password 
)

Definition at line 290 of file DecodingKey.cc.

                                                               {  
  std::map< std::string, ServiceCredentials >::iterator iK = m_services.find( serviceName );
  if( iK == m_services.end() ){
    iK = m_services.insert( std::make_pair( serviceName, ServiceCredentials() ) ).first;
  }
  iK->second.connectionString = connectionString;
  iK->second.userName = userName;
  iK->second.password = password;
}
size_t cond::DecodingKey::createFromInputFile ( const std::string &  inputFileName,
size_t  generatedKeySize = 0 
)

Definition at line 189 of file DecodingKey.cc.

References CONNECTIONPREFIX(), cond::ServiceCredentials::connectionString, relval_steps::gen(), analyzePatCleaning_cfg::inputFile, KEYPREFIX(), geometryCSVtoXML::line, cond::KeyGenerator::make(), lumiQueryAPI::msg, NAMEPREFIX(), OWNERPREFIX(), cond::parseLineForNamedParams(), cond::ServiceCredentials::password, PASSWORDPREFIX(), serviceName, SERVICEPREFIX(), cond::throwException(), cond::ServiceCredentials::userName, and USERPREFIX().

                                                                                                    {
  size_t nelem = 0;
  if(inputFileName.empty()){
    std::string msg("Provided input file name is empty.");
    throwException(msg,"DecodingKey::readFromInputFile");    
  }
  m_principalName.clear();
  m_principalKey.clear();
  m_owner.clear();
  m_services.clear();
  std::ifstream inputFile (inputFileName.c_str());
  if (inputFile.is_open()){
    std::map<std::string,std::string> params;
    while ( inputFile.good() ){
      std::string line;
      getline (inputFile, line);
      params.clear();
      if(line.size()>3 ){
        if( line.substr(0,2)==NAMEPREFIX ){
          m_principalName = line.substr(2);
        } else if ( line.substr(0,2)== KEYPREFIX ){
          m_principalKey = line.substr(2);
        } else if ( line.substr(0,2)== OWNERPREFIX ){
          m_owner = line.substr(2);
        } else if ( line.substr(0,2)== SERVICEPREFIX ){
          parseLineForNamedParams( line, params );
          std::string& serviceName = params[ SERVICEPREFIX ];
          ServiceCredentials creds;
          creds.connectionString = params[ CONNECTIONPREFIX ];
          creds.userName = params[ USERPREFIX ];
          creds.password = params[ PASSWORDPREFIX ];
          m_services.insert( std::make_pair( serviceName, creds ) );
          nelem++;
        }
      }
    }
    inputFile.close();
    if( m_principalKey.empty() && generatedKeySize){
      KeyGenerator gen;
      m_principalKey = gen.make( generatedKeySize );
    }

  } else {
    std::string msg = "Provided Input File \""+inputFileName+"\n is invalid.";
    throwException(msg,"DecodingKey::readFromInputFile");      
  }
  return nelem;
}
void cond::DecodingKey::flush ( )

Definition at line 251 of file DecodingKey.cc.

References ws_sso_content_reader::content, cond::Cipher::encrypt(), ItemSeparator, KEY_HEADER(), KEYPREFIX(), LineSeparator, lumiQueryAPI::msg, NAMEPREFIX(), dbtoconf::out, OWNERPREFIX(), SERVICEPREFIX(), and cond::throwException().

                           {
  std::ofstream outFile ( m_fileName.c_str(),std::ios::binary);
  if (outFile.is_open()){
    std::stringstream content;
    content << KEY_HEADER << LineSeparator;
    if( !m_principalName.empty() ){
      content << NAMEPREFIX << m_principalName << LineSeparator;
    }
    if( !m_principalKey.empty() ){
      content << KEYPREFIX << m_principalKey << LineSeparator;
    }
    if( !m_owner.empty() ){
      content << OWNERPREFIX << m_owner << LineSeparator;
    }
    for( std::map< std::string, ServiceCredentials >::const_iterator iD = m_services.begin();
         iD != m_services.end(); ++iD ){
      content << SERVICEPREFIX << iD->first << ItemSeparator;
      content << iD->second.connectionString << ItemSeparator;
      content << iD->second.userName << ItemSeparator;
      content << iD->second.password << ItemSeparator;
      content << LineSeparator;
    }
    Cipher cipher( m_pwd );
    unsigned char* out;
    size_t outSize = cipher.encrypt( content.str(), out );
    outFile.write( reinterpret_cast<char*>(out),outSize);
    free (out );
  } else {
    std::string msg("");
    msg += "Provided Key File \""+m_fileName+"\n is invalid.";
    throwException(msg,"DecodingKey::flush");            
  }
  outFile.close();
}
size_t cond::DecodingKey::init ( const std::string &  keyFileName,
const std::string &  password,
bool  readMode = true 
)

Definition at line 111 of file DecodingKey.cc.

References ws_sso_content_reader::content, cond::Cipher::decrypt(), cond::getLoginName(), recoMuon::in, ItemSeparator, KEY_HEADER(), keyFile(), KEYPREFIX(), geometryCSVtoXML::line, LineSeparator, lumiQueryAPI::msg, NAMEPREFIX(), OWNERPREFIX(), SERVICEPREFIX(), and cond::throwException().

                                                                                                    {
  if(keyFileName.empty()){
    std::string msg("Provided key file name is empty.");
    throwException(msg,"DecodingKey::init");    
  }
  m_fileName = keyFileName;
  m_pwd = password;
  m_mode = readMode;
  m_principalName.clear();
  m_principalKey.clear();
  m_owner.clear();
  m_services.clear();
  size_t nelem = 0;
  if( m_mode ){
    std::ifstream keyFile (m_fileName.c_str(),std::ios::in|std::ios::binary|std::ios::ate);
    if (keyFile.is_open()){
      size_t fsize = keyFile.tellg();
      unsigned char* buff = (unsigned char*)malloc( fsize );
      keyFile.seekg (0, std::ios::beg);
      keyFile.read (reinterpret_cast<char*>(buff), fsize);
      Cipher cipher( m_pwd );
      std::string content = cipher.decrypt( buff, fsize );
      free ( buff );
      // skip the header + line separator
      if( content.substr( 0, KEY_HEADER.size() )!=KEY_HEADER ){
        std::string msg("Provided key content is invalid.");    
        throwException(msg,"DecodingKey::init");        
      } 
      std::stringstream str( content.substr( KEY_HEADER.size()+1) );
      while( str.good() ){
        std::string line;
        getline ( str, line,LineSeparator ); 
        if(line.size()>3 ){
          if( line.substr(0,2)==NAMEPREFIX ){
            m_principalName = line.substr(2);
          } else if ( line.substr(0,2)== KEYPREFIX ){
            m_principalKey = line.substr(2);
          } else if ( line.substr(0,2)== OWNERPREFIX ){
            m_owner = line.substr(2);
          } else if ( line.substr(0,2)== SERVICEPREFIX ){
            std::stringstream serviceStr( line.substr(2) );
            std::vector<std::string> sdata;
            while( serviceStr.good() ){
              sdata.push_back( std::string("") );
              getline( serviceStr, sdata.back(), ItemSeparator);
            }
            std::map< std::string, ServiceCredentials >::iterator iS =  m_services.insert( std::make_pair( sdata[0], ServiceCredentials() ) ).first;
            iS->second.connectionString = sdata[1];
            iS->second.userName = sdata[2];
            iS->second.password = sdata[3];
            nelem++;
          }
        }
      }
      keyFile.close();
      if( m_principalName.empty() || m_principalKey.empty() ){
        std::string msg = "Provided key is invalid.";
        throwException(msg,"DecodingKey::init");    
      }
      if( !m_owner.empty() ){
        std::string currentUser = getLoginName();
        if(m_owner != currentUser ){
          m_principalName.clear();
          m_principalKey.clear();
          m_owner.clear();
          m_services.clear();
          std::string msg = "Provided key is invalid for user=" + currentUser;
          throwException(msg,"DecodingKey::init");    
        }
      }
    } else {
      std::string msg = "Provided Key File \""+m_fileName+"\" is invalid.";
      throwException(msg,"DecodingKey::init");      
    }
  }
  return nelem;
}
bool cond::DecodingKey::isNominal ( ) const [inline]

Definition at line 115 of file DecodingKey.h.

                                 {
  return  !m_owner.empty();
}
void cond::DecodingKey::list ( std::ostream &  out)

Definition at line 238 of file DecodingKey.cc.

References CONNECTIONPREFIX(), KEYPREFIX(), NAMEPREFIX(), OWNERPREFIX(), PASSWORDPREFIX(), SERVICEPREFIX(), and USERPREFIX().

                                           {
  out <<NAMEPREFIX<<m_principalName<<std::endl;
  out <<KEYPREFIX<<m_principalKey<<std::endl;
  out <<OWNERPREFIX<<m_owner<<std::endl;
  for( std::map< std::string, ServiceCredentials >::const_iterator iS = m_services.begin();
       iS != m_services.end(); iS++ ){
    out <<SERVICEPREFIX<<iS->first<<";";
    out <<CONNECTIONPREFIX<<iS->second.connectionString<<";";
    out <<USERPREFIX<<iS->second.userName<<";";
    out <<PASSWORDPREFIX<<iS->second.password<<";"<<std::endl;
  }
}
const std::string & cond::DecodingKey::ownerName ( ) const [inline]

Definition at line 121 of file DecodingKey.h.

                                 {
  return  m_owner;
}
const std::string & cond::DecodingKey::principalKey ( ) const [inline]

Definition at line 109 of file DecodingKey.h.

                                    {
  return  m_principalKey;
}
const std::string & cond::DecodingKey::principalName ( ) const [inline]

Definition at line 103 of file DecodingKey.h.

                                     {
  return m_principalName;
}
const std::map< std::string, cond::ServiceCredentials > & cond::DecodingKey::services ( ) const [inline]

Definition at line 127 of file DecodingKey.h.

{ return m_services; }
std::string cond::DecodingKey::templateFile ( ) [static]

Definition at line 99 of file DecodingKey.cc.

References CONNECTIONPREFIX(), KEYPREFIX(), NAMEPREFIX(), OWNERPREFIX(), PASSWORDPREFIX(), alignCSCRings::s, SERVICEPREFIX(), and USERPREFIX().

                                       {
  std::stringstream s;
  s<<NAMEPREFIX<<"<principal_name>"<<std::endl;
  s<<OWNERPREFIX<<"<owner_name, optional>"<<std::endl;
  s<<KEYPREFIX<<"<key, leave empty if generated>"<<std::endl;
  //s<<DATEPREFIX<<"<expiring date, optional>"<<std::endl;
  s<<SERVICEPREFIX<<"<service_name0>;"<<CONNECTIONPREFIX<<"<service0_connection_string>;"<<USERPREFIX<<"<user0_name>;"<<PASSWORDPREFIX<<"<password0>;"<<std::endl;
  s<<SERVICEPREFIX<<"<service_name1>;"<<CONNECTIONPREFIX<<"<service1_connection_string>;"<<USERPREFIX<<"<user1_name>;"<<PASSWORDPREFIX<<"<password1>;"<<std::endl;
  s<<SERVICEPREFIX<<"<service_name2>;"<<CONNECTIONPREFIX<<"<service2_connection_string>;"<<USERPREFIX<<"<user2_name>;"<<PASSWORDPREFIX<<"<password2>;"<<std::endl;
  return s.str();
}

Member Data Documentation

const size_t cond::DecodingKey::DEFAULT_KEY_SIZE = 100 [static]

Definition at line 38 of file DecodingKey.h.

const std::string cond::DecodingKey::FILE_NAME [static]

Definition at line 36 of file DecodingKey.h.

const std::string cond::DecodingKey::FILE_PATH [static]

Definition at line 37 of file DecodingKey.h.

Referenced by cond::CredentialStore::setUpForService().

std::string cond::DecodingKey::m_fileName [private]

Definition at line 72 of file DecodingKey.h.

bool cond::DecodingKey::m_mode [private]

Definition at line 74 of file DecodingKey.h.

std::string cond::DecodingKey::m_owner [private]

Definition at line 82 of file DecodingKey.h.

std::string cond::DecodingKey::m_principalKey [private]

Definition at line 80 of file DecodingKey.h.

std::string cond::DecodingKey::m_principalName [private]

Definition at line 78 of file DecodingKey.h.

std::string cond::DecodingKey::m_pwd [private]

Definition at line 76 of file DecodingKey.h.

std::map< std::string, ServiceCredentials > cond::DecodingKey::m_services [private]

Definition at line 84 of file DecodingKey.h.