~ubuntu-branches/ubuntu/wily/davix/wily

« back to all changes in this revision

Viewing changes to src/modules/modules_profiles.cpp

  • Committer: Package Import Robot
  • Author(s): Mattias Ellert
  • Date: 2014-07-08 09:59:36 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20140708095936-imto9hahxsnxwvw5
Tags: 0.3.1-1
Update to version 0.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "modules_profiles.hpp"
 
2
#include <sstream>
 
3
 
 
4
#include <davixcontext.hpp>
 
5
#include <utils/davix_logger_internal.hpp>
 
6
#include <system_utils/env_utils.hpp>
 
7
 
 
8
namespace Davix{
 
9
 
 
10
 
 
11
struct GridEnv{
 
12
    std::string cert_path;
 
13
    std::string key_path;
 
14
    std::string ca_path;
 
15
};
 
16
 
 
17
GridEnv createGridEnv(){
 
18
 
 
19
    DAVIX_TRACE("Enable GRID profile for DAVIX");
 
20
 
 
21
    GridEnv env;
 
22
    env.ca_path = EnvUtils::getEnv("X509_CERT_DIR", "/etc/grid-security/certificates/");
 
23
    DAVIX_TRACE("Add CA path %s to valid CA path list", env.ca_path.c_str());
 
24
 
 
25
    std::string proxy = EnvUtils::getEnv("X509_USER_PROXY", std::string());
 
26
    if(proxy.size() ==0){
 
27
        std::ostringstream ss;
 
28
        ss << "/tmp/x509up_u" << geteuid();
 
29
        proxy = ss.str();
 
30
        if(access(proxy.c_str(), R_OK) !=0){
 
31
            DAVIX_LOG(DAVIX_LOG_WARNING, "Unable to read proxy file %s", proxy.c_str());
 
32
            proxy.clear();
 
33
        }
 
34
    }
 
35
    if(proxy.size() > 0){
 
36
        DAVIX_TRACE("Define %s proxy certificate for use", proxy.c_str());
 
37
        env.cert_path = env.key_path = proxy;
 
38
    }else{
 
39
        // No proxy, load simply creds
 
40
        env.key_path = EnvUtils::getEnv("X509_USER_KEY", std::string());
 
41
        env.cert_path = EnvUtils::getEnv("X509_USER_CERT", std::string());
 
42
        DAVIX_TRACE("Define to use GRID key %s and GRID cert %s ", env.key_path.c_str(), env.cert_path.c_str());
 
43
    }
 
44
    return env;
 
45
}
 
46
 
 
47
 
 
48
void awesomeGridHook(RequestParams& p, HttpRequest & req, Uri & u, RequestPreRunHook previous_hook, GridEnv env_grid){
 
49
 
 
50
    // initialize environment
 
51
    // add grid CA path
 
52
    if(env_grid.ca_path.size() >0){
 
53
        p.addCertificateAuthorityPath(env_grid.ca_path);
 
54
    }
 
55
    // if no cert auth configured, configure one
 
56
    if(env_grid.key_path.size() > 0){
 
57
        X509Credential x509;
 
58
        DavixError* tmp_err=NULL;
 
59
        if( x509.loadFromFilePEM(env_grid.key_path, env_grid.cert_path, "", &tmp_err) <0){
 
60
            DAVIX_LOG(DAVIX_LOG_WARNING, "Impossible to load GRID certificate %s %s: %s",
 
61
                      env_grid.key_path.c_str(),
 
62
                      env_grid.cert_path.c_str(),
 
63
                      tmp_err->getErrMsg().c_str());
 
64
        }else{
 
65
            // in current state, GRID profiles ignore all manually defined callbacks
 
66
            p.setClientCertCallbackX509(NULL, NULL);
 
67
            p.setClientCertX509(x509);
 
68
        }
 
69
    }
 
70
 
 
71
    if(previous_hook){
 
72
        previous_hook(p, req, u);
 
73
    }
 
74
}
 
75
 
 
76
 
 
77
void loadGridProfile(Context & context){
 
78
    GridEnv grid_env = createGridEnv();
 
79
 
 
80
    RequestPreRunHook previous_hook = context.getHook<RequestPreRunHook>();
 
81
    RequestPreRunHook new_hook = std::bind(awesomeGridHook, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, previous_hook, grid_env);
 
82
    context.setHook<RequestPreRunHook>(new_hook);
 
83
}
 
84
 
 
85
}