CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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,
ServiceCredentials
m_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.

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

Definition at line 48 of file DecodingKey.h.

48 {}

Member Function Documentation

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

Definition at line 283 of file DecodingKey.cc.

References DEFAULT_SERVICE().

283  {
285 }
void addService(const std::string &serviceName, const std::string &connectionString, const std::string &userName, const std::string &password)
Definition: DecodingKey.cc:287
static const std::string DEFAULT_SERVICE("Cond_Default_Service")
string connectionString
Definition: autoCondHLT.py:4
void cond::auth::DecodingKey::addService ( const std::string &  serviceName,
const std::string &  connectionString,
const std::string &  userName,
const std::string &  password 
)

Definition at line 287 of file DecodingKey.cc.

References autoCondHLT::connectionString.

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

Definition at line 186 of file DecodingKey.cc.

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

186  {
187  size_t nelem = 0;
188  if(inputFileName.empty()){
189  std::string msg("Provided input file name is empty.");
190  throwException(msg,"DecodingKey::readFromInputFile");
191  }
192  m_principalName.clear();
193  m_principalKey.clear();
194  m_owner.clear();
195  m_services.clear();
196  std::ifstream inputFile (inputFileName.c_str());
197  if (inputFile.is_open()){
198  std::map<std::string,std::string> params;
199  while ( inputFile.good() ){
201  getline (inputFile, line);
202  params.clear();
203  if(line.size()>3 ){
204  if( line.substr(0,2)==NAMEPREFIX ){
205  m_principalName = line.substr(2);
206  } else if ( line.substr(0,2)== KEYPREFIX ){
207  m_principalKey = line.substr(2);
208  } else if ( line.substr(0,2)== OWNERPREFIX ){
209  m_owner = line.substr(2);
210  } else if ( line.substr(0,2)== SERVICEPREFIX ){
211  parseLineForNamedParams( line, params );
213  ServiceCredentials creds;
214  creds.connectionString = params[ CONNECTIONPREFIX ];
215  creds.userName = params[ USERPREFIX ];
216  creds.password = params[ PASSWORDPREFIX ];
217  m_services.insert( std::make_pair( serviceName, creds ) );
218  nelem++;
219  }
220  }
221  }
222  inputFile.close();
223  if( m_principalKey.empty() && generatedKeySize){
224  KeyGenerator gen;
225  m_principalKey = gen.make( generatedKeySize );
226  }
227 
228  } else {
229  std::string msg = "Provided Input File \""+inputFileName+"\n is invalid.";
230  throwException(msg,"DecodingKey::readFromInputFile");
231  }
232  return nelem;
233 }
static const std::string KEYPREFIX("K=")
void parseLineForNamedParams(const std::string &line, std::map< std::string, std::string > &params)
Definition: DecodingKey.cc:57
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=")
def gen
run2 Cosmic #### Run 256259 @ 0T 2015C### Run 272133 @ 3.8T 2016B###
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=")
void cond::auth::DecodingKey::flush ( )

Definition at line 248 of file DecodingKey.cc.

References ws_sso_content_reader::content, cond::auth::Cipher::encrypt(), ItemSeparator, KEY_HEADER(), KEYPREFIX(), LineSeparator, visualization-live-secondInstance_cfg::msg, NAMEPREFIX(), GenerateHcalLaserBadRunList::out, GetRecoTauVFromDQM_MC_cff::outFile, OWNERPREFIX(), SERVICEPREFIX(), AlCaHLTBitMon_QueryRunRegistry::string, and cond::throwException().

248  {
249  std::ofstream outFile ( m_fileName.c_str(),std::ios::binary);
250  if (outFile.is_open()){
251  std::stringstream content;
252  content << KEY_HEADER << LineSeparator;
253  if( !m_principalName.empty() ){
254  content << NAMEPREFIX << m_principalName << LineSeparator;
255  }
256  if( !m_principalKey.empty() ){
257  content << KEYPREFIX << m_principalKey << LineSeparator;
258  }
259  if( !m_owner.empty() ){
260  content << OWNERPREFIX << m_owner << LineSeparator;
261  }
262  for( std::map< std::string, ServiceCredentials >::const_iterator iD = m_services.begin();
263  iD != m_services.end(); ++iD ){
264  content << SERVICEPREFIX << iD->first << ItemSeparator;
265  content << iD->second.connectionString << ItemSeparator;
266  content << iD->second.userName << ItemSeparator;
267  content << iD->second.password << ItemSeparator;
268  content << LineSeparator;
269  }
270  Cipher cipher( m_pwd );
271  unsigned char* out;
272  size_t outSize = cipher.encrypt( content.str(), out );
273  outFile.write( reinterpret_cast<char*>(out),outSize);
274  free (out );
275  } else {
276  std::string msg("");
277  msg += "Provided Key File \""+m_fileName+"\n is invalid.";
278  throwException(msg,"DecodingKey::flush");
279  }
280  outFile.close();
281 }
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:14
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:13
static const std::string OWNERPREFIX("O=")
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 108 of file DecodingKey.cc.

References ws_sso_content_reader::content, cond::auth::Cipher::decrypt(), cond::getLoginName(), recoMuon::in, ItemSeparator, KEY_HEADER(), keyFile(), KEYPREFIX(), geometryCSVtoXML::line, LineSeparator, visualization-live-secondInstance_cfg::msg, NAMEPREFIX(), OWNERPREFIX(), SERVICEPREFIX(), AlCaHLTBitMon_QueryRunRegistry::string, and cond::throwException().

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

Definition at line 119 of file DecodingKey.h.

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

Definition at line 235 of file DecodingKey.cc.

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

235  {
236  out <<NAMEPREFIX<<m_principalName<<std::endl;
237  out <<KEYPREFIX<<m_principalKey<<std::endl;
238  out <<OWNERPREFIX<<m_owner<<std::endl;
239  for( std::map< std::string, ServiceCredentials >::const_iterator iS = m_services.begin();
240  iS != m_services.end(); iS++ ){
241  out <<SERVICEPREFIX<<iS->first<<";";
242  out <<CONNECTIONPREFIX<<iS->second.connectionString<<";";
243  out <<USERPREFIX<<iS->second.userName<<";";
244  out <<PASSWORDPREFIX<<iS->second.password<<";"<<std::endl;
245  }
246 }
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.

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

Definition at line 113 of file DecodingKey.h.

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

Definition at line 107 of file DecodingKey.h.

107  {
108  return m_principalName;
109 }
std::string m_principalName
Definition: DecodingKey.h:81
const std::map< std::string, cond::auth::ServiceCredentials > & cond::auth::DecodingKey::services ( ) const
inline

Definition at line 131 of file DecodingKey.h.

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 96 of file DecodingKey.cc.

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

96  {
97  std::stringstream s;
98  s<<NAMEPREFIX<<"<principal_name>"<<std::endl;
99  s<<OWNERPREFIX<<"<owner_name, optional>"<<std::endl;
100  s<<KEYPREFIX<<"<key, leave empty if generated>"<<std::endl;
101  //s<<DATEPREFIX<<"<expiring date, optional>"<<std::endl;
102  s<<SERVICEPREFIX<<"<service_name0>;"<<CONNECTIONPREFIX<<"<service0_connection_string>;"<<USERPREFIX<<"<user0_name>;"<<PASSWORDPREFIX<<"<password0>;"<<std::endl;
103  s<<SERVICEPREFIX<<"<service_name1>;"<<CONNECTIONPREFIX<<"<service1_connection_string>;"<<USERPREFIX<<"<user1_name>;"<<PASSWORDPREFIX<<"<password1>;"<<std::endl;
104  s<<SERVICEPREFIX<<"<service_name2>;"<<CONNECTIONPREFIX<<"<service2_connection_string>;"<<USERPREFIX<<"<user2_name>;"<<PASSWORDPREFIX<<"<password2>;"<<std::endl;
105  return s.str();
106 }
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.

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

Definition at line 83 of file DecodingKey.h.

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

Definition at line 81 of file DecodingKey.h.

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.