Skip to content

Commit

Permalink
ci: test all major keycloak versions (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawknewton authored Jun 2, 2020
1 parent 9f65194 commit 0f9368e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 79 deletions.
94 changes: 44 additions & 50 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,56 +1,59 @@
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+)?/
branches:
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
- restore_cache:
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:
Expand All @@ -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:
Expand Down
53 changes: 25 additions & 28 deletions keycloak/keycloak_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion provider/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/mrparkers/terraform-provider-keycloak/keycloak"
"math/rand"
"os"
"regexp"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 0f9368e

Please sign in to comment.