-
-
Notifications
You must be signed in to change notification settings - Fork 67
fix: avoid nil interface panic on Keycloak config discovery #272
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -238,11 +238,11 @@ func (c *microcksClient) GetKeycloakURL() (string, error) { | |
|
|
||
| // Retrieve auth server url and realm name. | ||
| enabled := configResp["enabled"].(bool) | ||
| authServerURL := configResp["auth-server-url"].(string) | ||
| realmName := configResp["realm"].(string) | ||
|
|
||
| // Return a proper URL or 'null' if Keycloak is disables. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor typo: "disables" to "disabled" |
||
| if enabled { | ||
| authServerURL := configResp["auth-server-url"].(string) | ||
| realmName := configResp["realm"].(string) | ||
| return authServerURL + "/realms/" + realmName + "/", nil | ||
| } | ||
| return "null", nil | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why return the string "null" and not just ""? Is something downstream actually expecting the literal string "null"? A comment here would help a lot. |
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests cover enabled/disabled but not the missing- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package connectors | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestGetKeycloakURL_Disabled(t *testing.T) { | ||
| // Create a mock server that returns keycloak disabled response | ||
| ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.URL.Path == "/api/keycloak/config" { | ||
| w.Header().Set("Content-Type", "application/json") | ||
| w.WriteHeader(http.StatusOK) | ||
| w.Write([]byte(`{"enabled": false}`)) | ||
| return | ||
| } | ||
| w.WriteHeader(http.StatusNotFound) | ||
| })) | ||
| defer ts.Close() | ||
|
|
||
| // Initialize the MicrocksClient with the mock server URL | ||
| client := NewMicrocksClient(ts.URL) | ||
|
|
||
| // Call GetKeycloakURL and check for panic/error | ||
| url, err := client.GetKeycloakURL() | ||
| if err != nil { | ||
| t.Fatalf("Expected no error, got %v", err) | ||
| } | ||
|
|
||
| if url != "null" { | ||
| t.Errorf("Expected 'null', got '%s'", url) | ||
| } | ||
| } | ||
|
|
||
| func TestGetKeycloakURL_Enabled(t *testing.T) { | ||
| // Create a mock server that returns keycloak enabled response | ||
| ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.URL.Path == "/api/keycloak/config" { | ||
| w.Header().Set("Content-Type", "application/json") | ||
| w.WriteHeader(http.StatusOK) | ||
| w.Write([]byte(`{"enabled": true, "auth-server-url": "http://keycloak:8080/auth", "realm": "microcks"}`)) | ||
| return | ||
| } | ||
| w.WriteHeader(http.StatusNotFound) | ||
| })) | ||
| defer ts.Close() | ||
|
|
||
| // Initialize the MicrocksClient with the mock server URL | ||
| client := NewMicrocksClient(ts.URL) | ||
|
|
||
| // Call GetKeycloakURL and check for panic/error | ||
| url, err := client.GetKeycloakURL() | ||
| if err != nil { | ||
| t.Fatalf("Expected no error, got %v", err) | ||
| } | ||
|
|
||
| expected := "http://keycloak:8080/auth/realms/microcks/" | ||
| if url != expected { | ||
| t.Errorf("Expected '%s', got '%s'", expected, url) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still an unchecked type assertion. If the
enabledkey is missing or"enabled": nullin the response, this line panics; exactly the class of bug this PR is meant to fix.