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

Bugfix: fix missing use of oidc_token_file_path, CLI auth with Cloud Shell #20824

Merged
merged 4 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions .github/workflows/provider-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ jobs:
needs: [secrets-check]
if: needs.secrets-check.outputs.available == 'true'
steps:
- name: Azure CLI login
run: az login --output none --username="${{ secrets.AZCLI_USERNAME }}" --password="${{ secrets.AZCLI_PASSWORD }}"

- name: Set OIDC Token
run: |
echo "ARM_OIDC_TOKEN=$(curl -H "Accept: application/json; api-version=2.0" -H "Authorization: Bearer ${ACTIONS_ID_TOKEN_REQUEST_TOKEN}" -H "Content-Type: application/json" -G --data-urlencode "audience=api://AzureADTokenExchange" "${ACTIONS_ID_TOKEN_REQUEST_URL}" | jq -r '.value')" >>${GITHUB_ENV}

- name: Checkout
uses: actions/checkout@v3

Expand All @@ -50,6 +43,16 @@ jobs:
with:
go-version-file: ./.go-version

- name: Azure CLI login
run: az login --output none --username="${{ secrets.AZCLI_USERNAME }}" --password="${{ secrets.AZCLI_PASSWORD }}"

- name: Set OIDC Token
run: |
echo "ARM_OIDC_TOKEN=$(curl -H "Accept: application/json; api-version=2.0" -H "Authorization: Bearer ${ACTIONS_ID_TOKEN_REQUEST_TOKEN}" -H "Content-Type: application/json" -G --data-urlencode "audience=api://AzureADTokenExchange" "${ACTIONS_ID_TOKEN_REQUEST_URL}" | jq -r '.value')" >>${GITHUB_ENV}

- name: Set OIDC Token File Path
run: echo "${ARM_OIDC_TOKEN}" >"${RUNNER_TEMP}/oidc-token.jwt" && echo "ARM_OIDC_TOKEN_FILE_PATH=${RUNNER_TEMP}/oidc-token.jwt" >>${GITHUB_ENV}

- name: Run provider tests
run: make testacc TEST=./internal/provider TESTARGS="-run '^TestAcc'"
env:
Expand All @@ -60,4 +63,8 @@ jobs:
ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }}
ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }}

- name: Clean Up OIDC Token File Path
manicminer marked this conversation as resolved.
Show resolved Hide resolved
run: rm -f "${RUNNER_TEMP}/oidc-token.jwt"
if: always()

# vim: set ts=2 sts=2 sw=2 et:
30 changes: 28 additions & 2 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,13 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc {
}
}

oidcToken, err := getOidcToken(d)
if err != nil {
return nil, diag.FromErr(err)
}

var (
env *environments.Environment
err error

envName = d.Get("environment").(string)
metadataHost = d.Get("metadata_host").(string)
Expand Down Expand Up @@ -399,7 +403,7 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc {
ClientCertificatePassword: d.Get("client_certificate_password").(string),
ClientSecret: d.Get("client_secret").(string),

OIDCAssertionToken: d.Get("oidc_token").(string),
OIDCAssertionToken: *oidcToken,
GitHubOIDCTokenRequestURL: d.Get("oidc_request_url").(string),
GitHubOIDCTokenRequestToken: d.Get("oidc_request_token").(string),

Expand Down Expand Up @@ -484,6 +488,28 @@ func decodeCertificate(clientCertificate string) ([]byte, error) {
return pfx, nil
}

func getOidcToken(d *schema.ResourceData) (*string, error) {
idToken := strings.TrimSpace(d.Get("oidc_token").(string))

if path := d.Get("oidc_token_file_path").(string); path != "" {
fileTokenRaw, err := os.ReadFile(path)

if err != nil {
return nil, fmt.Errorf("reading OIDC Token from file %q: %v", path, err)
}

fileToken := strings.TrimSpace(string(fileTokenRaw))

if idToken != "" && idToken != fileToken {
return nil, fmt.Errorf("mismatch between supplied OIDC token and supplied OIDC token file contents - please either remove one or ensure they match")
}

idToken = fileToken
}

return &idToken, nil
}

const resourceProviderRegistrationErrorFmt = `Error ensuring Resource Providers are registered.

Terraform automatically attempts to register the Resource Providers it supports to
Expand Down
11 changes: 8 additions & 3 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ func TestAccProvider_genericOidcAuth(t *testing.T) {
if os.Getenv("TF_ACC") == "" {
t.Skip("TF_ACC not set")
}
if os.Getenv("ARM_OIDC_TOKEN") == "" {
t.Skip("ARM_OIDC_TOKEN not set")
if os.Getenv("ARM_OIDC_TOKEN") == "" && os.Getenv("ARM_OIDC_TOKEN_FILE_PATH") == "" {
t.Skip("ARM_OIDC_TOKEN or ARM_OIDC_TOKEN_FILE_PATH not set")
}

logging.SetOutput(t)
Expand All @@ -289,12 +289,17 @@ func TestAccProvider_genericOidcAuth(t *testing.T) {
t.Fatalf("configuring environment %q: %v", envName, err)
}

oidcToken, err := getOidcToken(d)
if err != nil {
return nil, diag.FromErr(err)
}

authConfig := &auth.Credentials{
Environment: *env,
TenantID: d.Get("tenant_id").(string),
ClientID: d.Get("client_id").(string),
EnableAuthenticationUsingOIDC: true,
OIDCAssertionToken: d.Get("oidc_token").(string),
OIDCAssertionToken: *oidcToken,
}

return buildClient(ctx, provider, d, authConfig)
Expand Down