Debian Codegraph @

src/modules/modules_profiles.cpp

raw
1 /*
2 * This File is part of Davix, The IO library for HTTP based protocols
3 * Copyright (C) CERN 2013
4 * Author: Adrien Devresse <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include "modules_profiles.hpp"
23 #include <sstream>
24
25 #include <davixcontext.hpp>
26 #include <utils/davix_logger_internal.hpp>
27 #include <system_utils/env_utils.hpp>
28
29 namespace Davix{
30
31
32 struct GridEnv{
33 std::string cert_path;
34 std::string key_path;
35 std::string ca_path;
36 };
37
38 GridEnv createGridEnv(){
39
40 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_CORE, "Enable GRID profile for DAVIX");
41
42 GridEnv env;
43 env.ca_path = EnvUtils::getEnv("X509_CERT_DIR", "/etc/grid-security/certificates/");
44 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_CORE, "Adding CA path {} to valid CA path list", env.ca_path);
45
46 std::string proxy = EnvUtils::getEnv("X509_USER_PROXY", std::string());
47 std::string key = EnvUtils::getEnv("X509_USER_KEY", std::string());
48 std::string cert = EnvUtils::getEnv("X509_USER_CERT", std::string());
49
50 std::string standard_location = fmt::format("/tmp/x509up_u{}", geteuid());
51
52 if(!proxy.empty()) {
53 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_CORE, "Using X509_USER_PROXY to supply credentials: {}", proxy);
54 env.cert_path = env.key_path = proxy;
55 }
56 else if(access(standard_location.c_str(), R_OK) == 0) {
57 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_CORE, "Using standard location for proxy: {}", standard_location);
58 env.cert_path = env.key_path = standard_location;
59 }
60 else if(!cert.empty()) {
61 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_CORE, "Using X509_USER_CERT and X509_USER_KEY to supply credentials: {}, {}", cert, key);
62 env.cert_path = cert;
63 env.key_path = key;
64 if(key.empty()) env.key_path = cert;
65 }
66 else {
67 DAVIX_SLOG(DAVIX_LOG_WARNING, DAVIX_LOG_CORE, "Unable to find a proxy or cert/key pair using either X509_USER_* variables or {}", standard_location);
68 }
69
70 return env;
71 }
72
73
74 void awesomeGridHook(RequestParams& p, HttpRequest & req, Uri & u, RequestPreRunHook previous_hook, GridEnv env_grid){
75
76 // initialize environment
77 // add grid CA path
78 if(env_grid.ca_path.size() >0){
79 p.addCertificateAuthorityPath(env_grid.ca_path);
80 }
81 // if no cert auth configured, configure one
82 if(env_grid.cert_path.size() > 0){
83 X509Credential x509;
84 DavixError* tmp_err=NULL;
85 if( x509.loadFromFilePEM(env_grid.key_path, env_grid.cert_path, "", &tmp_err) <0){
86 DAVIX_SLOG(DAVIX_LOG_WARNING, DAVIX_LOG_CORE, "Impossible to load GRID certificate {} {}: {}",
87 env_grid.key_path,
88 env_grid.cert_path,
89 tmp_err->getErrMsg());
90
91 if(tmp_err){
92 std::cerr << "("<< tmp_err->getErrScope() <<") Error: "<< tmp_err->getErrMsg() << std::endl;
93 DavixError::clearError(&tmp_err);
94 }
95 }else{
96 // in current state, GRID profiles ignore all manually defined callbacks
97 p.setClientCertCallbackX509(NULL, NULL);
98 p.setClientCertX509(x509);
99 }
100 }
101
102 if(previous_hook){
103 previous_hook(p, req, u);
104 }
105 }
106
107
108 void loadGridProfile(Context & context){
109 GridEnv grid_env = createGridEnv();
110
111 RequestPreRunHook previous_hook = context.getHook<RequestPreRunHook>();
112 RequestPreRunHook new_hook = std::bind(awesomeGridHook, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, previous_hook, grid_env);
113 context.setHook<RequestPreRunHook>(new_hook);
114 }
115
116 }