@catladder/cli 5.0.1 → 5.1.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.
Files changed (131) hide show
  1. package/dist/apps/cli/src/apps/cli/commands/project/doctor/checkGithub.js +3 -3
  2. package/dist/apps/cli/src/apps/cli/commands/project/doctor/checkGithub.js.map +1 -1
  3. package/dist/apps/cli/src/apps/cli/commands/project/doctor/checkGithubImageCasing.d.ts +15 -0
  4. package/dist/apps/cli/src/apps/cli/commands/project/doctor/checkGithubImageCasing.js +60 -0
  5. package/dist/apps/cli/src/apps/cli/commands/project/doctor/checkGithubImageCasing.js.map +1 -0
  6. package/dist/apps/cli/src/apps/cli/commands/project/doctor/index.js +2 -0
  7. package/dist/apps/cli/src/apps/cli/commands/project/doctor/index.js.map +1 -1
  8. package/dist/apps/cli/src/apps/cli/commands/project/setup/setupAccessTokens.js +2 -2
  9. package/dist/apps/cli/src/apps/cli/commands/project/setup/setupAccessTokens.js.map +1 -1
  10. package/dist/apps/cli/src/apps/cli/commands/project/setup/setupGithub.js +2 -2
  11. package/dist/apps/cli/src/apps/cli/commands/project/setup/setupGithub.js.map +1 -1
  12. package/dist/apps/cli/src/apps/cli/commands/project/setup/setupKubernetes.js +23 -7
  13. package/dist/apps/cli/src/apps/cli/commands/project/setup/setupKubernetes.js.map +1 -1
  14. package/dist/apps/cli/src/commands/index.d.ts +1 -0
  15. package/dist/apps/cli/src/commands/index.js +4 -1
  16. package/dist/apps/cli/src/commands/index.js.map +1 -1
  17. package/dist/apps/cli/src/commands/project/commandConfigSecrets.js +2 -1
  18. package/dist/apps/cli/src/commands/project/commandConfigSecrets.js.map +1 -1
  19. package/dist/apps/cli/src/commands/project/commandMrPin.d.ts +21 -0
  20. package/dist/apps/cli/src/commands/project/commandMrPin.js +119 -0
  21. package/dist/apps/cli/src/commands/project/commandMrPin.js.map +1 -0
  22. package/dist/apps/cli/src/commands/project/commandSecretsSyncGithub.d.ts +1 -42
  23. package/dist/apps/cli/src/commands/project/commandSecretsSyncGithub.js +5 -124
  24. package/dist/apps/cli/src/commands/project/commandSecretsSyncGithub.js.map +1 -1
  25. package/dist/apps/cli/src/commands/project/secrets/commandSecretsPush.js +2 -1
  26. package/dist/apps/cli/src/commands/project/secrets/commandSecretsPush.js.map +1 -1
  27. package/dist/apps/cli/src/commands/project/secrets/commandSecretsSet.js +2 -2
  28. package/dist/apps/cli/src/commands/project/secrets/commandSecretsSet.js.map +1 -1
  29. package/dist/apps/cli/src/commands/project/secrets/document.d.ts +1 -12
  30. package/dist/apps/cli/src/commands/project/secrets/document.js +1 -79
  31. package/dist/apps/cli/src/commands/project/secrets/document.js.map +1 -1
  32. package/dist/apps/cli/src/gcloud/serviceAccounts.js +15 -4
  33. package/dist/apps/cli/src/gcloud/serviceAccounts.js.map +1 -1
  34. package/dist/apps/cli/src/secrets/github.d.ts +41 -0
  35. package/dist/apps/cli/src/secrets/github.js +127 -0
  36. package/dist/apps/cli/src/secrets/github.js.map +1 -0
  37. package/dist/apps/cli/src/secrets/index.d.ts +2 -0
  38. package/dist/apps/cli/src/secrets/index.js +19 -0
  39. package/dist/apps/cli/src/secrets/index.js.map +1 -0
  40. package/dist/apps/cli/src/secrets/writeSecrets.d.ts +18 -0
  41. package/dist/apps/cli/src/secrets/writeSecrets.js +88 -0
  42. package/dist/apps/cli/src/secrets/writeSecrets.js.map +1 -0
  43. package/dist/apps/cli/src/utils/github.js +2 -2
  44. package/dist/apps/cli/src/utils/github.js.map +1 -1
  45. package/dist/apps/cli/src/utils/gitlab.d.ts +10 -0
  46. package/dist/apps/cli/src/utils/gitlab.js +30 -5
  47. package/dist/apps/cli/src/utils/gitlab.js.map +1 -1
  48. package/dist/apps/cli/src/vault/BitwardenVault.d.ts +6 -0
  49. package/dist/apps/cli/src/vault/BitwardenVault.js +11 -1
  50. package/dist/apps/cli/src/vault/BitwardenVault.js.map +1 -1
  51. package/dist/apps/cli/src/vault/GitlabVault.d.ts +2 -2
  52. package/dist/apps/cli/src/vault/GitlabVault.js +3 -2
  53. package/dist/apps/cli/src/vault/GitlabVault.js.map +1 -1
  54. package/dist/apps/cli/src/vault/VaultManager.d.ts +2 -2
  55. package/dist/apps/cli/src/vault/VaultManager.js +2 -2
  56. package/dist/apps/cli/src/vault/VaultManager.js.map +1 -1
  57. package/dist/apps/cli/src/vault/types.d.ts +16 -2
  58. package/dist/bundles/catenv/index.js +265 -28
  59. package/dist/bundles/cli/index.js +851 -329
  60. package/dist/bundles/skills/catladder-cli/references/commands.md +13 -0
  61. package/dist/bundles/skills/catladder-config/SKILL.md +3 -1
  62. package/dist/bundles/skills/catladder-deploys/SKILL.md +7 -0
  63. package/dist/bundles/skills/catladder-migrate-ci-backend/SKILL.md +18 -2
  64. package/dist/bundles/skills/catladder-pipelines/SKILL.md +51 -1
  65. package/dist/bundles/skills/catladder-secrets/SKILL.md +16 -0
  66. package/dist/packages/pipeline/src/autoStop.d.ts +22 -0
  67. package/dist/packages/pipeline/src/autoStop.js +30 -0
  68. package/dist/packages/pipeline/src/autoStop.js.map +1 -0
  69. package/dist/packages/pipeline/src/backends/github/GithubBackend.js +11 -5
  70. package/dist/packages/pipeline/src/backends/github/GithubBackend.js.map +1 -1
  71. package/dist/packages/pipeline/src/backends/github/ciVariables.d.ts +13 -2
  72. package/dist/packages/pipeline/src/backends/github/ciVariables.js +18 -5
  73. package/dist/packages/pipeline/src/backends/github/ciVariables.js.map +1 -1
  74. package/dist/packages/pipeline/src/backends/github/createGithubJobs.js +1 -1
  75. package/dist/packages/pipeline/src/backends/github/createGithubJobs.js.map +1 -1
  76. package/dist/packages/pipeline/src/backends/github/ghcr.d.ts +29 -0
  77. package/dist/packages/pipeline/src/backends/github/ghcr.js +40 -0
  78. package/dist/packages/pipeline/src/backends/github/ghcr.js.map +1 -0
  79. package/dist/packages/pipeline/src/backends/github/index.d.ts +2 -0
  80. package/dist/packages/pipeline/src/backends/github/index.js +2 -0
  81. package/dist/packages/pipeline/src/backends/github/index.js.map +1 -1
  82. package/dist/packages/pipeline/src/backends/github/registryImage.d.ts +20 -0
  83. package/dist/packages/pipeline/src/backends/github/registryImage.js +49 -0
  84. package/dist/packages/pipeline/src/backends/github/registryImage.js.map +1 -0
  85. package/dist/packages/pipeline/src/backends/gitlab/GitlabBackend.js +2 -0
  86. package/dist/packages/pipeline/src/backends/gitlab/GitlabBackend.js.map +1 -1
  87. package/dist/packages/pipeline/src/backends/gitlab/createGitlabPipeline.d.ts +4 -1
  88. package/dist/packages/pipeline/src/backends/gitlab/createGitlabPipeline.js +21 -1
  89. package/dist/packages/pipeline/src/backends/gitlab/createGitlabPipeline.js.map +1 -1
  90. package/dist/packages/pipeline/src/customImages/jobImagesPlan.d.ts +15 -3
  91. package/dist/packages/pipeline/src/customImages/jobImagesPlan.js +14 -3
  92. package/dist/packages/pipeline/src/customImages/jobImagesPlan.js.map +1 -1
  93. package/dist/packages/pipeline/src/deploy/base/deploy.js +8 -2
  94. package/dist/packages/pipeline/src/deploy/base/deploy.js.map +1 -1
  95. package/dist/packages/pipeline/src/index.d.ts +1 -0
  96. package/dist/packages/pipeline/src/index.js +1 -0
  97. package/dist/packages/pipeline/src/index.js.map +1 -1
  98. package/dist/packages/pipeline/src/types/config.d.ts +44 -0
  99. package/dist/skills/catladder-cli/references/commands.md +13 -0
  100. package/dist/skills/catladder-config/SKILL.md +3 -1
  101. package/dist/skills/catladder-deploys/SKILL.md +7 -0
  102. package/dist/skills/catladder-migrate-ci-backend/SKILL.md +18 -2
  103. package/dist/skills/catladder-pipelines/SKILL.md +51 -1
  104. package/dist/skills/catladder-secrets/SKILL.md +16 -0
  105. package/dist/tsconfig.tsbuildinfo +1 -1
  106. package/package.json +1 -1
  107. package/src/apps/cli/commands/project/doctor/checkGithub.ts +1 -1
  108. package/src/apps/cli/commands/project/doctor/checkGithubImageCasing.ts +87 -0
  109. package/src/apps/cli/commands/project/doctor/index.ts +2 -0
  110. package/src/apps/cli/commands/project/setup/setupAccessTokens.ts +3 -2
  111. package/src/apps/cli/commands/project/setup/setupGithub.ts +1 -1
  112. package/src/apps/cli/commands/project/setup/setupKubernetes.ts +31 -20
  113. package/src/commands/index.ts +1 -0
  114. package/src/commands/project/commandConfigSecrets.ts +1 -1
  115. package/src/commands/project/commandMrPin.ts +184 -0
  116. package/src/commands/project/commandSecretsSyncGithub.ts +7 -154
  117. package/src/commands/project/secrets/commandSecretsPush.ts +2 -5
  118. package/src/commands/project/secrets/commandSecretsSet.ts +2 -2
  119. package/src/commands/project/secrets/document.ts +2 -127
  120. package/src/gcloud/serviceAccounts.ts +16 -7
  121. package/src/secrets/__tests__/writeSecrets.test.ts +147 -0
  122. package/src/secrets/github.ts +157 -0
  123. package/src/secrets/index.ts +2 -0
  124. package/src/secrets/writeSecrets.ts +138 -0
  125. package/src/utils/github.ts +2 -4
  126. package/src/utils/gitlab.ts +34 -7
  127. package/src/vault/BitwardenVault.ts +10 -1
  128. package/src/vault/GitlabVault.ts +9 -2
  129. package/src/vault/VaultManager.ts +3 -2
  130. package/src/vault/__tests__/BitwardenVault.test.ts +88 -0
  131. package/src/vault/types.ts +17 -1
@@ -1,21 +1,6 @@
1
- import type { Config } from "@catladder/pipeline";
2
- import {
3
- getEnabledPipelineTypes,
4
- getSecretVarName,
5
- getVaultConfig,
6
- } from "@catladder/pipeline";
7
- import {
8
- getEnvironment,
9
- getProjectConfig,
10
- } from "../../../config/getProjectConfig";
1
+ import { getSecretVarName } from "@catladder/pipeline";
11
2
  import type { IO } from "../../../core/types";
12
- import { isGhAuthenticated } from "../../../utils/github";
13
- import { upsertAllVariables } from "../../../utils/gitlab";
14
- import {
15
- collectSecretTargets,
16
- getConfiguredGithubRepo,
17
- pushSecretsToGithub,
18
- } from "../commandSecretsSyncGithub";
3
+ import type { SecretsWrite } from "../../../secrets";
19
4
  import type { ScopedSecretKeys } from "./scope";
20
5
 
21
6
  /** placeholder for secrets that have no value in the vault yet */
@@ -31,13 +16,6 @@ export type SecretsDocument = {
31
16
  };
32
17
  };
33
18
 
34
- /** secrets of one env of one component, ready to be written */
35
- export type SecretsWrite = {
36
- env: string;
37
- componentName: string;
38
- secrets: Record<string, unknown>;
39
- };
40
-
41
19
  /* for convenience, parse json values. that makes it easier to edit secrets that are objects */
42
20
  const parseJsonValue = (value: string): unknown => {
43
21
  try {
@@ -164,106 +142,3 @@ export const collectSecretsWrites = (
164
142
  }
165
143
  return { writes, skipped };
166
144
  };
167
-
168
- /**
169
- * writes secrets to the vault (the source of truth) and mirrors them
170
- * to every enabled CI backend — which backends exist is invisible to
171
- * the caller
172
- */
173
- export const writeSecretsAndMirror = async (
174
- io: IO,
175
- writes: SecretsWrite[],
176
- ): Promise<void> => {
177
- const config = await getProjectConfig();
178
- if (!config) {
179
- throw new Error("no catladder config found");
180
- }
181
-
182
- io.log("");
183
- io.log("writing secrets to the vault, please wait...");
184
- io.log("");
185
- for (const { env, componentName, secrets } of writes) {
186
- io.log("writing " + env + ":" + componentName + "...\n");
187
- await (
188
- await io.getVaultManager()
189
- ).writeSecrets(env, componentName, secrets);
190
- io.log("");
191
- io.log("✅ " + env + ":" + componentName);
192
- io.log("--------------------------------\n");
193
- }
194
-
195
- await mirrorSecretsToCiBackends(io, config, writes);
196
- };
197
-
198
- /**
199
- * the vault is the source of truth; every enabled CI backend receives
200
- * a mirrored copy of the written secrets (gitlab variables, github
201
- * secrets), so all CI systems stay in sync
202
- */
203
- const mirrorSecretsToCiBackends = async (
204
- io: IO,
205
- config: Config,
206
- writes: SecretsWrite[],
207
- ) => {
208
- const enabled = getEnabledPipelineTypes(config);
209
-
210
- // gitlab mirror (unless gitlab is the vault itself, then it is
211
- // already written)
212
- if (getVaultConfig(config).type !== "gitlab" && enabled.includes("gitlab")) {
213
- for (const { env, componentName, secrets } of writes) {
214
- await upsertAllVariables(io, secrets, env, componentName);
215
- }
216
- io.log("✅ mirrored to gitlab");
217
- }
218
-
219
- if (!enabled.includes("github")) {
220
- return;
221
- }
222
- const kinds = new Map<string, "secret" | "variable">();
223
- for (const { env, componentName } of writes) {
224
- const { secretEnvVarKeys, jobOnlyVars } = await getEnvironment(
225
- env,
226
- componentName,
227
- );
228
- [
229
- ...secretEnvVarKeys,
230
- ...jobOnlyVars.build.secretEnvVarKeys,
231
- ...jobOnlyVars.deploy.secretEnvVarKeys,
232
- ].forEach(({ key, kind }) =>
233
- kinds.set(getSecretVarName(env, componentName, key), kind ?? "secret"),
234
- );
235
- }
236
- const secrets = writes.flatMap(({ env, componentName, secrets }) =>
237
- Object.entries(secrets).map(([key, value]) => {
238
- const name = getSecretVarName(env, componentName, key);
239
- return {
240
- name,
241
- value: typeof value === "string" ? value : JSON.stringify(value ?? ""),
242
- kind: kinds.get(name) ?? ("secret" as const),
243
- };
244
- }),
245
- );
246
- if (secrets.length === 0) {
247
- return;
248
- }
249
- if (!(await isGhAuthenticated())) {
250
- io.log(
251
- "⚠️ github pipeline is enabled, but the github cli (gh) is not authenticated — run `project secrets-sync-github` later to mirror the secrets",
252
- );
253
- return;
254
- }
255
- const repo = await getConfiguredGithubRepo();
256
- if (!repo) {
257
- io.log(
258
- "⚠️ github pipeline is enabled, but no github repository was found — run `project secrets-sync-github` later to mirror the secrets",
259
- );
260
- return;
261
- }
262
- io.log(`mirroring ${secrets.length} secrets to github ('${repo}')...`);
263
- await pushSecretsToGithub(
264
- io,
265
- repo,
266
- secrets,
267
- await collectSecretTargets(config),
268
- );
269
- };
@@ -2,7 +2,7 @@ import type { ComponentContext } from "@catladder/pipeline";
2
2
 
3
3
  import { exec } from "child-process-promise";
4
4
  import type { IO } from "../core/types";
5
- import { upsertAllVariables } from "../utils/gitlab";
5
+ import { writeSecretsAndMirror } from "../secrets";
6
6
  import { getGcloudServiceAccountNames } from "./serviceAccountNames";
7
7
 
8
8
  export const accountExists = async (fullIdentifier: string) => {
@@ -76,13 +76,22 @@ export const upsertGcloudServiceAccountAndSaveSecret = async (
76
76
  instance.log("upserting service account " + account.name + "...");
77
77
  const key = await upsertGcloudServiceAccount(context, account);
78
78
 
79
- await upsertAllVariables(
79
+ // through the vault (and from there to every enabled ci backend) —
80
+ // the key is a secret like any other, the deploy jobs read it under
81
+ // its declared name. Writing it to gitlab directly would break every
82
+ // project that has no gitlab.
83
+ await writeSecretsAndMirror(
80
84
  instance,
81
- {
82
- [secretName]: key,
83
- },
84
- context.env,
85
- context.name,
85
+ [
86
+ {
87
+ env: context.env,
88
+ componentName: context.name,
89
+ secrets: { [secretName]: key },
90
+ },
91
+ ],
92
+ // a fresh key is created on every run, the previous one is deleted
93
+ // at google — a backup copy of it would only be a dead secret
94
+ { backup: false },
86
95
  );
87
96
  instance.log("done!");
88
97
  };
@@ -0,0 +1,147 @@
1
+ import type { Config } from "@catladder/pipeline";
2
+ import { beforeEach, describe, expect, it, vi } from "vitest";
3
+ import type { IO } from "../../core/types";
4
+ import { writeSecretsAndMirror } from "../writeSecrets";
5
+
6
+ const { getProjectConfig, getEnvironment } = vi.hoisted(() => ({
7
+ getProjectConfig: vi.fn(),
8
+ getEnvironment: vi.fn(),
9
+ }));
10
+ const { upsertAllVariables } = vi.hoisted(() => ({
11
+ upsertAllVariables: vi.fn(),
12
+ }));
13
+ const { isGhAuthenticated } = vi.hoisted(() => ({
14
+ isGhAuthenticated: vi.fn(),
15
+ }));
16
+ const { collectSecretTargets, getConfiguredGithubRepo, pushSecretsToGithub } =
17
+ vi.hoisted(() => ({
18
+ collectSecretTargets: vi.fn(),
19
+ getConfiguredGithubRepo: vi.fn(),
20
+ pushSecretsToGithub: vi.fn(),
21
+ }));
22
+
23
+ vi.mock("../../config/getProjectConfig", () => ({
24
+ getProjectConfig,
25
+ getEnvironment,
26
+ }));
27
+ vi.mock("../../utils/gitlab", () => ({ upsertAllVariables }));
28
+ vi.mock("../../utils/github", () => ({ isGhAuthenticated }));
29
+ vi.mock("../github", () => ({
30
+ collectSecretTargets,
31
+ getConfiguredGithubRepo,
32
+ pushSecretsToGithub,
33
+ }));
34
+
35
+ const vaultWriteSecrets = vi.fn();
36
+ const io = {
37
+ log: vi.fn(),
38
+ getVaultManager: async () => ({ writeSecrets: vaultWriteSecrets }),
39
+ } as unknown as IO;
40
+
41
+ const config = (partial: Partial<Config>): Config =>
42
+ ({
43
+ customerName: "pan",
44
+ appName: "app",
45
+ components: {},
46
+ ...partial,
47
+ }) as Config;
48
+
49
+ const writes = [
50
+ { env: "dev", componentName: "www", secrets: { API_KEY: "sesame" } },
51
+ ];
52
+
53
+ beforeEach(() => {
54
+ vi.clearAllMocks();
55
+ getEnvironment.mockResolvedValue({
56
+ secretEnvVarKeys: [{ key: "API_KEY" }],
57
+ jobOnlyVars: {
58
+ build: { secretEnvVarKeys: [] },
59
+ deploy: { secretEnvVarKeys: [] },
60
+ },
61
+ });
62
+ isGhAuthenticated.mockResolvedValue(true);
63
+ getConfiguredGithubRepo.mockResolvedValue("panter/app");
64
+ collectSecretTargets.mockResolvedValue(
65
+ new Map([["CL_dev_www_API_KEY", new Set(["dev"])]]),
66
+ );
67
+ });
68
+
69
+ describe("writeSecretsAndMirror", () => {
70
+ it("never touches the gitlab api on a project without a gitlab pipeline", async () => {
71
+ getProjectConfig.mockResolvedValue(
72
+ config({
73
+ pipelines: { github: true },
74
+ secrets: { vault: { type: "bitwarden" } },
75
+ }),
76
+ );
77
+
78
+ await writeSecretsAndMirror(io, writes);
79
+
80
+ expect(vaultWriteSecrets).toHaveBeenCalledWith(
81
+ "dev",
82
+ "www",
83
+ { API_KEY: "sesame" },
84
+ io,
85
+ {},
86
+ );
87
+ expect(upsertAllVariables).not.toHaveBeenCalled();
88
+ expect(pushSecretsToGithub).toHaveBeenCalledWith(
89
+ io,
90
+ "panter/app",
91
+ [{ name: "CL_dev_www_API_KEY", value: "sesame", kind: "secret" }],
92
+ expect.any(Map),
93
+ );
94
+ });
95
+
96
+ it("does not mirror to gitlab when gitlab IS the vault (already written)", async () => {
97
+ getProjectConfig.mockResolvedValue(config({ pipelines: { gitlab: true } }));
98
+
99
+ await writeSecretsAndMirror(io, writes);
100
+
101
+ expect(vaultWriteSecrets).toHaveBeenCalledTimes(1);
102
+ expect(upsertAllVariables).not.toHaveBeenCalled();
103
+ expect(pushSecretsToGithub).not.toHaveBeenCalled();
104
+ });
105
+
106
+ it("mirrors to gitlab when it is only a backend, and forwards the backup option", async () => {
107
+ getProjectConfig.mockResolvedValue(
108
+ config({
109
+ pipelines: { gitlab: true, github: true },
110
+ secrets: { vault: "bitwarden" },
111
+ }),
112
+ );
113
+
114
+ await writeSecretsAndMirror(io, writes, { backup: false });
115
+
116
+ expect(vaultWriteSecrets).toHaveBeenCalledWith(
117
+ "dev",
118
+ "www",
119
+ { API_KEY: "sesame" },
120
+ io,
121
+ { backup: false },
122
+ );
123
+ expect(upsertAllVariables).toHaveBeenCalledWith(
124
+ io,
125
+ { API_KEY: "sesame" },
126
+ "dev",
127
+ "www",
128
+ false,
129
+ );
130
+ expect(pushSecretsToGithub).toHaveBeenCalled();
131
+ });
132
+
133
+ it("keeps the secrets in the vault when the github mirror cannot run", async () => {
134
+ getProjectConfig.mockResolvedValue(
135
+ config({
136
+ pipelines: { github: true },
137
+ secrets: { vault: "bitwarden" },
138
+ }),
139
+ );
140
+ isGhAuthenticated.mockResolvedValue(false);
141
+
142
+ await writeSecretsAndMirror(io, writes);
143
+
144
+ expect(vaultWriteSecrets).toHaveBeenCalledTimes(1);
145
+ expect(pushSecretsToGithub).not.toHaveBeenCalled();
146
+ });
147
+ });
@@ -0,0 +1,157 @@
1
+ import type { Config } from "@catladder/pipeline";
2
+ import {
3
+ getPipelineGitRemote,
4
+ getSecretVarName,
5
+ GithubBackend,
6
+ } from "@catladder/pipeline";
7
+ import {
8
+ getAllComponentsWithAllEnvsHierarchical,
9
+ getEnvironment,
10
+ getProjectConfig,
11
+ } from "../config/getProjectConfig";
12
+ import type { IO } from "../core/types";
13
+ import {
14
+ ensureGithubEnvironment,
15
+ getGithubRepoFromRemote,
16
+ setGithubSecret,
17
+ setGithubVariable,
18
+ } from "../utils/github";
19
+
20
+ /**
21
+ * all secrets declared in the config, with their values read from the
22
+ * vault (github secrets are write-only and can never act as a source)
23
+ */
24
+ export const collectSecretsFromVault = async (io: IO, envFilter?: string[]) => {
25
+ const envAndComponents = await getAllComponentsWithAllEnvsHierarchical();
26
+
27
+ const kinds = new Map<string, "secret" | "variable">();
28
+ for (const [componentName, envs] of Object.entries(envAndComponents)) {
29
+ for (const env of envs) {
30
+ if (envFilter && !envFilter.includes(env)) {
31
+ continue;
32
+ }
33
+ const { secretEnvVarKeys, jobOnlyVars } = await getEnvironment(
34
+ env,
35
+ componentName,
36
+ );
37
+ const keys = [
38
+ ...secretEnvVarKeys,
39
+ ...jobOnlyVars.build.secretEnvVarKeys,
40
+ ...jobOnlyVars.deploy.secretEnvVarKeys,
41
+ ];
42
+ keys.forEach(({ key, kind }) =>
43
+ kinds.set(getSecretVarName(env, componentName, key), kind ?? "secret"),
44
+ );
45
+ }
46
+ }
47
+
48
+ const names = [...kinds.keys()];
49
+ const values = await (await io.getVaultManager()).readSecrets(names, io);
50
+ const secrets = names
51
+ .filter((name) => values[name] !== undefined)
52
+ .map((name) => ({
53
+ name,
54
+ value: values[name],
55
+ kind: kinds.get(name) ?? ("secret" as const),
56
+ }));
57
+ const missing = names.filter((name) => values[name] === undefined);
58
+ return { secrets, missing };
59
+ };
60
+
61
+ /** marker for repo-level (environment-less) targets */
62
+ const REPO_LEVEL = "";
63
+
64
+ /**
65
+ * derives which github environment(s) each secret/variable must live in
66
+ * by scanning the GENERATED workflows: a `${{ secrets.X }}` / `${{
67
+ * vars.X }}` reference in a job's env targets that job's environment
68
+ * (or the repo level when the job has none). Deriving from the real
69
+ * output means the sync can never drift from what the workflows expect.
70
+ *
71
+ * Environment secrets are the answer to github's cap of 100 secrets
72
+ * per REPO (a multi-env project easily exceeds it): the cap applies
73
+ * per environment instead.
74
+ */
75
+ export const collectSecretTargets = async (
76
+ config: Config,
77
+ ): Promise<Map<string, Set<string>>> => {
78
+ const workflows = await new GithubBackend().createWorkflows(config);
79
+ const targets = new Map<string, Set<string>>();
80
+ for (const workflow of Object.values(workflows)) {
81
+ for (const job of Object.values(workflow.jobs)) {
82
+ const environment =
83
+ typeof job.environment === "object"
84
+ ? job.environment.name
85
+ : job.environment;
86
+ for (const value of Object.values(job.env ?? {})) {
87
+ if (typeof value !== "string") continue;
88
+ for (const match of value.matchAll(
89
+ /\$\{\{\s*(?:secrets|vars)\.([A-Za-z0-9_]+)\s*\}\}/g,
90
+ )) {
91
+ const name = match[1];
92
+ if (!targets.has(name)) {
93
+ targets.set(name, new Set());
94
+ }
95
+ targets.get(name)!.add(environment ?? REPO_LEVEL);
96
+ }
97
+ }
98
+ }
99
+ }
100
+ return targets;
101
+ };
102
+
103
+ /**
104
+ * pushes vault values to github via the gh cli: sensitive ones as
105
+ * secrets (masked), "variable"-kind ones as variables (plain) — each
106
+ * into the environment(s) whose jobs reference them
107
+ */
108
+ export const pushSecretsToGithub = async (
109
+ io: IO,
110
+ repo: string,
111
+ secrets: { name: string; value: string; kind: "secret" | "variable" }[],
112
+ targets: Map<string, Set<string>>,
113
+ ) => {
114
+ const environments = [
115
+ ...new Set(
116
+ [...targets.values()].flatMap((envs) =>
117
+ [...envs].filter((env) => env !== REPO_LEVEL),
118
+ ),
119
+ ),
120
+ ];
121
+ for (const environment of environments) {
122
+ await ensureGithubEnvironment(repo, environment);
123
+ io.log(`🌍 environment ${environment}`);
124
+ }
125
+
126
+ for (const { name, value, kind } of secrets) {
127
+ const envs = targets.get(name);
128
+ if (!envs) {
129
+ io.log(`⏭️ ${name} (not referenced by any generated workflow)`);
130
+ continue;
131
+ }
132
+ for (const environment of envs) {
133
+ const suffix = environment === REPO_LEVEL ? "" : ` → ${environment}`;
134
+ if (kind === "variable") {
135
+ await setGithubVariable(repo, name, value, environment || undefined);
136
+ io.log(`📖 ${name} (variable)${suffix}`);
137
+ } else {
138
+ await setGithubSecret(repo, name, value, environment || undefined);
139
+ io.log(`✅ ${name}${suffix}`);
140
+ }
141
+ }
142
+ }
143
+ };
144
+
145
+ /**
146
+ * resolves the github repository from the configured git remote
147
+ * (`pipelines.github.gitRemote`, defaults to origin)
148
+ */
149
+ export const getConfiguredGithubRepo = async (): Promise<
150
+ string | undefined
151
+ > => {
152
+ const config = await getProjectConfig();
153
+ if (!config) {
154
+ return undefined;
155
+ }
156
+ return getGithubRepoFromRemote(getPipelineGitRemote(config, "github"));
157
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./github";
2
+ export * from "./writeSecrets";
@@ -0,0 +1,138 @@
1
+ import type { Config } from "@catladder/pipeline";
2
+ import {
3
+ getEnabledPipelineTypes,
4
+ getSecretVarName,
5
+ getVaultConfig,
6
+ } from "@catladder/pipeline";
7
+ import { getEnvironment, getProjectConfig } from "../config/getProjectConfig";
8
+ import type { IO } from "../core/types";
9
+ import { isGhAuthenticated } from "../utils/github";
10
+ import { upsertAllVariables } from "../utils/gitlab";
11
+ import type { WriteSecretsOptions } from "../vault/types";
12
+ import {
13
+ collectSecretTargets,
14
+ getConfiguredGithubRepo,
15
+ pushSecretsToGithub,
16
+ } from "./github";
17
+
18
+ /** secrets of one env of one component, ready to be written */
19
+ export type SecretsWrite = {
20
+ env: string;
21
+ componentName: string;
22
+ secrets: Record<string, unknown>;
23
+ };
24
+
25
+ /**
26
+ * the one door for writing secret values: they go into the vault (the
27
+ * source of truth) and are mirrored to every enabled CI backend —
28
+ * which vault and which backends exist is invisible to the caller.
29
+ *
30
+ * Everything that produces a secret value must come through here: the
31
+ * secrets commands, but also the credentials `project setup`
32
+ * provisions (gcloud service account keys, kubernetes tokens).
33
+ */
34
+ export const writeSecretsAndMirror = async (
35
+ io: IO,
36
+ writes: SecretsWrite[],
37
+ options: WriteSecretsOptions = {},
38
+ ): Promise<void> => {
39
+ const config = await getProjectConfig();
40
+ if (!config) {
41
+ throw new Error("no catladder config found");
42
+ }
43
+
44
+ io.log("");
45
+ io.log("writing secrets to the vault, please wait...");
46
+ io.log("");
47
+ for (const { env, componentName, secrets } of writes) {
48
+ io.log("writing " + env + ":" + componentName + "...\n");
49
+ await (
50
+ await io.getVaultManager()
51
+ ).writeSecrets(env, componentName, secrets, io, options);
52
+ io.log("");
53
+ io.log("✅ " + env + ":" + componentName);
54
+ io.log("--------------------------------\n");
55
+ }
56
+
57
+ await mirrorSecretsToCiBackends(io, config, writes, options);
58
+ };
59
+
60
+ /**
61
+ * the vault is the source of truth; every enabled CI backend receives
62
+ * a mirrored copy of the written secrets (gitlab variables, github
63
+ * secrets), so all CI systems stay in sync
64
+ */
65
+ const mirrorSecretsToCiBackends = async (
66
+ io: IO,
67
+ config: Config,
68
+ writes: SecretsWrite[],
69
+ options: WriteSecretsOptions,
70
+ ) => {
71
+ const enabled = getEnabledPipelineTypes(config);
72
+
73
+ // gitlab mirror (unless gitlab is the vault itself, then it is
74
+ // already written)
75
+ if (getVaultConfig(config).type !== "gitlab" && enabled.includes("gitlab")) {
76
+ for (const { env, componentName, secrets } of writes) {
77
+ await upsertAllVariables(
78
+ io,
79
+ secrets,
80
+ env,
81
+ componentName,
82
+ options.backup ?? true,
83
+ );
84
+ }
85
+ io.log("✅ mirrored to gitlab");
86
+ }
87
+
88
+ if (!enabled.includes("github")) {
89
+ return;
90
+ }
91
+ const kinds = new Map<string, "secret" | "variable">();
92
+ for (const { env, componentName } of writes) {
93
+ const { secretEnvVarKeys, jobOnlyVars } = await getEnvironment(
94
+ env,
95
+ componentName,
96
+ );
97
+ [
98
+ ...secretEnvVarKeys,
99
+ ...jobOnlyVars.build.secretEnvVarKeys,
100
+ ...jobOnlyVars.deploy.secretEnvVarKeys,
101
+ ].forEach(({ key, kind }) =>
102
+ kinds.set(getSecretVarName(env, componentName, key), kind ?? "secret"),
103
+ );
104
+ }
105
+ const secrets = writes.flatMap(({ env, componentName, secrets }) =>
106
+ Object.entries(secrets).map(([key, value]) => {
107
+ const name = getSecretVarName(env, componentName, key);
108
+ return {
109
+ name,
110
+ value: typeof value === "string" ? value : JSON.stringify(value ?? ""),
111
+ kind: kinds.get(name) ?? ("secret" as const),
112
+ };
113
+ }),
114
+ );
115
+ if (secrets.length === 0) {
116
+ return;
117
+ }
118
+ if (!(await isGhAuthenticated())) {
119
+ io.log(
120
+ "⚠️ github pipeline is enabled, but the github cli (gh) is not authenticated — run `project secrets-sync-github` later to mirror the secrets",
121
+ );
122
+ return;
123
+ }
124
+ const repo = await getConfiguredGithubRepo();
125
+ if (!repo) {
126
+ io.log(
127
+ "⚠️ github pipeline is enabled, but no github repository was found — run `project secrets-sync-github` later to mirror the secrets",
128
+ );
129
+ return;
130
+ }
131
+ io.log(`mirroring ${secrets.length} secrets to github ('${repo}')...`);
132
+ await pushSecretsToGithub(
133
+ io,
134
+ repo,
135
+ secrets,
136
+ await collectSecretTargets(config),
137
+ );
138
+ };
@@ -1,5 +1,6 @@
1
1
  import { execFile as execFileCb, spawn } from "child_process";
2
2
  import { promisify } from "util";
3
+ import { parseGithubRepoFromRemoteUrl } from "@catladder/pipeline";
3
4
 
4
5
  const execFile = promisify(execFileCb);
5
6
 
@@ -25,10 +26,7 @@ export const getGithubRepoFromRemote = async (
25
26
  ): Promise<string | undefined> => {
26
27
  try {
27
28
  const { stdout } = await execFile("git", ["remote", "get-url", remoteName]);
28
- const match = stdout.match(
29
- /github\.com[:/]([\w.-]+\/[\w.-]+?)(\.git)?\s*$/,
30
- );
31
- return match?.[1];
29
+ return parseGithubRepoFromRemoteUrl(stdout);
32
30
  } catch {
33
31
  return undefined;
34
32
  }