Skip to content

Commit 65bc792

Browse files
au-eefrpicardMarkus Renschler
authored
feat: add admin url variable to separate authn and admin api urls (forked and added reviewer suggestions) (#1305)
* feat: add admin url first class citizen variable to send authentication and admin api calls to the correct URL Signed-off-by: frpicard <[email protected]> * fix: applied @sschu's review comments Signed-off-by: Markus Renschler <[email protected]> --------- Signed-off-by: frpicard <[email protected]> Signed-off-by: Markus Renschler <[email protected]> Co-authored-by: frpicard <[email protected]> Co-authored-by: Markus Renschler <[email protected]>
1 parent d06f0b9 commit 65bc792

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ The following arguments are supported:
100100

101101
- `client_id` - (Required) The `client_id` for the client that was created in the "Keycloak Setup" section. Use the `admin-cli` client if you are using the password grant. Defaults to the environment variable `KEYCLOAK_CLIENT_ID`.
102102
- `url` - (Required) The URL of the Keycloak instance, before `/auth/admin`. Defaults to the environment variable `KEYCLOAK_URL`.
103+
- `admin_url` - (Optional) The admin URL of the Keycloak instance if different from the base URL, before `/auth/admin`. Defaults to the environment variable `KEYCLOAK_ADMIN_URL`.
103104
- `client_secret` - (Optional) The secret for the client used by the provider for authentication via the client credentials grant. This can be found or changed using the "Credentials" tab in the client settings. Defaults to the environment variable `KEYCLOAK_CLIENT_SECRET`. This attribute is required when using the client credentials grant, and cannot be set when using the password grant.
104105
- `username` - (Optional) The username of the user used by the provider for authentication via the password grant. Defaults to the environment variable `KEYCLOAK_USER`. This attribute is required when using the password grant, and cannot be set when using the client credentials grant.
105106
- `password` - (Optional) The password of the user used by the provider for authentication via the password grant. Defaults to the environment variable `KEYCLOAK_PASSWORD`. This attribute is required when using the password grant, and cannot be set when using the client credentials grant.

keycloak/keycloak_client.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
type KeycloakClient struct {
3131
baseUrl string
32+
authUrl string
3233
realm string
3334
clientCredentials *ClientCredentials
3435
httpClient *http.Client
@@ -66,7 +67,7 @@ var redHatSSO7VersionMap = map[int]string{
6667
4: "9.0.17",
6768
}
6869

69-
func NewKeycloakClient(ctx context.Context, url, basePath, clientId, clientSecret, realm, username, password, jwtSigningAlg, jwtSigningKey string, initialLogin bool, clientTimeout int, caCert string, tlsInsecureSkipVerify bool, userAgent string, redHatSSO bool, additionalHeaders map[string]string) (*KeycloakClient, error) {
70+
func NewKeycloakClient(ctx context.Context, url, basePath, adminUrl, clientId, clientSecret, realm, username, password, jwtSigningAlg, jwtSigningKey string, initialLogin bool, clientTimeout int, caCert string, tlsInsecureSkipVerify bool, userAgent string, redHatSSO bool, additionalHeaders map[string]string) (*KeycloakClient, error) {
7071
clientCredentials := &ClientCredentials{
7172
ClientId: clientId,
7273
ClientSecret: clientSecret,
@@ -93,8 +94,15 @@ func NewKeycloakClient(ctx context.Context, url, basePath, clientId, clientSecre
9394
return nil, fmt.Errorf("failed to create http client: %v", err)
9495
}
9596

97+
authUrl := url + basePath
98+
baseUrl := authUrl
99+
if adminUrl != "" {
100+
baseUrl = adminUrl + basePath
101+
}
102+
96103
keycloakClient := KeycloakClient{
97-
baseUrl: url + basePath,
104+
baseUrl: baseUrl,
105+
authUrl: authUrl,
98106
clientCredentials: clientCredentials,
99107
httpClient: httpClient,
100108
initialLogin: initialLogin,
@@ -121,7 +129,7 @@ func NewKeycloakClient(ctx context.Context, url, basePath, clientId, clientSecre
121129
}
122130

123131
func (keycloakClient *KeycloakClient) login(ctx context.Context) error {
124-
accessTokenUrl := fmt.Sprintf(tokenUrl, keycloakClient.baseUrl, keycloakClient.realm)
132+
accessTokenUrl := fmt.Sprintf(tokenUrl, keycloakClient.authUrl, keycloakClient.realm)
125133
accessTokenData, err := keycloakClient.getAuthenticationFormData(ctx, accessTokenUrl)
126134
if err != nil {
127135
return err
@@ -215,7 +223,7 @@ func (keycloakClient *KeycloakClient) login(ctx context.Context) error {
215223
}
216224

217225
func (keycloakClient *KeycloakClient) Refresh(ctx context.Context) error {
218-
refreshTokenUrl := fmt.Sprintf(tokenUrl, keycloakClient.baseUrl, keycloakClient.realm)
226+
refreshTokenUrl := fmt.Sprintf(tokenUrl, keycloakClient.authUrl, keycloakClient.realm)
219227
refreshTokenData, err := keycloakClient.getAuthenticationFormData(ctx, refreshTokenUrl)
220228
if err != nil {
221229
return err

keycloak/keycloak_client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestAccKeycloakApiClientRefresh(t *testing.T) {
4949
t.Fatal("KEYCLOAK_CLIENT_TIMEOUT must be an integer")
5050
}
5151

52-
keycloakClient, err := NewKeycloakClient(ctx, os.Getenv("KEYCLOAK_URL"), "", os.Getenv("KEYCLOAK_CLIENT_ID"), os.Getenv("KEYCLOAK_CLIENT_SECRET"), os.Getenv("KEYCLOAK_REALM"), os.Getenv("KEYCLOAK_USER"), os.Getenv("KEYCLOAK_PASSWORD"), "", "", true, clientTimeout, "", false, "", false, map[string]string{
52+
keycloakClient, err := NewKeycloakClient(ctx, os.Getenv("KEYCLOAK_URL"), "", os.Getenv("KEYCLOAK_ADMIN_URL"), os.Getenv("KEYCLOAK_CLIENT_ID"), os.Getenv("KEYCLOAK_CLIENT_SECRET"), os.Getenv("KEYCLOAK_REALM"), os.Getenv("KEYCLOAK_USER"), os.Getenv("KEYCLOAK_PASSWORD"), "", "", true, clientTimeout, "", false, "", false, map[string]string{
5353
"foo": "bar",
5454
})
5555
if err != nil {

provider/provider.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ func KeycloakProvider(client *keycloak.KeycloakClient) *schema.Provider {
173173
Description: "The base URL of the Keycloak instance, before `/auth`",
174174
DefaultFunc: schema.EnvDefaultFunc("KEYCLOAK_URL", nil),
175175
},
176+
"admin_url": {
177+
Optional: true,
178+
Type: schema.TypeString,
179+
Description: "The admin URL of the Keycloak instance if different from the main URL, before `/auth`",
180+
DefaultFunc: schema.EnvDefaultFunc("KEYCLOAK_ADMIN_URL", nil),
181+
},
176182
"initial_login": {
177183
Optional: true,
178184
Type: schema.TypeBool,
@@ -225,6 +231,7 @@ func KeycloakProvider(client *keycloak.KeycloakClient) *schema.Provider {
225231

226232
url := data.Get("url").(string)
227233
basePath := data.Get("base_path").(string)
234+
adminUrl := data.Get("admin_url").(string)
228235
clientId := data.Get("client_id").(string)
229236
clientSecret := data.Get("client_secret").(string)
230237
username := data.Get("username").(string)
@@ -246,7 +253,7 @@ func KeycloakProvider(client *keycloak.KeycloakClient) *schema.Provider {
246253

247254
userAgent := fmt.Sprintf("HashiCorp Terraform/%s (+https://www.terraform.io) Terraform Plugin SDK/%s", provider.TerraformVersion, meta.SDKVersionString())
248255

249-
keycloakClient, err := keycloak.NewKeycloakClient(ctx, url, basePath, clientId, clientSecret, realm, username, password, jwtSigningAlg, jwtSigningKey, initialLogin, clientTimeout, rootCaCertificate, tlsInsecureSkipVerify, userAgent, redHatSSO, additionalHeaders)
256+
keycloakClient, err := keycloak.NewKeycloakClient(ctx, url, basePath, adminUrl, clientId, clientSecret, realm, username, password, jwtSigningAlg, jwtSigningKey, initialLogin, clientTimeout, rootCaCertificate, tlsInsecureSkipVerify, userAgent, redHatSSO, additionalHeaders)
250257
if err != nil {
251258
diags = append(diags, diag.Diagnostic{
252259
Severity: diag.Error,

provider/provider_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func init() {
5858
}
5959
}
6060

61-
keycloakClient, err = keycloak.NewKeycloakClient(testCtx, os.Getenv("KEYCLOAK_URL"), "", os.Getenv("KEYCLOAK_CLIENT_ID"), os.Getenv("KEYCLOAK_CLIENT_SECRET"), os.Getenv("KEYCLOAK_REALM"), "", "", "", "", true, 120, "", false, userAgent, false, map[string]string{
61+
keycloakClient, err = keycloak.NewKeycloakClient(testCtx, os.Getenv("KEYCLOAK_URL"), "", os.Getenv("KEYCLOAK_ADMIN_URL"), os.Getenv("KEYCLOAK_CLIENT_ID"), os.Getenv("KEYCLOAK_CLIENT_SECRET"), os.Getenv("KEYCLOAK_REALM"), "", "", "", "", true, 120, "", false, userAgent, false, map[string]string{
6262
"foo": "bar",
6363
})
6464
if err != nil {

0 commit comments

Comments
 (0)