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 keycloak versions to circleci test job #294

Merged
merged 12 commits into from
Jun 2, 2020
95 changes: 45 additions & 50 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,56 +1,60 @@
version: 2
version: 2.1
workflows:
version: 2
test:
jobs:
- test-7.0.1
- test-8.0.1
- test:
matrix:
parameters:
keycloak-version:
# 4.8.3 doesn't have a docker image
hawknewton marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last suggestion: can you remove this comment?

- '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 +63,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+)`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my only suggestion here is that go-version does export their regular expressions that we can use instead: /~https://github.com/hashicorp/go-version/blob/master/version.go#L20-L31

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my only suggestion here is that go-version does export their regular expressions that we can use instead: /~https://github.com/hashicorp/go-version/blob/master/version.go#L20-L31

	regex := `v?([0-9]+(\.[0-9]+)*?)` +
		`(-([0-9]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)|(-([A-Za-z\-~]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)))?` +
		`(\+([0-9A-Za-z\-~]+(\.[0-9A-Za-z\-~]+)*))?` +
		`?`

	r := regexp.MustCompile(regex)
	fmt.Printf("%#v\n", r.FindStringSubmatch(`4.4.3.Final`))
[]string{"4", "4", "", "", "", "", "", "", "", "", "", ""}

No bueno, unfortunately.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, nevermind then. I think what you have is good enough, especially since this is only used for tests.

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