Skip to content
Open
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
30 changes: 10 additions & 20 deletions base/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type coreConfig struct {
// services
VmaasAddress string
RbacAddress string
RbacAuthenticated bool
RbacCACertificate string
CandlepinAddress string
CandlepinCert string
CandlepinKey string
Expand All @@ -99,7 +101,6 @@ type coreConfig struct {
KesselAuthClientID string
KesselAuthClientSecret string
KesselAuthOIDCIssuer string
RbacURL string

// prometheus pushgateway
PrometheusPushGateway string
Expand Down Expand Up @@ -276,15 +277,6 @@ func initKafkaFromClowder() {
type Endpoint clowder.DependencyEndpoint
type PrivateEndpoint clowder.PrivateDependencyEndpoint

// resolveRbacV2Address returns the RBAC address from V2 dependency endpoints,
// or empty string if V2 is not available.
func resolveRbacV2Address() string {
if endpoint, ok := clowder.GetV2DependencyEndpoint("rbac", "service"); ok && endpoint.Uri != "" {
return endpoint.Uri
}
return ""
}

func initServicesFromClowder() {
webappName := "webapp-service"
if PodConfig.GetBool("use_vmaas_go", true) {
Expand All @@ -296,20 +288,19 @@ func initServicesFromClowder() {
if strings.Contains(endpoint.Name, webappName) {
CoreCfg.VmaasAddress = (*Endpoint)(&endpoint).buildURL()
}
case "rbac":
// Prefer V2 dependency endpoint; fall back to V1 flat list.
if addr := resolveRbacV2Address(); addr != "" {
CoreCfg.RbacAddress = addr
} else {
CoreCfg.RbacAddress = (*Endpoint)(&endpoint).buildURL()
}
case "rbac-service":
CoreCfg.RbacURL = (*Endpoint)(&endpoint).buildURL()
case "content-sources-backend":
CoreCfg.ContentSourcesAddress = (*Endpoint)(&endpoint).buildURL()
}
}

if rbac, ok := clowder.GetV2DependencyEndpoint("rbac", "service"); ok {
CoreCfg.RbacAddress = rbac.Uri
CoreCfg.RbacAuthenticated = rbac.Authenticated
if rbac.CaCertificate != nil {
CoreCfg.RbacCACertificate = *rbac.CaCertificate
}
}

for _, e := range clowder.LoadedConfig.PrivateEndpoints {
if e.App == "patchman" {
switch e.Name {
Expand Down Expand Up @@ -386,7 +377,6 @@ func initKesselFromEnv() {
CoreCfg.KesselAuthClientID = Getenv("KESSEL_AUTH_CLIENT_ID", CoreCfg.KesselAuthClientID)
CoreCfg.KesselAuthClientSecret = Getenv("KESSEL_AUTH_CLIENT_SECRET", CoreCfg.KesselAuthClientSecret)
CoreCfg.KesselAuthOIDCIssuer = Getenv("KESSEL_AUTH_OIDC_ISSUER", CoreCfg.KesselAuthOIDCIssuer)
CoreCfg.RbacURL = Getenv("RBAC_URL", CoreCfg.RbacURL)
}

// PrintClowderParams Print Clowder params to export environment variables.
Expand Down
107 changes: 29 additions & 78 deletions base/utils/config_clowder_v2_test.go
Original file line number Diff line number Diff line change
@@ -1,98 +1,49 @@
package utils

import (
"os"
"testing"

clowder "github.com/redhatinsights/app-common-go/pkg/api/v1"
"github.com/stretchr/testify/assert"
)

func TestResolveRbacV2Address(t *testing.T) {
func TestRbacFromClowderV2(t *testing.T) {
origV2 := clowder.DependencyEndpointsV2
defer func() { clowder.DependencyEndpointsV2 = origV2 }()

t.Run("V2 available with URI", func(t *testing.T) {
clowder.DependencyEndpointsV2 = map[string]map[string]clowder.DependencyEndpointV2{
"rbac": {
"service": {Uri: "https://rbac-service.svc:8443", Authenticated: false},
},
}
assert.Equal(t, "https://rbac-service.svc:8443", resolveRbacV2Address())
})

t.Run("V2 nil map", func(t *testing.T) {
clowder.DependencyEndpointsV2 = nil
assert.Equal(t, "", resolveRbacV2Address())
})

t.Run("V2 empty URI", func(t *testing.T) {
clowder.DependencyEndpointsV2 = map[string]map[string]clowder.DependencyEndpointV2{
"rbac": {
"service": {Uri: "", Authenticated: false},
},
}
assert.Equal(t, "", resolveRbacV2Address())
})

t.Run("V2 wrong app key", func(t *testing.T) {
clowder.DependencyEndpointsV2 = map[string]map[string]clowder.DependencyEndpointV2{
"other-app": {
"service": {Uri: "https://other.svc:8443", Authenticated: false},
},
}
assert.Equal(t, "", resolveRbacV2Address())
})

t.Run("V2 wrong deployment key", func(t *testing.T) {
clowder.DependencyEndpointsV2 = map[string]map[string]clowder.DependencyEndpointV2{
"rbac": {
"wrong-deploy": {Uri: "https://rbac.svc:8443", Authenticated: false},
},
}
assert.Equal(t, "", resolveRbacV2Address())
})

t.Run("V2 with CA certificate", func(t *testing.T) {
caPath := "/tmp/ca.crt"
clowder.DependencyEndpointsV2 = map[string]map[string]clowder.DependencyEndpointV2{
"rbac": {
"service": {
Uri: "https://rbac-service.svc:8443",
Authenticated: false,
CaCertificate: &caPath,
},
},
}
assert.Equal(t, "https://rbac-service.svc:8443", resolveRbacV2Address())
})
origLoaded := clowder.LoadedConfig
origCfg := CoreCfg
if clowder.LoadedConfig == nil {
clowder.LoadedConfig = &clowder.AppConfig{}
}
defer func() {
clowder.DependencyEndpointsV2 = origV2
clowder.LoadedConfig = origLoaded
CoreCfg = origCfg
}()

caPath := "/tmp/ca.crt"
clowder.DependencyEndpointsV2 = map[string]map[string]clowder.DependencyEndpointV2{
"rbac": {"service": {Uri: "https://ewgw:443", Authenticated: true, CaCertificate: &caPath}},
}
CoreCfg.RbacAddress, CoreCfg.RbacAuthenticated, CoreCfg.RbacCACertificate = "", false, ""
initServicesFromClowder()
assert.Equal(t, "https://ewgw:443", CoreCfg.RbacAddress)
assert.True(t, CoreCfg.RbacAuthenticated)
assert.Equal(t, caPath, CoreCfg.RbacCACertificate)

clowder.DependencyEndpointsV2 = map[string]map[string]clowder.DependencyEndpointV2{
"rbac": {"service": {Uri: "http://rbac-service.svc:8000", Authenticated: false}},
}
CoreCfg.RbacAddress, CoreCfg.RbacAuthenticated, CoreCfg.RbacCACertificate = "", true, caPath
initServicesFromClowder()
assert.Equal(t, "http://rbac-service.svc:8000", CoreCfg.RbacAddress)
assert.False(t, CoreCfg.RbacAuthenticated)
}

func TestRbacAddressEnvOverride(t *testing.T) {
origAddr := CoreCfg.RbacAddress
defer func() { CoreCfg.RbacAddress = origAddr }()

// Simulate V2-resolved address
CoreCfg.RbacAddress = "https://rbac-service.svc:8443"

// Env var override takes precedence (initServicesFromEnv behavior)
t.Setenv("RBAC_ADDRESS", "http://localhost:8080")
initServicesFromEnv()
assert.Equal(t, "http://localhost:8080", CoreCfg.RbacAddress)
}

func TestRbacAddressEnvPreservesV2WhenUnset(t *testing.T) {
origAddr := CoreCfg.RbacAddress
defer func() { CoreCfg.RbacAddress = origAddr }()

// Simulate V2-resolved address
CoreCfg.RbacAddress = "https://rbac-service.svc:8443"

// No env override: Getenv returns current value as default.
// t.Setenv registers cleanup to restore original value after test;
// os.Unsetenv then clears it so LookupEnv returns ok=false.
t.Setenv("RBAC_ADDRESS", "")
os.Unsetenv("RBAC_ADDRESS")
initServicesFromEnv()
assert.Equal(t, "https://rbac-service.svc:8443", CoreCfg.RbacAddress)
}
6 changes: 5 additions & 1 deletion base/utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net/http"
"net/http/httputil"
"regexp"
"time"

"github.com/lestrrat-go/backoff/v2"
Expand All @@ -15,6 +16,8 @@ import (
_ "net/http/pprof" //nolint:gosec
)

var authHeaderRegexp = regexp.MustCompile(`(?mi)^Authorization:.*`)

func HTTPCallRetry(httpCallFun func() (outputDataPtr interface{}, resp *http.Response, err error),
exponentialRetry bool, maxRetries int, codesToRetry ...int) (outputDataPtr interface{}, err error) {
ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -69,7 +72,8 @@ func CallAPI(client *http.Client, request *http.Request, debugEnabled bool) (*ht
if err != nil {
return nil, err
}
LogDebug("dump", fmt.Sprintf("\n%s\n", string(dump)), "http call")
LogDebug("dump", fmt.Sprintf("\n%s\n", authHeaderRegexp.ReplaceAllString(string(dump), "Authorization: [redacted]")),
"http call")
}

resp, err := client.Do(request) // #nosec G704
Expand Down
17 changes: 10 additions & 7 deletions conf/cdappconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@
"hostname": "platform",
"name": "webapp-go",
"port": 9001
},
{
"apiPath": "api/patch/v3",
"app": "rbac",
"hostname": "platform",
"name": "",
"port": 9001
}
],
"privateEndpoints": [
Expand Down Expand Up @@ -110,5 +103,15 @@
"secretAccessKey": ""
},
"type": "null"
},
"dependencyEndpoints": {
"v2": {
"rbac": {
"service": {
"uri": "http://platform:9001",
"authenticated": false
}
}
}
}
}
1 change: 0 additions & 1 deletion conf/test.env
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ POD_CONFIG=label=upload;vmaas_call_max_retries=100;template_change_eval=false;up
KESSEL_URL=platform:9005
KESSEL_INSECURE=true
KESSEL_AUTH_ENABLED=false
RBAC_URL=http://platform:9001/api/rbac
10 changes: 10 additions & 0 deletions manager/middlewares/kessel.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ var credentials = auth.NewOAuth2ClientCredentials(
utils.CoreCfg.KesselAuthOIDCIssuer,
)

// requireAuthConfig fails when the OAuth settings needed for an authenticated call are missing.
func requireAuthConfig() error {
if utils.CoreCfg.KesselAuthClientID == "" || utils.CoreCfg.KesselAuthClientSecret == "" ||
utils.CoreCfg.KesselAuthOIDCIssuer == "" {
return errors.New("authenticated endpoint requires KESSEL_AUTH_CLIENT_ID, KESSEL_AUTH_CLIENT_SECRET " +
"and KESSEL_AUTH_OIDC_ISSUER")
}
return nil
}

func setupClient() (kesselv2.KesselInventoryServiceClient, *grpc.ClientConn, error) {
clientBuilder := kesselv2.NewClientBuilder(utils.CoreCfg.KesselURL)
if utils.CoreCfg.KesselAuthEnabled {
Expand Down
Loading
Loading