hazo_env 0.15.2 → 0.16.0
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/CHANGE_LOG.md +84 -0
- package/README.md +316 -0
- package/dist/cli.js +102 -14
- package/dist/deploy/build.d.ts.map +1 -1
- package/dist/deploy/build.js +19 -0
- package/dist/deploy/clean.d.ts +2 -1
- package/dist/deploy/clean.d.ts.map +1 -1
- package/dist/deploy/clean.js +36 -6
- package/dist/deploy/generate-readme-cli.d.ts +3 -0
- package/dist/deploy/generate-readme-cli.d.ts.map +1 -0
- package/dist/deploy/generate-readme-cli.js +40 -0
- package/dist/deploy/help.d.ts +55 -0
- package/dist/deploy/help.d.ts.map +1 -0
- package/dist/deploy/help.js +526 -0
- package/dist/deploy/index.d.ts +3 -0
- package/dist/deploy/index.d.ts.map +1 -1
- package/dist/deploy/index.js +3 -0
- package/dist/deploy/init.d.ts +8 -0
- package/dist/deploy/init.d.ts.map +1 -1
- package/dist/deploy/init.js +12 -1
- package/dist/deploy/rollback.d.ts +10 -1
- package/dist/deploy/rollback.d.ts.map +1 -1
- package/dist/deploy/rollback.js +25 -1
- package/dist/deploy/share.d.ts +28 -0
- package/dist/deploy/share.d.ts.map +1 -0
- package/dist/deploy/share.js +53 -0
- package/dist/deploy/shared-dirs.d.ts +128 -0
- package/dist/deploy/shared-dirs.d.ts.map +1 -0
- package/dist/deploy/shared-dirs.js +486 -0
- package/dist/deploy/status.d.ts +8 -0
- package/dist/deploy/status.d.ts.map +1 -1
- package/dist/deploy/status.js +6 -0
- package/dist/deploy/switch.d.ts.map +1 -1
- package/dist/deploy/switch.js +25 -0
- package/dist/deploy/types.d.ts +18 -0
- package/dist/deploy/types.d.ts.map +1 -1
- package/dist/envsync/engine.d.ts.map +1 -1
- package/dist/envsync/engine.js +21 -7
- package/dist/envsync/paths.d.ts +22 -0
- package/dist/envsync/paths.d.ts.map +1 -1
- package/dist/envsync/paths.js +55 -0
- package/dist/resolve/deploy.d.ts.map +1 -1
- package/dist/resolve/deploy.js +165 -0
- package/dist/resolve/envsync.d.ts +16 -0
- package/dist/resolve/envsync.d.ts.map +1 -1
- package/dist/resolve/envsync.js +19 -0
- package/package.json +3 -2
package/dist/envsync/engine.js
CHANGED
|
@@ -11,7 +11,7 @@ import { runCmd, runShell } from './exec.js';
|
|
|
11
11
|
import { assertFreeSpace } from './retention.js';
|
|
12
12
|
import { assertConfirmed, assertNotProd } from './guards.js';
|
|
13
13
|
import { mergeEnvOverrides, diffEnvAgainstCurrent, parseDotenvText, resolveFinalEnv, serializeDotenv, } from './dotenv-merge.js';
|
|
14
|
-
import { resolveArtifactPath } from './paths.js';
|
|
14
|
+
import { resolveArtifactPath, resolveFilesRoot } from './paths.js';
|
|
15
15
|
import { redactingProgress, redactConnectionString } from './redact.js';
|
|
16
16
|
// Free-space floor checked before writing a new dump/archive into work_dir.
|
|
17
17
|
// Not configurable in Phase 1 — a fixed conservative floor is good enough
|
|
@@ -87,7 +87,11 @@ export async function uploadDb(cfg, dumpPath, opts) {
|
|
|
87
87
|
export async function downloadFiles(cfg, opts) {
|
|
88
88
|
assertFreeSpace(cfg.work_dir, MIN_FREE_BYTES);
|
|
89
89
|
fs.mkdirSync(cfg.work_dir, { recursive: true });
|
|
90
|
-
|
|
90
|
+
// Confine files_root to the literal configured path or a declared
|
|
91
|
+
// shared-dir link (see resolveFilesRoot) BEFORE touching it — a symlink
|
|
92
|
+
// pointing anywhere else is rejected here, not discovered mid-archive.
|
|
93
|
+
const realFilesRoot = resolveFilesRoot(cfg.files_root, cfg.shared_root, cfg.shared_dirs ?? []);
|
|
94
|
+
fs.mkdirSync(realFilesRoot, { recursive: true });
|
|
91
95
|
const id = opts?.id ?? defaultId(opts?.now);
|
|
92
96
|
const filePath = path.join(cfg.work_dir, `files-${id}.tar.gz`);
|
|
93
97
|
const onProgress = redactingProgress(opts?.onProgress);
|
|
@@ -97,7 +101,7 @@ export async function downloadFiles(cfg, opts) {
|
|
|
97
101
|
const stagedEnvLocal = path.join(stageDir, ENV_LOCAL_MEMBER);
|
|
98
102
|
fs.writeFileSync(stagedEnvLocal, fs.existsSync(envLocalSrc) ? fs.readFileSync(envLocalSrc) : '');
|
|
99
103
|
onProgress?.(`Archiving ${cfg.files_root} -> ${filePath}`);
|
|
100
|
-
await runCmd('tar', ['-czf', filePath, '-C',
|
|
104
|
+
await runCmd('tar', ['-czf', filePath, '-C', realFilesRoot, '.', '-C', stageDir, ENV_LOCAL_MEMBER], { onProgress });
|
|
101
105
|
}
|
|
102
106
|
finally {
|
|
103
107
|
fs.rmSync(stageDir, { recursive: true, force: true });
|
|
@@ -135,6 +139,10 @@ export async function uploadFiles(cfg, archivePath, opts) {
|
|
|
135
139
|
const onProgress = redactingProgress(opts.onProgress);
|
|
136
140
|
// ── validate the source artifact before ANY destructive step ────────────
|
|
137
141
|
const safeArchivePath = resolveArtifactPath(archivePath, cfg.work_dir, 'archive path');
|
|
142
|
+
// Also confine files_root itself (see resolveFilesRoot) before anything
|
|
143
|
+
// destructive runs — an escaping symlink is rejected up front, same as a
|
|
144
|
+
// bad archive path, rather than discovered after the wipe below.
|
|
145
|
+
const realFilesRoot = resolveFilesRoot(cfg.files_root, cfg.shared_root, cfg.shared_dirs ?? []);
|
|
138
146
|
try {
|
|
139
147
|
// List-only pass: proves the file is a readable tar.gz. No onProgress —
|
|
140
148
|
// this would otherwise dump every member name into the operator's log.
|
|
@@ -183,16 +191,22 @@ export async function uploadFiles(cfg, archivePath, opts) {
|
|
|
183
191
|
const ts = Date.now();
|
|
184
192
|
fs.copyFileSync(targetEnvLocalPath, `${targetEnvLocalPath}.bak-${ts}`);
|
|
185
193
|
}
|
|
194
|
+
// Wipe+recreate the REAL target, not cfg.files_root's literal path — when
|
|
195
|
+
// files_root is a shared-dir symlink, rm/mkdir on the literal path would
|
|
196
|
+
// delete the symlink itself (fs.rmSync never follows a symlink) and leave
|
|
197
|
+
// a private real directory in its place, silently un-sharing this slot.
|
|
198
|
+
// Operating on realFilesRoot clears/recreates the shared target's
|
|
199
|
+
// contents instead, leaving the slot's symlink untouched.
|
|
186
200
|
onProgress?.(`Clearing ${cfg.files_root}...`);
|
|
187
|
-
fs.rmSync(
|
|
188
|
-
fs.mkdirSync(
|
|
201
|
+
fs.rmSync(realFilesRoot, { recursive: true, force: true });
|
|
202
|
+
fs.mkdirSync(realFilesRoot, { recursive: true });
|
|
189
203
|
onProgress?.(`Extracting ${safeArchivePath} -> ${cfg.files_root}...`);
|
|
190
|
-
await runCmd('tar', ['-xzf', safeArchivePath, '-C',
|
|
204
|
+
await runCmd('tar', ['-xzf', safeArchivePath, '-C', realFilesRoot], { onProgress });
|
|
191
205
|
// __env.local extracts as a real file inside files_root — it isn't a real
|
|
192
206
|
// asset, so remove it rather than trying to get tar to exclude it during
|
|
193
207
|
// extraction (simplest correct approach per spec). Done regardless of
|
|
194
208
|
// skipEnv: the member is in the archive either way.
|
|
195
|
-
const extractedEnvMember = path.join(
|
|
209
|
+
const extractedEnvMember = path.join(realFilesRoot, ENV_LOCAL_MEMBER);
|
|
196
210
|
if (fs.existsSync(extractedEnvMember))
|
|
197
211
|
fs.rmSync(extractedEnvMember, { force: true });
|
|
198
212
|
if (!opts.skipEnv) {
|
package/dist/envsync/paths.d.ts
CHANGED
|
@@ -7,4 +7,26 @@
|
|
|
7
7
|
* is empty, or resolves outside `workDir`.
|
|
8
8
|
*/
|
|
9
9
|
export declare function resolveArtifactPath(candidate: string, workDir: string, label: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* Resolve `files_root` for a downloadFiles/uploadFiles call, confining it to
|
|
12
|
+
* either the literal configured location (the ordinary, per-slot case) or a
|
|
13
|
+
* configured shared dir (`[deploy] shared_root`/`shared_dirs` — see
|
|
14
|
+
* ../deploy/shared-dirs.ts) that it's been symlinked into.
|
|
15
|
+
*
|
|
16
|
+
* Deliberately NOT a realpath-diff like resolveArtifactPath above: an
|
|
17
|
+
* ancestor directory outside envsync's control (the OS temp dir, a
|
|
18
|
+
* symlinked home dir, ...) can legitimately differ between its lexical and
|
|
19
|
+
* real form without files_root itself being the shared-dir link this check
|
|
20
|
+
* cares about. So this lstats files_root itself — if it isn't a symlink,
|
|
21
|
+
* it's the ordinary case and always in-bounds (zero behavior change for
|
|
22
|
+
* every app that doesn't use shared dirs); only when it IS a symlink does
|
|
23
|
+
* its target have to resolve into one of the configured shared dirs.
|
|
24
|
+
*
|
|
25
|
+
* Returns the path callers should use for every fs/tar operation against
|
|
26
|
+
* files_root: the literal path in the ordinary case, or the symlink's real
|
|
27
|
+
* (fully resolved) target when it's an allowed shared-dir link — so a
|
|
28
|
+
* destructive rm+mkdir (uploadFiles) clears and recreates the shared
|
|
29
|
+
* target's contents instead of deleting the slot's symlink itself.
|
|
30
|
+
*/
|
|
31
|
+
export declare function resolveFilesRoot(filesRoot: string, sharedRoot: string | undefined, sharedDirs: string[]): string;
|
|
10
32
|
//# sourceMappingURL=paths.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../../src/envsync/paths.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../../src/envsync/paths.ts"],"names":[],"mappings":"AA2CA;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAkC7F;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,SAAS,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,MAAM,CA8BhH"}
|
package/dist/envsync/paths.js
CHANGED
|
@@ -16,8 +16,17 @@
|
|
|
16
16
|
// inside work_dir pointing at /etc, or a `../..` traversal, both fail. The
|
|
17
17
|
// resolved path is what callers should pass to the child process — that also
|
|
18
18
|
// closes the symlink-swap window between check and use.
|
|
19
|
+
//
|
|
20
|
+
// resolveFilesRoot below is a second, narrower containment check for
|
|
21
|
+
// files_root specifically — see its own doc comment for why it's a
|
|
22
|
+
// lstat/readlink check rather than a realpath diff like the one above.
|
|
19
23
|
import fs from 'node:fs';
|
|
20
24
|
import path from 'node:path';
|
|
25
|
+
/** True when `child` is `parent` itself, or lives anywhere underneath it. */
|
|
26
|
+
function isInsideOrEqual(child, parent) {
|
|
27
|
+
const rel = path.relative(parent, child);
|
|
28
|
+
return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
|
|
29
|
+
}
|
|
21
30
|
function realDirOrResolve(dir) {
|
|
22
31
|
try {
|
|
23
32
|
return fs.realpathSync(dir);
|
|
@@ -65,3 +74,49 @@ export function resolveArtifactPath(candidate, workDir, label) {
|
|
|
65
74
|
}
|
|
66
75
|
return real;
|
|
67
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Resolve `files_root` for a downloadFiles/uploadFiles call, confining it to
|
|
79
|
+
* either the literal configured location (the ordinary, per-slot case) or a
|
|
80
|
+
* configured shared dir (`[deploy] shared_root`/`shared_dirs` — see
|
|
81
|
+
* ../deploy/shared-dirs.ts) that it's been symlinked into.
|
|
82
|
+
*
|
|
83
|
+
* Deliberately NOT a realpath-diff like resolveArtifactPath above: an
|
|
84
|
+
* ancestor directory outside envsync's control (the OS temp dir, a
|
|
85
|
+
* symlinked home dir, ...) can legitimately differ between its lexical and
|
|
86
|
+
* real form without files_root itself being the shared-dir link this check
|
|
87
|
+
* cares about. So this lstats files_root itself — if it isn't a symlink,
|
|
88
|
+
* it's the ordinary case and always in-bounds (zero behavior change for
|
|
89
|
+
* every app that doesn't use shared dirs); only when it IS a symlink does
|
|
90
|
+
* its target have to resolve into one of the configured shared dirs.
|
|
91
|
+
*
|
|
92
|
+
* Returns the path callers should use for every fs/tar operation against
|
|
93
|
+
* files_root: the literal path in the ordinary case, or the symlink's real
|
|
94
|
+
* (fully resolved) target when it's an allowed shared-dir link — so a
|
|
95
|
+
* destructive rm+mkdir (uploadFiles) clears and recreates the shared
|
|
96
|
+
* target's contents instead of deleting the slot's symlink itself.
|
|
97
|
+
*/
|
|
98
|
+
export function resolveFilesRoot(filesRoot, sharedRoot, sharedDirs) {
|
|
99
|
+
const literal = path.resolve(filesRoot);
|
|
100
|
+
let st;
|
|
101
|
+
try {
|
|
102
|
+
st = fs.lstatSync(literal);
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
// Nothing on disk yet (first download/restore here) — nothing to
|
|
106
|
+
// resolve through; the caller will mkdir the literal path fresh.
|
|
107
|
+
return literal;
|
|
108
|
+
}
|
|
109
|
+
if (!st.isSymbolicLink()) {
|
|
110
|
+
return literal;
|
|
111
|
+
}
|
|
112
|
+
const real = fs.realpathSync(literal);
|
|
113
|
+
// Resolve each allowed root the same way `real` was resolved (ancestor
|
|
114
|
+
// symlinks and all — e.g. macOS's /var -> /private/var) so a shared_root
|
|
115
|
+
// that itself sits behind OS-level symlinks still compares equal.
|
|
116
|
+
const allowedRoots = sharedRoot ? sharedDirs.map((dir) => realDirOrResolve(path.join(sharedRoot, dir))) : [];
|
|
117
|
+
const inBounds = allowedRoots.some((root) => isInsideOrEqual(real, root));
|
|
118
|
+
if (!inBounds) {
|
|
119
|
+
throw new Error(`Refusing: files_root "${filesRoot}" is a symlink to "${real}", which is not one of the configured shared dirs ([deploy] shared_root/shared_dirs). Envsync only follows files_root through a declared shared-dir link.`);
|
|
120
|
+
}
|
|
121
|
+
return real;
|
|
122
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deploy.d.ts","sourceRoot":"","sources":["../../src/resolve/deploy.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"deploy.d.ts","sourceRoot":"","sources":["../../src/resolve/deploy.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAyRvD;;;;GAIG;AACH,wBAAgB,mBAAmB,IAAI,YAAY,CA+DlD"}
|
package/dist/resolve/deploy.js
CHANGED
|
@@ -4,9 +4,12 @@
|
|
|
4
4
|
// Follows the pattern of resolve/connect.ts / resolve/files.ts: load the INI
|
|
5
5
|
// via HazoConfig, read one section, throw a typed HazoConfigError on missing
|
|
6
6
|
// required fields.
|
|
7
|
+
import os from 'node:os';
|
|
7
8
|
import path from 'node:path';
|
|
8
9
|
import { HazoConfig } from 'hazo_config/server';
|
|
9
10
|
import { HazoConfigError } from 'hazo_core';
|
|
11
|
+
import { resolvePeerDir } from '../deploy/peer.js';
|
|
12
|
+
import { resolveConnectConfig } from './connect.js';
|
|
10
13
|
const DEFAULTS = {
|
|
11
14
|
prod_port: 3000,
|
|
12
15
|
staging_port: 3001,
|
|
@@ -108,6 +111,164 @@ function collectEnvFiles(section) {
|
|
|
108
111
|
.filter((s) => s.length > 0);
|
|
109
112
|
}
|
|
110
113
|
const VALID_DB_STRATEGIES = new Set(['auto', 'copy', 'shared']);
|
|
114
|
+
/** Expand a leading `~` (home dir) in a path value. Leaves everything else unchanged. */
|
|
115
|
+
function expandHome(value) {
|
|
116
|
+
if (value === '~')
|
|
117
|
+
return os.homedir();
|
|
118
|
+
if (value.startsWith('~/'))
|
|
119
|
+
return path.join(os.homedir(), value.slice(2));
|
|
120
|
+
return value;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* True when `child` is `parent` itself, or lives anywhere underneath it.
|
|
124
|
+
* Works for absolute paths (shared_root vs. slot dirs) and for slot-relative
|
|
125
|
+
* paths compared against each other (shared_dirs entries, the sqlite db
|
|
126
|
+
* path): `path.relative` resolves both against the same implicit base, so
|
|
127
|
+
* the relative result between them is correct either way.
|
|
128
|
+
*/
|
|
129
|
+
function isInsideOrEqual(child, parent) {
|
|
130
|
+
const rel = path.relative(parent, child);
|
|
131
|
+
return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Resolve `[deploy] shared_root`: default `~/<app_name>-shared`, `~`
|
|
135
|
+
* expanded, must resolve to an absolute path, and must sit outside both the
|
|
136
|
+
* current slot dir and its peer (best-effort — a peer that can't be
|
|
137
|
+
* resolved, e.g. no blue/green suffix yet, is simply not checked).
|
|
138
|
+
*/
|
|
139
|
+
function resolveSharedRoot(section, appName, slot) {
|
|
140
|
+
const raw = section['shared_root']?.trim();
|
|
141
|
+
const expanded = raw ? expandHome(raw) : path.join(os.homedir(), `${appName}-shared`);
|
|
142
|
+
if (!path.isAbsolute(expanded)) {
|
|
143
|
+
throw new HazoConfigError({
|
|
144
|
+
code: 'HAZO_ENV_CONFIG_SHARED_ROOT_NOT_ABSOLUTE',
|
|
145
|
+
pkg: 'hazo_env',
|
|
146
|
+
message: `[deploy] shared_root "${raw}" must resolve to an absolute path. Use "~/..." or an absolute path.`,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
const resolved = path.resolve(expanded);
|
|
150
|
+
const absCwd = path.resolve(slot.cwd);
|
|
151
|
+
if (isInsideOrEqual(resolved, absCwd)) {
|
|
152
|
+
throw new HazoConfigError({
|
|
153
|
+
code: 'HAZO_ENV_CONFIG_SHARED_ROOT_INSIDE_SLOT',
|
|
154
|
+
pkg: 'hazo_env',
|
|
155
|
+
message: `[deploy] shared_root "${resolved}" must be outside the app directory ("${absCwd}"). Point it at a sibling location instead.`,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
let peerDir;
|
|
159
|
+
try {
|
|
160
|
+
peerDir = resolvePeerDir(absCwd, undefined, slot.dirSuffix);
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
peerDir = undefined; // Can't determine the peer yet (e.g. no -blue/-green suffix) — nothing to check against.
|
|
164
|
+
}
|
|
165
|
+
if (peerDir && isInsideOrEqual(resolved, peerDir)) {
|
|
166
|
+
throw new HazoConfigError({
|
|
167
|
+
code: 'HAZO_ENV_CONFIG_SHARED_ROOT_INSIDE_SLOT',
|
|
168
|
+
pkg: 'hazo_env',
|
|
169
|
+
message: `[deploy] shared_root "${resolved}" must be outside the peer directory ("${peerDir}"). Point it at a sibling location instead.`,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
return resolved;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Parse `shared_dirs` (comma-separated, slot-relative paths, e.g.
|
|
176
|
+
* `shared_dirs = var/uploads, data/blog-images`) and reject anything that
|
|
177
|
+
* could resolve outside the slot or collide with another entry: absolute
|
|
178
|
+
* paths, `..` traversal, duplicates, and one entry nested inside another.
|
|
179
|
+
*/
|
|
180
|
+
function collectSharedDirs(section) {
|
|
181
|
+
const raw = section['shared_dirs'];
|
|
182
|
+
if (typeof raw !== 'string' || !raw.trim())
|
|
183
|
+
return [];
|
|
184
|
+
const entries = raw
|
|
185
|
+
.split(',')
|
|
186
|
+
.map((s) => s.trim())
|
|
187
|
+
.filter((s) => s.length > 0);
|
|
188
|
+
const normalized = [];
|
|
189
|
+
for (const entry of entries) {
|
|
190
|
+
if (path.isAbsolute(entry)) {
|
|
191
|
+
throw new HazoConfigError({
|
|
192
|
+
code: 'HAZO_ENV_CONFIG_SHARED_DIR_ABSOLUTE',
|
|
193
|
+
pkg: 'hazo_env',
|
|
194
|
+
message: `[deploy] shared_dirs entry "${entry}" must be relative (slot-relative), not absolute.`,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
const clean = entry.replace(/\/+$/, '');
|
|
198
|
+
const norm = path.normalize(clean);
|
|
199
|
+
if (norm.split(path.sep).includes('..')) {
|
|
200
|
+
throw new HazoConfigError({
|
|
201
|
+
code: 'HAZO_ENV_CONFIG_SHARED_DIR_TRAVERSAL',
|
|
202
|
+
pkg: 'hazo_env',
|
|
203
|
+
message: `[deploy] shared_dirs entry "${entry}" must not contain ".." path traversal.`,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
normalized.push(norm);
|
|
207
|
+
}
|
|
208
|
+
const seen = new Set();
|
|
209
|
+
for (const dir of normalized) {
|
|
210
|
+
if (seen.has(dir)) {
|
|
211
|
+
throw new HazoConfigError({
|
|
212
|
+
code: 'HAZO_ENV_CONFIG_SHARED_DIR_DUPLICATE',
|
|
213
|
+
pkg: 'hazo_env',
|
|
214
|
+
message: `[deploy] shared_dirs contains a duplicate entry: "${dir}".`,
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
seen.add(dir);
|
|
218
|
+
}
|
|
219
|
+
for (let i = 0; i < normalized.length; i++) {
|
|
220
|
+
for (let j = 0; j < normalized.length; j++) {
|
|
221
|
+
if (i === j)
|
|
222
|
+
continue;
|
|
223
|
+
if (normalized[j] === normalized[i] || normalized[j].startsWith(normalized[i] + path.sep)) {
|
|
224
|
+
throw new HazoConfigError({
|
|
225
|
+
code: 'HAZO_ENV_CONFIG_SHARED_DIR_NESTED',
|
|
226
|
+
pkg: 'hazo_env',
|
|
227
|
+
message: `[deploy] shared_dirs entry "${normalized[j]}" is nested inside "${normalized[i]}" — list only the outermost directory.`,
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return normalized;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Reject any `shared_dirs` entry that contains the resolved sqlite db path,
|
|
236
|
+
* when the db strategy resolves to "copy" (per-slot sqlite file). Best-effort:
|
|
237
|
+
* if the db config can't be resolved at all (no [db.<env>] section, env
|
|
238
|
+
* mismatch, ...) this silently skips the check — it's a safety net on top
|
|
239
|
+
* of an otherwise-valid config, not a required resolution step. Only runs
|
|
240
|
+
* when `shared_dirs` is non-empty, so a config that doesn't use the feature
|
|
241
|
+
* never pays for (or can be broken by) this extra resolution.
|
|
242
|
+
*/
|
|
243
|
+
function checkSharedDirsAgainstSqlitePath(shared_dirs, db_strategy) {
|
|
244
|
+
if (shared_dirs.length === 0)
|
|
245
|
+
return;
|
|
246
|
+
let dbConfig;
|
|
247
|
+
try {
|
|
248
|
+
dbConfig = resolveConnectConfig();
|
|
249
|
+
}
|
|
250
|
+
catch {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
if (dbConfig.type !== 'sqlite' || !dbConfig.sqlite)
|
|
254
|
+
return;
|
|
255
|
+
const dbStrategyResolved = db_strategy === 'auto' ? 'copy' : db_strategy;
|
|
256
|
+
if (dbStrategyResolved !== 'copy')
|
|
257
|
+
return;
|
|
258
|
+
const rawDbPath = dbConfig.sqlite.database_path;
|
|
259
|
+
if (path.isAbsolute(rawDbPath))
|
|
260
|
+
return; // Shared across slots by config already — not a per-slot path to protect.
|
|
261
|
+
const dbPathRel = path.normalize(rawDbPath);
|
|
262
|
+
for (const dir of shared_dirs) {
|
|
263
|
+
if (isInsideOrEqual(dbPathRel, dir)) {
|
|
264
|
+
throw new HazoConfigError({
|
|
265
|
+
code: 'HAZO_ENV_CONFIG_SHARED_DIR_CONTAINS_SQLITE_DB',
|
|
266
|
+
pkg: 'hazo_env',
|
|
267
|
+
message: `[deploy] shared_dirs entry "${dir}" contains the sqlite db path "${dbPathRel}", but db_strategy resolves to "copy" (per-slot). Remove "${dir}" from shared_dirs, or change db_strategy.`,
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
111
272
|
/**
|
|
112
273
|
* Resolve the `[deploy]` section of hazo_env_config.ini into a typed
|
|
113
274
|
* DeployConfig. `app_name` and `dir_suffix` are the only fields without a
|
|
@@ -147,6 +308,8 @@ export function resolveDeployConfig() {
|
|
|
147
308
|
message: `[deploy] db_strategy "${db_strategy_raw}" is invalid. Expected "auto", "copy", or "shared".`,
|
|
148
309
|
});
|
|
149
310
|
}
|
|
311
|
+
const shared_dirs = collectSharedDirs(section);
|
|
312
|
+
checkSharedDirsAgainstSqlitePath(shared_dirs, db_strategy_raw);
|
|
150
313
|
return {
|
|
151
314
|
app_name,
|
|
152
315
|
dir_suffix,
|
|
@@ -165,5 +328,7 @@ export function resolveDeployConfig() {
|
|
|
165
328
|
sidecar_apps: collectSidecarApps(section),
|
|
166
329
|
env_files: collectEnvFiles(section),
|
|
167
330
|
verify_tsc_cmd: (section['verify_tsc_cmd'] ?? '').trim() || 'npx tsc --noEmit',
|
|
331
|
+
shared_root: resolveSharedRoot(section, app_name, { cwd: process.cwd(), dirSuffix: dir_suffix }),
|
|
332
|
+
shared_dirs,
|
|
168
333
|
};
|
|
169
334
|
}
|
|
@@ -39,6 +39,22 @@ export interface EnvsyncConfig {
|
|
|
39
39
|
allowProd?: boolean;
|
|
40
40
|
/** Every KEY=value pair from [migrate.env_overrides], ${VAR}-expanded */
|
|
41
41
|
envOverrides: Record<string, string>;
|
|
42
|
+
/**
|
|
43
|
+
* `[deploy] shared_root`, when a [deploy] section resolves successfully —
|
|
44
|
+
* undefined otherwise (no [deploy] section, or it fails to resolve, e.g.
|
|
45
|
+
* missing app_name/dir_suffix). Used by envsync/paths.ts's
|
|
46
|
+
* resolveFilesRoot to recognize files_root as an allowed shared-dir link
|
|
47
|
+
* rather than an escaping symlink. Best-effort: envsync must keep working
|
|
48
|
+
* for apps with no [deploy] section at all.
|
|
49
|
+
*/
|
|
50
|
+
shared_root?: string;
|
|
51
|
+
/**
|
|
52
|
+
* `[deploy] shared_dirs`, or undefined/[] when unresolved — see
|
|
53
|
+
* shared_root above. Optional (rather than defaulted to `[]` on this
|
|
54
|
+
* type) so existing `EnvsyncConfig` literals built by hand in tests don't
|
|
55
|
+
* need updating; callers treat a missing value as `[]`.
|
|
56
|
+
*/
|
|
57
|
+
shared_dirs?: string[];
|
|
42
58
|
}
|
|
43
59
|
/**
|
|
44
60
|
* Resolve the [envsync] section from hazo_env_config.ini.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"envsync.d.ts","sourceRoot":"","sources":["../../src/resolve/envsync.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"envsync.d.ts","sourceRoot":"","sources":["../../src/resolve/envsync.ts"],"names":[],"mappings":"AAUA,MAAM,WAAW,aAAa;IAC5B,gEAAgE;IAChE,SAAS,EAAE,MAAM,CAAC;IAClB,oEAAoE;IACpE,SAAS,EAAE,MAAM,CAAC;IAClB,kCAAkC;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,0EAA0E;IAC1E,UAAU,EAAE,MAAM,CAAC;IACnB,oEAAoE;IACpE,QAAQ,EAAE,MAAM,CAAC;IACjB,oEAAoE;IACpE,IAAI,EAAE,MAAM,CAAC;IACb,yEAAyE;IACzE,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,qEAAqE;IACrE,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;;;;;;;OAQG;IACH,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,yEAAyE;IACzE,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrC;;;;;;;OAOG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;CACxB;AAgBD;;;;;GAKG;AACH,wBAAgB,oBAAoB,IAAI,aAAa,GAAG,IAAI,CAyF3D"}
|
package/dist/resolve/envsync.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import path from 'node:path';
|
|
6
6
|
import { HazoConfig } from 'hazo_config/server';
|
|
7
7
|
import { resolveFilesConfig } from './files.js';
|
|
8
|
+
import { resolveDeployConfig } from './deploy.js';
|
|
8
9
|
const DEFAULT_KEEP = 3;
|
|
9
10
|
function tryLoadEnvConfig() {
|
|
10
11
|
try {
|
|
@@ -73,6 +74,22 @@ export function resolveEnvsyncConfig() {
|
|
|
73
74
|
}
|
|
74
75
|
}
|
|
75
76
|
}
|
|
77
|
+
// Best-effort: pull shared_root/shared_dirs from [deploy] so paths.ts's
|
|
78
|
+
// resolveFilesRoot can recognize files_root as an allowed shared-dir link.
|
|
79
|
+
// Most envsync configs have no [deploy] section at all (envsync predates
|
|
80
|
+
// the deploy module and is used standalone in some apps) — that, or any
|
|
81
|
+
// other resolution failure (missing app_name/dir_suffix, ...), must never
|
|
82
|
+
// break envsync itself; it just means the shared-dirs allowance is empty.
|
|
83
|
+
let shared_root;
|
|
84
|
+
let shared_dirs = [];
|
|
85
|
+
try {
|
|
86
|
+
const deployConfig = resolveDeployConfig();
|
|
87
|
+
shared_root = deployConfig.shared_root;
|
|
88
|
+
shared_dirs = deployConfig.shared_dirs;
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
// No [deploy] section, or it doesn't resolve — leave shared dirs empty.
|
|
92
|
+
}
|
|
76
93
|
return {
|
|
77
94
|
source_db,
|
|
78
95
|
target_db: expandEnvVars(target_db_raw, extraVars),
|
|
@@ -86,5 +103,7 @@ export function resolveEnvsyncConfig() {
|
|
|
86
103
|
prodDbPattern: prod_db_pattern_raw !== undefined ? prod_db_pattern_raw.trim() : undefined,
|
|
87
104
|
allowProd,
|
|
88
105
|
envOverrides,
|
|
106
|
+
shared_root,
|
|
107
|
+
shared_dirs,
|
|
89
108
|
};
|
|
90
109
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hazo_env",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"description": "Canonical environment resolver — typed env names, per-env DB/file/secret config, doctor and CLI for hazo apps",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
"lint": "tsc --noEmit",
|
|
34
34
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
|
|
35
35
|
"dev:test-app": "npm run build && cd test-app && npm run dev",
|
|
36
|
-
"build:test-app": "npm run build && cd test-app && npm run build"
|
|
36
|
+
"build:test-app": "npm run build && cd test-app && npm run build",
|
|
37
|
+
"docs:deploy": "npm run build && node dist/deploy/generate-readme-cli.js"
|
|
37
38
|
},
|
|
38
39
|
"dependencies": {
|
|
39
40
|
"dotenv": "^16.4.5",
|