-
Notifications
You must be signed in to change notification settings - Fork 9
/
rsakey.js
158 lines (138 loc) · 4.54 KB
/
rsakey.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/** This THiNX Device Management API module is responsible for managing RSA Keys. */
const fs = require("fs-extra");
const sha256 = require("sha256");
const { generateKeyPair } = require('crypto');
//const forge = require('node-forge');
let Globals = require("./globals");
const Util = require("./util");
const Sanitka = require("./sanitka"); var sanitka = new Sanitka();
module.exports = class RSAKey {
constructor() {
this.ssh_keys = Globals.app_config().ssh_keys;
}
getKeyPathsForOwner(owner) {
var files = fs.readdirSync(this.ssh_keys);
var result = files.filter(
file => ((file.indexOf(owner) !== -1) && (file.indexOf(".pub") === -1))
);
if (typeof(result) === "undefined") result = 0;
return result;
}
revokeUserKeys(revoked_filenames, owner) {
var revoked_keys = [];
var key_paths = this.getKeyPathsForOwner(owner);
console.log("🔨 [debug] revokeUserKeys Revoked filenames: ", revoked_filenames);
for (var kindex in revoked_filenames) {
if (!Util.isDefined(key_paths[kindex])) continue;
var priv_path = this.ssh_keys + "/" + key_paths[kindex];
var pub_path = this.ssh_keys + "/" + key_paths[kindex] + ".pub";
if (fs.existsSync(priv_path)) fs.unlinkSync(priv_path);
if (fs.existsSync(pub_path)) fs.unlinkSync(pub_path);
revoked_keys.push(key_paths[kindex]);
}
return revoked_keys;
}
exportKeysForList(key_paths, owner) {
var exportedKeys = [];
for (var kindex in key_paths) {
const key_data = fs.readFileSync(this.ssh_keys + "/" + key_paths[kindex] + ".pub");
var date = new Date(parseInt(key_paths[kindex].replace(owner+"-", ""), 10)).toString();
var info = {
name: date,
fingerprint: sha256(key_data.toString('utf8')),
date: date,
pubkey: key_data.toString('utf8'),
filename: key_paths[kindex]
};
exportedKeys.push(info);
}
return exportedKeys;
}
keyPathsExist(owner) {
var key_paths = this.getKeyPathsForOwner(owner);
if (typeof(key_paths) === "undefined") return 0;
return (key_paths.length > 0);
}
revokeUserKeysAndCallback(res, revoked_filenames, owner, callback) {
var revoked_keys = this.revokeUserKeys(revoked_filenames, owner);
if (typeof(callback) === "function") {
var success = (revoked_keys.length > 0) ? true : false;
callback(res, success, revoked_keys);
}
}
// public
/**
* Revoke RSA Key for owner
* @param {string} owner - owner._id
* @param {string} revoked_filenames - array od RSA Key fingerprints to be revoked
* @param {function} callback(success, message) - operation result callback
*/
revoke(owner, revoked_filenames, callback, res) {
if (!this.keyPathsExist(owner)) {
console.log("no_rsa_keys_found");
return callback(res, true, false);
}
this.revokeUserKeysAndCallback(res, revoked_filenames, owner, callback);
}
list(owner, callback) {
var key_paths = this.getKeyPathsForOwner(owner);
if (key_paths.count < 1) {
console.log("no_rsa_key_found");
return callback(true, []);
}
callback(true, this.exportKeysForList(key_paths, owner));
}
validateOwner(owner) {
let valid = sanitka.owner(owner);
return (valid == null) ? false : true;
}
newUniqueIdentifier() {
return new Date().getTime();
}
newNameWithOwner(i_owner, date) {
const owner = sanitka.owner(i_owner);
const filename = owner + "-" + date;
return this.ssh_keys + "/" + filename;
}
generate(i_owner, date, callback) {
const keyname = this.newNameWithOwner(i_owner, date);
const private_path = keyname;
const public_path = keyname + '.pub';
// Generates RSA Key pair
generateKeyPair('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: 'thinx' // TODO: once we'll have Secure Storage (e.g. Vault), keys can be encrypted but anyway, it will be accessible from this container using vault key env...
}
}, (err, publicKey, privateKey) => {
fs.writeFileSync(public_path, publicKey);
fs.fchmodSync(fs.openSync(public_path), 0o644);
fs.writeFileSync(private_path, privateKey);
fs.fchmodSync(fs.openSync(private_path), 0o600);
callback(err, publicKey, privateKey);
});
}
create(i_owner, callback) {
const date = this.newUniqueIdentifier();
const owner = sanitka.owner(i_owner);
const filename = this.newNameWithOwner(i_owner, date) + '.pub';
this.generate(owner, date, (err, pubKey /* , privKey */) => {
if (err) {
console.log(err);
return callback(false);
}
callback(true, {
name: date,
pubkey: pubKey,
filename: filename
});
});
}
};