Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate action to Typescript #36

Merged
merged 47 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
94471ca
Make function for executing script
edif2008 Apr 10, 2023
c7236d5
Migrate auth validation
edif2008 Apr 10, 2023
6c9a28c
Migrate load secret functionality
edif2008 Apr 10, 2023
c8ce875
Fix CLI installation process
edif2008 Apr 10, 2023
1cf5241
Fix conditional of appending protocol
edif2008 Apr 10, 2023
98fac4e
Improve code
edif2008 Apr 10, 2023
4d1bf78
Update CLI version and improve script
edif2008 Apr 10, 2023
ade7b48
Use core.addPath
edif2008 Apr 10, 2023
cdeff7c
Use version from package.json
edif2008 Apr 10, 2023
01b4970
Update dependencies
edif2008 Apr 10, 2023
e25f48a
Upgrade to Typescript 5
edif2008 Apr 11, 2023
c625e2e
Merge branch 'main' into eddy/migrate-action-ts
edif2008 Apr 11, 2023
e6d1e6d
Prettify test.yml
edif2008 Apr 11, 2023
e57ec50
Move constants to constants.ts
edif2008 Apr 11, 2023
06d0dba
Move 'validateAuth' to 'utils.ts'
edif2008 Apr 11, 2023
6e0c1c6
Add validate auth tests
edif2008 Apr 11, 2023
208d260
Extract functionality for extracting a secret
edif2008 Apr 11, 2023
33c5cc6
Add tests for extracting secret
edif2008 Apr 11, 2023
aeb83b5
Move 'unsetPrevious' to 'utils.ts'
edif2008 Apr 11, 2023
5d5973b
Add unit test pipeline
edif2008 Apr 11, 2023
1db0fc1
Add tests for 'unsetPrevious'
edif2008 Apr 12, 2023
d16183d
Improve disabling eslint rules
edif2008 May 22, 2023
6ecd5ce
Improve code based on PR review feedback
edif2008 May 22, 2023
17a2a72
Improve CLI installation functionality
edif2008 May 22, 2023
7c957c5
Simplify extractSecret functionality
edif2008 May 22, 2023
1b4d231
Update dist/index.js
edif2008 May 22, 2023
1cbdee3
Fix CLI version
edif2008 May 22, 2023
e46d4d6
Update packages
edif2008 May 24, 2023
ad89fe7
Move loadSecrets function to utils.ts
edif2008 May 25, 2023
3963458
Improve code
edif2008 May 25, 2023
1dd370e
Simplify code related to mocking
edif2008 May 25, 2023
57ddba0
Update packages
edif2008 Jul 4, 2023
35650f8
Use semverToInt from op-js
edif2008 Jul 4, 2023
1b79bb1
Imporve CLI installation script
edif2008 Jul 4, 2023
3daee42
Change from debug messages to info
edif2008 Jul 6, 2023
8c1801e
use toHaveBeenCalled consistently in tests
edif2008 Jul 6, 2023
27cd56a
Add warning if both configs are provided
edif2008 Jul 6, 2023
2c980ca
Add comment about cli validation process
edif2008 Jul 6, 2023
f34a5d1
Merge `main` into `eddy/migrate-action-ts`
edif2008 Jul 6, 2023
22a24ec
Build index.js
edif2008 Jul 6, 2023
37781db
Merge branch 'main' into eddy/migrate-action-ts
edif2008 Jul 6, 2023
433acb3
Update packages
edif2008 Jul 6, 2023
6fea62b
test: assertions for loadSecrets function
dustin-ruetz Jul 6, 2023
62959c4
Improve loadSecrets function
edif2008 Jul 7, 2023
190efe7
Merge branch 'main' into eddy/migrate-action-ts
edif2008 Feb 19, 2024
54bdc9a
Update dependencies
edif2008 Feb 19, 2024
b6d91a5
Upgrade action to use Node20
edif2008 Feb 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@ on: push
name: Run acceptance tests

jobs:
unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
edif2008 marked this conversation as resolved.
Show resolved Hide resolved
- run: npm ci
- run: npm test

test-with-output-secrets:
strategy:
matrix:
os: [ ubuntu-latest, macos-latest ]
auth: [ connect, service-account ]
os: [ubuntu-latest, macos-latest]
auth: [connect, service-account]
exclude:
- os: macos-latest
auth: connect
Expand Down Expand Up @@ -49,8 +59,8 @@ jobs:
test-with-export-env:
strategy:
matrix:
os: [ ubuntu-latest, macos-latest ]
auth: [ connect, service-account ]
os: [ubuntu-latest, macos-latest]
auth: [connect, service-account]
exclude:
- os: macos-latest
auth: connect
Expand Down Expand Up @@ -93,8 +103,8 @@ jobs:
test-references-with-ids:
strategy:
matrix:
os: [ ubuntu-latest, macos-latest ]
auth: [ connect, service-account ]
os: [ubuntu-latest, macos-latest]
auth: [connect, service-account]
exclude:
- os: macos-latest
auth: connect
Expand Down
8 changes: 7 additions & 1 deletion config/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ const jestConfig = {
testEnvironment: "node",
testRegex: "(/__tests__/.*|(\\.|/)test)\\.ts",
transform: {
".ts": ["ts-jest"],
".ts": [
"ts-jest",
{
dustin-ruetz marked this conversation as resolved.
Show resolved Hide resolved
isolatedModules: true,
useESM: true,
},
],
},
verbose: true,
};
Expand Down
125 changes: 118 additions & 7 deletions dist/index.js

Large diffs are not rendered by default.

121 changes: 3 additions & 118 deletions entrypoint.sh
dustin-ruetz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,56 +1,20 @@
#!/bin/bash
# shellcheck disable=SC2046,SC2001,SC2086
set -e

# Pass User-Agent Inforomation to the 1Password CLI
export OP_INTEGRATION_NAME="1Password GitHub Action"
export OP_INTEGRATION_ID="GHA"
export OP_INTEGRATION_BUILDNUMBER="1010001"

readonly CONNECT="CONNECT"
readonly SERVICE_ACCOUNT="SERVICE_ACCOUNT"

auth_type=$CONNECT
managed_variables_var="OP_MANAGED_VARIABLES"
IFS=','

if [[ "$OP_CONNECT_HOST" != "http://"* ]] && [[ "$OP_CONNECT_HOST" != "https://"* ]]; then
export OP_CONNECT_HOST="http://"$OP_CONNECT_HOST
fi

# Unset all secrets managed by 1Password if `unset-previous` is set.
unset_prev_secrets() {
if [ "$INPUT_UNSET_PREVIOUS" == "true" ]; then
echo "Unsetting previous values..."

# Find environment variables that are managed by 1Password.
for env_var in "${managed_variables[@]}"; do
echo "Unsetting $env_var"
unset $env_var

echo "$env_var=" >> $GITHUB_ENV

# Keep the masks, just in case.
done

managed_variables=()
fi
}

# Install op-cli
install_op_cli() {
CLI_VERSION="v2.16.0-beta.01"
dustin-ruetz marked this conversation as resolved.
Show resolved Hide resolved
OP_INSTALL_DIR="$(mktemp -d)"
if [[ ! -d "$OP_INSTALL_DIR" ]]; then
echo "Install dir $OP_INSTALL_DIR not found"
exit 1
fi
export OP_INSTALL_DIR
echo "::debug::OP_INSTALL_DIR: ${OP_INSTALL_DIR}"
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
curl -sSfLo op.zip "https://cache.agilebits.com/dist/1P/op2/pkg/v2.10.0-beta.02/op_linux_amd64_v2.10.0-beta.02.zip"
curl -sSfLo op.zip "https://cache.agilebits.com/dist/1P/op2/pkg/$CLI_VERSION/op_linux_amd64_$CLI_VERSION.zip"
unzip -od "$OP_INSTALL_DIR" op.zip && rm op.zip
elif [[ "$OSTYPE" == "darwin"* ]]; then
curl -sSfLo op.pkg "https://cache.agilebits.com/dist/1P/op2/pkg/v2.10.0-beta.02/op_apple_universal_v2.10.0-beta.02.pkg"
curl -sSfLo op.pkg "https://cache.agilebits.com/dist/1P/op2/pkg/$CLI_VERSION/op_apple_universal_$CLI_VERSION.pkg"
pkgutil --expand op.pkg temp-pkg
tar -xvf temp-pkg/op.pkg/Payload -C "$OP_INSTALL_DIR"
rm -rf temp-pkg && rm op.pkg
Expand All @@ -64,83 +28,4 @@ uninstall_op_cli() {
fi
}
edif2008 marked this conversation as resolved.
Show resolved Hide resolved

populating_secret() {
ref=$(printenv $1)

echo "Populating variable: $1"
secret_value=$("${OP_INSTALL_DIR}/op" read "$ref")

if [ -z "$secret_value" ]; then
echo "Could not find or access secret $ref"
exit 1
fi

# Register a mask for the secret to prevent accidental log exposure.
# To support multiline secrets, escape percent signs and add a mask per line.
escaped_mask_value=$(echo "$secret_value" | sed -e 's/%/%25/g')
IFS=$'\n'
for line in $escaped_mask_value; do
if [ "${#line}" -lt 3 ]; then
# To avoid false positives and unreadable logs, omit mask for lines that are too short.
continue
fi
echo "::add-mask::$line"
done
unset IFS

if [ "$INPUT_EXPORT_ENV" == "true" ]; then
# To support multiline secrets, we'll use the heredoc syntax to populate the environment variables.
# As the heredoc identifier, we'll use a randomly generated 64-character string,
# so that collisions are practically impossible.
random_heredoc_identifier=$(openssl rand -hex 32)

{
# Populate env var, using heredoc syntax with generated identifier
echo "$env_var<<${random_heredoc_identifier}"
echo "$secret_value"
echo "${random_heredoc_identifier}"
} >> $GITHUB_ENV
echo "GITHUB_ENV: $(cat $GITHUB_ENV)"

else
# Prepare the secret_value to be outputed properly (especially multiline secrets)
secret_value=$(echo "$secret_value" | awk -v ORS='%0A' '1')

echo "::set-output name=$env_var::$secret_value"
fi

managed_variables+=("$env_var")
}

# Load environment variables using op cli. Iterate over them to find 1Password references, load the secret values,
# and make them available as environment variables in the next steps.
extract_secrets() {
IFS=$'\n'
for env_var in $("${OP_INSTALL_DIR}/op" env ls); do
populating_secret $env_var
done
}

read -r -a managed_variables <<< "$(printenv $managed_variables_var)"

if [ -z "$OP_CONNECT_TOKEN" ] || [ -z "$OP_CONNECT_HOST" ]; then
if [ -z "$OP_SERVICE_ACCOUNT_TOKEN" ]; then
echo "(\$OP_CONNECT_TOKEN and \$OP_CONNECT_HOST) or \$OP_SERVICE_ACCOUNT_TOKEN must be set"
exit 1
fi

auth_type=$SERVICE_ACCOUNT
fi

printf "Authenticated with %s \n" $auth_type

unset_prev_secrets
install_op_cli
dustin-ruetz marked this conversation as resolved.
Show resolved Hide resolved
extract_secrets
uninstall_op_cli

unset IFS
# Add extra env var that lists which secrets are managed by 1Password so that in a later step
# these can be unset again.
managed_variables_str=$(IFS=','; echo "${managed_variables[*]}")
echo "$managed_variables_var=$managed_variables_str" >> $GITHUB_ENV
Loading