Skip to content
Closed
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
4 changes: 2 additions & 2 deletions pkg/connectors/microcks_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ func (c *microcksClient) GetKeycloakURL() (string, error) {

// Retrieve auth server url and realm name.
enabled := configResp["enabled"].(bool)

Copy link
Copy Markdown

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 enabled key is missing or "enabled": null in the response, this line panics; exactly the class of bug this PR is meant to fix.

Suggested change
enabled := configResp["enabled"].(bool)
enabled, ok := configResp["enabled"].(bool)
if !ok || !enabled {
return "null", nil
}

authServerURL := configResp["auth-server-url"].(string)
realmName := configResp["realm"].(string)

// Return a proper URL or 'null' if Keycloak is disables.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.

Expand Down
62 changes: 62 additions & 0 deletions pkg/connectors/microcks_client_test.go

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests cover enabled/disabled but not the missing-enabled-key case that the comma-ok fix addresses.

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)
}
}
Loading