Repository navigation
Expand file tree
/
Copy pathsecrets.test.ts
More file actions
160 lines (132 loc) · 5.09 KB
/
Copy pathsecrets.test.ts
File metadata and controls
160 lines (132 loc) · 5.09 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
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
159
160
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { loadSecretsFromEnv } from '../../../../core/shared/config/secrets';
describe('Config Secrets', function () {
let tmpDir: string;
function writeSecret(name: string, contents: string): string {
const filePath = path.join(tmpDir, name);
fs.writeFileSync(filePath, contents);
return filePath;
}
beforeEach(function () {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ghost-secrets-'));
});
afterEach(function () {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
it('resolves a nested key from a file', function () {
const filePath = writeSecret('db', 'hunter2');
assert.deepEqual(loadSecretsFromEnv({ database__connection__password_FILE: filePath }), {
database: { connection: { password: 'hunter2' } },
});
});
it('matches the suffix case-insensitively', function () {
const filePath = writeSecret('db', 'hunter2');
assert.deepEqual(loadSecretsFromEnv({ database__connection__password_file: filePath }), {
database: { connection: { password: 'hunter2' } },
});
});
it('does not parse values', function () {
const filePath = writeSecret('db', '01234');
assert.deepEqual(loadSecretsFromEnv({ database__connection__password_FILE: filePath }), {
database: { connection: { password: '01234' } },
});
});
it('strips a single trailing newline but keeps other whitespace', function () {
const trailing = writeSecret('trailing', 'hunter2\n');
const surrounding = writeSecret('surrounding', ' hunter2 ');
const multiple = writeSecret('multiple', 'hunter2\n\n');
assert.deepEqual(loadSecretsFromEnv({ a__b_FILE: trailing }), { a: { b: 'hunter2' } });
assert.deepEqual(loadSecretsFromEnv({ a__b_FILE: surrounding }), { a: { b: ' hunter2 ' } });
assert.deepEqual(loadSecretsFromEnv({ a__b_FILE: multiple }), { a: { b: 'hunter2\n' } });
});
it('merges multiple secrets under a shared parent', function () {
const user = writeSecret('user', 'ghost');
const pass = writeSecret('pass', 'hunter2');
assert.deepEqual(
loadSecretsFromEnv({
mail__options__auth__user_FILE: user,
mail__options__auth__pass_FILE: pass,
}),
{
mail: { options: { auth: { user: 'ghost', pass: 'hunter2' } } },
},
);
});
it('ignores env vars that are not nested config keys', function () {
const filePath = writeSecret('ca', 'not-a-secret');
assert.deepEqual(
loadSecretsFromEnv({
SSL_CERT_FILE: filePath,
CURL_CA_BUNDLE: filePath,
_FILE: filePath,
database____password_FILE: filePath,
}),
{},
);
});
it('ignores env vars with an empty value', function () {
assert.deepEqual(loadSecretsFromEnv({ database__connection__password_FILE: '' }), {});
});
it('keeps numeric key segments as plain objects', function () {
const filePath = writeSecret('token', 'abc');
const store = loadSecretsFromEnv({ adapters__0__token_FILE: filePath });
assert.deepEqual(store, { adapters: { 0: { token: 'abc' } } });
assert.equal(Array.isArray(store.adapters), false);
});
it('throws if the value and its file reference are both set', function () {
const filePath = writeSecret('db', 'hunter2');
assert.throws(
() =>
loadSecretsFromEnv({
database__connection__password: 'hunter2',
database__connection__password_FILE: filePath,
}),
/Cannot set both database__connection__password and database__connection__password_FILE/,
);
});
it('allows the same key set twice if both point at the same file', function () {
const filePath = writeSecret('db', 'hunter2');
assert.deepEqual(
loadSecretsFromEnv({
database__connection__password_FILE: filePath,
database__connection__password_file: filePath,
}),
{
database: { connection: { password: 'hunter2' } },
},
);
});
it('throws if the same key is set twice pointing at different files', function () {
assert.throws(
() =>
loadSecretsFromEnv({
database__connection__password_FILE: writeSecret('one', 'hunter2'),
database__connection__password_file: writeSecret('two', 'hunter3'),
}),
/to different files/,
);
});
it('throws if the file cannot be read', function () {
const filePath = path.join(tmpDir, 'nope');
assert.throws(
() => loadSecretsFromEnv({ database__connection__password_FILE: filePath }),
(err: Error) => {
assert.match(
err.message,
/Could not read the secret file referenced by database__connection__password_FILE/,
);
assert.equal((err.cause as NodeJS.ErrnoException).code, 'ENOENT');
return true;
},
);
});
it('supports a custom separator', function () {
const filePath = writeSecret('db', 'hunter2');
assert.deepEqual(loadSecretsFromEnv({ 'database.connection.password_FILE': filePath }, '.'), {
database: { connection: { password: 'hunter2' } },
});
});
});