@ductape/cli 0.5.0 → 0.5.2
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/dist/commands/apps.js +12 -4
- package/dist/commands/cloud.d.ts +8 -0
- package/dist/commands/cloud.js +40 -1
- package/dist/commands/deploy-builds.d.ts +3 -1
- package/dist/commands/deploy-builds.js +11 -3
- package/dist/commands/deploy-remote.d.ts +34 -1
- package/dist/commands/deploy-remote.js +109 -3
- package/dist/commands/deploy-worker.d.ts +30 -0
- package/dist/commands/deploy-worker.js +85 -0
- package/dist/commands/secrets-scan.d.ts +49 -0
- package/dist/commands/secrets-scan.js +113 -0
- package/dist/index.js +92 -13
- package/dist/lib/api-client.js +4 -2
- package/dist/lib/cloud-connection.d.ts +89 -1
- package/dist/lib/cloud-connection.js +417 -5
- package/dist/lib/env-scan.d.ts +57 -0
- package/dist/lib/env-scan.js +161 -0
- package/dist/lib/platform-api.js +3 -9
- package/package.json +1 -1
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading a project's .env file to find the values that are secrets, so they can be saved to the workspace's secrets (and then used as
|
|
3
|
+
* `{ "secret": "NAME" }` in a release's configuration). Everything here is pure and works on text; nothing here prints, logs or returns a
|
|
4
|
+
* value except `parseDotenv`, whose output stays inside the command and goes only to the workspace. A result meant for a person or an
|
|
5
|
+
* assistant (`describePlan`) carries names, reasons and lengths, never any part of a value.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Reads dotenv text: `NAME=value`, an optional `export `, single and double quotes (a double-quoted value may span lines and uses \n, \t and \"
|
|
9
|
+
* escapes), `#` comments on their own line or after an unquoted value, and blank lines. A name that is not a valid variable name is skipped.
|
|
10
|
+
* When a name appears more than once the last one wins, as in every dotenv loader.
|
|
11
|
+
*/
|
|
12
|
+
export function parseDotenv(text) {
|
|
13
|
+
const entries = new Map();
|
|
14
|
+
const lines = text.replace(/^\uFEFF/, '').split(/\r?\n/);
|
|
15
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
16
|
+
const raw = lines[i];
|
|
17
|
+
const match = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_.-]*)\s*=\s*(.*)$/.exec(raw);
|
|
18
|
+
if (!match || /^\s*#/.test(raw))
|
|
19
|
+
continue;
|
|
20
|
+
const [, name, rest] = match;
|
|
21
|
+
let value;
|
|
22
|
+
const start = i + 1;
|
|
23
|
+
if (rest.startsWith('"')) {
|
|
24
|
+
// A double-quoted value runs to the closing quote, across lines if it has to.
|
|
25
|
+
let body = rest.slice(1);
|
|
26
|
+
let closed = false;
|
|
27
|
+
let result = '';
|
|
28
|
+
for (;;) {
|
|
29
|
+
let j = 0;
|
|
30
|
+
for (; j < body.length; j += 1) {
|
|
31
|
+
if (body[j] === '\\' && j + 1 < body.length) {
|
|
32
|
+
result += body[j] + body[j + 1];
|
|
33
|
+
j += 1;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (body[j] === '"') {
|
|
37
|
+
closed = true;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
result += body[j];
|
|
41
|
+
}
|
|
42
|
+
if (closed || i + 1 >= lines.length)
|
|
43
|
+
break;
|
|
44
|
+
i += 1;
|
|
45
|
+
result += '\n';
|
|
46
|
+
body = lines[i];
|
|
47
|
+
}
|
|
48
|
+
value = result.replace(/\\(["\\nrt])/g, (_m, c) => (c === 'n' ? '\n' : c === 'r' ? '\r' : c === 't' ? '\t' : c));
|
|
49
|
+
}
|
|
50
|
+
else if (rest.startsWith("'")) {
|
|
51
|
+
const end = rest.indexOf("'", 1);
|
|
52
|
+
value = end === -1 ? rest.slice(1) : rest.slice(1, end);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
// Unquoted: a ` #` starts a comment.
|
|
56
|
+
value = rest.replace(/\s+#.*$/, '').trim();
|
|
57
|
+
}
|
|
58
|
+
const previous = entries.get(name);
|
|
59
|
+
entries.set(name, { name, value, line: start, ...(previous ? { duplicate: true } : {}) });
|
|
60
|
+
}
|
|
61
|
+
return [...entries.values()];
|
|
62
|
+
}
|
|
63
|
+
const SECRET_NAME = /(^|_)(SECRET|TOKEN|PASSWORD|PASSWD|PWD|PASS|PRIVATE|CREDENTIAL|CREDENTIALS|AUTH|BEARER|COOKIE|SALT|SIGNATURE|SIGNING|WEBHOOK|DSN|CERT|PEM)(_|$)|API_?KEY|ACCESS_?KEY|SECRET_?KEY|PRIVATE_?KEY|CLIENT_?SECRET|CONNECTION_?STRING|ENCRYPTION_?KEY|(^|_)KEY$|(^|_)KEYS$/i;
|
|
64
|
+
const CONFIG_NAME = /^(NODE_ENV|ENV|ENVIRONMENT|PORT|HOST|HOSTNAME|REGION|LOG_?LEVEL|DEBUG|TZ|LANG|TIMEZONE|APP_?NAME|APP_?ENV|VERSION|BASE_?URL|PUBLIC_.*|NEXT_PUBLIC_.*|VITE_.*|REACT_APP_.*)$|(^|_)(PORT|HOST|REGION|ENABLED|DISABLED|TIMEOUT|LIMIT|URL|URI|ENDPOINT|DOMAIN|BUCKET|NAMESPACE|LEVEL|MODE|NAME)$/i;
|
|
65
|
+
const PLACEHOLDER = /^(changeme|change_me|change-me|todo|tbd|xxx+|\*+|your[-_ ].*|<.*>|\[.*\]|replace[-_ ]?me.*|example.*|placeholder.*|none|null|undefined|secret|password)$/i;
|
|
66
|
+
const VALUE_SHAPES = [
|
|
67
|
+
[/^-----BEGIN [A-Z ]*PRIVATE KEY-----/, 'a private key'],
|
|
68
|
+
[/^(sk|rk)_(live|test)_[A-Za-z0-9]{10,}$/, 'a payment provider secret key'],
|
|
69
|
+
[/^(AKIA|ASIA)[A-Z0-9]{16}$/, 'an AWS access key id'],
|
|
70
|
+
[/^gh[pousr]_[A-Za-z0-9]{20,}$/, 'a GitHub token'],
|
|
71
|
+
[/^xox[abprs]-[A-Za-z0-9-]{10,}$/, 'a Slack token'],
|
|
72
|
+
[/^eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{5,}$/, 'a signed token'],
|
|
73
|
+
[/^[a-z][a-z0-9+.-]*:\/\/[^\s/:@]+:[^\s/@]+@/, 'a URL with a password in it'],
|
|
74
|
+
];
|
|
75
|
+
const entropy = (value) => {
|
|
76
|
+
const counts = new Map();
|
|
77
|
+
for (const ch of value)
|
|
78
|
+
counts.set(ch, (counts.get(ch) ?? 0) + 1);
|
|
79
|
+
let bits = 0;
|
|
80
|
+
for (const n of counts.values())
|
|
81
|
+
bits -= (n / value.length) * Math.log2(n / value.length);
|
|
82
|
+
return bits;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Decides whether an entry looks like a secret, from its name and the shape of its value. It is a guide, not a judgement: the person (or the
|
|
86
|
+
* assistant, with the person) chooses what to save, with `--only` and `--all`. A value is only ever looked at, never kept or shown.
|
|
87
|
+
*/
|
|
88
|
+
export function classifyEntry(entry) {
|
|
89
|
+
const { name, value } = entry;
|
|
90
|
+
if (value === '')
|
|
91
|
+
return { name, classification: 'skip', reason: 'it has no value' };
|
|
92
|
+
if (PLACEHOLDER.test(value.trim()))
|
|
93
|
+
return { name, classification: 'skip', reason: 'the value is a placeholder, not a real value' };
|
|
94
|
+
if (/\$\{[A-Za-z_][A-Za-z0-9_]*\}|\$[A-Za-z_][A-Za-z0-9_]*/.test(value) && !/^-----BEGIN/.test(value)) {
|
|
95
|
+
return { name, classification: 'skip', reason: 'it refers to another variable, so the text on its own is not the value' };
|
|
96
|
+
}
|
|
97
|
+
for (const [pattern, what] of VALUE_SHAPES)
|
|
98
|
+
if (pattern.test(value))
|
|
99
|
+
return { name, classification: 'secret', reason: `the value looks like ${what}` };
|
|
100
|
+
// These prefixes mean the value is put into code that runs in a browser, so it is public by design whatever else the name says.
|
|
101
|
+
if (/^(NEXT_PUBLIC_|VITE_|REACT_APP_|PUBLIC_|EXPO_PUBLIC_)/i.test(name))
|
|
102
|
+
return { name, classification: 'config', reason: 'the name says it is sent to browsers, so it is public' };
|
|
103
|
+
if (SECRET_NAME.test(name))
|
|
104
|
+
return { name, classification: 'secret', reason: 'the name says it is a credential' };
|
|
105
|
+
if (CONFIG_NAME.test(name))
|
|
106
|
+
return { name, classification: 'config', reason: 'the name says it is configuration' };
|
|
107
|
+
if (/^(true|false|yes|no|on|off|\d+(\.\d+)?)$/i.test(value))
|
|
108
|
+
return { name, classification: 'config', reason: 'the value is a flag or a number' };
|
|
109
|
+
if (value.length >= 24 && !/\s/.test(value) && entropy(value) >= 3.5)
|
|
110
|
+
return { name, classification: 'secret', reason: 'the value is a long, random-looking string' };
|
|
111
|
+
return { name, classification: 'config', reason: 'nothing about the name or the value says it is a secret' };
|
|
112
|
+
}
|
|
113
|
+
const KEY = /^[A-Za-z_][A-Za-z0-9_.-]{0,254}$/;
|
|
114
|
+
/** What would be saved, and why each of the rest is not. Pure: no value leaves this function except as a length. */
|
|
115
|
+
export function planScan(entries, existingKeys, options = {}) {
|
|
116
|
+
const only = options.only ? new Set(options.only) : undefined;
|
|
117
|
+
const exclude = new Set(options.exclude ?? []);
|
|
118
|
+
const known = new Set(entries.map((e) => e.name));
|
|
119
|
+
for (const name of only ?? [])
|
|
120
|
+
if (!known.has(name))
|
|
121
|
+
throw new Error(`"${name}" is not in the file, so it cannot be saved. The names in it: ${[...known].sort().join(', ') || '(none)'}`);
|
|
122
|
+
const prefix = options.prefix ?? '';
|
|
123
|
+
if (prefix && !/^[A-Za-z_][A-Za-z0-9_.-]*$/.test(prefix))
|
|
124
|
+
throw new Error('--prefix must start with a letter or underscore and use letters, digits, underscores, dots or hyphens');
|
|
125
|
+
return entries.map((entry) => {
|
|
126
|
+
const verdict = classifyEntry(entry);
|
|
127
|
+
const key = `${prefix}${entry.name}`;
|
|
128
|
+
const base = { ...verdict, key, length: entry.value.length, ...(entry.duplicate ? { duplicate: true } : {}) };
|
|
129
|
+
if (!KEY.test(key))
|
|
130
|
+
return { ...base, classification: 'skip', action: 'skip', reason: 'the name cannot be used as a secret key' };
|
|
131
|
+
if (exclude.has(entry.name))
|
|
132
|
+
return { ...base, action: 'excluded' };
|
|
133
|
+
const forced = only?.has(entry.name) ?? false;
|
|
134
|
+
// A name asked for is saved unless there is nothing to save; an empty value or a reference to another variable never is.
|
|
135
|
+
if (verdict.classification === 'skip' && !(forced && /placeholder/.test(verdict.reason)))
|
|
136
|
+
return { ...base, action: 'skip' };
|
|
137
|
+
const wanted = only ? forced : options.all ? true : verdict.classification === 'secret';
|
|
138
|
+
if (!wanted)
|
|
139
|
+
return { ...base, action: 'config' };
|
|
140
|
+
if (existingKeys.has(key))
|
|
141
|
+
return { ...base, action: options.overwrite ? 'update' : 'exists' };
|
|
142
|
+
return { ...base, action: 'create' };
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/** What a person or an assistant is shown. Names, the decision, the reason and a length: never any part of a value. */
|
|
146
|
+
export function describePlan(rows, source, applied) {
|
|
147
|
+
const count = (action) => rows.filter((r) => r.action === action).length;
|
|
148
|
+
return {
|
|
149
|
+
source,
|
|
150
|
+
applied,
|
|
151
|
+
summary: {
|
|
152
|
+
found: rows.length, [applied ? 'created' : 'would_create']: count('create'), [applied ? 'updated' : 'would_update']: count('update'),
|
|
153
|
+
already_in_workspace: count('exists'), left_as_configuration: count('config'), skipped: count('skip'), excluded: count('excluded'),
|
|
154
|
+
},
|
|
155
|
+
variables: rows.map((r) => ({ name: r.name, key: r.key, decision: r.action, classified_as: r.classification, reason: r.reason, value_length: r.length, ...(r.duplicate ? { duplicate_in_file: true } : {}) })),
|
|
156
|
+
use_in_configuration: rows.filter((r) => r.action === 'create' || r.action === 'update' || r.action === 'exists').map((r) => ({ [r.name]: { secret: r.key } })),
|
|
157
|
+
note: applied
|
|
158
|
+
? 'The values were read from the file on this machine and sent to the workspace. They are not shown here, and are not in this output.'
|
|
159
|
+
: 'Nothing was saved. Run again with --apply to save the variables marked create or update. The values are never printed.',
|
|
160
|
+
};
|
|
161
|
+
}
|
package/dist/lib/platform-api.js
CHANGED
|
@@ -429,21 +429,15 @@ export async function createApp(ctx, body) {
|
|
|
429
429
|
return unwrapOne(result);
|
|
430
430
|
}
|
|
431
431
|
export async function updateApp(ctx, appId, body) {
|
|
432
|
-
const q =
|
|
433
|
-
|
|
434
|
-
public_key: ctx.credentials.public_key,
|
|
435
|
-
};
|
|
436
|
-
const result = await client(ctx).put(`/apps/v1/${appId}?${new URLSearchParams(q).toString()}`, body);
|
|
432
|
+
const q = workspaceAuthQuery(ctx);
|
|
433
|
+
const result = await client(ctx).put(`/apps/v1/${appId}?${new URLSearchParams(q).toString()}`, { component: 'app', ...body });
|
|
437
434
|
return unwrapOne(result);
|
|
438
435
|
}
|
|
439
436
|
export async function updateAppViaProxy(ctx, tag, body) {
|
|
440
437
|
return proxy(ctx).execute('app', 'update', [tag, body]);
|
|
441
438
|
}
|
|
442
439
|
export async function deleteApp(ctx, appId) {
|
|
443
|
-
const q =
|
|
444
|
-
user_id: ctx.credentials.user_id,
|
|
445
|
-
public_key: ctx.credentials.public_key,
|
|
446
|
-
};
|
|
440
|
+
const q = workspaceAuthQuery(ctx);
|
|
447
441
|
return client(ctx).delete(`/apps/v1/${appId}?${new URLSearchParams(q).toString()}`);
|
|
448
442
|
}
|
|
449
443
|
// —— App actions (sdk-proxy — admin op, requires access key) ——
|
package/package.json
CHANGED