KeycloakClient strips realm path when realm URL lacks trailing slash

Offen Anfängerfreundlich
#606 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
88/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
go
Bereich
authentication

Rechercherichtung

Beginne mit pkg/connectors/keycloak_client.go, insbesondere mit NewKeycloakClientWithContext, und prüfe anschließend pkg/connectors/keycloak_client_test.go. Führe die Connector-Tests aus und überprüfe, dass eine Realm-URL ohne abschließenden Schrägstrich ihren Realm-Pfad in Token- und OIDC-Konfigurationsanfragen beibehält; die Arbeit ist abgeschlossen, wenn der Regressionstest für diese Eingabe besteht.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Describe the bug

When initializing a KeycloakClient using connectors.NewKeycloakClient or connectors.NewKeycloakClientWithContext, if the provided realmURL does not include a trailing slash (e.g., http://keycloak.example.com/realms/microcks), subsequent requests to obtain tokens or fetch OIDC configuration fail with HTTP 404.

This happens because relative endpoints (protocol/openid-connect/token and .well-known/openid-configuration) are resolved against kc.BaseURL using c.BaseURL.ResolveReference(rel).

Under RFC 3986 relative URL resolution rules, resolving a relative path against a base URL without a trailing slash strips the final path segment (microcks).

As a result, the request resolves to:

http://keycloak.example.com/realms/protocol/openid-connect/token

instead of:

http://keycloak.example.com/realms/microcks/protocol/openid-connect/token

In comparison, MicrocksClient in pkg/connectors/microcks_client.go defensively normalizes its incoming base URL, but KeycloakClient does not.

Expected behavior

NewKeycloakClient and NewKeycloakClientWithContext should ensure that kc.BaseURL.Path ends with a trailing /, so relative endpoints always resolve under the specified realm.

For example:

http://keycloak.example.com/realms/microcks/protocol/openid-connect/token

This should work regardless of whether the input URL includes a trailing slash.

Actual behavior

The realm name is stripped from the URL path.

  • Configured URL: http://keycloak.example.com/realms/microcks
  • Resolved Token URL: http://keycloak.example.com/realms/protocol/openid-connect/token (HTTP 404)
How to Reproduce?
  1. Initialize a KeycloakClient with a realm URL that omits the trailing slash:

    kc, err := connectors.NewKeycloakClientWithContext(
        context.Background(),
        "http://keycloak.example.com/realms/microcks",
        "client-id",
        "client-secret",
    )
    
    
  2. Call kc.ConnectAndGetToken() or kc.GetOIDCConfig().

  3. Inspect the resolved request URL. The realm segment /microcks will be stripped from the path.

Microcks version or git rev

master

Install method (docker-compose, helm chart, operator, docker-desktop extension,...)

No response

Additional information

This can be addressed in pkg/connectors/keycloak_client.go, inside NewKeycloakClientWithContext, by ensuring that the URL path always ends with a trailing slash:

if !strings.HasSuffix(u.Path, "/") {
    u.Path += "/"
}

Along with a unit test in pkg/connectors/keycloak_client_test.go covering a realm URL with a subpath.

Vorherrschende Sprache
Go
Sterne
59
Forks
74
Ø Merge
4 T. 5 Std.
Gemergte PRs (30 T.)
15

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus microcks/microcks-cli

Alle Issues in microcks/microcks-cli

Ähnliche Issues

Weitere Issues zu Go

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.