How to pass masked secrets between steps and jobs in Github Actions #25225
|
I have below syntax. Since custom action has no access to secrets\env or other context of parent workflow, I have to pass this secret as input. However, this secret is produced in previous step. I need to pass this secret from step1 to step2, I made it via
|
Replies: 11 comments 24 replies
|
There is a workflow command for masking customs strings. Does using that together with the step output solve your problem? |
|
yes. masking worked. My code looks like this
|
|
Nice, and thank you for sharing your solution! 🙂 |
|
does any one know how to share masked secrets between jobs i see that masked values cannot be outputed so how to handle this situation |
|
We have the same issue - want to use azure/login action in a reusable workflow, but cannot pass the secrets from the parent workflow, or access the ones in the repo settings from within the reusable workflow. |
|
hii for now we got one work around this is what was earlier the change is highlighted in bold letters |
|
Found out a hack to make this happen. Consists on temporarily "obfuscating" the secret to the eyes of Github. In the job where I retrieve the secret I encode it and export it to GITHUB_OUTPUT: In the job where I need the secret I decode it (and use successfully where needed): |
|
Here's the github documention for it for how to do it between jobs/workflows securely: |
BACKGROUND CONTEXTAlthough there are now docs on masking and passing secrets between jobs or workflows (as @hawkeyetwolf linked), it didn't meet requirements like:
PROBLEM STATEMENTSWith these factors in mind, the main blockers are:
PROPOSED SOLUTION
WORKING EXAMPLEThese methods are sourced from TF-via-PR repository, which hosts a reusable workflow to run Terraform commands via PR comments, like a CLI. As a bonus, any number of secrets can be securely passed into the reusable workflow to be used as environment variables, for example. The repository also contains recent GitHub Actions workflow runs to verify that the secrets remain masked throughout. # caller-workflow.yml
jobs:
credentials:
runs-on: ubuntu-latest
outputs:
CREDENTIAL1: ${{ steps.credentials.outputs.CREDENTIAL1 }}
CREDENTIAL2: ${{ steps.credentials.outputs.CREDENTIAL2 }}
steps:
- name: Output encoded credentials
id: credentials
env:
CREDENTIAL1: ${{ secrets.CREDENTIAL1 }}
CREDENTIAL2: ${{ secrets.CREDENTIAL2 }}
run: |
echo "CREDENTIAL1=$(echo $CREDENTIAL1 | base64 -w0 | base64 -w0)" >> $GITHUB_OUTPUT
echo "CREDENTIAL2=$(echo $CREDENTIAL2 | base64 -w0 | base64 -w0)" >> $GITHUB_OUTPUT
reusable-workflow:
needs: credentials
uses: reusable-workflow.yml
secrets:
env_vars: |
CREDENTIAL1=${{ needs.credentials.outputs.CREDENTIAL1 }}
CREDENTIAL2=${{ needs.credentials.outputs.CREDENTIAL2 }}# reusable-workflow.yml
on:
workflow_call:
secrets:
env_vars:
required: true
jobs:
parse-credentials:
runs-on: ubuntu-latest
env:
env_vars: ${{ secrets.env_vars }}
steps:
- name: Decode credentials as environment variables
run: |
for i in $env_vars; do
i=$(echo $i | sed 's/=.*//g')=$(echo ${i#*=} | base64 -di | base64 -di)
echo ::add-mask::${i#*=}
printf '%s\n' "$i" >> $GITHUB_ENV
done
- name: Validate credentials
run: |
# Secrets are now available as masked environment variable.
echo $CREDENTIAL1 # or ${{ env.CREDENTIAL1 }}
echo $CREDENTIAL2 # or ${{ env.CREDENTIAL2 }}Where:
FLAWS
|
|
Here is an example of an action that handles encrypting and decrypting a generated secret, passed securely between jobs in encrypted cache. The steps are:
One day I'd like to publish an action that handles the encryption/decryption of a generated secret in a more generic way. But for now, feel free to copy from my Terminus use-case linked above :D |
|
Can we store a symmetric encryption key in Github action secrets, encrypt the secret -> Send it as output -> Decrypt it using the same key in the receiving job? |
There is a workflow command for masking customs strings. Does using that together with the step output solve your problem?