-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeystore-cli.ts
More file actions
116 lines (101 loc) · 2.69 KB
/
keystore-cli.ts
File metadata and controls
116 lines (101 loc) · 2.69 KB
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
#!/usr/bin/env node
import * as readlineSync from 'readline-sync';
import { Command } from 'commander';
import { Keystore, KEY_DESCRIPTION, getKeyId } from './keystore';
import { execSync } from 'child_process';
import { VERSION } from "./version";
import * as fs from 'fs';
function getVersion() {
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
return packageJson.version;
}
async function keystoreInit() {
await keystoreUnlock();
const ks = new Keystore();
ks.encrypt();
}
async function keystoreUnlock() {
const password = readlineSync.question('Enter your password: ', {
hideEchoBack: true,
});
const keyName = KEY_DESCRIPTION;
const storeCommand = `keyctl add user ${keyName} ${password} @u`;
execSync(storeCommand).toString().trim();
}
async function keystoreLock() {
const keyId = getKeyId();
execSync(`keyctl unlink ${keyId} @u`);
console.log(`🔒 Key with ID ${keyId} invalidated`);
}
async function keystoreAdd(name: string) {
const value = readlineSync.question('Enter the value: ', {
hideEchoBack: true,
});
const value2 = readlineSync.question('Enter the value: ', {
hideEchoBack: true,
});
if (value !== value2) {
console.log('Values do not match');
return;
}
var ks;
try {
ks = Keystore.decrypt();
} catch (error) {
console.error('Error decrypting keystore');
return;
}
ks.addKeyValuePair(name, value);
ks.encrypt();
}
async function keystoreGet(name: string) {
var ks;
try {
ks = Keystore.decrypt();
} catch (error) {
console.error('Error decrypting keystore');
return;
}
const value = ks.getValue(name);
if (!value) {
console.error('Key not found');
return
}
console.log(value);
}
const program = new Command();
program
.name('keystore')
.description('CLI to manage keystore')
.version(VERSION);
program
.command('init')
.description('Initialize the keystore')
.action(async () => {
await keystoreInit();
});
program
.command('unlock')
.description('Unlock the keystore')
.action(async () => {
await keystoreUnlock();
});
program
.command('lock')
.description('Lock the keystore')
.action(async () => {
await keystoreLock();
});
program
.command('add <name>')
.description('Add a key-value pair to the keystore')
.action(async (name) => {
await keystoreAdd(name);
});
program
.command('get <name>')
.description('Get a value from the keystore')
.action(async (name) => {
await keystoreGet(name);
});
program.parse(process.argv);