|
| 1 | +import { expect } from "chai"; |
| 2 | +import * as sinon from "sinon"; |
| 3 | +import * as env from "./env"; |
| 4 | +import * as promptNS from "../prompt"; |
| 5 | +import * as config from "./config"; |
| 6 | +import * as gcsmNS from "../gcp/secretManager"; |
| 7 | +import * as secretsNS from "./secrets"; |
| 8 | +import * as utilsNS from "../utils"; |
| 9 | +import { Document } from "yaml"; |
| 10 | + |
| 11 | +describe("env", () => { |
| 12 | + let prompt: sinon.SinonStubbedInstance<typeof promptNS>; |
| 13 | + let gcsm: sinon.SinonStubbedInstance<typeof gcsmNS>; |
| 14 | + let secrets: sinon.SinonStubbedInstance<typeof secretsNS>; |
| 15 | + let utils: sinon.SinonStubbedInstance<typeof utilsNS>; |
| 16 | + |
| 17 | + function makeDocument(...envs: config.Env[]): Document { |
| 18 | + const doc = new Document(); |
| 19 | + for (const e of envs) { |
| 20 | + config.upsertEnv(doc, e); |
| 21 | + } |
| 22 | + return doc; |
| 23 | + } |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + prompt = sinon.stub(promptNS); |
| 27 | + gcsm = sinon.stub(gcsmNS); |
| 28 | + secrets = sinon.stub(secretsNS); |
| 29 | + utils = sinon.stub(utilsNS); |
| 30 | + |
| 31 | + utils.logLabeledWarning.resolves(); |
| 32 | + prompt.input.rejects(new Error("Should not be called")); |
| 33 | + gcsm.accessSecretVersion.rejects(new Error("Should not be called")); |
| 34 | + gcsm.addVersion.rejects(new Error("Should not be called")); |
| 35 | + secrets.upsertSecret.rejects(new Error("Should not be called")); |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(() => { |
| 39 | + sinon.verifyAndRestore(); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should diffEnvs", async () => { |
| 43 | + gcsm.accessSecretVersion |
| 44 | + .withArgs("test-project", "matching-secret", "latest") |
| 45 | + .resolves("unchanged"); |
| 46 | + gcsm.accessSecretVersion |
| 47 | + .withArgs("test-project", "changed-secret", "latest") |
| 48 | + .resolves("original-value"); |
| 49 | + gcsm.accessSecretVersion |
| 50 | + .withArgs("test-project", "error-secret", "latest") |
| 51 | + .rejects(new Error("Cannot access secret")); |
| 52 | + |
| 53 | + const existingEnv = makeDocument( |
| 54 | + { variable: "MATCHING_PLAIN", value: "existing" }, |
| 55 | + { variable: "CHANGED_PLAIN", value: "original-value" }, |
| 56 | + { variable: "UNREFERENCED_PLAIN", value: "existing" }, |
| 57 | + |
| 58 | + { variable: "MATCHING_SECRET", secret: "matching-secret" }, |
| 59 | + { variable: "UNREFERENCED_SECRET", secret: "unreferenced-secret" }, |
| 60 | + { variable: "CHANGED_SECRET", secret: "changed-secret" }, |
| 61 | + { variable: "ERROR_SECRET", secret: "error-secret" }, |
| 62 | + ); |
| 63 | + |
| 64 | + const importingEnv = { |
| 65 | + MATCHING_PLAIN: "existing", |
| 66 | + CHANGED_PLAIN: "new-value", |
| 67 | + NEW_PLAIN: "new", |
| 68 | + |
| 69 | + MATCHING_SECRET: "unchanged", |
| 70 | + NEW_SECRET: "new", |
| 71 | + CHANGED_SECRET: "changed-value", |
| 72 | + ERROR_SECRET: "attempted-value", |
| 73 | + }; |
| 74 | + |
| 75 | + await expect(env.diffEnvs("test-project", importingEnv, existingEnv)).to.eventually.deep.equal({ |
| 76 | + newVars: ["NEW_PLAIN", "NEW_SECRET"], |
| 77 | + matched: ["MATCHING_PLAIN", "MATCHING_SECRET"], |
| 78 | + conflicts: ["CHANGED_PLAIN", "CHANGED_SECRET", "ERROR_SECRET"], |
| 79 | + }); |
| 80 | + expect(gcsm.accessSecretVersion).to.have.been.calledThrice; |
| 81 | + expect(utils.logLabeledWarning).to.have.been.calledWith( |
| 82 | + "apphosting", |
| 83 | + "Cannot read value of existing secret error-secret to see if it has changed. Assuming it has changed.", |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + describe("confirmConflicts", () => { |
| 88 | + it("should return an empty array if no conflicts", async () => { |
| 89 | + const result = await env.confirmConflicts([]); |
| 90 | + expect(result).to.be.empty; |
| 91 | + }); |
| 92 | + |
| 93 | + it("should prompt the user to resolve conflicts", async () => { |
| 94 | + prompt.checkbox.resolves(["FOO"]); |
| 95 | + const result = await env.confirmConflicts(["FOO", "BAZ"]); |
| 96 | + expect(result).to.deep.equal(["FOO"]); |
| 97 | + expect(prompt.checkbox).to.have.been.calledOnce; |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe("chooseNewSecrets", () => { |
| 102 | + it("should return an empty array if no vars", async () => { |
| 103 | + const result = await env.chooseNewSecrets([]); |
| 104 | + expect(result).to.be.empty; |
| 105 | + }); |
| 106 | + |
| 107 | + it("should suggest which values to store as secrets", async () => { |
| 108 | + prompt.checkbox.resolves(["MY_KEY"]); |
| 109 | + const result = await env.chooseNewSecrets(["FOO", "BAZ", "MY_KEY", "MY_SECRET"]); |
| 110 | + expect(result).to.deep.equal(["MY_KEY"]); |
| 111 | + expect(prompt.checkbox).to.have.been.calledWithMatch({ |
| 112 | + message: |
| 113 | + "Sensitive data should be stored in Cloud Secrets Manager so that access to its value is protected. Which variables are sensitive?", |
| 114 | + choices: [ |
| 115 | + { value: "FOO", checked: false }, |
| 116 | + { value: "BAZ", checked: false }, |
| 117 | + { value: "MY_KEY", checked: true }, |
| 118 | + { value: "MY_SECRET", checked: true }, |
| 119 | + ], |
| 120 | + }); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe("importEnv", () => { |
| 125 | + // We could break this into multiple tests, but the same code is execrcised in all cases. |
| 126 | + it("should keep existing secrets as secrets, prompt for new vars to be secrets, and store only selected info", async () => { |
| 127 | + const existingEnv = makeDocument( |
| 128 | + { variable: "EXISTING_PLAIN1", value: "existing" }, |
| 129 | + { variable: "EXISTING_PLAIN2", value: "existing" }, |
| 130 | + { variable: "EXISTING_SECRET1", secret: "existing-secret1" }, |
| 131 | + { variable: "EXISTING_SECRET2", secret: "existing-secret2" }, |
| 132 | + ); |
| 133 | + |
| 134 | + const importingEnv = { |
| 135 | + EXISTING_PLAIN1: "new", |
| 136 | + EXISTING_PLAIN2: "new", |
| 137 | + NEW_PLAIN: "new", |
| 138 | + EXISTING_SECRET1: "new", |
| 139 | + EXISTING_SECRET2: "new", |
| 140 | + NEW_SECRET: "new", |
| 141 | + }; |
| 142 | + |
| 143 | + sinon.stub(env, "diffEnvs").resolves({ |
| 144 | + newVars: ["NEW_PLAIN", "NEW_SECRET"], |
| 145 | + conflicts: ["EXISTING_PLAIN1", "EXISTING_PLAIN2", "EXISTING_SECRET1", "EXISTING_SECRET2"], |
| 146 | + matched: [], |
| 147 | + }); |
| 148 | + // Leave #2 alone and verify that they haven't been modified |
| 149 | + sinon.stub(env, "confirmConflicts").resolves(["EXISTING_PLAIN1", "EXISTING_SECRET1"]); |
| 150 | + // Verify that only new variables are offered to be stored as secrets |
| 151 | + sinon.stub(env, "chooseNewSecrets").resolves(["NEW_SECRET"]); |
| 152 | + secrets.upsertSecret.withArgs("test-project", "NEW_SECRET").resolves(); |
| 153 | + gcsm.addVersion.withArgs("test-project", "NEW_SECRET", "new").resolves(); |
| 154 | + gcsm.addVersion.withArgs("test-project", "existing-secret1", "new").resolves(); |
| 155 | + |
| 156 | + const createdSecrets = await env.importEnv("test-project", importingEnv, existingEnv); |
| 157 | + |
| 158 | + // Confirm new variables are not part of the confirm prompt |
| 159 | + expect(env.confirmConflicts).calledWithMatch([ |
| 160 | + "EXISTING_PLAIN1", |
| 161 | + "EXISTING_PLAIN2", |
| 162 | + "EXISTING_SECRET1", |
| 163 | + "EXISTING_SECRET2", |
| 164 | + ]); |
| 165 | + |
| 166 | + // Confirm that variables which already existed are not asked to be stored as secrets |
| 167 | + expect(env.chooseNewSecrets).calledWithMatch(["NEW_PLAIN", "NEW_SECRET"]); |
| 168 | + |
| 169 | + // Confirm that we don't unnecessarily upsert existing secrets |
| 170 | + expect(secrets.upsertSecret).to.have.been.calledOnceWith("test-project", "NEW_SECRET"); |
| 171 | + |
| 172 | + // Confirm that we updated the versions of the secrets we were asked to update |
| 173 | + expect(gcsm.addVersion).to.have.been.calledWith("test-project", "NEW_SECRET", "new"); |
| 174 | + expect(gcsm.addVersion).to.have.been.calledWith("test-project", "existing-secret1", "new"); |
| 175 | + |
| 176 | + // Confirm that we return the list of created secrets for furhter IAM granting |
| 177 | + expect(createdSecrets).to.deep.equal(["NEW_SECRET"]); |
| 178 | + |
| 179 | + // Confirm that the existing env was properly updated |
| 180 | + expect(config.findEnv(existingEnv, "EXISTING_PLAIN1")?.value).to.equal("new"); |
| 181 | + expect(config.findEnv(existingEnv, "EXISTING_PLAIN2")?.value).to.equal("existing"); |
| 182 | + expect(config.findEnv(existingEnv, "NEW_PLAIN")?.value).to.equal("new"); |
| 183 | + expect(config.findEnv(existingEnv, "EXISTING_SECRET1")?.secret).to.equal("existing-secret1"); |
| 184 | + expect(config.findEnv(existingEnv, "EXISTING_SECRET2")?.secret).to.equal("existing-secret2"); |
| 185 | + expect(config.findEnv(existingEnv, "NEW_SECRET")?.secret).to.equal("NEW_SECRET"); |
| 186 | + }); |
| 187 | + }); |
| 188 | +}); |
0 commit comments