CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Attributes
cond::auth::DecodingKey Class Reference

#include <DecodingKey.h>

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 constexpr size_t DEFAULT_KEY_SIZE = 100
 
static constexpr const char *const FILE_NAME = "db.key"
 
static constexpr const char *const FILE_PATH = ".cms_cond/db.key"
 

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, ServiceCredentialsm_services
 

Detailed Description

Definition at line 34 of file DecodingKey.h.

Constructor & Destructor Documentation

cond::auth::DecodingKey::DecodingKey ( )
inline

Definition at line 102 of file DecodingKey.h.

References AlCaHLTBitMon_QueryRunRegistry::string.

102  :m_fileName(""),m_mode( true ),m_pwd(""),m_principalName(""),m_principalKey(""),m_owner(""),m_services(){
103 }
std::string m_fileName
Definition: DecodingKey.h:75
std::map< std::string, ServiceCredentials > m_services
Definition: DecodingKey.h:87
std::string m_principalName
Definition: DecodingKey.h:81
std::string m_principalKey
Definition: DecodingKey.h:83
virtual cond::auth::DecodingKey::~DecodingKey ( )
inlinevirtual

Member Function Documentation

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

Definition at line 284 of file DecodingKey.cc.

References DEFAULT_SERVICE().

284  {
285  addService( DEFAULT_SERVICE, connectionString, "", "" );
286 }
void addService(const std::string &serviceName, const std::string &connectionString, const std::string &userName, const std::string &password)
Definition: DecodingKey.cc:288
static const std::string DEFAULT_SERVICE("Cond_Default_Service")
void cond::auth::DecodingKey::addService ( const std::string &  serviceName,
const std::string &  connectionString,
const std::string &  userName,
const std::string &  password 
)

Definition at line 288 of file DecodingKey.cc.

291  {
292  std::map< std::string, ServiceCredentials >::iterator iK = m_services.find( serviceName );
293  if( iK == m_services.end() ){
294  iK = m_services.insert( std::make_pair( serviceName, ServiceCredentials() ) ).first;
295  }
296  iK->second.connectionString = connectionString;
297  iK->second.userName = userName;
298  iK->second.password = password;
299 }
static const std::string serviceName
std::map< std::string, ServiceCredentials > m_services
Definition: DecodingKey.h:87
size_t cond::auth::DecodingKey::createFromInputFile ( const std::string &  inputFileName,
size_t  generatedKeySize = 0 
)

Definition at line 187 of file DecodingKey.cc.

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

187  {
188  size_t nelem = 0;
189  if(inputFileName.empty()){
190  std::string msg("Provided input file name is empty.");
191  throwException(msg,"DecodingKey::readFromInputFile");
192  }
193  m_principalName.clear();
194  m_principalKey.clear();
195  m_owner.clear();
196  m_services.clear();
197  std::ifstream inputFile (inputFileName.c_str());
198  if (inputFile.is_open()){
199  std::map<std::string,std::string> params;
200  while ( inputFile.good() ){
202  getline (inputFile, line);
203  params.clear();
204  if(line.size()>3 ){
205  if( line.substr(0,2)==NAMEPREFIX ){
206  m_principalName = line.substr(2);
207  } else if ( line.substr(0,2)== KEYPREFIX ){
208  m_principalKey = line.substr(2);
209  } else if ( line.substr(0,2)== OWNERPREFIX ){
210  m_owner = line.substr(2);
211  } else if ( line.substr(0,2)== SERVICEPREFIX ){
212  parseLineForNamedParams( line, params );
214  ServiceCredentials creds;
215  creds.connectionString = params[ CONNECTIONPREFIX ];
216  creds.userName = params[ USERPREFIX ];
217  creds.password = params[ PASSWORDPREFIX ];
218  m_services.insert( std::make_pair( serviceName, creds ) );
219  nelem++;
220  }
221  }
222  }
223  inputFile.close();
224  if( m_principalKey.empty() && generatedKeySize){
225  KeyGenerator gen;
226  m_principalKey = gen.make( generatedKeySize );
227  }
228 
229  } else {
230  std::string msg = "Provided Input File \""+inputFileName+"\n is invalid.";
231  throwException(msg,"DecodingKey::readFromInputFile");
232  }
233  return nelem;
234 }
static const std::string KEYPREFIX("K=")
void parseLineForNamedParams(const std::string &line, std::map< std::string, std::string > &params)
Definition: DecodingKey.cc:58
static const std::string USERPREFIX("U=")
static const std::string serviceName
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:21
static const std::string SERVICEPREFIX("S=")
std::map< std::string, ServiceCredentials > m_services
Definition: DecodingKey.h:87
def gen(fragment, howMuch)
Production test section ####.
std::string m_principalName
Definition: DecodingKey.h:81
static const std::string PASSWORDPREFIX("P=")
static const std::string OWNERPREFIX("O=")
tuple msg
Definition: mps_check.py:279
std::string m_principalKey
Definition: DecodingKey.h:83
static const std::string CONNECTIONPREFIX("C=")
static const std::string NAMEPREFIX("N=")
void cond::auth::DecodingKey::flush ( )

Definition at line 249 of file DecodingKey.cc.

References ws_sso_content_reader::content, cond::auth::Cipher::encrypt(), ItemSeparator, KEY_HEADER(), KEYPREFIX(), LineSeparator, mps_check::msg, NAMEPREFIX(), MillePedeFileConverter_cfg::out, GetRecoTauVFromDQM_MC_cff::outFile, OWNERPREFIX(), SERVICEPREFIX(), AlCaHLTBitMon_QueryRunRegistry::string, and cond::throwException().

249  {
250  std::ofstream outFile ( m_fileName.c_str(),std::ios::binary);
251  if (outFile.is_open()){
252  std::stringstream content;
253  content << KEY_HEADER << LineSeparator;
254  if( !m_principalName.empty() ){
255  content << NAMEPREFIX << m_principalName << LineSeparator;
256  }
257  if( !m_principalKey.empty() ){
258  content << KEYPREFIX << m_principalKey << LineSeparator;
259  }
260  if( !m_owner.empty() ){
261  content << OWNERPREFIX << m_owner << LineSeparator;
262  }
263  for( std::map< std::string, ServiceCredentials >::const_iterator iD = m_services.begin();
264  iD != m_services.end(); ++iD ){
265  content << SERVICEPREFIX << iD->first << ItemSeparator;
266  content << iD->second.connectionString << ItemSeparator;
267  content << iD->second.userName << ItemSeparator;
268  content << iD->second.password << ItemSeparator;
269  content << LineSeparator;
270  }
271  Cipher cipher( m_pwd );
272  unsigned char* out;
273  size_t outSize = cipher.encrypt( content.str(), out );
274  outFile.write( reinterpret_cast<char*>(out),outSize);
275  free (out );
276  } else {
277  std::string msg("");
278  msg += "Provided Key File \""+m_fileName+"\n is invalid.";
279  throwException(msg,"DecodingKey::flush");
280  }
281  outFile.close();
282 }
static const std::string KEYPREFIX("K=")
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:21
static const std::string SERVICEPREFIX("S=")
static const std::string KEY_HEADER("Cond_Authentication_Key")
char LineSeparator
Definition: DecodingKey.cc:15
std::string m_fileName
Definition: DecodingKey.h:75
std::map< std::string, ServiceCredentials > m_services
Definition: DecodingKey.h:87
std::string m_principalName
Definition: DecodingKey.h:81
char ItemSeparator
Definition: DecodingKey.cc:14
static const std::string OWNERPREFIX("O=")
tuple msg
Definition: mps_check.py:279
std::string m_principalKey
Definition: DecodingKey.h:83
static const std::string NAMEPREFIX("N=")
size_t cond::auth::DecodingKey::init ( const std::string &  keyFileName,
const std::string &  password,
bool  readMode = true 
)

Definition at line 109 of file DecodingKey.cc.

References ws_sso_content_reader::content, cond::auth::Cipher::decrypt(), cond::getLoginName(), recoMuon::in, ItemSeparator, KEY_HEADER(), keyFile(), KEYPREFIX(), mps_splice::line, LineSeparator, mps_check::msg, NAMEPREFIX(), OWNERPREFIX(), SERVICEPREFIX(), str, AlCaHLTBitMon_QueryRunRegistry::string, and cond::throwException().

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

109  {
110  if(keyFileName.empty()){
111  std::string msg("Provided key file name is empty.");
112  throwException(msg,"DecodingKey::init");
113  }
114  m_fileName = keyFileName;
115  m_pwd = password;
116  m_mode = readMode;
117  m_principalName.clear();
118  m_principalKey.clear();
119  m_owner.clear();
120  m_services.clear();
121  size_t nelem = 0;
122  if( m_mode ){
123  std::ifstream keyFile (m_fileName.c_str(),std::ios::in|std::ios::binary|std::ios::ate);
124  if (keyFile.is_open()){
125  size_t fsize = keyFile.tellg();
126  unsigned char* buff = (unsigned char*)malloc( fsize );
127  keyFile.seekg (0, std::ios::beg);
128  keyFile.read (reinterpret_cast<char*>(buff), fsize);
129  Cipher cipher( m_pwd );
130  std::string content = cipher.decrypt( buff, fsize );
131  free ( buff );
132  // skip the header + line separator
133  if( content.substr( 0, KEY_HEADER.size() )!=KEY_HEADER ){
134  std::string msg("Provided key content is invalid.");
135  throwException(msg,"DecodingKey::init");
136  }
137  std::stringstream str( content.substr( KEY_HEADER.size()+1) );
138  while( str.good() ){
140  getline ( str, line,LineSeparator );
141  if(line.size()>3 ){
142  if( line.substr(0,2)==NAMEPREFIX ){
143  m_principalName = line.substr(2);
144  } else if ( line.substr(0,2)== KEYPREFIX ){
145  m_principalKey = line.substr(2);
146  } else if ( line.substr(0,2)== OWNERPREFIX ){
147  m_owner = line.substr(2);
148  } else if ( line.substr(0,2)== SERVICEPREFIX ){
149  std::stringstream serviceStr( line.substr(2) );
150  std::vector<std::string> sdata;
151  while( serviceStr.good() ){
152  sdata.push_back( std::string("") );
153  getline( serviceStr, sdata.back(), ItemSeparator);
154  }
155  std::map< std::string, ServiceCredentials >::iterator iS = m_services.insert( std::make_pair( sdata[0], ServiceCredentials() ) ).first;
156  iS->second.connectionString = sdata[1];
157  iS->second.userName = sdata[2];
158  iS->second.password = sdata[3];
159  nelem++;
160  }
161  }
162  }
163  keyFile.close();
164  if( m_principalName.empty() || m_principalKey.empty() ){
165  std::string msg = "Provided key is invalid.";
166  throwException(msg,"DecodingKey::init");
167  }
168  if( !m_owner.empty() ){
169  std::string currentUser = getLoginName();
170  if(m_owner != currentUser ){
171  m_principalName.clear();
172  m_principalKey.clear();
173  m_owner.clear();
174  m_services.clear();
175  std::string msg = "Provided key is invalid for user=" + currentUser;
176  throwException(msg,"DecodingKey::init");
177  }
178  }
179  } else {
180  std::string msg = "Required Key File \""+m_fileName+"\" is missing or unreadable.";
181  throwException(msg,"DecodingKey::init");
182  }
183  }
184  return nelem;
185 }
static const std::string KEYPREFIX("K=")
std::string getLoginName()
Definition: DecodingKey.cc:42
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:21
static const std::string SERVICEPREFIX("S=")
static const std::string KEY_HEADER("Cond_Authentication_Key")
char LineSeparator
Definition: DecodingKey.cc:15
std::string m_fileName
Definition: DecodingKey.h:75
std::map< std::string, ServiceCredentials > m_services
Definition: DecodingKey.h:87
std::string m_principalName
Definition: DecodingKey.h:81
char ItemSeparator
Definition: DecodingKey.cc:14
static const std::string OWNERPREFIX("O=")
tuple msg
Definition: mps_check.py:279
std::string m_principalKey
Definition: DecodingKey.h:83
static const std::string NAMEPREFIX("N=")
static const std::string keyFile("/nfshome0/hcalsw/.ReadOMDSKey")
#define str(s)
bool cond::auth::DecodingKey::isNominal ( ) const
inline

Definition at line 119 of file DecodingKey.h.

References m_owner, and AlCaHLTBitMon_QueryRunRegistry::string.

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

Definition at line 236 of file DecodingKey.cc.

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

236  {
237  out <<NAMEPREFIX<<m_principalName<<std::endl;
238  out <<KEYPREFIX<<m_principalKey<<std::endl;
239  out <<OWNERPREFIX<<m_owner<<std::endl;
240  for( std::map< std::string, ServiceCredentials >::const_iterator iS = m_services.begin();
241  iS != m_services.end(); iS++ ){
242  out <<SERVICEPREFIX<<iS->first<<";";
243  out <<CONNECTIONPREFIX<<iS->second.connectionString<<";";
244  out <<USERPREFIX<<iS->second.userName<<";";
245  out <<PASSWORDPREFIX<<iS->second.password<<";"<<std::endl;
246  }
247 }
static const std::string KEYPREFIX("K=")
static const std::string USERPREFIX("U=")
static const std::string SERVICEPREFIX("S=")
std::map< std::string, ServiceCredentials > m_services
Definition: DecodingKey.h:87
std::string m_principalName
Definition: DecodingKey.h:81
static const std::string PASSWORDPREFIX("P=")
static const std::string OWNERPREFIX("O=")
std::string m_principalKey
Definition: DecodingKey.h:83
static const std::string CONNECTIONPREFIX("C=")
static const std::string NAMEPREFIX("N=")
const std::string & cond::auth::DecodingKey::ownerName ( ) const
inline

Definition at line 125 of file DecodingKey.h.

References m_owner.

125  {
126  return m_owner;
127 }
const std::string & cond::auth::DecodingKey::principalKey ( ) const
inline

Definition at line 113 of file DecodingKey.h.

References m_principalKey.

Referenced by cond::CredentialStore::createSchema(), and cond::CredentialStore::resetAdmin().

113  {
114  return m_principalKey;
115 }
std::string m_principalKey
Definition: DecodingKey.h:83
const std::string & cond::auth::DecodingKey::principalName ( ) const
inline
const std::map< std::string, cond::auth::ServiceCredentials > & cond::auth::DecodingKey::services ( ) const
inline

Definition at line 131 of file DecodingKey.h.

References m_services.

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

131 { return m_services; }
std::map< std::string, ServiceCredentials > m_services
Definition: DecodingKey.h:87
std::string cond::auth::DecodingKey::templateFile ( )
static

Definition at line 97 of file DecodingKey.cc.

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

97  {
98  std::stringstream s;
99  s<<NAMEPREFIX<<"<principal_name>"<<std::endl;
100  s<<OWNERPREFIX<<"<owner_name, optional>"<<std::endl;
101  s<<KEYPREFIX<<"<key, leave empty if generated>"<<std::endl;
102  //s<<DATEPREFIX<<"<expiring date, optional>"<<std::endl;
103  s<<SERVICEPREFIX<<"<service_name0>;"<<CONNECTIONPREFIX<<"<service0_connection_string>;"<<USERPREFIX<<"<user0_name>;"<<PASSWORDPREFIX<<"<password0>;"<<std::endl;
104  s<<SERVICEPREFIX<<"<service_name1>;"<<CONNECTIONPREFIX<<"<service1_connection_string>;"<<USERPREFIX<<"<user1_name>;"<<PASSWORDPREFIX<<"<password1>;"<<std::endl;
105  s<<SERVICEPREFIX<<"<service_name2>;"<<CONNECTIONPREFIX<<"<service2_connection_string>;"<<USERPREFIX<<"<user2_name>;"<<PASSWORDPREFIX<<"<password2>;"<<std::endl;
106  return s.str();
107 }
static const std::string KEYPREFIX("K=")
static const std::string USERPREFIX("U=")
static const std::string SERVICEPREFIX("S=")
static const std::string PASSWORDPREFIX("P=")
static const std::string OWNERPREFIX("O=")
static const std::string CONNECTIONPREFIX("C=")
static const std::string NAMEPREFIX("N=")

Member Data Documentation

constexpr size_t cond::auth::DecodingKey::DEFAULT_KEY_SIZE = 100
static

Definition at line 40 of file DecodingKey.h.

constexpr const char* const cond::auth::DecodingKey::FILE_NAME = "db.key"
static

Definition at line 38 of file DecodingKey.h.

constexpr const char* const cond::auth::DecodingKey::FILE_PATH = ".cms_cond/db.key"
static

Definition at line 39 of file DecodingKey.h.

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

std::string cond::auth::DecodingKey::m_fileName
private

Definition at line 75 of file DecodingKey.h.

bool cond::auth::DecodingKey::m_mode
private

Definition at line 77 of file DecodingKey.h.

std::string cond::auth::DecodingKey::m_owner
private

Definition at line 85 of file DecodingKey.h.

Referenced by isNominal(), and ownerName().

std::string cond::auth::DecodingKey::m_principalKey
private

Definition at line 83 of file DecodingKey.h.

Referenced by principalKey().

std::string cond::auth::DecodingKey::m_principalName
private

Definition at line 81 of file DecodingKey.h.

Referenced by principalName().

std::string cond::auth::DecodingKey::m_pwd
private

Definition at line 79 of file DecodingKey.h.

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

Definition at line 87 of file DecodingKey.h.

Referenced by services().