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