#include "TJAlienCredentials.h" #include #include #include #include #include #include using std::ifstream; 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"; 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(); loadJobTokenCertificate(); } 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; } } 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()); } } bool TJAlienCredentials::has(CredentialsKind kind) { return found_credentials.count(kind) == 1; } TJAlienCredentialsObject TJAlienCredentials::get(CredentialsKind kind) { if(this->has(kind)) { return found_credentials[kind]; } else { return TJAlienCredentialsObject(); } } 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(); } string TJAlienCredentialsObject::getKey() { return readFile(keypath.c_str()); } string TJAlienCredentialsObject::getCertificate() { 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()); } }