CMS 3D CMS Logo

XMLAuthenticationService.cc
Go to the documentation of this file.
4 #include "RelationalAccess/AuthenticationCredentials.h"
5 #include "CoralCommon/Cipher.h"
6 #include "RelationalAccess/AuthenticationServiceException.h"
7 #include "CoralKernel/IPropertyManager.h"
8 #include "CoralKernel/Property.h"
9 #include "CoralKernel/Context.h"
12 #include "xercesc/parsers/XercesDOMParser.hpp"
13 #include "xercesc/framework/MemBufInputSource.hpp"
14 #include "xercesc/dom/DOM.hpp"
15 #include "xercesc/sax/HandlerBase.hpp"
16 #include "xercesc/util/XMLString.hpp"
17 #include "xercesc/util/PlatformUtils.hpp"
19 
20 #include <cstdlib>
21 #include <fcntl.h>
22 #include <filesystem>
23 #include <fstream>
24 #include <memory>
25 #include <sys/stat.h>
26 
27 #include <boost/bind.hpp>
28 #include "CoralBase/MessageStream.h"
29 
30 constexpr char XML_AUTHENTICATION_FILE[] = "authentication.xml";
31 
33  const std::string& connectionName)
34  : m_serviceName(serviceName),
35  m_connectionName(connectionName),
36  m_default(new coral::AuthenticationCredentials(serviceName)),
37  m_data() {}
38 
40  delete m_default;
41  for (std::map<std::string, coral::AuthenticationCredentials*>::iterator iData = m_data.begin(); iData != m_data.end();
42  ++iData)
43  delete iData->second;
44 }
45 
47  const std::string& value) {
48  m_default->registerItem(item, value);
49 }
50 
52  const std::string& value,
53  const std::string& role) {
54  std::map<std::string, coral::AuthenticationCredentials*>::iterator iRole = m_data.find(role);
55  if (iRole == m_data.end()) {
56  iRole = m_data.insert(std::make_pair(role, new coral::AuthenticationCredentials(m_serviceName))).first;
57  }
58  iRole->second->registerItem(item, value);
59 }
60 
61 const coral::IAuthenticationCredentials& cond::XMLAuthenticationService::DataSourceEntry::credentials() const {
62  return *m_default;
63 }
64 
65 const coral::IAuthenticationCredentials& cond::XMLAuthenticationService::DataSourceEntry::credentials(
66  const std::string& role) const {
75  return *m_default;
76 }
77 
79  : coral::Service(key), m_isInitialized(false), m_inputFileName(""), m_data(), m_mutexLock(), m_callbackID(0) {
80  boost::function1<void, std::string> cb(
82 
83  coral::Property* pm = dynamic_cast<coral::Property*>(
84  coral::Context::instance().PropertyManager().property(auth::COND_AUTH_PATH_PROPERTY));
85  if (pm) {
86  setAuthenticationPath(pm->get());
87  m_callbackID = pm->registerCallback(cb);
88  }
89 }
90 
92  for (std::map<std::string, cond::XMLAuthenticationService::DataSourceEntry*>::iterator iConnection = m_data.begin();
93  iConnection != m_data.end();
94  ++iConnection)
95  delete iConnection->second;
96 }
97 
100  if (std::filesystem::is_directory(AuthPath)) {
102  }
103 
104  m_inputFileName = AuthPath.string();
105  reset();
106 }
107 
109  coral::MessageStream log("cond::XMLAuthenticationService::processFile");
110  //std::cout<< "Processing file \""<< inputFileName<<"\"" <<std::endl;
111  bool result = true;
112 
114  std::string cont("");
115  try {
116  inputFile.read(inputFileName);
117  cont = inputFile.content();
118  } catch (const cond::Exception& exc) {
119  log << coral::Error << "File \"" << inputFileName << "\" not found." << std::string(exc.what())
120  << coral::MessageStream::endmsg;
121  return false;
122  }
123 
125  std::string name = filePath.filename().string();
126 
148  xercesc::MemBufInputSource* memBufInputSource = nullptr;
149 
150  try {
151  xercesc::XercesDOMParser parser;
152  parser.setValidationScheme(xercesc::XercesDOMParser::Val_Always);
153  parser.setDoNamespaces(true);
154 
155  xercesc::HandlerBase errorHandler;
156  parser.setErrorHandler(&errorHandler);
157 
158  const char* bufferId = "InMemoryDocument";
159  const char* buffer = cont.c_str();
160 
161  memBufInputSource = new xercesc::MemBufInputSource((const XMLByte*)buffer, strlen(buffer), bufferId, false);
162 
163  parser.parse(*memBufInputSource);
164 
165  xercesc::DOMDocument* document = parser.getDocument();
166 
167  XMLCh tempStr[20];
168  xercesc::XMLString::transcode("connection", tempStr, 19);
169 
170  xercesc::DOMNodeList* connectionList = document->getElementsByTagName(tempStr);
171 
172  if (connectionList) {
173  XMLSize_t numberOfConnections = connectionList->getLength();
174 
175  for (XMLSize_t iConnection = 0; iConnection < numberOfConnections; ++iConnection) {
176  xercesc::DOMNode* connectionNode = connectionList->item(iConnection);
177 
178  if (connectionNode) {
179  char* connectionName =
180  xercesc::XMLString::transcode(connectionNode->getAttributes()->item(0)->getNodeValue());
181  std::string sConnectionName = connectionName;
182  xercesc::XMLString::release(&connectionName);
183 
184  // Locate the credential
186  std::map<std::string, cond::XMLAuthenticationService::DataSourceEntry*>::iterator iConnection =
187  m_data.find(sConnectionName);
188  if (iConnection != m_data.end()) {
189  credential = iConnection->second;
190  // Issue a warning here.
191  //coral::MessageStream log( this, this->name(),seal::Msg::Verbose );
192  log << coral::Debug << "Credential parameters for connection string \"" << sConnectionName
193  << "\" have already been defined. Only new elements are appended, while existing will be ignored."
194  << coral::MessageStream::endmsg;
195  } else {
196  credential = new cond::XMLAuthenticationService::DataSourceEntry(this->name(), sConnectionName);
197  m_data.insert(std::make_pair(sConnectionName, credential));
198  }
199 
200  xercesc::DOMNodeList* parameterList = connectionNode->getChildNodes();
201 
202  if (parameterList) {
203  XMLSize_t numberOfParameters = parameterList->getLength();
204 
205  for (XMLSize_t iParameter = 0; iParameter < numberOfParameters; ++iParameter) {
206  xercesc::DOMNode* parameterNode = parameterList->item(iParameter);
207 
208  if (parameterNode && parameterNode->getNodeType() == xercesc::DOMNode::ELEMENT_NODE) {
209  char* nodeName = xercesc::XMLString::transcode(parameterNode->getNodeName());
210  std::string sNodeName = nodeName;
211  xercesc::XMLString::release(&nodeName);
212 
213  if (sNodeName == "parameter") { // The default parameters
214  char* parameterName =
215  xercesc::XMLString::transcode(parameterNode->getAttributes()->item(0)->getNodeValue());
216  std::string sParameterName = parameterName;
217  xercesc::XMLString::release(&parameterName);
218  char* parameterValue =
219  xercesc::XMLString::transcode(parameterNode->getAttributes()->item(1)->getNodeValue());
220  std::string sParameterValue = parameterValue;
221  xercesc::XMLString::release(&parameterValue);
222 
223  credential->appendCredentialItem(sParameterName, sParameterValue);
224  } else if (sNodeName == "role") { // A role
225  char* roleName =
226  xercesc::XMLString::transcode(parameterNode->getAttributes()->item(0)->getNodeValue());
227  std::string sRoleName = roleName;
228  xercesc::XMLString::release(&roleName);
229 
230  // Retrieve the parameters for the role
231  xercesc::DOMNodeList* roleParameterList = parameterNode->getChildNodes();
232 
233  if (roleParameterList) {
234  XMLSize_t numberOfRoleParameters = roleParameterList->getLength();
235 
236  for (XMLSize_t iRoleParameter = 0; iRoleParameter < numberOfRoleParameters; ++iRoleParameter) {
237  xercesc::DOMNode* roleParameterNode = roleParameterList->item(iRoleParameter);
238  if (roleParameterNode && roleParameterNode->getNodeType() == xercesc::DOMNode::ELEMENT_NODE) {
239  char* roleNodeName = xercesc::XMLString::transcode(roleParameterNode->getNodeName());
240  std::string sRoleNodeName = roleNodeName;
241  xercesc::XMLString::release(&roleNodeName);
242 
243  if (sRoleNodeName == "parameter") {
244  char* roleParameterName = xercesc::XMLString::transcode(
245  roleParameterNode->getAttributes()->item(0)->getNodeValue());
246  std::string sRoleParameterName = roleParameterName;
247  xercesc::XMLString::release(&roleParameterName);
248  char* roleParameterValue = xercesc::XMLString::transcode(
249  roleParameterNode->getAttributes()->item(1)->getNodeValue());
250  std::string sRoleParameterValue = roleParameterValue;
251  xercesc::XMLString::release(&roleParameterValue);
252 
253  credential->appendCredentialItemForRole(sRoleParameterName, sRoleParameterValue, sRoleName);
254  }
255  }
256  }
257  }
258  }
259  }
260  }
261  }
262  }
263  }
264  }
265 
266  parser.reset();
267  } catch (const xercesc::XMLException& toCatch) {
268  char* message = xercesc::XMLString::transcode(toCatch.getMessage());
269  //coral::MessageStream log( this, this->name(),coral::Msg::Verbose );
270  //log << coral::Msg::Error << message << coral::flush;
271  log << coral::Error << std::string(message) << coral::MessageStream::endmsg;
272  xercesc::XMLString::release(&message);
273  result = false;
274  } catch (const xercesc::DOMException& toCatch) {
275  char* message = xercesc::XMLString::transcode(toCatch.msg);
276  //seal::MessageStream log( this, this->name(),seal::Msg::Verbose );
277  //log << seal::Msg::Error << message << seal::flush;
278  log << coral::Error << std::string(message) << coral::MessageStream::endmsg;
279  xercesc::XMLString::release(&message);
280  result = false;
281  } catch (const xercesc::SAXException& toCatch) {
282  char* message = xercesc::XMLString::transcode(toCatch.getMessage());
283  //seal::MessageStream log( this, this->name(),seal::Msg::Verbose );
284  //log << seal::Msg::Error << message << seal::flush;
285  log << coral::Error << std::string(message) << coral::MessageStream::endmsg;
286  xercesc::XMLString::release(&message);
287  result = false;
288  } catch (...) {
289  //seal::MessageStream log( this, this->name(),seal::Msg::Verbose );
290  //log << seal::Msg::Error << "Unexpected Exception parsing file \"" << inputFileName << "\"" << seal::flush;
291  log << coral::Error << "Unexpected Exception parsing file \"" << inputFileName << "\""
292  << coral::MessageStream::endmsg;
293  result = false;
294  }
295  if (memBufInputSource)
296  delete memBufInputSource;
297  return result;
298 }
299 
301  coral::MessageStream log("cond::XMLAuthenticationService::initialize");
302  std::set<std::string> inputFileNames = this->verifyFileName();
303  if (inputFileNames.empty()) {
304  //seal::MessageStream log( this, this->name(),seal::Msg::Verbose );
305  //std::cout<< "Could not open \"" << m_inputFileName << "\" for reading" << std::endl;
306  log << coral::Debug << "Could not open \"" << m_inputFileName << "\" for reading" << coral::MessageStream::endmsg;
307  return false;
308  }
309 
310  try {
312  } catch (const xercesc::XMLException& toCatch) {
313  char* message = xercesc::XMLString::transcode(toCatch.getMessage());
314  //seal::MessageStream log( this, this->name(),seal::Msg::Verbose );
315  //log << seal::Msg::Error << message << seal::flush;
316  log << coral::Error << std::string(message) << coral::MessageStream::endmsg;
317  xercesc::XMLString::release(&message);
318  return false;
319  }
320 
321  bool result = false;
322  for (std::set<std::string>::const_reverse_iterator iFileName = inputFileNames.rbegin();
323  iFileName != inputFileNames.rend();
324  ++iFileName) {
325  if (this->processFile(*iFileName)) {
326  result = true;
327  }
328  }
329 
331 
332  m_isInitialized = result;
333  if (!m_isInitialized)
334  reset();
335  return result;
336 }
337 
339  for (std::map<std::string, cond::XMLAuthenticationService::DataSourceEntry*>::iterator iConnection = m_data.begin();
340  iConnection != m_data.end();
341  ++iConnection)
342  delete iConnection->second;
343  m_data.clear();
344  m_isInitialized = false;
345 }
346 
348  const std::string& connectionString) const {
349  boost::mutex::scoped_lock lock(m_mutexLock);
350  if (!m_isInitialized) {
351  const_cast<cond::XMLAuthenticationService::XMLAuthenticationService*>(this)->initialize();
352  }
353  std::map<std::string, cond::XMLAuthenticationService::DataSourceEntry*>::const_iterator iConnection =
354  m_data.find(connectionString);
355  if (iConnection == m_data.end())
356  throw coral::UnknownConnectionException(this->name(), connectionString);
357  return iConnection->second->credentials();
358 }
359 
361  const std::string& connectionString, const std::string& role) const {
362  boost::mutex::scoped_lock lock(m_mutexLock);
363  if (!m_isInitialized) {
364  const_cast<cond::XMLAuthenticationService::XMLAuthenticationService*>(this)->initialize();
365  }
366  std::map<std::string, cond::XMLAuthenticationService::DataSourceEntry*>::const_iterator iConnection =
367  m_data.find(connectionString);
368  if (iConnection == m_data.end())
369  throw coral::UnknownConnectionException(this->name(), connectionString);
370  return iConnection->second->credentials(role);
371 }
372 
374  coral::MessageStream log("cond::XMLAuthenticationService::verifyFileName");
375  std::set<std::string> fileNames;
376 
377  // Try the file name as is...
378  std::filesystem::path filePath(m_inputFileName);
379  if (std::filesystem::exists(m_inputFileName)) {
380  if (std::filesystem::is_directory(m_inputFileName)) {
381  //seal::MessageStream log( this, this->name(),seal::Msg::Verbose );
382  log << coral::Error << "Provided path \"" << m_inputFileName << "\" is a directory."
383  << coral::MessageStream::endmsg;
384  return fileNames;
385  }
386  std::filesystem::path fullPath = filePath.lexically_normal();
387  fileNames.insert(fullPath.string());
388  if (filePath.is_absolute())
389  return fileNames;
390  }
391 
392  // Try to find other files in the path variable
393  const char* thePathVariable = std::getenv("CORAL_AUTH_PATH");
394  if (!thePathVariable)
395  return fileNames;
396  log << coral::Debug << "File \"" << m_inputFileName
397  << "\" not found in the current directory. Trying in the search path." << coral::MessageStream::endmsg;
398 
399  std::string searchPath(thePathVariable);
400  //std::cout<<"searchPath "<<searchPath<<std::endl;
401  if (std::filesystem::exists(searchPath)) {
402  if (!std::filesystem::is_directory(searchPath)) {
403  log << coral::Debug << "Search path \"" << searchPath << "\" is not a directory." << coral::MessageStream::endmsg;
404  return fileNames;
405  }
406  std::filesystem::path fullPath(searchPath);
407  fullPath /= filePath;
408  fileNames.insert(fullPath.string());
409  } else {
410  log << coral::Debug << "Search path \"" << searchPath << "\" does not exist." << coral::MessageStream::endmsg;
411  return fileNames;
412  }
413 
414  return fileNames;
415 }
416 
417 DEFINE_CORALSERVICE(cond::XMLAuthenticationService::XMLAuthenticationService, "COND/Services/XMLAuthenticationService");
cond::XMLAuthenticationService::XMLAuthenticationService::reset
void reset()
Reset parsed data.
Definition: XMLAuthenticationService.cc:338
Herwig7_Dummy_ReadLHE_GEN_SIM.numberOfParameters
numberOfParameters
Definition: Herwig7_Dummy_ReadLHE_GEN_SIM.py:6
funct::false
false
Definition: Factorize.h:29
Exception.h
cond::XMLAuthenticationService::DataSourceEntry
Definition: XMLAuthenticationService.h:27
contentValuesFiles.fullPath
fullPath
Definition: contentValuesFiles.py:64
cond::XMLAuthenticationService::XMLAuthenticationService::~XMLAuthenticationService
~XMLAuthenticationService() override
Standard Destructor.
Definition: XMLAuthenticationService.cc:91
generateEDF.cont
cont
load Luminosity info ##
Definition: generateEDF.py:629
writedatasetfile.parser
parser
Definition: writedatasetfile.py:7
cms::Exception::what
char const * what() const noexcept override
Definition: Exception.cc:103
serviceName
static const std::string serviceName
Definition: CredentialStore.cc:31
edmScanValgrind.buffer
buffer
Definition: edmScanValgrind.py:171
cond::FileReader
Definition: FileUtils.h:7
cond::XMLAuthenticationService::XMLAuthenticationService
Definition: XMLAuthenticationService.h:80
Debug
const bool Debug
Definition: CosmicMuonParameters.h:12
cond::XMLAuthenticationService::DataSourceEntry::DataSourceEntry
DataSourceEntry()=delete
InefficientDoubleROC.inputFileName
inputFileName
Definition: InefficientDoubleROC.py:437
coral
Definition: Binary.h:9
CoralServiceMacros.h
cond::XMLAuthenticationService::DataSourceEntry::~DataSourceEntry
~DataSourceEntry()
Destructor.
Definition: XMLAuthenticationService.cc:39
Service
dt4ml_dqm_sourceclient-live_cfg.filePath
filePath
CUSTOMIZE FOR ML.
Definition: dt4ml_dqm_sourceclient-live_cfg.py:45
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
valtools.processFile
def processFile(file, outputDir)
Definition: valtools.py:305
fetchall_from_DQM_v2.release
release
Definition: fetchall_from_DQM_v2.py:92
CommonMethods.lock
def lock()
Definition: CommonMethods.py:82
leef::Error
edm::ErrorSummaryEntry Error
Definition: LogErrorEventFilter.cc:29
cond::XMLAuthenticationService::DataSourceEntry::appendCredentialItemForRole
void appendCredentialItemForRole(const std::string &item, const std::string &value, const std::string &role)
Definition: XMLAuthenticationService.cc:51
dtResolutionTest_cfi.inputFile
inputFile
Definition: dtResolutionTest_cfi.py:14
Xerces.h
cms::concurrency::xercesTerminate
void xercesTerminate()
Definition: Xerces.cc:23
value
Definition: value.py:1
cond::persistency::Exception
Base exception class for the object to relational access.
Definition: Exception.h:11
cond::XMLAuthenticationService::XMLAuthenticationService::verifyFileName
std::set< std::string > verifyFileName()
Verifies the existence of the authentication files.
Definition: XMLAuthenticationService.cc:373
B2GTnPMonitor_cfi.item
item
Definition: B2GTnPMonitor_cfi.py:147
cond::XMLAuthenticationService::DataSourceEntry::credentials
const coral::IAuthenticationCredentials & credentials() const
Definition: XMLAuthenticationService.cc:61
cond::XMLAuthenticationService::XMLAuthenticationService::initialize
bool initialize()
Service framework related initialization.
Definition: XMLAuthenticationService.cc:300
cond::XMLAuthenticationService::DataSourceEntry::appendCredentialItem
void appendCredentialItem(const std::string &item, const std::string &value)
Definition: XMLAuthenticationService.cc:46
instance
static PFTauRenderPlugin instance
Definition: PFTauRenderPlugin.cc:70
cond::XMLAuthenticationService::XMLAuthenticationService::m_callbackID
coral::Property::CallbackID m_callbackID
Definition: XMLAuthenticationService.h:132
cond::XMLAuthenticationService::XMLAuthenticationService::processFile
bool processFile(const std::string &inputFileName)
Parses an xml file.
Definition: XMLAuthenticationService.cc:108
SiStripCommissioningSource_FromEDM_cfg.inputPath
inputPath
Definition: SiStripCommissioningSource_FromEDM_cfg.py:8
cond::XMLAuthenticationService::XMLAuthenticationService::XMLAuthenticationService
XMLAuthenticationService(const std::string &name)
Standard Constructor.
Definition: XMLAuthenticationService.cc:78
DEFINE_CORALSERVICE
#define DEFINE_CORALSERVICE(type, name)
Definition: CoralServiceMacros.h:7
cond::auth::COND_AUTH_PATH_PROPERTY
static constexpr const char *const COND_AUTH_PATH_PROPERTY
Definition: Auth.h:43
XML_AUTHENTICATION_FILE
constexpr char XML_AUTHENTICATION_FILE[]
Definition: XMLAuthenticationService.cc:30
FileUtils.h
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
dqm-mbProfile.log
log
Definition: dqm-mbProfile.py:17
LaserTracksInput_cfi.fileNames
fileNames
Definition: LaserTracksInput_cfi.py:8
Auth.h
reset
void reset(double vett[256])
Definition: TPedValues.cc:11
mps_fire.result
result
Definition: mps_fire.py:311
castor_dqm_sourceclient_file_cfg.path
path
Definition: castor_dqm_sourceclient_file_cfg.py:37
cond::XMLAuthenticationService::XMLAuthenticationService::setAuthenticationPath
void setAuthenticationPath(const std::string &inputPath)
Sets the input file name.
Definition: XMLAuthenticationService.cc:98
cond::XMLAuthenticationService::XMLAuthenticationService::credentials
const coral::IAuthenticationCredentials & credentials(const std::string &connectionString) const override
Definition: XMLAuthenticationService.cc:347
l1RCTOmdsFedVectorProducer_cfi.connectionString
connectionString
Definition: l1RCTOmdsFedVectorProducer_cfi.py:4
crabWrapper.key
key
Definition: crabWrapper.py:19
XMLAuthenticationService.h
mitigatedMETSequence_cff.inputFileNames
inputFileNames
Definition: mitigatedMETSequence_cff.py:35
cms::concurrency::xercesInitialize
void xercesInitialize()
Definition: Xerces.cc:18