Skip to content

Commit 2f91cfd

Browse files
BUILD-9976 Move AWS credentials to named profile
1 parent bf5a044 commit 2f91cfd

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,38 @@ Each environment has its own preconfigured S3 bucket and AWS Cognito pool for is
109109
- No long-lived AWS credentials required
110110
- Branch-specific paths provide isolation between branches
111111

112+
### AWS Credential Isolation
113+
114+
This action creates a dedicated AWS profile (`gh-action-cache-<run_id>`) to store its credentials.
115+
This ensures that cache operations work correctly even when you configure your own AWS credentials later in the workflow.
116+
117+
**Why this matters**: The cache save operation happens in a GitHub Actions post-step (after your job completes).
118+
If you use `aws-actions/configure-aws-credentials` during your job, it would normally override the cache action's credentials,
119+
causing cache save to fail.
120+
121+
**Example workflow that works correctly**:
122+
123+
```yaml
124+
jobs:
125+
build:
126+
steps:
127+
# Cache action authenticates and stores credentials in isolated profile
128+
- uses: SonarSource/gh-action-cache@v1
129+
with:
130+
path: ~/.cache
131+
key: my-cache-${{ hashFiles('**/lockfile') }}
132+
133+
# Your own AWS authentication - does NOT affect cache credentials
134+
- uses: aws-actions/configure-aws-credentials@v4
135+
with:
136+
role-to-assume: arn:aws:iam::123456789:role/my-role
137+
aws-region: us-east-1
138+
139+
- run: aws s3 ls # Uses YOUR credentials
140+
141+
# Post-step: Cache save uses isolated profile - works correctly!
142+
```
143+
112144
### Cleanup Policy
113145

114146
The AWS S3 bucket lifecycle rules apply to delete the old files. The content from default branches expires in 60 days and for feature

action.yml

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ runs:
9494
IDENTITY_PROVIDER_NAME: token.actions.githubusercontent.com
9595
AUDIENCE: cognito-identity.amazonaws.com
9696
AWS_REGION: eu-central-1
97+
GITHUB_RUN_ID: ${{ github.run_id }}
9798
run: |
9899
# Get GitHub Actions ID token using script
99100
ACCESS_TOKEN=$("$GITHUB_ACTION_PATH/scripts/get-github-token.sh")
@@ -137,11 +138,21 @@ runs:
137138
echo "::add-mask::$AWS_SECRET_ACCESS_KEY"
138139
echo "::add-mask::$AWS_SESSION_TOKEN"
139140
140-
{
141-
echo "AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID"
142-
echo "AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY"
143-
echo "AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN"
144-
} >> "$GITHUB_OUTPUT"
141+
# Create a unique AWS profile to isolate credentials from user-configured AWS credentials
142+
# This prevents credential override when users call aws-actions/configure-aws-credentials
143+
# between the cache restore (main step) and cache save (post step)
144+
PROFILE_NAME="gh-action-cache-${GITHUB_RUN_ID}"
145+
146+
mkdir -p ~/.aws
147+
chmod 700 ~/.aws
148+
149+
# Write credentials to a dedicated profile using AWS CLI (handles file format and permissions correctly)
150+
aws configure set aws_access_key_id "$AWS_ACCESS_KEY_ID" --profile "$PROFILE_NAME"
151+
aws configure set aws_secret_access_key "$AWS_SECRET_ACCESS_KEY" --profile "$PROFILE_NAME"
152+
aws configure set aws_session_token "$AWS_SESSION_TOKEN" --profile "$PROFILE_NAME"
153+
aws configure set region eu-central-1 --profile "$PROFILE_NAME"
154+
echo "Created AWS profile: $PROFILE_NAME"
155+
echo "AWS_PROFILE=$PROFILE_NAME" >> "$GITHUB_OUTPUT"
145156
146157
- name: Prepare cache keys
147158
if: steps.cache-backend.outputs.cache-backend == 's3'
@@ -163,9 +174,10 @@ runs:
163174
RUNS_ON_S3_BUCKET_CACHE: sonarsource-s3-cache-${{ inputs.environment }}-bucket
164175
AWS_DEFAULT_REGION: eu-central-1
165176
AWS_REGION: eu-central-1
166-
AWS_ACCESS_KEY_ID: ${{ steps.aws-auth.outputs.AWS_ACCESS_KEY_ID }}
167-
AWS_SECRET_ACCESS_KEY: ${{ steps.aws-auth.outputs.AWS_SECRET_ACCESS_KEY }}
168-
AWS_SESSION_TOKEN: ${{ steps.aws-auth.outputs.AWS_SESSION_TOKEN }}
177+
# Use AWS profile instead of direct credentials to prevent override issues
178+
# When users configure their own AWS credentials mid-job, the profile remains isolated
179+
AWS_PROFILE: ${{ steps.aws-auth.outputs.AWS_PROFILE }}
180+
AWS_DEFAULT_PROFILE: ${{ steps.aws-auth.outputs.AWS_PROFILE }}
169181
with:
170182
path: ${{ inputs.path }}
171183
key: ${{ steps.prepare-keys.outputs.branch-key }}

0 commit comments

Comments
 (0)