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

Add OIDC token exchange with JFrog instance #59

Merged
merged 2 commits into from
Apr 11, 2024
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
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
## 1.23.0 (Apr 11, 2024)

IMPROVEMENTS:

* Add support for exchanging OIDC ID token for access token using JFrog OIDC configuration

PR: [#59](https://github.com/jfrog/terraform-provider-shared/pull/59)

## 1.22.4 (Apr 4, 2024)

IMPROVEMENTS:

* Enable Resty's debug logging when `TF_LOG` is set to `DEBUG` or `TRACE`.

Issue [#16](https://github.com/jfrog/terraform-provider-shared/issues/16)
PR: [#58](https://github.com/jfrog/terraform-provider-shared/pull/57)
PR: [#58](https://github.com/jfrog/terraform-provider-shared/pull/58)

## 1.22.3 (Apr 4, 2024)

Expand Down
48 changes: 48 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,54 @@ func SendUsage(ctx context.Context, client *resty.Client, productId string, feat
}
}

type OIDCAccessTokenRequest struct {
GrantType string `json:"grant_type"`
SubjectTokenType string `json:"subject_token_type"`
SubjectToken string `json:"subject_token"`
ProviderName string `json:"provider_name"`
}

type OIDCAccessTokenResponse struct {
AccessToken string `json:"access_token"`
}

// OIDCTokenExchange use TFC_WORKLOAD_IDENTITY_TOKEN env var value to exchange for a access token using
// OIDC provider configured on JFrog platform
func OIDCTokenExchange(ctx context.Context, client *resty.Client, providerName string) (string, error) {
if client == nil {
return "", fmt.Errorf("client is nil")
}

tfcWorkloadIdentityToken := CheckEnvVars([]string{"TFC_WORKLOAD_IDENTITY_TOKEN"}, "")
if tfcWorkloadIdentityToken == "" || providerName == "" {
tflog.Info(ctx, "either TFC_WORKLOAD_IDENTITY_TOKEN or provider name is not set")
return "", nil
}

payload := OIDCAccessTokenRequest{
GrantType: "urn:ietf:params:oauth:grant-type:token-exchange",
SubjectTokenType: "urn:ietf:params:oauth:token-type:id_token",
SubjectToken: tfcWorkloadIdentityToken,
ProviderName: providerName,
}

var result OIDCAccessTokenResponse
response, err := client.R().
SetBody(payload).
SetResult(&result).
Post("/access/api/v1/oidc/token")

if err != nil {
return "", err
}

if response.IsError() {
return "", fmt.Errorf(response.String())
}

return result.AccessToken, nil
}

func CheckArtifactoryLicense(client *resty.Client, licenseTypesToCheck ...string) error {
if len(licenseTypesToCheck) == 0 {
return fmt.Errorf("licenseTypesToCheck is empty")
Expand Down
Loading