From 0f9368ef93c50fe1a6fd628175b7d11c023b456f Mon Sep 17 00:00:00 2001 From: Hawk Newton Date: Tue, 2 Jun 2020 07:39:44 -0700 Subject: [PATCH] ci: test all major keycloak versions (#294) --- .circleci/config.yml | 94 +++++++++++++++++-------------------- keycloak/keycloak_client.go | 53 ++++++++++----------- provider/test_utils.go | 7 ++- 3 files changed, 75 insertions(+), 79 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index d0e129203..2a243e9ab 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,13 +1,22 @@ -version: 2 +version: 2.1 workflows: - version: 2 test: jobs: - - test-7.0.1 - - test-8.0.1 + - test: + matrix: + parameters: + keycloak-version: + - '10.0.1' + - '9.0.3' + - '8.0.2' + - '7.0.1' + - '6.0.1' + - '5.0.0' + - '4.8.3.Final' release: jobs: - - test-8.0.1: + - test: + keycloak-version: '8.0.1' filters: tags: only: /\d+\.\d+\.\d+(-rc.\d+)?/ @@ -15,35 +24,27 @@ workflows: ignore: /.*/ - build-and-release: requires: - - test-8.0.1 - filters: - tags: - only: /\d+\.\d+\.\d+(-rc.\d+)?/ - branches: - ignore: /.*/ + - test defaults: go_image: &go_image - image: circleci/golang:1.13.5 - test_env: &test_env - GO111MODULE: "on" - KEYCLOAK_CLIENT_ID: "terraform" - KEYCLOAK_CLIENT_SECRET: "884e0f95-0f42-4a63-9b1f-94274655669e" - KEYCLOAK_CLIENT_TIMEOUT: "5" - KEYCLOAK_URL: "http://localhost:8080" - KEYCLOAK_REALM: "master" - KEYCLOAK_TEST_PASSWORD_GRANT: "true" - - keycloak_env: &keycloak_env - command: ["-b", "0.0.0.0", "-Dkeycloak.profile.feature.upload_scripts=enabled"] - environment: - DB_VENDOR: H2 - KEYCLOAK_LOGLEVEL: DEBUG - KEYCLOAK_USER: keycloak - KEYCLOAK_PASSWORD: password +jobs: + test: + parameters: + keycloak-version: + type: string + docker: + - <<: *go_image + - image: jboss/keycloak:<< parameters.keycloak-version >> + command: ["-b", "0.0.0.0", "-Dkeycloak.profile.feature.upload_scripts=enabled"] + environment: + DB_VENDOR: H2 + KEYCLOAK_LOGLEVEL: DEBUG + KEYCLOAK_USER: keycloak + KEYCLOAK_PASSWORD: password - testacc_job: &testacc_job working_directory: /go/src/github.com/mrparkers/terraform-provider-keycloak steps: - checkout @@ -51,6 +52,8 @@ defaults: keys: - go-cache-{{ checksum "go.sum" }} - run: go mod download + - run: go get github.com/jstemmer/go-junit-report + - run: mkdir $TEST_RESULTS - save_cache: key: go-cache-{{ checksum "go.sum" }} paths: @@ -59,29 +62,20 @@ defaults: command: | ./scripts/wait-for-local-keycloak.sh ./scripts/create-terraform-client.sh - make testacc - -jobs: - test-7.0.1: - docker: - - <<: *go_image - - image: jboss/keycloak:7.0.1 - <<: *keycloak_env - <<: *testacc_job - environment: - <<: *test_env - KEYCLOAK_VERSION: "7.0.1" - - - test-8.0.1: - docker: - - <<: *go_image - - image: jboss/keycloak:8.0.1 - <<: *keycloak_env - <<: *testacc_job + trap "go-junit-report <${TEST_RESULTS}/go-test.out > ${TEST_RESULTS}/go-test-report.xml" EXIT + make testacc | tee ${TEST_RESULTS}/go-test.out + - store_test_results: + path: /tmp/test-results environment: - <<: *test_env - KEYCLOAK_VERSION: "8.0.1" + GO111MODULE: "on" + KEYCLOAK_CLIENT_ID: "terraform" + KEYCLOAK_CLIENT_SECRET: "884e0f95-0f42-4a63-9b1f-94274655669e" + KEYCLOAK_CLIENT_TIMEOUT: "5" + KEYCLOAK_URL: "http://localhost:8080" + KEYCLOAK_REALM: "master" + KEYCLOAK_TEST_PASSWORD_GRANT: "true" + KEYCLOAK_VERSION: "<< parameters.keycloak-version >>" + TEST_RESULTS: /tmp/test-results build-and-release: diff --git a/keycloak/keycloak_client.go b/keycloak/keycloak_client.go index a0db76abb..1c55b22c9 100644 --- a/keycloak/keycloak_client.go +++ b/keycloak/keycloak_client.go @@ -105,21 +105,7 @@ func NewKeycloakClient(baseUrl, clientId, clientSecret, realm, username, passwor func (keycloakClient *KeycloakClient) login() error { accessTokenUrl := fmt.Sprintf(tokenUrl, keycloakClient.baseUrl, keycloakClient.realm) - accessTokenData := url.Values{} - accessTokenData.Set("client_id", keycloakClient.clientCredentials.ClientId) - accessTokenData.Set("grant_type", keycloakClient.clientCredentials.GrantType) - - if keycloakClient.clientCredentials.GrantType == "password" { - accessTokenData.Set("username", keycloakClient.clientCredentials.Username) - accessTokenData.Set("password", keycloakClient.clientCredentials.Password) - - if keycloakClient.clientCredentials.ClientSecret != "" { - accessTokenData.Set("client_secret", keycloakClient.clientCredentials.ClientSecret) - } - - } else if keycloakClient.clientCredentials.GrantType == "client_credentials" { - accessTokenData.Set("client_secret", keycloakClient.clientCredentials.ClientSecret) - } + accessTokenData := keycloakClient.getAuthenticationFormData() log.Printf("[DEBUG] Login request: %s", accessTokenData.Encode()) @@ -153,23 +139,14 @@ func (keycloakClient *KeycloakClient) login() error { func (keycloakClient *KeycloakClient) refresh() error { refreshTokenUrl := fmt.Sprintf(tokenUrl, keycloakClient.baseUrl, keycloakClient.realm) - refreshTokenData := url.Values{} - refreshTokenData.Set("client_id", keycloakClient.clientCredentials.ClientId) - refreshTokenData.Set("grant_type", keycloakClient.clientCredentials.GrantType) - - if keycloakClient.clientCredentials.GrantType == "password" { - refreshTokenData.Set("username", keycloakClient.clientCredentials.Username) - refreshTokenData.Set("password", keycloakClient.clientCredentials.Password) - } else if keycloakClient.clientCredentials.GrantType == "client_credentials" { - refreshTokenData.Set("client_secret", keycloakClient.clientCredentials.ClientSecret) - } + refreshTokenData := keycloakClient.getAuthenticationFormData() log.Printf("[DEBUG] Refresh request: %s", refreshTokenData.Encode()) - accessTokenRequest, _ := http.NewRequest(http.MethodPost, refreshTokenUrl, strings.NewReader(refreshTokenData.Encode())) - accessTokenRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded") + refreshTokenRequest, _ := http.NewRequest(http.MethodPost, refreshTokenUrl, strings.NewReader(refreshTokenData.Encode())) + refreshTokenRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded") - refreshTokenResponse, err := keycloakClient.httpClient.Do(accessTokenRequest) + refreshTokenResponse, err := keycloakClient.httpClient.Do(refreshTokenRequest) if err != nil { return err } @@ -200,6 +177,26 @@ func (keycloakClient *KeycloakClient) refresh() error { return nil } +func (keycloakClient *KeycloakClient) getAuthenticationFormData() url.Values { + authenticationFormData := url.Values{} + authenticationFormData.Set("client_id", keycloakClient.clientCredentials.ClientId) + authenticationFormData.Set("grant_type", keycloakClient.clientCredentials.GrantType) + + if keycloakClient.clientCredentials.GrantType == "password" { + authenticationFormData.Set("username", keycloakClient.clientCredentials.Username) + authenticationFormData.Set("password", keycloakClient.clientCredentials.Password) + + if keycloakClient.clientCredentials.ClientSecret != "" { + authenticationFormData.Set("client_secret", keycloakClient.clientCredentials.ClientSecret) + } + + } else if keycloakClient.clientCredentials.GrantType == "client_credentials" { + authenticationFormData.Set("client_secret", keycloakClient.clientCredentials.ClientSecret) + } + + return authenticationFormData +} + func (keycloakClient *KeycloakClient) addRequestHeaders(request *http.Request) { tokenType := keycloakClient.clientCredentials.TokenType accessToken := keycloakClient.clientCredentials.AccessToken diff --git a/provider/test_utils.go b/provider/test_utils.go index dc5ff5337..9c4630979 100644 --- a/provider/test_utils.go +++ b/provider/test_utils.go @@ -6,6 +6,7 @@ import ( "github.com/mrparkers/terraform-provider-keycloak/keycloak" "math/rand" "os" + "regexp" "strings" "testing" "time" @@ -86,7 +87,11 @@ func keycloakVersionIsGreaterThanOrEqualTo(keycloakClient *keycloak.KeycloakClie if err != nil { return false, fmt.Errorf("/serverInfo endpoint retuned an error, server Keycloak version could not be determined: %s", err) } - keycloakServerInfoVersion, err = version.NewVersion(serverInfo.SystemInfo.ServerVersion) + + regex := regexp.MustCompile(`^(\d+\.\d+\.\d+)`) + semver := regex.FindStringSubmatch(serverInfo.SystemInfo.ServerVersion)[0] + + keycloakServerInfoVersion, err = version.NewVersion(semver) if err != nil { return false, fmt.Errorf("/serverInfo endpoint retuned an unreadable version, server Keycloak version could not be determined: %s", err) }