Skip to content
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

[FEAT]: Switching Between PAT and GitHub App Authentication Without Modifying Terraform Code #1877

Open
1 task done
twokasa opened this issue Sep 6, 2023 · 6 comments · May be fixed by #2174
Open
1 task done

[FEAT]: Switching Between PAT and GitHub App Authentication Without Modifying Terraform Code #1877

twokasa opened this issue Sep 6, 2023 · 6 comments · May be fixed by #2174
Labels
hacktoberfest Issues for participation in Hacktoberfest Status: Up for grabs Issues that are ready to be worked on by anyone Type: Feature New feature or request

Comments

@twokasa
Copy link

twokasa commented Sep 6, 2023

Describe the need

Hello,

I'm trying to differentiate between authentication methods: using PAT (Personal Access Token) in my local environment and the GitHub App in the CI environment. However, after adding the app_auth block for the CI setup, I receive an error in the local environment indicating that id, installation_id, and pem_file are not set. Is there a way to toggle between authentication methods without changing the Terraform code?

Thank you for your assistance.

(Note: This message was translated with the assistance of a machine translation tool.)

SDK Version

No response

API Version

No response

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@twokasa twokasa added Status: Triage This is being looked at and prioritized Type: Feature New feature or request labels Sep 6, 2023
@kfcampbell kfcampbell added Status: Up for grabs Issues that are ready to be worked on by anyone and removed Status: Triage This is being looked at and prioritized labels Sep 8, 2023
@kfcampbell
Copy link
Member

Hi! Unfortunately there is currently no way to do so. That would be an interesting feature to add!

@nickfloyd nickfloyd added the hacktoberfest Issues for participation in Hacktoberfest label Sep 21, 2023
@wheelerlaw
Copy link

You can do this by using the GitHub CLI (gh) to authenticate. To do this, leave the provider "github" block empty. Then, make sure you have your local credentials configured by doing gh auth login. In your CI you will need to generate an app installation token from the app's private key. The following is an example if you are using GitHub Actions:

name: CI
on:
  push:
    branches: [main]
jobs:
  apply:
    name: TF Apply
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v3
      - name: Generate app token
        id: generate-app-token
        uses: tibdex/github-app-token@v1.9.0
        with:
          app_id: ${{ vars.YOUR_APP_ID }}
          private_key: ${{ secrets.YOUR_APP_PRIVATE_KEY }}
      - name: Terraform apply
        env:
          GITHUB_TOKEN: ${{ steps.generate-app-token.outputs.token }}
        run: terraform apply -auto-approve

@laughedelic
Copy link
Contributor

laughedelic commented Oct 11, 2023

Using the app installation token directly is a good workaround when you run Terraform from GitHub actions, but what about using it from Atlantis? Installation token is short-lived, so it can't be used in Atlantis as a static secret, but providing app credentials instead requires having app_auth {} in the code.

A potential solution here without breaking the existing interface or adding new functionality might be to look for app env vars even when there is no empty app_auth {} block, i.e.

provider "github" {}
  1. First looks for the GITHUB_TOKEN env var or credentials set by gh auth login as it does now, so existing workflows don't break
  2. Then it could check for the GITHUB_APP_ID+GITHUB_APP_INSTALLATION_ID+GITHUB_APP_PEM_FILE trio to use the app authentication (which would work in CI and Atlantis)

I see the docs say

When using environment variables, an empty app_auth block is required to allow provider configurations from environment variables to be specified. See: hashicorp/terraform-plugin-sdk#142

But I don't quite understand the linked issue. How does it work with an empty provider "github" {} and the GITHUB_TOKEN env var, but doesn't work with the three app env vars?
But I think it's still achievable via introduction of new parameters (making the app_auth block redundant)

P.S. Also having empty app_auth {} fails validation:

│ Error: Missing required argument
│ 
│   on provider.tf line 22, in provider "github":
│   22:   app_auth {}
│ 
│ The argument "pem_file" is required, but no definition was found.

@gulzat214
Copy link

Hello, is there an update on this ?

@gulzat214
Copy link

Hi! Unfortunately there is currently no way to do so. That would be an interesting feature to add!

Hello, do you know there has been any progress made on this issue ?

laughedelic added a commit to laughedelic/terraform-provider-github that referenced this issue Mar 4, 2024
This resolves [FEAT]: Switching Between PAT and GitHub App Authentication Without Modifying Terraform Code integrations#1877

New parameters mirror those in the app_auth block and make it possible to switch between token-based and app-based authentication via environment variables without altering existing provider configuration code. This allows flexibility of using a GitHub app for provider authentication when running in CI or another automated environment, and using a personal access token when developing locally.

Existing behavior is preserved and the only new case is when GITHUB_APP_* are set, GITHUB_TOKEN isn't set and there is no app_auth block: before it would be an error (app vars would be ignored), but now it works as an app-based configuration.
@laughedelic
Copy link
Contributor

laughedelic commented Mar 4, 2024

I took a stab at it in #2174

Any feedback and help are appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest Issues for participation in Hacktoberfest Status: Up for grabs Issues that are ready to be worked on by anyone Type: Feature New feature or request
Projects
None yet
6 participants