test
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::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 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.

99 }
std::string m_principalName
Definition: DecodingKey.h:78
std::map< std::string, ServiceCredentials > m_services
Definition: DecodingKey.h:84
std::string m_pwd
Definition: DecodingKey.h:76
std::string m_principalKey
Definition: DecodingKey.h:80
std::string m_owner
Definition: DecodingKey.h:82
std::string m_fileName
Definition: DecodingKey.h:72
virtual cond::DecodingKey::~DecodingKey ( )
inlinevirtual

Definition at line 45 of file DecodingKey.h.

45 {}

Member Function Documentation

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

Definition at line 286 of file DecodingKey.cc.

References DEFAULT_SERVICE().

286  {
288 }
void addService(const std::string &serviceName, const std::string &connectionString, const std::string &userName, const std::string &password)
Definition: DecodingKey.cc:290
static const std::string DEFAULT_SERVICE("Cond_Default_Service")
string connectionString
Definition: autoCondHLT.py:4
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.

References autoCondHLT::connectionString.

293  {
294  std::map< std::string, ServiceCredentials >::iterator iK = m_services.find( serviceName );
295  if( iK == m_services.end() ){
296  iK = m_services.insert( std::make_pair( serviceName, ServiceCredentials() ) ).first;
297  }
298  iK->second.connectionString = connectionString;
299  iK->second.userName = userName;
300  iK->second.password = password;
301 }
static const std::string serviceName
std::map< std::string, ServiceCredentials > m_services
Definition: DecodingKey.h:84
string connectionString
Definition: autoCondHLT.py:4
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(), visualization-live-secondInstance_cfg::msg, NAMEPREFIX(), OWNERPREFIX(), cond::parseLineForNamedParams(), cond::ServiceCredentials::password, PASSWORDPREFIX(), serviceName, SERVICEPREFIX(), AlCaHLTBitMon_QueryRunRegistry::string, cond::throwException(), cond::ServiceCredentials::userName, and USERPREFIX().

189  {
190  size_t nelem = 0;
191  if(inputFileName.empty()){
192  std::string msg("Provided input file name is empty.");
193  throwException(msg,"DecodingKey::readFromInputFile");
194  }
195  m_principalName.clear();
196  m_principalKey.clear();
197  m_owner.clear();
198  m_services.clear();
199  std::ifstream inputFile (inputFileName.c_str());
200  if (inputFile.is_open()){
201  std::map<std::string,std::string> params;
202  while ( inputFile.good() ){
204  getline (inputFile, line);
205  params.clear();
206  if(line.size()>3 ){
207  if( line.substr(0,2)==NAMEPREFIX ){
208  m_principalName = line.substr(2);
209  } else if ( line.substr(0,2)== KEYPREFIX ){
210  m_principalKey = line.substr(2);
211  } else if ( line.substr(0,2)== OWNERPREFIX ){
212  m_owner = line.substr(2);
213  } else if ( line.substr(0,2)== SERVICEPREFIX ){
214  parseLineForNamedParams( line, params );
216  ServiceCredentials creds;
217  creds.connectionString = params[ CONNECTIONPREFIX ];
218  creds.userName = params[ USERPREFIX ];
219  creds.password = params[ PASSWORDPREFIX ];
220  m_services.insert( std::make_pair( serviceName, creds ) );
221  nelem++;
222  }
223  }
224  }
225  inputFile.close();
226  if( m_principalKey.empty() && generatedKeySize){
227  KeyGenerator gen;
228  m_principalKey = gen.make( generatedKeySize );
229  }
230 
231  } else {
232  std::string msg = "Provided Input File \""+inputFileName+"\n is invalid.";
233  throwException(msg,"DecodingKey::readFromInputFile");
234  }
235  return nelem;
236 }
static const std::string KEYPREFIX("K=")
std::string m_principalName
Definition: DecodingKey.h:78
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
static const std::string SERVICEPREFIX("S=")
std::map< std::string, ServiceCredentials > m_services
Definition: DecodingKey.h:84
std::string m_principalKey
Definition: DecodingKey.h:80
static const std::string PASSWORDPREFIX("P=")
void throwException(std::string const &message, std::string const &methodName)
Definition: Exception.cc:17
static const std::string OWNERPREFIX("O=")
std::string m_owner
Definition: DecodingKey.h:82
static const std::string CONNECTIONPREFIX("C=")
static const std::string NAMEPREFIX("N=")
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, visualization-live-secondInstance_cfg::msg, NAMEPREFIX(), dbtoconf::out, GetRecoTauVFromDQM_MC_cff::outFile, OWNERPREFIX(), SERVICEPREFIX(), AlCaHLTBitMon_QueryRunRegistry::string, and cond::throwException().

251  {
252  std::ofstream outFile ( m_fileName.c_str(),std::ios::binary);
253  if (outFile.is_open()){
254  std::stringstream content;
255  content << KEY_HEADER << LineSeparator;
256  if( !m_principalName.empty() ){
257  content << NAMEPREFIX << m_principalName << LineSeparator;
258  }
259  if( !m_principalKey.empty() ){
260  content << KEYPREFIX << m_principalKey << LineSeparator;
261  }
262  if( !m_owner.empty() ){
263  content << OWNERPREFIX << m_owner << LineSeparator;
264  }
265  for( std::map< std::string, ServiceCredentials >::const_iterator iD = m_services.begin();
266  iD != m_services.end(); ++iD ){
267  content << SERVICEPREFIX << iD->first << ItemSeparator;
268  content << iD->second.connectionString << ItemSeparator;
269  content << iD->second.userName << ItemSeparator;
270  content << iD->second.password << ItemSeparator;
271  content << LineSeparator;
272  }
273  Cipher cipher( m_pwd );
274  unsigned char* out;
275  size_t outSize = cipher.encrypt( content.str(), out );
276  outFile.write( reinterpret_cast<char*>(out),outSize);
277  free (out );
278  } else {
279  std::string msg("");
280  msg += "Provided Key File \""+m_fileName+"\n is invalid.";
281  throwException(msg,"DecodingKey::flush");
282  }
283  outFile.close();
284 }
static const std::string KEYPREFIX("K=")
std::string m_principalName
Definition: DecodingKey.h:78
static const std::string SERVICEPREFIX("S=")
static const std::string KEY_HEADER("Cond_Authentication_Key")
char LineSeparator
Definition: DecodingKey.cc:14
std::map< std::string, ServiceCredentials > m_services
Definition: DecodingKey.h:84
std::string m_pwd
Definition: DecodingKey.h:76
tuple out
Definition: dbtoconf.py:99
std::string m_principalKey
Definition: DecodingKey.h:80
char ItemSeparator
Definition: DecodingKey.cc:13
void throwException(std::string const &message, std::string const &methodName)
Definition: Exception.cc:17
static const std::string OWNERPREFIX("O=")
std::string m_owner
Definition: DecodingKey.h:82
std::string m_fileName
Definition: DecodingKey.h:72
static const std::string NAMEPREFIX("N=")
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, visualization-live-secondInstance_cfg::msg, NAMEPREFIX(), OWNERPREFIX(), SERVICEPREFIX(), AlCaHLTBitMon_QueryRunRegistry::string, and cond::throwException().

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

Definition at line 115 of file DecodingKey.h.

115  {
116  return !m_owner.empty();
117 }
std::string m_owner
Definition: DecodingKey.h:82
void cond::DecodingKey::list ( std::ostream &  out)

Definition at line 238 of file DecodingKey.cc.

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

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

Definition at line 121 of file DecodingKey.h.

121  {
122  return m_owner;
123 }
std::string m_owner
Definition: DecodingKey.h:82
const std::string & cond::DecodingKey::principalKey ( ) const
inline

Definition at line 109 of file DecodingKey.h.

109  {
110  return m_principalKey;
111 }
std::string m_principalKey
Definition: DecodingKey.h:80
const std::string & cond::DecodingKey::principalName ( ) const
inline

Definition at line 103 of file DecodingKey.h.

103  {
104  return m_principalName;
105 }
std::string m_principalName
Definition: DecodingKey.h:78
const std::map< std::string, cond::ServiceCredentials > & cond::DecodingKey::services ( ) const
inline

Definition at line 127 of file DecodingKey.h.

127 { return m_services; }
std::map< std::string, ServiceCredentials > m_services
Definition: DecodingKey.h:84
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().

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

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.