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

feat(config): Add missing configs and refactor config logic #19

Merged
merged 2 commits into from
Dec 13, 2023
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
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
}
}
Loading