TJAlienCredentials.h 3.1 KB
Newer Older
Nikola Hardi's avatar
Nikola Hardi committed
1
2
3
// Author: Nikola Hardi   3/6/2019
#ifndef ROOT_TJAlienCredentials
#define ROOT_TJAlienCredentials
4
5
#include <cstring>
#include <iostream>
Nikola Hardi's avatar
Nikola Hardi committed
6
#include <map>
7
#include <string>
8
#include <termios.h>
Nikola Hardi's avatar
Nikola Hardi committed
9

10
11
12
13
14
15
16
17
18
19
20
#define __FILENAMEEXT__                                                        \
  (strrchr(__FILE__, '/') ? std::string(strrchr(__FILE__, '/') + 1)            \
                          : std::string(__FILE__))
#define __FILENAME__ __FILENAMEEXT__.substr(0, __FILENAMEEXT__.find('.'))
#define INFO(message)                                                          \
  std::cout << "\rInfo in <" << __FILENAME__ << "::" << __func__               \
            << ">: " << message << std::endl
#define ERROR(message)                                                         \
  std::cerr << "\rError in <" << __FILENAME__ << "::" << __func__              \
            << ">: " << message << std::endl

Nikola Hardi's avatar
Nikola Hardi committed
21
using std::map;
22
using std::string;
Nikola Hardi's avatar
Nikola Hardi committed
23

24
25
26
27
28
29
enum CredentialsKind {
  cNOT_FOUND = -1,
  cJBOX_TOKEN = 0,
  cFULL_GRID_CERT,
  cJOB_TOKEN,
  cOTHER_TOKEN,
Nikola Hardi's avatar
Nikola Hardi committed
30
31
};

32
class TJAlienCredentialsObject {
33
34
35
36
37
38
public:
  string certpath;
  string keypath;
  string password;
  CredentialsKind kind;
  bool autoremove;
Nikola Hardi's avatar
Nikola Hardi committed
39

40
  TJAlienCredentialsObject() {}
Nikola Hardi's avatar
Nikola Hardi committed
41

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  TJAlienCredentialsObject(string certpath, string keypath,
                           CredentialsKind kind = cOTHER_TOKEN,
                           bool autoremove = false) {
    this->certpath = certpath;
    this->keypath = keypath;
    this->kind = kind;
    this->autoremove = autoremove;
  };

  void wipe() {
    if (autoremove) {
      int gDebug = std::getenv("gDebug") ? std::stoi(std::getenv("gDebug")) : 0;
      if (gDebug)
        INFO("removing safe files: " << certpath.c_str() << keypath.c_str());
      remove(certpath.c_str());
      remove(keypath.c_str());
58
    }
59
  }
60

61
62
63
64
65
  bool exists();
  const string getKey();
  const string getCertificate();
  const string getPassword();
  void readPassword();
Nikola Hardi's avatar
Nikola Hardi committed
66
67
};

68
class TJAlienCredentials {
Nikola Hardi's avatar
Nikola Hardi committed
69
70
public:
  TJAlienCredentials();
71
72
  ~TJAlienCredentials();

73
74
  static string getTmpDir();
  static string getHomeDir();
Nikola Hardi's avatar
Nikola Hardi committed
75
76
  void loadCredentials();

77
78
79
  bool has(CredentialsKind kind) const;
  TJAlienCredentialsObject get(CredentialsKind kind) const;
  TJAlienCredentialsObject get();
80
81
  void removeCredentials(CredentialsKind kind);
  short count();
82
83
84
85
  void selectPreferedCredentials();
  CredentialsKind getPreferedCredentials() const;
  const string& getMessages() const;
  bool checkCertValidity(const char *path);
Nikola Hardi's avatar
Nikola Hardi committed
86

87
88
  static const char *ENV_JOBTOKEN_KEY;
  static const char *ENV_JOBTOKEN_CERT;
89
90
  static const char *TMP_JOBTOKEN_KEY_FNAME_PREFIX;
  static const char *TMP_JOBTOKEN_CERT_FNAME_PREFIX;
91

Nikola Hardi's avatar
Nikola Hardi committed
92
private:
93
  CredentialsKind preferedCredentials;
Nikola Hardi's avatar
Nikola Hardi committed
94
95
  void loadTokenCertificate();
  void loadFullGridCertificate();
96
  void loadJobTokenCertificate();
Nikola Hardi's avatar
Nikola Hardi committed
97
98
99
100
101
  string getUsercertPath();
  string getUserkeyPath();
  string getTokencertPath();
  string getTokenkeyPath();

102
103
  string getSafeFilename(const string &prefix);
  void writeSafeFile(const string &filepath, const string &content);
104

Nikola Hardi's avatar
Nikola Hardi committed
105
106
  string tmpdir;
  string homedir;
107
  string msg;
Nikola Hardi's avatar
Nikola Hardi committed
108
109
110
  map<CredentialsKind, TJAlienCredentialsObject> found_credentials;
};
#endif