Unverified Commit 500e0ee5 authored by Alexis Tual's avatar Alexis Tual Committed by GitHub
Browse files

Add support for short-lived tokens (#224)

The setup-gradle action tries to get a short-lived access token given the supplied Develocity access key.
This key can be passed either with the `DEVELOCITY_ACCESS_KEY` env var or via the  `develocity-access-key` input parameter.
If a token can be retrieved, then the `DEVELOCITY_ACCESS_KEY` env var will be set to the token. 
Otherwise the `DEVELOCITY_ACCESS_KEY` will be set to a blank string, to avoid a leak.

---------

Co-authored-by: default avatardaz <[email protected]>
parent eb13cf71
Loading
Loading
Loading
Loading
+86 −2
Changes for .github/workflows/integ-test-inject-develocity.yml: 86 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -26,14 +26,19 @@ jobs:
      DEVELOCITY_URL: https://ge.solutions-team.gradle.com
      DEVELOCITY_PLUGIN_VERSION: ${{ matrix.plugin-version }}
      DEVELOCITY_CCUD_PLUGIN_VERSION: '2.0'
      GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.DEVELOCITY_ACCESS_KEY }} # required to test against GE plugin 3.16.2
      DEVELOCITY_ACCESS_KEY: ${{ secrets.DEVELOCITY_ACCESS_KEY }}
      ${{matrix.accessKeyEnv}}: ${{ secrets.DEVELOCITY_ACCESS_KEY }}
    strategy:
      fail-fast: false
      matrix:
        gradle: [current, 7.6.2, 6.9.4, 5.6.4]
        os: ${{fromJSON(inputs.runner-os)}}
        plugin-version: [3.16.2, 3.17.3]
        include:
        - plugin-version: 3.16.2
          accessKeyEnv: GRADLE_ENTERPRISE_ACCESS_KEY
        - plugin-version: 3.17.3
          accessKeyEnv: DEVELOCITY_ACCESS_KEY

    runs-on: ${{ matrix.os }}
    steps:
    - name: Checkout sources
@@ -62,3 +67,82 @@ jobs:
      with:
        script: |
          core.setFailed('No Build Scan detected')
    - name: Check short lived token
      if: ${{ matrix.plugin-version == '3.17.3' }}
      run: "[ ${#DEVELOCITY_ACCESS_KEY} -gt 500 ] || (echo 'DEVELOCITY_ACCESS_KEY does not look like a short lived token'; exit 1)"

  inject-develocity-with-access-key:
    env:
      DEVELOCITY_INJECTION_ENABLED: true
      DEVELOCITY_URL: 'https://ge.solutions-team.gradle.com'
      DEVELOCITY_PLUGIN_VERSION: 3.17.3
      DEVELOCITY_CCUD_PLUGIN_VERSION: '2.0'
    strategy:
      fail-fast: false
      matrix:
        gradle: [current, 7.6.2, 6.9.4, 5.6.4]
        os: ${{fromJSON(inputs.runner-os)}}
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
      - name: Initialize integ-test
        uses: ./.github/actions/init-integ-test
      - name: Setup Java
        uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 8
      - name: Setup Gradle
        id: setup-gradle
        uses: ./setup-gradle
        with:
          cache-read-only: false # For testing, allow writing cache entries on non-default branches
          gradle-version: ${{ matrix.gradle }}
          develocity-access-key: ${{ secrets.DEVELOCITY_ACCESS_KEY }}
          develocity-token-expiry: 1
      - name: Run Gradle build
        id: gradle
        working-directory: .github/workflow-samples/no-ge
        run: gradle help
      - name: Check short lived token
        run: "[ ${#DEVELOCITY_ACCESS_KEY} -gt 500 ] || (echo 'DEVELOCITY_ACCESS_KEY does not look like a short lived token'; exit 1)"
      - name: Check Build Scan url
        if: ${{ !steps.gradle.outputs.build-scan-url }}
        uses: actions/github-script@v7
        with:
          script: |
            core.setFailed('No Build Scan detected')

  inject-develocity-short-lived-token-failed:
    env:
      DEVELOCITY_INJECTION_ENABLED: true
      DEVELOCITY_URL: 'https://localhost:3333/'
      DEVELOCITY_PLUGIN_VERSION: 3.17.3
      DEVELOCITY_CCUD_PLUGIN_VERSION: '2.0'
      # Access key also set as an env var, we want to check it does not leak
      DEVELOCITY_ACCESS_KEY: ${{ secrets.DEVELOCITY_ACCESS_KEY }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
      - name: Initialize integ-test
        uses: ./.github/actions/init-integ-test

      - name: Setup Java
        uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 8
      - name: Setup Gradle
        id: setup-gradle
        uses: ./setup-gradle
        with:
          cache-read-only: false # For testing, allow writing cache entries on non-default branches
          develocity-access-key: ${{ secrets.DEVELOCITY_ACCESS_KEY }}
      - name: Run Gradle build
        id: gradle
        working-directory: .github/workflow-samples/no-ge
        run: gradle help
      - name: Check access key is blank
        run: "[ \"${DEVELOCITY_ACCESS_KEY}\" == \"\" ] || (echo 'DEVELOCITY_ACCESS_KEY has leaked!'; exit 1)"
+7 −1
Changes for build: 7 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -16,12 +16,18 @@ case "$1" in
        # Run act
        $@
        # Revert the changes to the dist directory
        git co -- dist
        git checkout -- dist
        ;;
    init-scripts)
        cd test/init-scripts
        ./gradlew check
        ;;
    dist)
        npm install
        npm run build
        cd ..
        cp -r sources/dist .
        ;;
    *)
        npm install
        npm run build
+8
Changes for dependency-submission/action.yml: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -125,6 +125,14 @@ inputs:
    description: Indicate that you agree to the Build Scan® terms of use. This input value must be "yes".
    required: false

  develocity-access-key:
    description: Develocity access key. Should be set to a secret containing the Develocity Access key.
    required: false

  develocity-token-expiry:
    description: The Develocity short-lived access tokens expiry in hours. Default is 2 hours.
    required: false

  # DEPRECATED ACTION INPUTS
  build-scan-terms-of-service-url:
    description: The URL to the Build Scan® terms of use. This input must be set to 'https://gradle.com/terms-of-service'.
+23 −2
Changes for docs/setup-gradle.md: 23 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -727,8 +727,29 @@ Here's a minimal example:
This configuration will automatically apply `v3.17.3` of the [Develocity Gradle plugin](https://docs.gradle.com/develocity/gradle-plugin/), and publish build scans to https://develocity.your-server.com.

This example assumes that the `develocity.your-server.com` server allows anonymous publishing of build scans.
In the likely scenario that your Develocity server requires authentication, you will also need to configure an additional environment variable
with a valid [Develocity access key](https://docs.gradle.com/develocity/gradle-plugin/#via_environment_variable).
In the likely scenario that your Develocity server requires authentication, you will also need to pass a valid [Develocity access key](https://docs.gradle.com/develocity/gradle-plugin/#via_environment_variable) taken from a secret:

```yaml
    - name: Setup Gradle
      uses: gradle/actions/setup-gradle@v3
      with:
        develocity-access-key: ${{ secrets.MY_DEVELOCITY_ACCESS_KEY }}

    - name: Run a Gradle build with Develocity injection enabled
      run: ./gradlew build
      env:
        DEVELOCITY_INJECTION_ENABLED: true
        DEVELOCITY_URL: https://develocity.your-server.com
        DEVELOCITY_PLUGIN_VERSION: 3.17
```

This access key will be used during the action execution to get a short-lived token and set it to the DEVELOCITY_ACCESS_KEY environment variable.

### Short-lived access tokens
Develocity access keys are long-lived, creating risks if they are leaked. To avoid this, users can use short-lived access tokens to authenticate with Develocity. Access tokens can be used wherever an access key would be used. Access tokens are only valid for the Develocity instance that created them.
If a short-lived token fails to be retrieved (for example, if the Develocity server version is lower than `2024.1`), no access key will be set.
In that case, Develocity authenticated operations like build cache read/write and build scan publication will fail without failing the build.
For more information on short-lived tokens, see [Develocity API documentation](https://docs.gradle.com/develocity/api-manual/#short_lived_access_tokens).

## Configuring Develocity injection

+8
Changes for setup-gradle/action.yml: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -100,6 +100,14 @@ inputs:
    description: Indicate that you agree to the Build Scan® terms of use. This input value must be "yes".
    required: false

  develocity-access-key:
    description: Develocity access key. Should be set to a secret containing the Develocity Access key.
    required: false

  develocity-token-expiry:
    description: The Develocity short-lived access tokens expiry in hours. Default is 2 hours.
    required: false

  # Wrapper validation configuration
  validate-wrappers:
    description: |
Loading