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