-
Notifications
You must be signed in to change notification settings - Fork 12
Harden CI: Artifactory OIDC, NuGet lockfiles, tightened permissions #146
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
MichaelGHSeg
wants to merge
4
commits into
main
Choose a base branch
from
ci/harden-build-publish
base: main
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9579d32
Harden CI: Artifactory OIDC, NuGet lockfiles, tightened permissions
MichaelGHSeg 66bd706
Fix shell injection: pass github.ref_name via env var in deploy workflow
MichaelGHSeg eac9c64
fix(ci): correct Artifactory OIDC provider name and exchange pattern
MichaelGHSeg 835eea8
fix(ci): harden OIDC error redaction to cover all token fields
MichaelGHSeg 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| name: "Artifactory OIDC Auth" | ||
| description: "Exchange GitHub OIDC token for Artifactory access token and configure NuGet" | ||
|
|
||
| inputs: | ||
| artifactory-url: | ||
| description: "JFrog platform base URL. Falls back to ARTIFACTORY_URL env var." | ||
| required: false | ||
| default: "" | ||
| oidc-provider-name: | ||
| description: "OIDC provider name configured in Artifactory" | ||
| required: false | ||
| default: "github-actions-segmentio" | ||
| nuget-repo: | ||
| description: "Artifactory virtual NuGet repository name" | ||
| required: false | ||
| default: "virtual-nuget-thirdparty" | ||
|
|
||
| runs: | ||
| using: "composite" | ||
| steps: | ||
| - name: Exchange GitHub OIDC token for Artifactory token | ||
| shell: bash | ||
| env: | ||
| INPUT_ARTIFACTORY_URL: ${{ inputs.artifactory-url }} | ||
| OIDC_PROVIDER_NAME: ${{ inputs.oidc-provider-name }} | ||
| NUGET_REPO: ${{ inputs.nuget-repo }} | ||
| run: | | ||
| set -euo pipefail | ||
| ARTIFACTORY_URL="${INPUT_ARTIFACTORY_URL:-${ARTIFACTORY_URL:-}}" | ||
| if [ -z "${ARTIFACTORY_URL}" ]; then | ||
| echo "::error::ARTIFACTORY_URL is not set (pass as input or set as env var)"; exit 1 | ||
| fi | ||
|
|
||
| OIDC_JWT=$(curl -sS \ | ||
| "${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=${ARTIFACTORY_URL}" \ | ||
| -H "Authorization: Bearer ${ACTIONS_ID_TOKEN_REQUEST_TOKEN}" | jq -r '.value') | ||
|
|
||
| if [ -z "$OIDC_JWT" ] || [ "$OIDC_JWT" = "null" ]; then | ||
| echo "::error::Failed to obtain GitHub OIDC token"; exit 1 | ||
| fi | ||
|
|
||
| decode_seg() { local s="${1}"; local m=$(( ${#s} % 4 )); [ $m -ne 0 ] && s="${s}$(printf '=%.0s' $(seq 1 $((4-m))))"; echo "$s" | tr '_-' '/+' | base64 -d 2>/dev/null; } | ||
| PAYLOAD=$(decode_seg "$(echo "$OIDC_JWT" | cut -d. -f2)") | ||
| echo "OIDC token claims:" | ||
| echo " sub = $(echo "$PAYLOAD" | jq -r '.sub')" | ||
| echo " aud = $(echo "$PAYLOAD" | jq -r '.aud')" | ||
| echo " iss = $(echo "$PAYLOAD" | jq -r '.iss')" | ||
|
|
||
| RESP=$(curl -sS "${ARTIFACTORY_URL}/access/api/v1/oidc/token" \ | ||
| -H 'Content-Type: application/json' \ | ||
| -d "{\"grant_type\":\"urn:ietf:params:oauth:grant-type:token-exchange\", | ||
| \"subject_token_type\":\"urn:ietf:params:oauth:token-type:id_token\", | ||
| \"subject_token\":\"${OIDC_JWT}\", | ||
| \"provider_name\":\"${OIDC_PROVIDER_NAME}\"}") | ||
|
|
||
| ART_TOKEN=$(echo "$RESP" | jq -r '.access_token // empty') | ||
|
|
||
| if [ -z "$ART_TOKEN" ]; then | ||
| echo "::error::OIDC token exchange failed." | ||
| echo "$RESP" | jq 'walk(if type == "object" then with_entries(if (.key | test("token"; "i")) then .value = "<redacted>" else . end) else . end)' 2>/dev/null \ | ||
| || echo "::error::(response withheld — not valid JSON)" | ||
| exit 1 | ||
| fi | ||
| echo "::add-mask::$ART_TOKEN" | ||
|
|
||
| HOST=$(echo "${ARTIFACTORY_URL}" | sed -E 's#^https?://##') | ||
|
|
||
| mkdir -p ~/.nuget/NuGet | ||
| dotnet nuget add source \ | ||
| "https://${HOST}/artifactory/api/nuget/v3/${NUGET_REPO}/index.json" \ | ||
| --name artifactory \ | ||
| --username _ \ | ||
| --password "${ART_TOKEN}" \ | ||
| --store-password-in-clear-text \ | ||
| --configfile ~/.nuget/NuGet/NuGet.Config | ||
| dotnet nuget disable source nuget.org --configfile ~/.nuget/NuGet/NuGet.Config | ||
| echo "Configured NuGet to resolve through Artifactory (${NUGET_REPO})" |
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 |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] | ||
| pull_request: | ||
| branches: [main] | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| id-token: write | ||
| contents: read | ||
|
|
||
| env: | ||
| ARTIFACTORY_URL: ${{ vars.ARTIFACTORY_URL }} | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ${{ github.event.pull_request.head.repo.fork && 'ubuntu-latest' || 'ubuntu-x64' }} | ||
|
|
||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 | ||
|
|
||
| - name: Setup .NET SDK | ||
| uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # v4 | ||
| with: | ||
| dotnet-version: '8.0.x' | ||
|
|
||
| - name: Configure Artifactory NuGet source | ||
| if: ${{ !github.event.pull_request.head.repo.fork }} | ||
| uses: ./.github/actions/artifactory-oidc | ||
| with: | ||
| artifactory_url: ${{ vars.ARTIFACTORY_URL }} | ||
|
|
||
| - name: Restore dependencies | ||
| run: dotnet restore --locked-mode | ||
|
|
||
| - name: Build | ||
| run: dotnet build --no-restore | ||
|
|
||
| - name: Test | ||
| run: dotnet test --no-build --logger "trx;LogFileName=test-results.trx" | ||
|
|
||
| - name: Upload test results | ||
| if: always() | ||
| uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 | ||
| with: | ||
| name: test-results | ||
| path: '**/TestResults/*.trx' | ||
| if-no-files-found: ignore |
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 |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| name: Deploy | ||
|
|
||
| on: | ||
| push: | ||
| tags: | ||
| - '[0-9]+.[0-9]+.[0-9]+' | ||
| - '[0-9]+.[0-9]+.[0-9]+-alpha.[0-9]+' | ||
|
|
||
| permissions: | ||
| id-token: write | ||
| contents: read | ||
|
|
||
| env: | ||
| ARTIFACTORY_URL: ${{ vars.ARTIFACTORY_URL }} | ||
|
|
||
| jobs: | ||
| verify: | ||
| runs-on: ubuntu-x64 | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 | ||
|
|
||
| - name: Get tag | ||
| id: tag | ||
| run: echo "version=${GITHUB_REF#refs/tags/}" >> "$GITHUB_OUTPUT" | ||
|
|
||
| - name: Verify tag matches package version | ||
| run: | | ||
| VERSION=$(grep '<Version>' Analytics-CSharp/Analytics-CSharp.csproj | sed "s@.*<Version>\(.*\)</Version>.*@\1@") | ||
| if [ "${{ steps.tag.outputs.version }}" != "$VERSION" ]; then | ||
| echo "::error::Tag ${{ steps.tag.outputs.version }} does not match the package version ($VERSION)" | ||
| exit 1 | ||
| fi | ||
|
|
||
| - name: Setup .NET SDK | ||
| uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # v4 | ||
| with: | ||
| dotnet-version: '8.0.x' | ||
|
|
||
| - name: Configure Artifactory NuGet source | ||
| uses: ./.github/actions/artifactory-oidc | ||
| with: | ||
| artifactory_url: ${{ vars.ARTIFACTORY_URL }} | ||
|
|
||
| - name: Restore dependencies | ||
| run: dotnet restore --locked-mode | ||
|
|
||
| - name: Build | ||
| run: dotnet build --no-restore | ||
|
|
||
| - name: Test | ||
| run: dotnet test --no-build | ||
|
|
||
| publish: | ||
| needs: verify | ||
| runs-on: ubuntu-x64 | ||
| environment: nuget | ||
|
|
||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 | ||
|
|
||
| - name: Setup .NET SDK | ||
| uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # v4 | ||
| with: | ||
| dotnet-version: '8.0.x' | ||
|
|
||
| - name: Configure Artifactory NuGet source | ||
| uses: ./.github/actions/artifactory-oidc | ||
| with: | ||
| artifactory_url: ${{ vars.ARTIFACTORY_URL }} | ||
|
|
||
| - name: Restore dependencies | ||
| run: dotnet restore --locked-mode | ||
|
|
||
| - name: Pack | ||
| run: dotnet pack --no-restore -c Release | ||
|
|
||
| - name: Push to NuGet.org | ||
| run: | | ||
| dotnet nuget push "**/*.nupkg" \ | ||
| --source https://api.nuget.org/v3/index.json \ | ||
| --api-key ${{ secrets.NUGET_API_KEY }} \ | ||
| --skip-duplicate | ||
|
|
||
| - name: Create GitHub release | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| REF_NAME: ${{ github.ref_name }} | ||
| run: | | ||
| gh release create "$REF_NAME" \ | ||
| --title "$REF_NAME" \ | ||
| --generate-notes | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.