dmlite  0.6
DomeTalker.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016 CERN
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 
19 /// @file DomeTalker.h
20 /// @brief Utility class, used to send requests to Dome
21 /// @author Georgios Bitzes <georgios.bitzes@cern.ch>
22 /// @date Mar 2016
23 
24 #ifndef UTILS_DOMETALKER_H
25 #define UTILS_DOMETALKER_H
26 
27 #include <davix/davix.hpp>
28 #include "DavixPool.h"
29 #include "cpp/authn.h"
30 
31 #include <boost/property_tree/ptree.hpp>
32 
33 namespace dmlite {
34 
36  /// The remote user that originated a request. Typically a DN
37  std::string clientName;
38  /// The remote address of the user's machine
39  std::string remoteAddress;
40  /// The groups the user belongs to
41  std::vector<std::string> groups;
42 
43  DomeCredentials(std::string cn, std::string ra, std::vector<std::string> gr) :
44  clientName(cn), remoteAddress(ra), groups(gr) {}
45 
48  if(ctx) {
49 
51  if (!clientName.size())
52  clientName = ctx->user.name;
53 
55 
56  for(size_t i = 0; i < ctx->groups.size(); i++) {
57  groups.push_back(ctx->groups[i].name);
58  }
59  }
60  }
61 
62 
63 
64 
65 };
66 
68  DOME_HTTP_OK = 200,
69 
75 
78 };
79 
80 int http_status(const DmException &e);
81 
82 class DmStatus;
83 int http_status(const DmStatus &e);
84 
85 class DomeTalker {
86 public:
87  DomeTalker(DavixCtxPool &pool, const DomeCredentials &creds, std::string uri, std::string verb, std::string cmd);
88  DomeTalker(DavixCtxPool &pool, std::string uri, std::string verb, std::string cmd);
89  ~DomeTalker();
90 
91  bool execute();
92  bool execute(const boost::property_tree::ptree &params);
93  bool execute(const std::string &str);
94  bool execute(const std::ostringstream &ss);
95 
96  // only send a single json param
97  bool execute(const std::string &key, const std::string &value);
98 
99  // only send two
100  bool execute(const std::string &key1, const std::string &value1,
101  const std::string &key2, const std::string &value2);
102 
103  // be generous, send three
104  bool execute(const std::string &key1, const std::string &value1,
105  const std::string &key2, const std::string &value2,
106  const std::string &key3, const std::string &value3);
107 
108  // get error message, if it exists
109  std::string err();
110 
111  // get response status
112  int status();
113 
114  // produce the appropriate code for a dmlite exception
115  int dmlite_code();
116 
117  const boost::property_tree::ptree& jresp();
118  const std::string& response();
119 
120  void setcommand(const DomeCredentials &creds, const char *verb, const char *cmd);
121 protected:
122  // This has to be in sync with XrdHttpReq.hh
123  static const char *reqTypes[12];
124  int getXrdHttpReqIndex(const char *verb) {
125  for (int i = 0; i < 12; i++) {
126  if (!strcmp(verb, reqTypes[i])) return i;
127  }
128  return 0;
129  }
130 
131  /// Calculate the same url hash as XrdHttp.
132  /// This avoids the ssl handshake when using XrdHTTP
133  void calcXrdHttpHashes(
134  char *hash,
135  const char *fn,
136  int16_t request,
137  const char *sslclientshortname,
138  const char *sslclientvorg,
139  const char *sslclienthost,
140  const char *sslclientdn,
141  time_t tim,
142  const char *key);
143 
145  const char *h1,
146  const char *h2);
147 private:
150  std::string uri_;
151  std::string verb_;
152  std::string cmd_;
153 
154  std::string target_;
155 
158 
159  Davix::DavixError *err_;
160  std::string response_;
161  boost::property_tree::ptree json_;
163  int status_;
164 };
165 
166 }
167 #endif
Definition: DomeTalker.h:72
std::string remoteAddress
Definition: authn.h:25
Definition: DomeTalker.h:85
Definition: DomeTalker.h:70
Definition: DomeTalker.h:73
std::string clientName
The remote user that originated a request. Typically a DN.
Definition: DomeTalker.h:37
const boost::property_tree::ptree & jresp()
std::string name
Definition: authn.h:43
std::string err()
void calcXrdHttpHashes(char *hash, const char *fn, int16_t request, const char *sslclientshortname, const char *sslclientvorg, const char *sslclienthost, const char *sslclientdn, time_t tim, const char *key)
Security context. To be created by the Authn.
Definition: authn.h:64
DomeTalker(DavixCtxPool &pool, const DomeCredentials &creds, std::string uri, std::string verb, std::string cmd)
std::string target_
Definition: DomeTalker.h:154
Definition: DavixPool.h:48
std::string response_
Definition: DomeTalker.h:160
Definition: DomeTalker.h:35
int getXrdHttpReqIndex(const char *verb)
Definition: DomeTalker.h:124
Definition: DomeTalker.h:77
int status_
Definition: DomeTalker.h:163
Definition: DavixPool.h:90
Definition: DomeTalker.h:68
Authentication API. Any sort of security check is plugin-specific.
Definition: DavixPool.h:97
DomeCredentials(std::string cn, std::string ra, std::vector< std::string > gr)
Definition: DomeTalker.h:43
std::vector< GroupInfo > groups
Definition: authn.h:75
UserInfo user
Definition: authn.h:74
DomeHttpCode
Definition: DomeTalker.h:67
DavixCtxPool & pool_
Definition: DomeTalker.h:148
void setcommand(const DomeCredentials &creds, const char *verb, const char *cmd)
Davix::DavixError * err_
Definition: DomeTalker.h:159
std::string remoteAddress
The remote address of the user&#39;s machine.
Definition: DomeTalker.h:39
Definition: DomeTalker.h:71
static const char * reqTypes[12]
Definition: DomeTalker.h:123
SecurityCredentials credentials
Definition: authn.h:72
const std::string & response()
std::string clientName
Definition: authn.h:24
std::string uri_
Definition: DomeTalker.h:150
bool parsedJson_
Definition: DomeTalker.h:162
std::string cmd_
Definition: DomeTalker.h:152
std::string verb_
Definition: DomeTalker.h:151
DavixStuff * ds_
Definition: DomeTalker.h:157
Definition: DomeTalker.h:74
DavixGrabber grabber_
Definition: DomeTalker.h:156
DomeCredentials()
Definition: DomeTalker.h:46
Definition: DomeTalker.h:76
int http_status(const DmException &e)
boost::property_tree::ptree json_
Definition: DomeTalker.h:161
int compareXrdHttpHashes(const char *h1, const char *h2)
DomeCredentials creds_
Definition: DomeTalker.h:149
std::vector< std::string > groups
The groups the user belongs to.
Definition: DomeTalker.h:41
DomeCredentials(const SecurityContext *ctx)
Definition: DomeTalker.h:47