Skip to content

Commit

Permalink
Merge pull request #19 from crossplane-contrib/feat/config/missingcon…
Browse files Browse the repository at this point in the history
…figs

feat(config): Add missing configs and refactor config logic
  • Loading branch information
Breee authored Dec 13, 2023
2 parents 330f6e9 + fe25d0f commit 0cb44a4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ NPROCS ?= 1
# to half the number of CPU cores.
GO_TEST_PARALLEL := $(shell echo $$(( $(NPROCS) / 2 )))

GO_REQUIRED_VERSION ?= 1.19
GO_REQUIRED_VERSION ?= $(shell $(GO) version | sed -ne 's/[^0-9]*\(\([0-9]\.\)\{0,4\}[0-9][^.]\).*/\1/p')
GO_STATIC_PACKAGES = $(GO_PROJECT)/cmd/provider $(GO_PROJECT)/cmd/generator
GO_LDFLAGS += -X $(GO_PROJECT)/internal/version.Version=$(VERSION)
GO_SUBDIRS += cmd internal apis
Expand Down
79 changes: 34 additions & 45 deletions internal/clients/keycloak.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,24 @@ const (
// https://registry.terraform.io/providers/mrparkers/keycloak/latest/docs#argument-reference
// https://registry.terraform.io/providers/mrparkers/keycloak/latest/docs#example-usage-client-credentials-grant
// https://registry.terraform.io/providers/mrparkers/keycloak/latest/docs#example-usage-password-grant
const (
usernameKey = "username"
passwordKey = "password"
clientIDKey = "client_id"
clientSecretKey = "client_secret"
urlKey = "url"
realmKey = "realm"
basePathKey = "base_path"
additionalHeadersKey = "additional_headers"
rootCaCertificateKey = "root_ca_certificate"
)

var requiredKeycloakConfigKeys = []string{
"client_id",
"url",
}

var optionalKeycloakConfigKeys = []string{
"client_secret",
"username",
"password",
"realm",
"initial_login",
"client_timeout",
"tls_insecure_skip_verify",
"root_ca_certificate",
"base_path",
"additional_headers",
}

// TerraformSetupBuilder builds Terraform a terraform.SetupFn function which
// returns Terraform provider setup configuration
Expand Down Expand Up @@ -80,42 +87,24 @@ func TerraformSetupBuilder(version, providerSource, providerVersion string) terr

// set provider configuration
ps.Configuration = map[string]any{}
// username
if v, ok := creds[usernameKey]; ok {
ps.Configuration[usernameKey] = v
}
// password
if v, ok := creds[passwordKey]; ok {
ps.Configuration[passwordKey] = v
}
// client
if v, ok := creds[clientIDKey]; ok {
ps.Configuration[clientIDKey] = v
// Iterate over the requiredKeycloakConfigKeys, they must be set
for _, key := range requiredKeycloakConfigKeys {
if value, ok := creds[key]; ok {
if !ok {
// Return an error if a required key is missing
return ps, errors.Errorf("required Keycloak configuration key '%s' is missing", key)
}
ps.Configuration[key] = value
}
}
// secret
if v, ok := creds[clientSecretKey]; ok {
ps.Configuration[clientSecretKey] = v
}
// url
if v, ok := creds[urlKey]; ok {
ps.Configuration[urlKey] = v
}
// realm
if v, ok := creds[realmKey]; ok {
ps.Configuration[realmKey] = v
}
// basepath
if v, ok := creds[basePathKey]; ok {
ps.Configuration[basePathKey] = v
}
// additional headers
if v, ok := creds[additionalHeadersKey]; ok {
ps.Configuration[additionalHeadersKey] = v
}
// root ca certificate
if v, ok := creds[rootCaCertificateKey]; ok {
ps.Configuration[rootCaCertificateKey] = v

// Iterate over the optionalKeycloakConfigKeys, they can be set and do not have to be in the creds map
for _, key := range optionalKeycloakConfigKeys {
if value, ok := creds[key]; ok {
ps.Configuration[key] = value
}
}

return ps, nil
}
}

0 comments on commit 0cb44a4

Please sign in to comment.