@clicadigital/vault-sdk 0.1.0 → 0.3.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/package.json CHANGED
@@ -1,43 +1,43 @@
1
- {
2
- "name": "@clicadigital/vault-sdk",
3
- "version": "0.1.0",
4
- "description": "SDK + CLI to ensure and pull env vars from Clica VPN Vault",
5
- "type": "module",
6
- "license": "UNLICENSED",
7
- "main": "./dist/index.js",
8
- "types": "./dist/index.d.ts",
9
- "bin": {
10
- "vault-sdk": "./dist/cli.js"
11
- },
12
- "exports": {
13
- ".": {
14
- "types": "./dist/index.d.ts",
15
- "bun": "./src/index.ts",
16
- "import": "./dist/index.js",
17
- "default": "./dist/index.js"
18
- }
19
- },
20
- "files": [
21
- "dist",
22
- "src"
23
- ],
24
- "scripts": {
25
- "build": "tsc -p tsconfig.json",
26
- "prepublishOnly": "bun run build",
27
- "vault": "bun run src/cli.ts"
28
- },
29
- "engines": {
30
- "node": ">=18"
31
- },
32
- "keywords": [
33
- "vault",
34
- "env",
35
- "secrets",
36
- "kubernetes",
37
- "clicadigital"
38
- ],
39
- "devDependencies": {
40
- "@types/node": "^22.16.5",
41
- "typescript": "^5.8.3"
42
- }
43
- }
1
+ {
2
+ "name": "@clicadigital/vault-sdk",
3
+ "version": "0.3.0",
4
+ "description": "SDK + CLI to load vault envs via cluster ServiceAccount (k3s/k8s)",
5
+ "type": "module",
6
+ "license": "UNLICENSED",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "bin": {
10
+ "vault-sdk": "./dist/cli.js"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "bun": "./src/index.ts",
16
+ "import": "./dist/index.js",
17
+ "default": "./dist/index.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "src"
23
+ ],
24
+ "scripts": {
25
+ "build": "tsc -p tsconfig.json",
26
+ "prepublishOnly": "bun run build",
27
+ "vault": "bun run src/cli.ts"
28
+ },
29
+ "engines": {
30
+ "node": ">=18"
31
+ },
32
+ "keywords": [
33
+ "vault",
34
+ "env",
35
+ "secrets",
36
+ "kubernetes",
37
+ "clicadigital"
38
+ ],
39
+ "devDependencies": {
40
+ "@types/node": "^22.16.5",
41
+ "typescript": "^5.8.3"
42
+ }
43
+ }
package/src/cli.ts CHANGED
@@ -1,132 +1,134 @@
1
- #!/usr/bin/env node
2
- import { createClientFromEnv } from "./client.js";
3
- import { createEnv, ensureEnvironment, pullVault } from "./index.js";
4
-
5
- type Flags = Record<string, string | boolean | string[]>;
6
-
7
- function parseArgs(argv: string[]): { cmd: string; flags: Flags } {
8
- const [cmd = "help", ...rest] = argv;
9
- const flags: Flags = {};
10
- for (let i = 0; i < rest.length; i++) {
11
- const token = rest[i];
12
- if (!token?.startsWith("--")) continue;
13
- const key = token.slice(2);
14
- const next = rest[i + 1];
15
- if (!next || next.startsWith("--")) {
16
- flags[key] = true;
17
- continue;
18
- }
19
- if (key === "var") {
20
- const list = Array.isArray(flags.var) ? flags.var : [];
21
- list.push(next);
22
- flags.var = list;
23
- } else {
24
- flags[key] = next;
25
- }
26
- i++;
27
- }
28
- return { cmd, flags };
29
- }
30
-
31
- function str(flags: Flags, key: string, fallback?: string): string {
32
- const v = flags[key];
33
- if (typeof v === "string" && v.length > 0) return v;
34
- if (fallback !== undefined) return fallback;
35
- throw new Error(`Missing --${key}`);
36
- }
37
-
38
- function printHelp(): void {
39
- console.log(`@clicadigital/vault-sdk — cria e carrega envs via URL do vault na VPN
40
-
41
- Usage:
42
- vault-sdk create --project <name> --env <name> [--url <vpn-url>] [--var KEY=VAL]...
43
- vault-sdk ensure --project <name> --env <name> [--url <vpn-url>]
44
- vault-sdk pull --project <name> --env <name> [--out .env] [--no-inject] [--url <vpn-url>]
45
-
46
- Env:
47
- VAULT_URL URL do vault no cluster/VPN (ex: http://vault-api:3001)
48
- VAULT_PROJECT default --project
49
- VAULT_ENV default --env
50
-
51
- Auth: nenhuma chave — acesso só via rede interna (pods / VPN).
52
- `);
53
- }
54
-
55
- function applyUrl(flags: Flags): void {
56
- if (typeof flags.url === "string") process.env.VAULT_URL = flags.url;
57
- }
58
-
59
- function parseVarList(flags: Flags): Record<string, string> {
60
- const out: Record<string, string> = {};
61
- const list = Array.isArray(flags.var) ? flags.var : [];
62
- for (const pair of list) {
63
- const eq = pair.indexOf("=");
64
- if (eq < 1) throw new Error(`Invalid --var ${pair} (use KEY=VALUE)`);
65
- out[pair.slice(0, eq)] = pair.slice(eq + 1);
66
- }
67
- return out;
68
- }
69
-
70
- async function main(): Promise<void> {
71
- const { cmd, flags } = parseArgs(process.argv.slice(2));
72
-
73
- if (cmd === "help" || flags.help === true) {
74
- printHelp();
75
- return;
76
- }
77
-
78
- applyUrl(flags);
79
-
80
- const project = str(flags, "project", process.env.VAULT_PROJECT);
81
- const env = str(flags, "env", process.env.VAULT_ENV);
82
- const url = typeof flags.url === "string" ? flags.url : undefined;
83
- const client = url
84
- ? createClientFromEnv({ ...process.env, VAULT_URL: url })
85
- : undefined;
86
-
87
- if (cmd === "create") {
88
- const vars = parseVarList(flags);
89
- const result = await createEnv({ project, env, url, vars, client });
90
- console.log(JSON.stringify({ project, env, ...result }, null, 2));
91
- return;
92
- }
93
-
94
- if (cmd === "ensure") {
95
- const result = await ensureEnvironment({ project, env, client });
96
- console.log(
97
- JSON.stringify(
98
- {
99
- project,
100
- env,
101
- projectId: result.projectId,
102
- environmentId: result.environmentId,
103
- createdProject: result.createdProject,
104
- createdEnvironment: result.createdEnvironment,
105
- },
106
- null,
107
- 2,
108
- ),
109
- );
110
- return;
111
- }
112
-
113
- if (cmd === "pull") {
114
- const out = typeof flags.out === "string" ? flags.out : undefined;
115
- const inject = flags["no-inject"] !== true;
116
- const result = await pullVault({ project, env, out, inject, client });
117
- console.log(
118
- `pulled ${Object.keys(result.vars).length} vars for ${project}/${env}` +
119
- (result.out ? ` → ${result.out}` : "") +
120
- (inject ? " (injected into process.env)" : ""),
121
- );
122
- return;
123
- }
124
-
125
- printHelp();
126
- process.exit(1);
127
- }
128
-
129
- main().catch((err) => {
130
- console.error(err instanceof Error ? err.message : err);
131
- process.exit(1);
132
- });
1
+ #!/usr/bin/env node
2
+ import { createClientFromEnv } from "./client.js";
3
+ import { createEnv, ensureEnvironment, pullVault } from "./index.js";
4
+
5
+ type Flags = Record<string, string | boolean | string[]>;
6
+
7
+ function parseArgs(argv: string[]): { cmd: string; flags: Flags } {
8
+ const [cmd = "help", ...rest] = argv;
9
+ const flags: Flags = {};
10
+ for (let i = 0; i < rest.length; i++) {
11
+ const token = rest[i];
12
+ if (!token?.startsWith("--")) continue;
13
+ const key = token.slice(2);
14
+ const next = rest[i + 1];
15
+ if (!next || next.startsWith("--")) {
16
+ flags[key] = true;
17
+ continue;
18
+ }
19
+ if (key === "var") {
20
+ const list = Array.isArray(flags.var) ? flags.var : [];
21
+ list.push(next);
22
+ flags.var = list;
23
+ } else {
24
+ flags[key] = next;
25
+ }
26
+ i++;
27
+ }
28
+ return { cmd, flags };
29
+ }
30
+
31
+ function str(flags: Flags, key: string, fallback?: string): string {
32
+ const v = flags[key];
33
+ if (typeof v === "string" && v.length > 0) return v;
34
+ if (fallback !== undefined) return fallback;
35
+ throw new Error(`Missing --${key}`);
36
+ }
37
+
38
+ function printHelp(): void {
39
+ console.log(`@clicadigital/vault-sdk — cria e carrega envs via URL do vault na VPN
40
+
41
+ Usage:
42
+ vault-sdk create --project <name> --env <name> [--url <vpn-url>] [--var KEY=VAL]...
43
+ vault-sdk ensure --project <name> --env <name> [--url <vpn-url>]
44
+ vault-sdk pull --project <name> --env <name> [--out .env] [--no-inject] [--url <vpn-url>]
45
+
46
+ Env:
47
+ VAULT_URL opcional; default k8s: http://vault-api-service.vault.svc.cluster.local:3001
48
+ VAULT_PROJECT default --project
49
+ VAULT_ENV default --env
50
+ VAULT_SA_TOKEN_PATH path do token SA (default: /var/run/secrets/tokens/vault/token)
51
+
52
+ Auth no k3s: ServiceAccount token (audience=vault) + NetworkPolicy.
53
+ Local (VAULT_AUTH=none na API): sem token.
54
+ `);
55
+ }
56
+
57
+ function applyUrl(flags: Flags): void {
58
+ if (typeof flags.url === "string") process.env.VAULT_URL = flags.url;
59
+ }
60
+
61
+ function parseVarList(flags: Flags): Record<string, string> {
62
+ const out: Record<string, string> = {};
63
+ const list = Array.isArray(flags.var) ? flags.var : [];
64
+ for (const pair of list) {
65
+ const eq = pair.indexOf("=");
66
+ if (eq < 1) throw new Error(`Invalid --var ${pair} (use KEY=VALUE)`);
67
+ out[pair.slice(0, eq)] = pair.slice(eq + 1);
68
+ }
69
+ return out;
70
+ }
71
+
72
+ async function main(): Promise<void> {
73
+ const { cmd, flags } = parseArgs(process.argv.slice(2));
74
+
75
+ if (cmd === "help" || flags.help === true) {
76
+ printHelp();
77
+ return;
78
+ }
79
+
80
+ applyUrl(flags);
81
+
82
+ const project = str(flags, "project", process.env.VAULT_PROJECT);
83
+ const env = str(flags, "env", process.env.VAULT_ENV);
84
+ const url = typeof flags.url === "string" ? flags.url : undefined;
85
+ const client = url
86
+ ? createClientFromEnv({ ...process.env, VAULT_URL: url })
87
+ : undefined;
88
+
89
+ if (cmd === "create") {
90
+ const vars = parseVarList(flags);
91
+ const result = await createEnv({ project, env, url, vars, client });
92
+ console.log(JSON.stringify({ project, env, ...result }, null, 2));
93
+ return;
94
+ }
95
+
96
+ if (cmd === "ensure") {
97
+ const result = await ensureEnvironment({ project, env, client });
98
+ console.log(
99
+ JSON.stringify(
100
+ {
101
+ project,
102
+ env,
103
+ projectId: result.projectId,
104
+ environmentId: result.environmentId,
105
+ createdProject: result.createdProject,
106
+ createdEnvironment: result.createdEnvironment,
107
+ },
108
+ null,
109
+ 2,
110
+ ),
111
+ );
112
+ return;
113
+ }
114
+
115
+ if (cmd === "pull") {
116
+ const out = typeof flags.out === "string" ? flags.out : undefined;
117
+ const inject = flags["no-inject"] !== true;
118
+ const result = await pullVault({ project, env, out, inject, client });
119
+ console.log(
120
+ `pulled ${Object.keys(result.vars).length} vars for ${project}/${env}` +
121
+ (result.out ? ` → ${result.out}` : "") +
122
+ (inject ? " (injected into process.env)" : ""),
123
+ );
124
+ return;
125
+ }
126
+
127
+ printHelp();
128
+ process.exit(1);
129
+ }
130
+
131
+ main().catch((err) => {
132
+ console.error(err instanceof Error ? err.message : err);
133
+ process.exit(1);
134
+ });
package/src/client.ts CHANGED
@@ -1,74 +1,109 @@
1
- import type { EnvVar, ProjectTree } from "./types.js";
2
-
3
- export type VaultClientOptions = {
4
- /** Base URL da API do vault na VPN, ex: http://vault-api.vpn.local:3001 */
5
- baseUrl: string;
6
- fetch?: typeof globalThis.fetch;
7
- };
8
-
9
- export class VaultClient {
10
- readonly baseUrl: string;
11
- private readonly fetchFn: typeof globalThis.fetch;
12
-
13
- constructor(opts: VaultClientOptions) {
14
- this.baseUrl = opts.baseUrl.replace(/\/+$/, "");
15
- this.fetchFn = opts.fetch ?? globalThis.fetch.bind(globalThis);
16
- }
17
-
18
- private async request<T>(path: string, init?: RequestInit): Promise<T> {
19
- const res = await this.fetchFn(`${this.baseUrl}${path}`, {
20
- ...init,
21
- headers: {
22
- "content-type": "application/json",
23
- ...(init?.headers ?? {}),
24
- },
25
- });
26
- if (!res.ok) {
27
- const body = (await res.json().catch(() => null)) as { error?: string } | null;
28
- throw new Error(body?.error ?? `Vault API ${res.status} ${path}`);
29
- }
30
- if (res.status === 204) return undefined as T;
31
- return (await res.json()) as T;
32
- }
33
-
34
- listProjects(): Promise<ProjectTree[]> {
35
- return this.request("/v1/projects");
36
- }
37
-
38
- createProject(name: string): Promise<{ id: string; name: string }> {
39
- return this.request("/v1/projects", {
40
- method: "POST",
41
- body: JSON.stringify({ name }),
42
- });
43
- }
44
-
45
- createEnvironment(projectId: string, name: string): Promise<{ ok: boolean }> {
46
- return this.request(`/v1/projects/${projectId}/environments`, {
47
- method: "POST",
48
- body: JSON.stringify({ name }),
49
- });
50
- }
51
-
52
- listVars(environmentId: string): Promise<EnvVar[]> {
53
- return this.request(`/v1/environments/${environmentId}/vars`);
54
- }
55
-
56
- upsertVar(environmentId: string, key: string, value: string): Promise<{ ok: boolean }> {
57
- return this.request(`/v1/environments/${environmentId}/vars`, {
58
- method: "PUT",
59
- body: JSON.stringify({ key, value }),
60
- });
61
- }
62
- }
63
-
64
- export function createClientFromEnv(
65
- env: NodeJS.ProcessEnv = process.env,
66
- ): VaultClient {
67
- const baseUrl = env.VAULT_URL ?? env.VAULT_API_URL ?? env.API_URL;
68
- if (!baseUrl) {
69
- throw new Error(
70
- "VAULT_URL is required (URL local da VPN, ex: http://vault-api:3001)",
71
- );
72
- }
73
- return new VaultClient({ baseUrl });
74
- }
1
+ import { readFileSync, existsSync } from "node:fs";
2
+ import type { EnvVar, ProjectTree } from "./types.js";
3
+
4
+ const DEFAULT_TOKEN_PATHS = [
5
+ "/var/run/secrets/tokens/vault/token",
6
+ "/var/run/secrets/kubernetes.io/serviceaccount/token",
7
+ ];
8
+
9
+ /** DNS interno do Service no k3s (namespace `vault`). Override com VAULT_URL. */
10
+ export const DEFAULT_VAULT_URL =
11
+ "http://vault-api-service.vault.svc.cluster.local:3001";
12
+
13
+ export type VaultClientOptions = {
14
+ /** Base URL da API do vault na VPN. Default: DEFAULT_VAULT_URL */
15
+ baseUrl: string;
16
+ /** Bearer token (ServiceAccount projected). Se omitido, tenta ler do filesystem do pod. */
17
+ token?: string;
18
+ /** Paths para procurar o token SA (ordem de prioridade). */
19
+ tokenPaths?: string[];
20
+ fetch?: typeof globalThis.fetch;
21
+ };
22
+
23
+ function resolveToken(opts: VaultClientOptions): string | undefined {
24
+ if (opts.token) return opts.token;
25
+ const paths = opts.tokenPaths ?? DEFAULT_TOKEN_PATHS;
26
+ for (const path of paths) {
27
+ if (!existsSync(path)) continue;
28
+ try {
29
+ const token = readFileSync(path, "utf8").trim();
30
+ if (token) return token;
31
+ } catch {
32
+ // ignore unreadable path
33
+ }
34
+ }
35
+ return undefined;
36
+ }
37
+
38
+ export class VaultClient {
39
+ readonly baseUrl: string;
40
+ private readonly token: string | undefined;
41
+ private readonly fetchFn: typeof globalThis.fetch;
42
+
43
+ constructor(opts: VaultClientOptions) {
44
+ this.baseUrl = opts.baseUrl.replace(/\/+$/, "");
45
+ this.token = resolveToken(opts);
46
+ this.fetchFn = opts.fetch ?? globalThis.fetch.bind(globalThis);
47
+ }
48
+
49
+ private async request<T>(path: string, init?: RequestInit): Promise<T> {
50
+ const headers: Record<string, string> = {
51
+ "content-type": "application/json",
52
+ ...(init?.headers as Record<string, string> | undefined),
53
+ };
54
+ if (this.token) headers.authorization = `Bearer ${this.token}`;
55
+
56
+ const res = await this.fetchFn(`${this.baseUrl}${path}`, {
57
+ ...init,
58
+ headers,
59
+ });
60
+ if (!res.ok) {
61
+ const body = (await res.json().catch(() => null)) as { error?: string } | null;
62
+ throw new Error(body?.error ?? `Vault API ${res.status} ${path}`);
63
+ }
64
+ if (res.status === 204) return undefined as T;
65
+ return (await res.json()) as T;
66
+ }
67
+
68
+ listProjects(): Promise<ProjectTree[]> {
69
+ return this.request("/v1/projects");
70
+ }
71
+
72
+ createProject(name: string): Promise<{ id: string; name: string }> {
73
+ return this.request("/v1/projects", {
74
+ method: "POST",
75
+ body: JSON.stringify({ name }),
76
+ });
77
+ }
78
+
79
+ createEnvironment(projectId: string, name: string): Promise<{ ok: boolean }> {
80
+ return this.request(`/v1/projects/${projectId}/environments`, {
81
+ method: "POST",
82
+ body: JSON.stringify({ name }),
83
+ });
84
+ }
85
+
86
+ listVars(environmentId: string): Promise<EnvVar[]> {
87
+ return this.request(`/v1/environments/${environmentId}/vars`);
88
+ }
89
+
90
+ upsertVar(environmentId: string, key: string, value: string): Promise<{ ok: boolean }> {
91
+ return this.request(`/v1/environments/${environmentId}/vars`, {
92
+ method: "PUT",
93
+ body: JSON.stringify({ key, value }),
94
+ });
95
+ }
96
+ }
97
+
98
+ export function createClientFromEnv(
99
+ env: NodeJS.ProcessEnv = process.env,
100
+ ): VaultClient {
101
+ const baseUrl =
102
+ env.VAULT_URL ?? env.VAULT_API_URL ?? env.API_URL ?? DEFAULT_VAULT_URL;
103
+ const tokenPath = env.VAULT_SA_TOKEN_PATH;
104
+ return new VaultClient({
105
+ baseUrl,
106
+ token: env.VAULT_SA_TOKEN,
107
+ tokenPaths: tokenPath ? [tokenPath, ...DEFAULT_TOKEN_PATHS] : undefined,
108
+ });
109
+ }
package/src/dotenv.ts CHANGED
@@ -1,22 +1,22 @@
1
- export function toDotEnv(vars: Record<string, string>): string {
2
- return (
3
- Object.entries(vars)
4
- .sort(([a], [b]) => a.localeCompare(b))
5
- .map(([key, value]) => `${key}=${escapeEnvValue(value)}`)
6
- .join("\n") + "\n"
7
- );
8
- }
9
-
10
- function escapeEnvValue(value: string): string {
11
- if (value === "") return '""';
12
- if (/^[A-Za-z0-9_./:@%+=,-]+$/.test(value)) return value;
13
- return JSON.stringify(value);
14
- }
15
-
16
- export function varsToRecord(
17
- rows: { key: string; value: string }[],
18
- ): Record<string, string> {
19
- const out: Record<string, string> = {};
20
- for (const row of rows) out[row.key] = row.value;
21
- return out;
22
- }
1
+ export function toDotEnv(vars: Record<string, string>): string {
2
+ return (
3
+ Object.entries(vars)
4
+ .sort(([a], [b]) => a.localeCompare(b))
5
+ .map(([key, value]) => `${key}=${escapeEnvValue(value)}`)
6
+ .join("\n") + "\n"
7
+ );
8
+ }
9
+
10
+ function escapeEnvValue(value: string): string {
11
+ if (value === "") return '""';
12
+ if (/^[A-Za-z0-9_./:@%+=,-]+$/.test(value)) return value;
13
+ return JSON.stringify(value);
14
+ }
15
+
16
+ export function varsToRecord(
17
+ rows: { key: string; value: string }[],
18
+ ): Record<string, string> {
19
+ const out: Record<string, string> = {};
20
+ for (const row of rows) out[row.key] = row.value;
21
+ return out;
22
+ }