TJAlienCredentials.h 2.08 KB
Newer Older
Nikola Hardi's avatar
Nikola Hardi committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Author: Nikola Hardi   3/6/2019
#ifndef ROOT_TJAlienCredentials
#define ROOT_TJAlienCredentials
#include <string>
#include <map>
#include "TObject.h"
#include "TJClientFile.h"

using std::string;
using std::map;

enum CredentialsKind { cJBOX_TOKEN = 0,
                       cFULL_GRID_CERT,
                       cJOB_TOKEN,
                       cOTHER_TOKEN,
};

18

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
struct TJAlienCredentialsObject {
    string certpath;
    string keypath;
    string source;
    CredentialsKind kind;

    TJAlienCredentialsObject() {}
    TJAlienCredentialsObject(string certpath,
                             string keypath,
                             CredentialsKind kind = cOTHER_TOKEN,
                             string source = "")
    {
        this->certpath = certpath;
        this->keypath = keypath;
        this->kind = kind;
        this->source = source;
    };

Nikola Hardi's avatar
Nikola Hardi committed
37
38
39
    bool exists() { return fileExists(certpath) && fileExists(keypath); }
    string getKey();
    string getCertificate();
Nikola Hardi's avatar
Nikola Hardi committed
40
41
42
43
44
45
46

private:
    bool fileExists(string filename)
    {
        bool fileExists = false;
        FILE *f = fopen(filename.c_str(), "r");

Nikola Hardi's avatar
Nikola Hardi committed
47
48
49
50
51
52
        if (f != NULL ) {
            fclose(f);
            fileExists = true;
        } else {
            fileExists = false;
        }
Nikola Hardi's avatar
Nikola Hardi committed
53
54
55
56
57
58
59
60

        return fileExists;
    }
};

class TJAlienCredentials : public TObject {
public:
  TJAlienCredentials();
61
62
  ~TJAlienCredentials();

Nikola Hardi's avatar
Nikola Hardi committed
63
64
65
66
67
68
69
  string getTmpDir();
  string getHomeDir();
  void loadCredentials();

  bool has(CredentialsKind kind);
  TJAlienCredentialsObject get(CredentialsKind kind);

70
71
72
73
74
  static const char *ENV_JOBTOKEN_KEY;
  static const char *ENV_JOBTOKEN_CERT;
  static const char *TMP_JOBTOKEN_KEY_FNAME;
  static const char *TMP_JOBTOKEN_CERT_FNAME;

Nikola Hardi's avatar
Nikola Hardi committed
75
76
77
private:
  void loadTokenCertificate();
  void loadFullGridCertificate();
78
  void loadJobTokenCertificate();
Nikola Hardi's avatar
Nikola Hardi committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  string getUsercertPath();
  string getUserkeyPath();
  string getTokencertPath();
  string getTokenkeyPath();

  string tmpdir;
  string homedir;
  map<CredentialsKind, TJAlienCredentialsObject> found_credentials;

  TJClientFile jcf;

  ClassDef(TJAlienCredentials, 0)
};
#endif