TJAlienCredentials.cxx 4.64 KB
Newer Older
Nikola Hardi's avatar
Nikola Hardi committed
1
2
3
4
#include "TJAlienCredentials.h"
#include <sstream>
#include <cstdlib>
#include <unistd.h>
Nikola Hardi's avatar
Nikola Hardi committed
5
#include <fstream>
6
7
#include <cstdlib>
#include <iostream>
Nikola Hardi's avatar
Nikola Hardi committed
8
9

using std::ifstream;
10
11
12
13
14
15
16
17
18
using std::ofstream;
using std::stringstream;
using std::endl;
using std::getenv;

const char* TJAlienCredentials::ENV_JOBTOKEN_KEY = "JALIEN_TOKEN_KEY";
const char* TJAlienCredentials::ENV_JOBTOKEN_CERT = "JALIEN_TOKEN_CERT";
const char* TJAlienCredentials::TMP_JOBTOKEN_KEY_FNAME = "tmpjobtokenkey.pem";
const char* TJAlienCredentials::TMP_JOBTOKEN_CERT_FNAME = "tmpjobtokencert.pem";
Nikola Hardi's avatar
Nikola Hardi committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

string TJAlienCredentials::getTmpDir() {
  string tmpdir;

  if (getenv("TMPDIR") != NULL)
    tmpdir = getenv("TMPDIR");
  else if (getenv("TMP") != NULL)
    tmpdir = getenv("TMP");
  else if (getenv("TEMP") != NULL)
    tmpdir = getenv("TEMP");
  else
    tmpdir = P_tmpdir;

  return tmpdir;
}

string TJAlienCredentials::getHomeDir() {
  string homedir;

  if (getenv("HOME") != NULL)
    homedir = getenv("HOME");
  else
    homedir = "~";

  return homedir;
}

string TJAlienCredentials::getTokencertPath()
{
  std::stringstream tokencert_s;
  tokencert_s << tmpdir << "/tokencert_" << getuid() << ".pem";
  std::string tokencert = tokencert_s.str();
  std::string tokencertpath = std::getenv("JALIEN_TOKEN_CERT") ? : tokencert;

  return tokencertpath;
}

string TJAlienCredentials::getTokenkeyPath()
{
  std::stringstream tokenkey_s;
  tokenkey_s << tmpdir << "/tokenkey_" << getuid() << ".pem";
  std::string tokenkey = tokenkey_s.str();
  std::string tokenkeypath = std::getenv("JALIEN_TOKEN_KEY") ? : tokenkey;

  return tokenkeypath;
}

string TJAlienCredentials::getUsercertPath()
{
    std::string usercert = jcf.sUsercert.Data()[0] != '\0' ? jcf.sUsercert.Data() : homedir + "/.globus/usercert.pem";
    std::string usercertpath = std::getenv("X509_USER_CERT") ? : usercert;
    return usercertpath;
}

string TJAlienCredentials::getUserkeyPath()
{
    std::string userkey = jcf.sUserkey.Data()[0] != '\0' ? jcf.sUserkey.Data() : homedir + "/.globus/userkey.pem";
    std::string userkeypath = std::getenv("X509_USER_KEY") ? : userkey;
    return userkeypath;
}

TJAlienCredentials::TJAlienCredentials() {
    tmpdir = getTmpDir();
    homedir = getHomeDir();
    loadCredentials();
}

void TJAlienCredentials::loadCredentials() {
  found_credentials.clear();
  loadTokenCertificate();
  loadFullGridCertificate();
90
  loadJobTokenCertificate();
Nikola Hardi's avatar
Nikola Hardi committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
}

void TJAlienCredentials::loadTokenCertificate() {
    TJAlienCredentialsObject token_credentials(getTokencertPath(), getTokenkeyPath(), cJBOX_TOKEN);

    if(token_credentials.exists()) {
        found_credentials[cJBOX_TOKEN] = token_credentials;
    }
}

void TJAlienCredentials::loadFullGridCertificate() {
    TJAlienCredentialsObject grid_certificate(getUsercertPath(), getUserkeyPath(), cFULL_GRID_CERT);

    if(grid_certificate.exists()) {
        found_credentials[cFULL_GRID_CERT] = grid_certificate;
    }
}

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
void TJAlienCredentials::loadJobTokenCertificate() {
    const char *env_cert = getenv(ENV_JOBTOKEN_CERT);
    const char *env_key = getenv(ENV_JOBTOKEN_KEY);

    // if it doesn't have both environment variables
    if(!env_cert || !env_key) {
        return;
    }

    string tmpcertpath = getTmpDir() + "/" + TMP_JOBTOKEN_CERT_FNAME;
    ofstream certFile(tmpcertpath);
    certFile << env_cert;
    certFile.close();

    string tmpkeypath = getTmpDir() + "/" + TMP_JOBTOKEN_KEY_FNAME;
    ofstream keyFile(tmpkeypath);
    keyFile << env_key;
    keyFile.close();

    TJAlienCredentialsObject jobtoken_certificate(tmpcertpath, tmpkeypath, cJOB_TOKEN);
    if(jobtoken_certificate.exists()) {
        found_credentials[cJOB_TOKEN] = jobtoken_certificate;
    } else {
        remove(tmpkeypath.c_str());
        remove(tmpcertpath.c_str());
    }

}

Nikola Hardi's avatar
Nikola Hardi committed
138
139
140
141
142
143
144
bool TJAlienCredentials::has(CredentialsKind kind) {
    return found_credentials.count(kind) == 1;
}

TJAlienCredentialsObject TJAlienCredentials::get(CredentialsKind kind) {
    if(this->has(kind)) {
        return found_credentials[kind];
145
146
    } else {
      return TJAlienCredentialsObject();
Nikola Hardi's avatar
Nikola Hardi committed
147
148
    }
}
Nikola Hardi's avatar
Nikola Hardi committed
149

150
151
152
153
154
155
156
157
158
159
160
161
162
string readFile(const char* filename) {
    string line;
    stringstream contents;
    ifstream f(filename);

    if(f.is_open()) {
        while(getline(f, line)) {
            contents << line << endl;
        }
    }
    return contents.str();
}

Nikola Hardi's avatar
Nikola Hardi committed
163
string TJAlienCredentialsObject::getKey() {
164
    return readFile(keypath.c_str());
Nikola Hardi's avatar
Nikola Hardi committed
165
166
167
}

string TJAlienCredentialsObject::getCertificate() {
168
169
170
171
172
173
174
175
176
    return readFile(certpath.c_str());
}

TJAlienCredentials::~TJAlienCredentials() {
    if(has(cJOB_TOKEN)) {
        TJAlienCredentialsObject creds = get(cJOB_TOKEN);
        remove(creds.certpath.c_str());
        remove(creds.keypath.c_str());
    }
Nikola Hardi's avatar
Nikola Hardi committed
177
}