envcraft-cli 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +350 -2
- package/package.json +38 -4
- package/src/cli-store.js +233 -0
- package/src/cli.js +236 -0
- package/src/config.js +90 -0
- package/src/manager.js +261 -0
- package/src/storage/db.js +309 -0
- package/src/storage/encryption.js +99 -0
- package/src/storage/firebase-db.js +241 -0
- package/src/storage/ssh-file-db.js +360 -0
- package/src/tui.js +130 -0
- package/src/utils/logger.js +78 -0
- package/src/utils/parser.js +45 -0
- package/src/vm.js +49 -0
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import sqlite3 from 'sqlite3';
|
|
2
|
+
import { SecureEncryption } from './encryption.js';
|
|
3
|
+
import { logError, logSuccess, logInfo } from '../utils/logger.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Secure database storage for environment variables
|
|
7
|
+
* - Encrypts all secret values
|
|
8
|
+
* - Maintains access logs
|
|
9
|
+
* - Supports access control
|
|
10
|
+
*/
|
|
11
|
+
export class SecureEnvDB {
|
|
12
|
+
constructor(dbFile = 'env-store.db', encryptionKey = null) {
|
|
13
|
+
this.dbFile = dbFile;
|
|
14
|
+
this.db = null;
|
|
15
|
+
|
|
16
|
+
if (!encryptionKey) {
|
|
17
|
+
throw new Error('Encryption key required. Generate one with: SecureEncryption.generateKey()');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
this.encryption = new SecureEncryption(encryptionKey);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Initialize the database with required tables
|
|
25
|
+
*/
|
|
26
|
+
async init() {
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
this.db = new sqlite3.Database(this.dbFile, (err) => {
|
|
29
|
+
if (err) reject(err);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Enable foreign keys
|
|
33
|
+
this.db.run('PRAGMA foreign_keys = ON', (err) => {
|
|
34
|
+
if (err) reject(err);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Create tables
|
|
38
|
+
this.db.serialize(() => {
|
|
39
|
+
// Secrets table
|
|
40
|
+
this.db.run(`
|
|
41
|
+
CREATE TABLE IF NOT EXISTS secrets (
|
|
42
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
43
|
+
key TEXT UNIQUE NOT NULL,
|
|
44
|
+
encrypted_value TEXT NOT NULL,
|
|
45
|
+
value_hash TEXT NOT NULL,
|
|
46
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
47
|
+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
48
|
+
created_by TEXT,
|
|
49
|
+
description TEXT
|
|
50
|
+
)
|
|
51
|
+
`);
|
|
52
|
+
|
|
53
|
+
// Access log table
|
|
54
|
+
this.db.run(`
|
|
55
|
+
CREATE TABLE IF NOT EXISTS access_logs (
|
|
56
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
57
|
+
secret_id INTEGER NOT NULL,
|
|
58
|
+
action TEXT NOT NULL,
|
|
59
|
+
user TEXT,
|
|
60
|
+
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
61
|
+
ip_address TEXT,
|
|
62
|
+
FOREIGN KEY (secret_id) REFERENCES secrets(id) ON DELETE CASCADE
|
|
63
|
+
)
|
|
64
|
+
`);
|
|
65
|
+
|
|
66
|
+
// Create indexes
|
|
67
|
+
this.db.run('CREATE INDEX IF NOT EXISTS idx_secrets_key ON secrets(key)');
|
|
68
|
+
this.db.run('CREATE INDEX IF NOT EXISTS idx_access_logs_secret ON access_logs(secret_id)');
|
|
69
|
+
this.db.run('CREATE INDEX IF NOT EXISTS idx_access_logs_timestamp ON access_logs(timestamp)');
|
|
70
|
+
|
|
71
|
+
resolve();
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Set a secret value
|
|
78
|
+
*/
|
|
79
|
+
async set(key, value, createdBy = 'unknown', description = '') {
|
|
80
|
+
return new Promise((resolve, reject) => {
|
|
81
|
+
const encryptedValue = this.encryption.encrypt(value);
|
|
82
|
+
const valueHash = SecureEncryption.hash(value);
|
|
83
|
+
|
|
84
|
+
this.db.run(
|
|
85
|
+
`INSERT OR REPLACE INTO secrets
|
|
86
|
+
(key, encrypted_value, value_hash, created_by, description, updated_at)
|
|
87
|
+
VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`,
|
|
88
|
+
[key, encryptedValue, valueHash, createdBy, description],
|
|
89
|
+
function(err) {
|
|
90
|
+
if (err) {
|
|
91
|
+
reject(new Error(`Failed to set secret: ${err.message}`));
|
|
92
|
+
} else {
|
|
93
|
+
resolve(this.lastID);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Get a decrypted secret value
|
|
102
|
+
*/
|
|
103
|
+
async get(key, user = 'unknown') {
|
|
104
|
+
return new Promise((resolve, reject) => {
|
|
105
|
+
this.db.get(
|
|
106
|
+
'SELECT id, encrypted_value FROM secrets WHERE key = ?',
|
|
107
|
+
[key],
|
|
108
|
+
(err, row) => {
|
|
109
|
+
if (err) {
|
|
110
|
+
reject(new Error(`Failed to get secret: ${err.message}`));
|
|
111
|
+
} else if (!row) {
|
|
112
|
+
resolve(null);
|
|
113
|
+
} else {
|
|
114
|
+
try {
|
|
115
|
+
const decryptedValue = this.encryption.decrypt(row.encrypted_value);
|
|
116
|
+
this._logAccess(row.id, 'READ', user);
|
|
117
|
+
resolve(decryptedValue);
|
|
118
|
+
} catch (error) {
|
|
119
|
+
reject(error);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* List all keys (values remain encrypted in DB)
|
|
129
|
+
*/
|
|
130
|
+
async listKeys() {
|
|
131
|
+
return new Promise((resolve, reject) => {
|
|
132
|
+
this.db.all(
|
|
133
|
+
'SELECT id, key, created_at, updated_at, created_by, description FROM secrets ORDER BY key',
|
|
134
|
+
(err, rows) => {
|
|
135
|
+
if (err) {
|
|
136
|
+
reject(new Error(`Failed to list keys: ${err.message}`));
|
|
137
|
+
} else {
|
|
138
|
+
resolve(rows || []);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Delete a secret
|
|
147
|
+
*/
|
|
148
|
+
async delete(key, user = 'unknown') {
|
|
149
|
+
return new Promise((resolve, reject) => {
|
|
150
|
+
this.db.run(
|
|
151
|
+
'DELETE FROM secrets WHERE key = ?',
|
|
152
|
+
[key],
|
|
153
|
+
function(err) {
|
|
154
|
+
if (err) {
|
|
155
|
+
reject(new Error(`Failed to delete secret: ${err.message}`));
|
|
156
|
+
} else {
|
|
157
|
+
if (this.changes > 0) {
|
|
158
|
+
resolve(true);
|
|
159
|
+
} else {
|
|
160
|
+
resolve(false);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Get access logs for a secret
|
|
170
|
+
*/
|
|
171
|
+
async getAccessLogs(key, limit = 50) {
|
|
172
|
+
return new Promise((resolve, reject) => {
|
|
173
|
+
this.db.all(
|
|
174
|
+
`SELECT l.* FROM access_logs l
|
|
175
|
+
JOIN secrets s ON l.secret_id = s.id
|
|
176
|
+
WHERE s.key = ?
|
|
177
|
+
ORDER BY l.timestamp DESC
|
|
178
|
+
LIMIT ?`,
|
|
179
|
+
[key, limit],
|
|
180
|
+
(err, rows) => {
|
|
181
|
+
if (err) {
|
|
182
|
+
reject(new Error(`Failed to get access logs: ${err.message}`));
|
|
183
|
+
} else {
|
|
184
|
+
resolve(rows || []);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
);
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Get all access logs
|
|
193
|
+
*/
|
|
194
|
+
async getAllAccessLogs(limit = 100) {
|
|
195
|
+
return new Promise((resolve, reject) => {
|
|
196
|
+
this.db.all(
|
|
197
|
+
`SELECT l.id, s.key, l.action, l.user, l.timestamp, l.ip_address
|
|
198
|
+
FROM access_logs l
|
|
199
|
+
JOIN secrets s ON l.secret_id = s.id
|
|
200
|
+
ORDER BY l.timestamp DESC
|
|
201
|
+
LIMIT ?`,
|
|
202
|
+
[limit],
|
|
203
|
+
(err, rows) => {
|
|
204
|
+
if (err) {
|
|
205
|
+
reject(new Error(`Failed to get access logs: ${err.message}`));
|
|
206
|
+
} else {
|
|
207
|
+
resolve(rows || []);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
);
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Verify a secret hasn't changed (using hash)
|
|
216
|
+
*/
|
|
217
|
+
async verify(key, value) {
|
|
218
|
+
return new Promise((resolve, reject) => {
|
|
219
|
+
this.db.get(
|
|
220
|
+
'SELECT value_hash FROM secrets WHERE key = ?',
|
|
221
|
+
[key],
|
|
222
|
+
(err, row) => {
|
|
223
|
+
if (err) {
|
|
224
|
+
reject(new Error(`Failed to verify secret: ${err.message}`));
|
|
225
|
+
} else if (!row) {
|
|
226
|
+
resolve(false);
|
|
227
|
+
} else {
|
|
228
|
+
const isValid = SecureEncryption.verify(value, row.value_hash);
|
|
229
|
+
resolve(isValid);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
);
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Export to encrypted JSON file
|
|
238
|
+
*/
|
|
239
|
+
async exportToFile(filename, exportKey) {
|
|
240
|
+
try {
|
|
241
|
+
const keys = await this.listKeys();
|
|
242
|
+
const secrets = {};
|
|
243
|
+
|
|
244
|
+
for (const k of keys) {
|
|
245
|
+
const value = await this.get(k.key);
|
|
246
|
+
secrets[k.key] = value;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const exportEncryption = new SecureEncryption(exportKey);
|
|
250
|
+
const encryptedExport = exportEncryption.encrypt(JSON.stringify(secrets));
|
|
251
|
+
|
|
252
|
+
return { secrets, encryptedExport };
|
|
253
|
+
} catch (error) {
|
|
254
|
+
throw new Error(`Export failed: ${error.message}`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Import from encrypted JSON
|
|
260
|
+
*/
|
|
261
|
+
async importFromFile(encryptedData, exportKey, importedBy = 'unknown') {
|
|
262
|
+
try {
|
|
263
|
+
const exportEncryption = new SecureEncryption(exportKey);
|
|
264
|
+
const decrypted = exportEncryption.decrypt(encryptedData);
|
|
265
|
+
const secrets = JSON.parse(decrypted);
|
|
266
|
+
|
|
267
|
+
let count = 0;
|
|
268
|
+
for (const [key, value] of Object.entries(secrets)) {
|
|
269
|
+
await this.set(key, value, importedBy, 'Imported');
|
|
270
|
+
count++;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return count;
|
|
274
|
+
} catch (error) {
|
|
275
|
+
throw new Error(`Import failed: ${error.message}`);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Close database connection
|
|
281
|
+
*/
|
|
282
|
+
close() {
|
|
283
|
+
return new Promise((resolve, reject) => {
|
|
284
|
+
if (this.db) {
|
|
285
|
+
this.db.close((err) => {
|
|
286
|
+
if (err) reject(err);
|
|
287
|
+
else resolve();
|
|
288
|
+
});
|
|
289
|
+
} else {
|
|
290
|
+
resolve();
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Internal: Log access to a secret
|
|
297
|
+
*/
|
|
298
|
+
_logAccess(secretId, action, user, ipAddress = null) {
|
|
299
|
+
this.db.run(
|
|
300
|
+
'INSERT INTO access_logs (secret_id, action, user, ip_address) VALUES (?, ?, ?, ?)',
|
|
301
|
+
[secretId, action, user, ipAddress],
|
|
302
|
+
(err) => {
|
|
303
|
+
if (err) {
|
|
304
|
+
console.error('Failed to log access:', err.message);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Encryption utilities for storing secrets safely
|
|
5
|
+
*/
|
|
6
|
+
export class SecureEncryption {
|
|
7
|
+
constructor(masterKey) {
|
|
8
|
+
// Master key should be 32 bytes for AES-256
|
|
9
|
+
if (!masterKey) {
|
|
10
|
+
throw new Error('Master key is required');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// If string, hash it to 32 bytes
|
|
14
|
+
if (typeof masterKey === 'string') {
|
|
15
|
+
this.masterKey = crypto
|
|
16
|
+
.createHash('sha256')
|
|
17
|
+
.update(masterKey)
|
|
18
|
+
.digest();
|
|
19
|
+
} else {
|
|
20
|
+
this.masterKey = masterKey;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Encrypt a secret value
|
|
26
|
+
* Returns: iv:encryptedData:authTag (all base64 encoded)
|
|
27
|
+
*/
|
|
28
|
+
encrypt(plaintext) {
|
|
29
|
+
const iv = crypto.randomBytes(16);
|
|
30
|
+
const cipher = crypto.createCipheriv('aes-256-gcm', this.masterKey, iv);
|
|
31
|
+
|
|
32
|
+
let encrypted = cipher.update(plaintext, 'utf8', 'hex');
|
|
33
|
+
encrypted += cipher.final('hex');
|
|
34
|
+
|
|
35
|
+
const authTag = cipher.getAuthTag();
|
|
36
|
+
|
|
37
|
+
// Combine IV, encrypted data, and auth tag
|
|
38
|
+
return [
|
|
39
|
+
iv.toString('base64'),
|
|
40
|
+
encrypted,
|
|
41
|
+
authTag.toString('base64')
|
|
42
|
+
].join(':');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Decrypt a secret value
|
|
47
|
+
*/
|
|
48
|
+
decrypt(encryptedData) {
|
|
49
|
+
try {
|
|
50
|
+
const parts = encryptedData.split(':');
|
|
51
|
+
if (parts.length !== 3) {
|
|
52
|
+
throw new Error('Invalid encrypted data format');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const iv = Buffer.from(parts[0], 'base64');
|
|
56
|
+
const encrypted = parts[1];
|
|
57
|
+
const authTag = Buffer.from(parts[2], 'base64');
|
|
58
|
+
|
|
59
|
+
const decipher = crypto.createDecipheriv('aes-256-gcm', this.masterKey, iv);
|
|
60
|
+
decipher.setAuthTag(authTag);
|
|
61
|
+
|
|
62
|
+
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
|
|
63
|
+
decrypted += decipher.final('utf8');
|
|
64
|
+
|
|
65
|
+
return decrypted;
|
|
66
|
+
} catch (error) {
|
|
67
|
+
throw new Error(`Decryption failed: ${error.message}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Hash a value using PBKDF2 (one-way, for verification)
|
|
73
|
+
*/
|
|
74
|
+
static hash(value, salt = null) {
|
|
75
|
+
if (!salt) {
|
|
76
|
+
salt = crypto.randomBytes(16);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const hash = crypto.pbkdf2Sync(value, salt, 100000, 64, 'sha256');
|
|
80
|
+
return salt.toString('base64') + ':' + hash.toString('base64');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Verify a hashed value
|
|
85
|
+
*/
|
|
86
|
+
static verify(value, storedHash) {
|
|
87
|
+
const [saltBase64, hashBase64] = storedHash.split(':');
|
|
88
|
+
const salt = Buffer.from(saltBase64, 'base64');
|
|
89
|
+
const hash = crypto.pbkdf2Sync(value, salt, 100000, 64, 'sha256');
|
|
90
|
+
return hash.toString('base64') === hashBase64;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Generate a random encryption key
|
|
95
|
+
*/
|
|
96
|
+
static generateKey() {
|
|
97
|
+
return crypto.randomBytes(32).toString('base64');
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import admin from 'firebase-admin';
|
|
2
|
+
import { SecureEncryption } from './encryption.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Firebase Firestore implementation for secure secret storage
|
|
6
|
+
*
|
|
7
|
+
* Benefits:
|
|
8
|
+
* - No infrastructure to manage
|
|
9
|
+
* - Auto-scaling
|
|
10
|
+
* - Built-in backups
|
|
11
|
+
* - Global distribution
|
|
12
|
+
* - Audit logging
|
|
13
|
+
*
|
|
14
|
+
* Setup:
|
|
15
|
+
* 1. npm install firebase-admin
|
|
16
|
+
* 2. export FIREBASE_PROJECT_ID="your-project"
|
|
17
|
+
* 3. export GOOGLE_APPLICATION_CREDENTIALS="/path/to/serviceAccountKey.json"
|
|
18
|
+
*/
|
|
19
|
+
export class FirebaseEnvDB {
|
|
20
|
+
constructor(encryptionKey, projectId = null) {
|
|
21
|
+
if (!encryptionKey) {
|
|
22
|
+
throw new Error('Encryption key is required');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
this.encryption = new SecureEncryption(encryptionKey);
|
|
26
|
+
|
|
27
|
+
// Initialize Firebase if not already initialized
|
|
28
|
+
if (!admin.apps.length) {
|
|
29
|
+
const config = {
|
|
30
|
+
credential: admin.credential.applicationDefault(),
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// Use provided projectId or from environment
|
|
34
|
+
const projectId_ = projectId || process.env.FIREBASE_PROJECT_ID;
|
|
35
|
+
if (projectId_) {
|
|
36
|
+
config.projectId = projectId_;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
admin.initializeApp(config);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
this.db = admin.firestore();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Set a secret value
|
|
47
|
+
*/
|
|
48
|
+
async set(key, value, createdBy = 'unknown', description = '') {
|
|
49
|
+
const encryptedValue = this.encryption.encrypt(value);
|
|
50
|
+
const valueHash = SecureEncryption.hash(value);
|
|
51
|
+
|
|
52
|
+
await this.db.collection('secrets').doc(key).set({
|
|
53
|
+
key,
|
|
54
|
+
encrypted_value: encryptedValue,
|
|
55
|
+
value_hash: valueHash,
|
|
56
|
+
created_at: new Date(),
|
|
57
|
+
updated_at: new Date(),
|
|
58
|
+
created_by: createdBy,
|
|
59
|
+
description
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
return key;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Get and decrypt a secret value
|
|
67
|
+
*/
|
|
68
|
+
async get(key, user = 'unknown') {
|
|
69
|
+
const doc = await this.db.collection('secrets').doc(key).get();
|
|
70
|
+
|
|
71
|
+
if (!doc.exists) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const data = doc.data();
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
const decrypted = this.encryption.decrypt(data.encrypted_value);
|
|
79
|
+
|
|
80
|
+
// Log access
|
|
81
|
+
await this._logAccess(key, 'READ', user);
|
|
82
|
+
|
|
83
|
+
return decrypted;
|
|
84
|
+
} catch (error) {
|
|
85
|
+
throw new Error(`Failed to decrypt secret: ${error.message}`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* List all secret keys (values encrypted)
|
|
91
|
+
*/
|
|
92
|
+
async listKeys() {
|
|
93
|
+
const snapshot = await this.db.collection('secrets')
|
|
94
|
+
.orderBy('key')
|
|
95
|
+
.get();
|
|
96
|
+
|
|
97
|
+
return snapshot.docs.map(doc => ({
|
|
98
|
+
id: doc.id,
|
|
99
|
+
key: doc.data().key,
|
|
100
|
+
created_at: doc.data().created_at,
|
|
101
|
+
updated_at: doc.data().updated_at,
|
|
102
|
+
created_by: doc.data().created_by,
|
|
103
|
+
description: doc.data().description
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Delete a secret
|
|
109
|
+
*/
|
|
110
|
+
async delete(key, user = 'unknown') {
|
|
111
|
+
const doc = await this.db.collection('secrets').doc(key).get();
|
|
112
|
+
|
|
113
|
+
if (!doc.exists) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
await this.db.collection('secrets').doc(key).delete();
|
|
118
|
+
await this._logAccess(key, 'DELETE', user);
|
|
119
|
+
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Verify secret integrity
|
|
125
|
+
*/
|
|
126
|
+
async verify(key, value) {
|
|
127
|
+
const doc = await this.db.collection('secrets').doc(key).get();
|
|
128
|
+
|
|
129
|
+
if (!doc.exists) {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const data = doc.data();
|
|
134
|
+
return SecureEncryption.verify(value, data.value_hash);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Get access logs for a specific secret
|
|
139
|
+
*/
|
|
140
|
+
async getAccessLogs(key, limit = 50) {
|
|
141
|
+
const snapshot = await this.db.collection('access_logs')
|
|
142
|
+
.where('secret_id', '==', key)
|
|
143
|
+
.orderBy('timestamp', 'desc')
|
|
144
|
+
.limit(limit)
|
|
145
|
+
.get();
|
|
146
|
+
|
|
147
|
+
return snapshot.docs.map(doc => ({
|
|
148
|
+
id: doc.id,
|
|
149
|
+
...doc.data()
|
|
150
|
+
}));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Get all access logs
|
|
155
|
+
*/
|
|
156
|
+
async getAllAccessLogs(limit = 100) {
|
|
157
|
+
const snapshot = await this.db.collection('access_logs')
|
|
158
|
+
.orderBy('timestamp', 'desc')
|
|
159
|
+
.limit(limit)
|
|
160
|
+
.get();
|
|
161
|
+
|
|
162
|
+
return snapshot.docs.map(doc => ({
|
|
163
|
+
id: doc.id,
|
|
164
|
+
...doc.data()
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Export to encrypted JSON
|
|
170
|
+
*/
|
|
171
|
+
async exportToFile(exportKey) {
|
|
172
|
+
const keys = await this.listKeys();
|
|
173
|
+
const secrets = {};
|
|
174
|
+
|
|
175
|
+
for (const k of keys) {
|
|
176
|
+
const value = await this.get(k.key);
|
|
177
|
+
secrets[k.key] = value;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const exportEncryption = new SecureEncryption(exportKey);
|
|
181
|
+
const encryptedExport = exportEncryption.encrypt(JSON.stringify(secrets));
|
|
182
|
+
|
|
183
|
+
return { secrets, encryptedExport };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Import from encrypted JSON
|
|
188
|
+
*/
|
|
189
|
+
async importFromFile(encryptedData, exportKey, importedBy = 'unknown') {
|
|
190
|
+
const exportEncryption = new SecureEncryption(exportKey);
|
|
191
|
+
const decrypted = exportEncryption.decrypt(encryptedData);
|
|
192
|
+
const secrets = JSON.parse(decrypted);
|
|
193
|
+
|
|
194
|
+
let count = 0;
|
|
195
|
+
for (const [key, value] of Object.entries(secrets)) {
|
|
196
|
+
await this.set(key, value, importedBy, 'Imported');
|
|
197
|
+
count++;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return count;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Create database indexes (run once)
|
|
205
|
+
* Firestore requires composite indexes for complex queries
|
|
206
|
+
*/
|
|
207
|
+
async createIndexes() {
|
|
208
|
+
console.log('Firestore indexes are auto-managed.');
|
|
209
|
+
console.log('If needed, visit: https://console.firebase.google.com');
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Check connection
|
|
215
|
+
*/
|
|
216
|
+
async health() {
|
|
217
|
+
try {
|
|
218
|
+
await this.db.collection('secrets').limit(1).get();
|
|
219
|
+
return { status: 'healthy', database: 'Firebase Firestore' };
|
|
220
|
+
} catch (error) {
|
|
221
|
+
return { status: 'error', error: error.message };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Internal: Log access to a secret
|
|
227
|
+
*/
|
|
228
|
+
async _logAccess(secretId, action, user, ipAddress = null) {
|
|
229
|
+
try {
|
|
230
|
+
await this.db.collection('access_logs').add({
|
|
231
|
+
secret_id: secretId,
|
|
232
|
+
action,
|
|
233
|
+
user,
|
|
234
|
+
timestamp: new Date(),
|
|
235
|
+
ip_address: ipAddress || null
|
|
236
|
+
});
|
|
237
|
+
} catch (error) {
|
|
238
|
+
console.error('Failed to log access:', error.message);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|