Skip to content

Commit 564bf5b

Browse files
Merge pull request #118 from wcarlsen/main
feature: enable loading 1password secrets from file
2 parents 2c12b97 + 0ff92dd commit 564bf5b

File tree

9 files changed

+50
-7
lines changed

9 files changed

+50
-7
lines changed

.github/workflows/acceptance-test.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ jobs:
3636
if: |
3737
github.event_name != 'repository_dispatch' &&
3838
(
39-
github.ref == 'refs/heads/main' ||
39+
github.ref == 'refs/heads/main' ||
4040
(
41-
github.event_name == 'pull_request' &&
41+
github.event_name == 'pull_request' &&
4242
github.event.pull_request.head.repo.full_name == github.repository
4343
)
4444
)
@@ -96,12 +96,14 @@ jobs:
9696
SECRET: ${{ inputs.secret }}
9797
SECRET_IN_SECTION: ${{ inputs.secret-in-section }}
9898
MULTILINE_SECRET: ${{ inputs.multiline-secret }}
99+
OP_ENV_FILE: ./tests/.env.tpl
99100
- name: Assert test secret values [step output]
100101
if: ${{ !inputs.export-env }}
101102
env:
102103
SECRET: ${{ steps.load_secrets.outputs.SECRET }}
103104
SECRET_IN_SECTION: ${{ steps.load_secrets.outputs.SECRET_IN_SECTION }}
104105
MULTILINE_SECRET: ${{ steps.load_secrets.outputs.MULTILINE_SECRET }}
106+
OP_ENV_FILE: ./tests/.env.tpl
105107
run: ./tests/assert-env-set.sh
106108
- name: Assert test secret values [exported env]
107109
if: ${{ inputs.export-env }}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
env:
4040
OP_SERVICE_ACCOUNT_TOKEN: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
4141
SECRET: op://app-cicd/hello-world/secret
42+
OP_ENV_FILE: "./path/to/.env.tpl" # see tests/.env.tpl for example
4243

4344
- name: Print masked secret
4445
run: 'echo "Secret: ${{ steps.load_secrets.outputs.SECRET }}"'
@@ -63,6 +64,7 @@ jobs:
6364
env:
6465
OP_SERVICE_ACCOUNT_TOKEN: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
6566
SECRET: op://app-cicd/hello-world/secret
67+
OP_ENV_FILE: "./path/to/.env.tpl" # see tests/.env.tpl for example
6668

6769
- name: Print masked secret
6870
run: 'echo "Secret: $SECRET"'

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@1password/op-js": "^0.1.11",
4444
"@actions/core": "^1.10.1",
4545
"@actions/exec": "^1.1.1",
46+
"dotenv": "^17.2.2",
4647
"op-cli-installer": "github:1Password/op-cli-installer#e6c1c758bc3339e5fe9b06255728039f688f73fa"
4748
},
4849
"devDependencies": {

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export const envConnectHost = "OP_CONNECT_HOST";
22
export const envConnectToken = "OP_CONNECT_TOKEN";
33
export const envServiceAccountToken = "OP_SERVICE_ACCOUNT_TOKEN";
44
export const envManagedVariables = "OP_MANAGED_VARIABLES";
5+
export const envFilePath = "OP_ENV_FILE";
56

67
export const authErr = `Authentication error with environment variables: you must set either 1) ${envServiceAccountToken}, or 2) both ${envConnectHost} and ${envConnectToken}.`;

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import * as core from "@actions/core";
22
import { validateCli } from "@1password/op-js";
33
import { installCliOnGithubActionRunner } from "op-cli-installer";
4+
import dotenv from "dotenv";
45
import { loadSecrets, unsetPrevious, validateAuth } from "./utils";
6+
import { envFilePath } from "./constants";
57

68
const loadSecretsAction = async () => {
79
try {
@@ -17,6 +19,13 @@ const loadSecretsAction = async () => {
1719
// Validate that a proper authentication configuration is set for the CLI
1820
validateAuth();
1921

22+
// Set environment variables from OP_ENV_FILE
23+
const file = process.env[envFilePath];
24+
if (file) {
25+
core.info(`Loading environment variables from file: ${file}`);
26+
dotenv.config({ path: file });
27+
}
28+
2029
// Download and install the CLI
2130
await installCLI();
2231

tests/.env.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FILE_SECRET=op://acceptance-tests/test-secret/password
2+
FILE_SECRET_IN_SECTION=op://acceptance-tests/test-secret/test-section/password
3+
FILE_MULTILINE_SECRET=op://acceptance-tests/multiline-secret/notesPlain

tests/assert-env-set.sh

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ assert_env_equals() {
99
fi
1010
}
1111

12-
assert_env_equals "SECRET" "RGVhciBzZWN1cml0eSByZXNlYXJjaGVyLCB0aGlzIGlzIGp1c3QgYSBkdW1teSBzZWNyZXQuIFBsZWFzZSBkb24ndCByZXBvcnQgaXQu"
13-
14-
assert_env_equals "SECRET_IN_SECTION" "RGVhciBzZWN1cml0eSByZXNlYXJjaGVyLCB0aGlzIGlzIGp1c3QgYSBkdW1teSBzZWNyZXQuIFBsZWFzZSBkb24ndCByZXBvcnQgaXQu"
15-
16-
assert_env_equals "MULTILINE_SECRET" "$(cat << EOF
12+
readonly SECRET="RGVhciBzZWN1cml0eSByZXNlYXJjaGVyLCB0aGlzIGlzIGp1c3QgYSBkdW1teSBzZWNyZXQuIFBsZWFzZSBkb24ndCByZXBvcnQgaXQu"
13+
MULTILINE_SECRET="$(cat << EOF
1714
-----BEGIN PRIVATE KEY-----
1815
RGVhciBzZWN1cml0eSByZXNlYXJjaGVyLApXaGls
1916
ZSB3ZSBkZWVwbHkgYXBwcmVjaWF0ZSB5b3VyIHZp
@@ -28,3 +25,13 @@ IApTbyBwbGVhc2UgZG9uJ3QgcmVwb3J0IGl0IQo=
2825
-----END PRIVATE KEY-----
2926
EOF
3027
)"
28+
readonly MULTILINE_SECRET
29+
30+
assert_env_equals "SECRET" "${SECRET}"
31+
assert_env_equals "FILE_SECRET" "${SECRET}"
32+
33+
assert_env_equals "SECRET_IN_SECTION" "${SECRET}"
34+
assert_env_equals "FILE_SECRET_IN_SECTION" "${SECRET}"
35+
36+
assert_env_equals "MULTILINE_SECRET" "${MULTILINE_SECRET}"
37+
assert_env_equals "FILE_MULTILINE_SECRET" "${MULTILINE_SECRET}"

tests/assert-env-unset.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@ assert_env_unset() {
1010
}
1111

1212
assert_env_unset "SECRET"
13+
assert_env_unset "FILE_SECRET"
14+
1315
assert_env_unset "SECRET_IN_SECTION"
16+
assert_env_unset "FILE_SECRET_IN_SECTION"
17+
1418
assert_env_unset "MULTILINE_SECRET"
19+
assert_env_unset "FILE_MULTILINE_SECRET"

0 commit comments

Comments
 (0)