-
Notifications
You must be signed in to change notification settings - Fork 492
chore(ci): mirror commercial layer visibility #8422
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
Open
dreamorosi
wants to merge
2
commits into
develop
Choose a base branch
from
ci/mirror-partition-layer-permissions
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−17
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,6 +98,21 @@ jobs: | |
| run: | | ||
| aws --region us-east-1 lambda get-layer-version-by-arn --arn arn:aws:lambda:us-east-1:017000801446:layer:${{ matrix.layer }}-${{ matrix.arch }}:${{ inputs.version }} --query 'Content.Location' | xargs curl -L -o ${{ matrix.layer }}-${{ matrix.arch }}.zip | ||
| aws --region us-east-1 lambda get-layer-version-by-arn --arn arn:aws:lambda:us-east-1:017000801446:layer:${{ matrix.layer }}-${{ matrix.arch }}:${{ inputs.version }} > ${{ matrix.layer }}-${{ matrix.arch }}.json | ||
| - name: Grab Policy | ||
| env: | ||
| LAYER_NAME: ${{ matrix.layer }}-${{ matrix.arch }} | ||
| VERSION: ${{ inputs.version }} | ||
| run: | | ||
| if ! aws --region us-east-1 lambda get-layer-version-policy \ | ||
| --layer-name "arn:aws:lambda:us-east-1:017000801446:layer:${LAYER_NAME}" \ | ||
| --version-number "$VERSION" > "${LAYER_NAME}.policy.json" 2> policy-error.txt; then | ||
| if grep -q ResourceNotFoundException policy-error.txt; then | ||
| echo '{"Policy":null}' > "${LAYER_NAME}.policy.json" | ||
| else | ||
| cat policy-error.txt | ||
| exit 1 | ||
| fi | ||
| fi | ||
| - name: Store Zip | ||
| uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 | ||
| with: | ||
|
|
@@ -112,6 +127,13 @@ jobs: | |
| path: ${{ matrix.layer }}-${{ matrix.arch }}.json | ||
| retention-days: 1 | ||
| if-no-files-found: error | ||
| - name: Store Policy | ||
| uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 | ||
| with: | ||
| name: ${{ matrix.layer }}-${{ matrix.arch }}.policy.json | ||
| path: ${{ matrix.layer }}-${{ matrix.arch }}.policy.json | ||
| retention-days: 1 | ||
| if-no-files-found: error | ||
|
|
||
| copy: | ||
| name: Copy | ||
|
|
@@ -146,10 +168,37 @@ jobs: | |
| uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 | ||
| with: | ||
| name: ${{ matrix.layer }}-${{ matrix.arch }}.json | ||
| - name: Download Policy | ||
| uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 | ||
| with: | ||
| name: ${{ matrix.layer }}-${{ matrix.arch }}.policy.json | ||
| - name: Verify Layer Signature | ||
| run: | | ||
| SHA=$(jq -r '.Content.CodeSha256' '${{ matrix.layer }}-${{ matrix.arch }}.json') | ||
| test "$(openssl dgst -sha256 -binary ${{ matrix.layer }}-${{ matrix.arch }}.zip | openssl enc -base64)" == "$SHA" && echo "SHA OK: ${SHA}" || exit 1 | ||
| - id: source_policy | ||
| name: Source Layer Visibility | ||
| run: | | ||
| if jq -e ' | ||
| (.Policy | if type == "string" then fromjson else . end) | ||
| | any(.Statement[]?; | ||
| .Effect == "Allow" | ||
| and ((.Action // [] | if type == "array" then . else [.] end) | any(. == "*" or . == "lambda:*" or . == "lambda:GetLayerVersion")) | ||
| and (((.Principal | if type == "object" then .AWS // "" else . end) | if type == "array" then . else [.] end) | index("*") != null) | ||
| and ((.Condition // {}) | length == 0) | ||
| ) | ||
| ' '${{ matrix.layer }}-${{ matrix.arch }}.policy.json' > /dev/null; then | ||
| echo 'public=true' >> "$GITHUB_OUTPUT" | ||
| echo 'Commercial source layer is public' | ||
| else | ||
| RESULT=$? | ||
| if (( RESULT > 1 )); then | ||
| echo "Unable to evaluate source layer policy (jq exit ${RESULT})" | ||
| exit 1 | ||
| fi | ||
| echo 'public=false' >> "$GITHUB_OUTPUT" | ||
| echo 'Commercial source layer is private' | ||
| fi | ||
| - id: transform | ||
| run: | | ||
| echo 'CONVERTED_REGION=${{ matrix.region }}' | tr 'a-z\-' 'A-Z_' >> "$GITHUB_OUTPUT" | ||
|
|
@@ -160,8 +209,29 @@ jobs: | |
| aws-region: ${{ matrix.region}} | ||
| mask-aws-account-id: true | ||
| audience: ${{ needs.setup.outputs.aud }} | ||
| - name: Validate target account | ||
| env: | ||
| AWS_ACCOUNT: ${{ secrets[format('AWS_ACCOUNT_{0}', steps.transform.outputs.CONVERTED_REGION)] }} | ||
| PARTITION: ${{ needs.setup.outputs.partition }} | ||
| REGION: ${{ matrix.region }} | ||
| run: | | ||
| if [[ ! "$AWS_ACCOUNT" =~ ^[0-9]{12}$ ]]; then | ||
| echo "AWS account secret for ${REGION} is missing or invalid" | ||
| exit 1 | ||
| fi | ||
|
|
||
| CALLER_ACCOUNT=$(aws --region "$REGION" sts get-caller-identity --query Account --output text) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this could be one call: |
||
| CALLER_ARN=$(aws --region "$REGION" sts get-caller-identity --query Arn --output text) | ||
|
|
||
| if [[ "$CALLER_ACCOUNT" != "$AWS_ACCOUNT" ]] || [[ "$CALLER_ARN" != "arn:${PARTITION}:"* ]]; then | ||
| echo "Assumed role does not match the expected account and partition for ${REGION}" | ||
| exit 1 | ||
| fi | ||
| - name: Create Layer | ||
| id: create-layer | ||
| env: | ||
| SOURCE_IS_PUBLIC: ${{ steps.source_policy.outputs.public }} | ||
| VERSION: ${{ inputs.version }} | ||
| run: | | ||
| jq '{"LayerName": "${{ matrix.layer }}-${{ matrix.arch }}", "Description": .Description, "CompatibleRuntimes": .CompatibleRuntimes, "CompatibleArchitectures": .CompatibleArchitectures, "LicenseInfo": .LicenseInfo} | with_entries(select(.value != null))' '${{ matrix.layer }}-${{ matrix.arch }}.json' > input.json | ||
|
|
||
|
|
@@ -171,17 +241,25 @@ jobs: | |
| --query 'Version' \ | ||
| --output text) | ||
|
|
||
| if (( LAYER_VERSION != VERSION )); then | ||
| echo "Expected to publish as version ${VERSION}, received ${LAYER_VERSION}" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "LAYER_VERSION=$LAYER_VERSION" >> "$GITHUB_OUTPUT" | ||
|
|
||
| aws --region ${{ matrix.region}} lambda add-layer-version-permission \ | ||
| --layer-name ${{ matrix.layer }}-${{ matrix.arch }} \ | ||
| --statement-id 'PublicLayer' \ | ||
| --action lambda:GetLayerVersion \ | ||
| --principal '*' \ | ||
| --version-number "$LAYER_VERSION" | ||
| if [[ "$SOURCE_IS_PUBLIC" == "true" ]]; then | ||
| aws --region ${{ matrix.region}} lambda add-layer-version-permission \ | ||
| --layer-name ${{ matrix.layer }}-${{ matrix.arch }} \ | ||
| --statement-id 'PublicLayer' \ | ||
| --action lambda:GetLayerVersion \ | ||
| --principal '*' \ | ||
| --version-number "$LAYER_VERSION" | ||
| fi | ||
| - name: Verify Layer | ||
| env: | ||
| LAYER_VERSION: ${{ steps.create-layer.outputs.LAYER_VERSION }} | ||
| SOURCE_IS_PUBLIC: ${{ steps.source_policy.outputs.public }} | ||
| run: | | ||
| export layer_output='${{ matrix.layer }}-${{ matrix.arch }}-${{matrix.region}}.json' | ||
| aws --region ${{ matrix.region}} lambda get-layer-version-by-arn --arn 'arn:${{ needs.setup.outputs.partition }}:lambda:${{ matrix.region}}:${{ secrets[format('AWS_ACCOUNT_{0}', steps.transform.outputs.CONVERTED_REGION)] }}:layer:${{ matrix.layer }}-${{ matrix.arch }}:${{ env.LAYER_VERSION }}' > $layer_output | ||
|
|
@@ -190,6 +268,41 @@ jobs: | |
| test "$REMOTE_SHA" == "$LOCAL_SHA" && echo "SHA OK: ${LOCAL_SHA}" || exit 1 | ||
| jq -s -r '["Layer Arn", "Runtimes", "Version", "Description", "SHA256"], ([.[0], .[1]] | .[] | [.LayerArn, (.CompatibleRuntimes | join("/")), .Version, .Description, .Content.CodeSha256]) |@tsv' '${{ matrix.layer }}-${{ matrix.arch }}.json' $layer_output | column -t -s $'\t' | ||
|
|
||
| if ! aws --region ${{ matrix.region}} lambda get-layer-version-policy \ | ||
| --layer-name '${{ matrix.layer }}-${{ matrix.arch }}' \ | ||
| --version-number "$LAYER_VERSION" > target-policy.json 2> policy-error.txt; then | ||
| if grep -q ResourceNotFoundException policy-error.txt; then | ||
| echo '{"Policy":null}' > target-policy.json | ||
| else | ||
| cat policy-error.txt | ||
| exit 1 | ||
| fi | ||
| fi | ||
|
|
||
| TARGET_IS_PUBLIC=false | ||
| if jq -e ' | ||
| (.Policy | if type == "string" then fromjson else . end) | ||
| | any(.Statement[]?; | ||
| .Effect == "Allow" | ||
| and ((.Action // [] | if type == "array" then . else [.] end) | any(. == "*" or . == "lambda:*" or . == "lambda:GetLayerVersion")) | ||
| and (((.Principal | if type == "object" then .AWS // "" else . end) | if type == "array" then . else [.] end) | index("*") != null) | ||
| and ((.Condition // {}) | length == 0) | ||
| ) | ||
| ' target-policy.json > /dev/null; then | ||
| TARGET_IS_PUBLIC=true | ||
| else | ||
| RESULT=$? | ||
| if (( RESULT > 1 )); then | ||
| echo "Unable to evaluate target layer policy (jq exit ${RESULT})" | ||
| exit 1 | ||
| fi | ||
| fi | ||
|
|
||
| if [[ "$TARGET_IS_PUBLIC" != "$SOURCE_IS_PUBLIC" ]]; then | ||
| echo "Published layer visibility does not match its commercial source" | ||
| exit 1 | ||
| fi | ||
|
|
||
| - name: Store Metadata - ${{ matrix.region }} | ||
| uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 | ||
| with: | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this function is repeated several times in the PR, is there any way we can centralised?