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

Use the terraform-plugin-sdk user agent string with running tf version #311

Merged
merged 4 commits into from
Jun 5, 2020
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
16 changes: 15 additions & 1 deletion keycloak/keycloak_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type KeycloakClient struct {
clientCredentials *ClientCredentials
httpClient *http.Client
initialLogin bool
userAgent string
}

type ClientCredentials struct {
Expand All @@ -43,7 +44,7 @@ const (
tokenUrl = "%s/auth/realms/%s/protocol/openid-connect/token"
)

func NewKeycloakClient(baseUrl, clientId, clientSecret, realm, username, password string, initialLogin bool, clientTimeout int, caCert string, tlsInsecureSkipVerify bool) (*KeycloakClient, error) {
func NewKeycloakClient(baseUrl, clientId, clientSecret, realm, username, password string, initialLogin bool, clientTimeout int, caCert string, tlsInsecureSkipVerify bool, userAgent string) (*KeycloakClient, error) {
cookieJar, err := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
Expand Down Expand Up @@ -91,6 +92,7 @@ func NewKeycloakClient(baseUrl, clientId, clientSecret, realm, username, passwor
httpClient: httpClient,
initialLogin: initialLogin,
realm: realm,
userAgent: userAgent,
}

if keycloakClient.initialLogin {
Expand All @@ -112,6 +114,10 @@ func (keycloakClient *KeycloakClient) login() error {
accessTokenRequest, _ := http.NewRequest(http.MethodPost, accessTokenUrl, strings.NewReader(accessTokenData.Encode()))
accessTokenRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded")

if keycloakClient.userAgent != "" {
accessTokenRequest.Header.Set("User-Agent", keycloakClient.userAgent)
}

accessTokenResponse, err := keycloakClient.httpClient.Do(accessTokenRequest)
if err != nil {
return err
Expand Down Expand Up @@ -146,6 +152,10 @@ func (keycloakClient *KeycloakClient) refresh() error {
refreshTokenRequest, _ := http.NewRequest(http.MethodPost, refreshTokenUrl, strings.NewReader(refreshTokenData.Encode()))
refreshTokenRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded")

if keycloakClient.userAgent != "" {
refreshTokenRequest.Header.Set("User-Agent", keycloakClient.userAgent)
}

refreshTokenResponse, err := keycloakClient.httpClient.Do(refreshTokenRequest)
if err != nil {
return err
Expand Down Expand Up @@ -204,6 +214,10 @@ func (keycloakClient *KeycloakClient) addRequestHeaders(request *http.Request) {
request.Header.Set("Authorization", fmt.Sprintf("%s %s", tokenType, accessToken))
request.Header.Set("Accept", "application/json")

if keycloakClient.userAgent != "" {
request.Header.Set("User-Agent", keycloakClient.userAgent)
}

if request.Method == http.MethodPost || request.Method == http.MethodPut || request.Method == http.MethodDelete {
request.Header.Set("Content-type", "application/json")
}
Expand Down
2 changes: 1 addition & 1 deletion keycloak/keycloak_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestAccKeycloakApiClientRefresh(t *testing.T) {
t.Fatal("KEYCLOAK_CLIENT_TIMEOUT must be an integer")
}

keycloakClient, err := NewKeycloakClient(os.Getenv("KEYCLOAK_URL"), os.Getenv("KEYCLOAK_CLIENT_ID"), os.Getenv("KEYCLOAK_CLIENT_SECRET"), os.Getenv("KEYCLOAK_REALM"), os.Getenv("KEYCLOAK_USER"), os.Getenv("KEYCLOAK_PASSWORD"), true, clientTimeout, "", false)
keycloakClient, err := NewKeycloakClient(os.Getenv("KEYCLOAK_URL"), os.Getenv("KEYCLOAK_CLIENT_ID"), os.Getenv("KEYCLOAK_CLIENT_SECRET"), os.Getenv("KEYCLOAK_REALM"), os.Getenv("KEYCLOAK_USER"), os.Getenv("KEYCLOAK_PASSWORD"), true, clientTimeout, "", false, "")
if err != nil {
t.Fatalf("%s", err)
}
Expand Down
23 changes: 19 additions & 4 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package provider

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/httpclient"
"github.com/mrparkers/terraform-provider-keycloak/keycloak"
)

func KeycloakProvider() *schema.Provider {
return &schema.Provider{
provider := &schema.Provider{
DataSourcesMap: map[string]*schema.Resource{
"keycloak_group": dataSourceKeycloakGroup(),
"keycloak_openid_client": dataSourceKeycloakOpenidClient(),
Expand Down Expand Up @@ -138,11 +139,25 @@ func KeycloakProvider() *schema.Provider {
Default: false,
},
},
ConfigureFunc: configureKeycloakProvider,
}

provider.ConfigureFunc = func(d *schema.ResourceData) (interface{}, error) {
terraformVersion := provider.TerraformVersion
if terraformVersion == "" {
// Terraform 0.12 introduced this field to the protocol
// We can therefore assume that if it's missing it's 0.10 or 0.11
terraformVersion = "0.11+compatible"
}

userAgent := httpclient.TerraformUserAgent(terraformVersion)

return configureKeycloakProvider(d, userAgent)
}

return provider
}

func configureKeycloakProvider(data *schema.ResourceData) (interface{}, error) {
func configureKeycloakProvider(data *schema.ResourceData, userAgent string) (interface{}, error) {
url := data.Get("url").(string)
clientId := data.Get("client_id").(string)
clientSecret := data.Get("client_secret").(string)
Expand All @@ -154,5 +169,5 @@ func configureKeycloakProvider(data *schema.ResourceData) (interface{}, error) {
tlsInsecureSkipVerify := data.Get("tls_insecure_skip_verify").(bool)
rootCaCertificate := data.Get("root_ca_certificate").(string)

return keycloak.NewKeycloakClient(url, clientId, clientSecret, realm, username, password, initialLogin, clientTimeout, rootCaCertificate, tlsInsecureSkipVerify)
return keycloak.NewKeycloakClient(url, clientId, clientSecret, realm, username, password, initialLogin, clientTimeout, rootCaCertificate, tlsInsecureSkipVerify, userAgent)
}
3 changes: 2 additions & 1 deletion provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/httpclient"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/mrparkers/terraform-provider-keycloak/keycloak"
"os"
Expand All @@ -25,7 +26,7 @@ func init() {
"keycloak": testAccProvider,
}

keycloakClient, _ = keycloak.NewKeycloakClient(os.Getenv("KEYCLOAK_URL"), os.Getenv("KEYCLOAK_CLIENT_ID"), os.Getenv("KEYCLOAK_CLIENT_SECRET"), os.Getenv("KEYCLOAK_REALM"), "", "", true, 5, "", false)
keycloakClient, _ = keycloak.NewKeycloakClient(os.Getenv("KEYCLOAK_URL"), os.Getenv("KEYCLOAK_CLIENT_ID"), os.Getenv("KEYCLOAK_CLIENT_SECRET"), os.Getenv("KEYCLOAK_REALM"), "", "", true, 5, "", false, httpclient.TerraformUserAgent(testAccProvider.TerraformVersion))
}

func TestProvider(t *testing.T) {
Expand Down