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
32 changes: 18 additions & 14 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
RbacCACert string
CandlepinAddress string
CandlepinCert string
CandlepinKey string
Expand Down Expand Up @@ -188,7 +190,10 @@ func initTopicsFromEnv() {

func initServicesFromEnv() {
CoreCfg.VmaasAddress = Getenv("VMAAS_ADDRESS", CoreCfg.VmaasAddress)
CoreCfg.RbacAddress = Getenv("RBAC_ADDRESS", CoreCfg.RbacAddress)
if addr := Getenv("RBAC_ADDRESS", CoreCfg.RbacAddress); addr != CoreCfg.RbacAddress {
// v2 auth metadata describes the Clowder endpoint only; never send its bearer or CA to an override
CoreCfg.RbacAddress, CoreCfg.RbacAuthenticated, CoreCfg.RbacCACert = addr, false, ""
}
CoreCfg.CandlepinAddress = Getenv("CANDLEPIN_ADDRESS", CoreCfg.CandlepinAddress)
CoreCfg.CandlepinCert = Getenv("CANDLEPIN_CERT", CoreCfg.CandlepinCert)
CoreCfg.CandlepinKey = Getenv("CANDLEPIN_KEY", CoreCfg.CandlepinKey)
Expand Down Expand Up @@ -276,16 +281,22 @@ 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
// initRbacFromClowder reads RBAC from the Clowder V2 dependency endpoint only.
// A missing endpoint leaves RbacAddress empty; the manager fails at startup unless RBAC_ADDRESS is set.
func initRbacFromClowder() {
endpoint, ok := clowder.GetV2DependencyEndpoint("rbac", "service")
if !ok || endpoint.Uri == "" {
return
}
CoreCfg.RbacAddress = endpoint.Uri
CoreCfg.RbacAuthenticated = endpoint.Authenticated
if endpoint.CaCertificate != nil {
CoreCfg.RbacCACert = *endpoint.CaCertificate
}
return ""
}

func initServicesFromClowder() {
initRbacFromClowder()
webappName := "webapp-service"
if PodConfig.GetBool("use_vmaas_go", true) {
webappName = "webapp-go"
Expand All @@ -296,13 +307,6 @@ 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":
Expand Down
128 changes: 65 additions & 63 deletions base/utils/config_clowder_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,91 +8,93 @@ import (
"github.com/stretchr/testify/assert"
)

func TestResolveRbacV2Address(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())
})
const testRbacURI = "https://rbac-service.svc:8443"
const testRbacApp = "rbac"

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())
})
func TestInitRbacFromClowder(t *testing.T) {
origV2 := clowder.DependencyEndpointsV2
origCfg := CoreCfg
defer func() {
clowder.DependencyEndpointsV2 = origV2
CoreCfg = origCfg
}()

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())
})
run := func(v2 map[string]map[string]clowder.DependencyEndpointV2) {
CoreCfg.RbacAddress, CoreCfg.RbacAuthenticated, CoreCfg.RbacCACert = "", false, ""
clowder.DependencyEndpointsV2 = v2
initRbacFromClowder()
}

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 unauthenticated", func(t *testing.T) {
run(map[string]map[string]clowder.DependencyEndpointV2{
testRbacApp: {"service": {Uri: testRbacURI}},
})
assert.Equal(t, testRbacURI, CoreCfg.RbacAddress)
assert.False(t, CoreCfg.RbacAuthenticated)
assert.Equal(t, "", CoreCfg.RbacCACert)
})

t.Run("V2 with CA certificate", func(t *testing.T) {
t.Run("V2 authenticated 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())
run(map[string]map[string]clowder.DependencyEndpointV2{
testRbacApp: {"service": {Uri: "https://ewgw.example:443", Authenticated: true, CaCertificate: &caPath}},
})
assert.Equal(t, "https://ewgw.example:443", CoreCfg.RbacAddress)
assert.True(t, CoreCfg.RbacAuthenticated)
assert.Equal(t, caPath, CoreCfg.RbacCACert)
})

missing := map[string]map[string]map[string]clowder.DependencyEndpointV2{
"nil map": nil,
"empty URI": {testRbacApp: {"service": {Uri: ""}}},
"wrong app": {"other-app": {"service": {Uri: "https://other.svc:8443"}}},
"wrong endpoint": {testRbacApp: {"wrong-deploy": {Uri: "https://rbac.svc:8443"}}},
}
for name, v2 := range missing {
t.Run("V2 missing: "+name, func(t *testing.T) {
run(v2)
assert.Equal(t, "", CoreCfg.RbacAddress)
assert.False(t, CoreCfg.RbacAuthenticated)
})
}
}

func TestRbacAddressEnvOverride(t *testing.T) {
origAddr := CoreCfg.RbacAddress
defer func() { CoreCfg.RbacAddress = origAddr }()
// setAuthenticatedV2Rbac simulates an authenticated V2-resolved RBAC endpoint.
func setAuthenticatedV2Rbac(t *testing.T) {
origCfg := CoreCfg
t.Cleanup(func() { CoreCfg = origCfg })
CoreCfg.RbacAddress, CoreCfg.RbacAuthenticated, CoreCfg.RbacCACert = testRbacURI, true, "/tmp/ca.crt"
}

// Simulate V2-resolved address
CoreCfg.RbacAddress = "https://rbac-service.svc:8443"
func TestRbacAddressEnvOverrideDropsV2Auth(t *testing.T) {
setAuthenticatedV2Rbac(t)

// Env var override takes precedence (initServicesFromEnv behavior)
// Env var override takes precedence; the V2 bearer/CA must not follow it
t.Setenv("RBAC_ADDRESS", "http://localhost:8080")
initServicesFromEnv()
assert.Equal(t, "http://localhost:8080", CoreCfg.RbacAddress)
assert.False(t, CoreCfg.RbacAuthenticated)
assert.Equal(t, "", CoreCfg.RbacCACert)
}

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

// Simulate V2-resolved address
CoreCfg.RbacAddress = "https://rbac-service.svc:8443"
t.Setenv("RBAC_ADDRESS", testRbacURI)
initServicesFromEnv()
assert.True(t, CoreCfg.RbacAuthenticated)
assert.Equal(t, "/tmp/ca.crt", CoreCfg.RbacCACert)
}

func TestRbacAddressEnvPreservesV2WhenUnset(t *testing.T) {
setAuthenticatedV2Rbac(t)

// 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)
assert.Equal(t, testRbacURI, CoreCfg.RbacAddress)
assert.True(t, CoreCfg.RbacAuthenticated)
}
8 changes: 8 additions & 0 deletions base/utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ func closeHTTPResponse(resp *http.Response) {

func CallAPI(client *http.Client, request *http.Request, debugEnabled bool) (*http.Response, error) {
if debugEnabled {
// redact in place: DumpRequestOut restores the body of the request it is given, so a clone would lose it
auth := request.Header.Get("Authorization")
if auth != "" {
request.Header.Set("Authorization", "REDACTED")
}
dump, err := httputil.DumpRequestOut(request, true)
if auth != "" {
request.Header.Set("Authorization", auth)
}
if err != nil {
return nil, err
}
Expand Down
29 changes: 29 additions & 0 deletions base/utils/http_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package utils

import (
"bytes"
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"

log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -139,3 +142,29 @@ func TestCloseHTTPResponseLimitsDrain(t *testing.T) {
assert.True(t, body.closed.Load())
assert.Equal(t, int64(maxHTTPResponseDrain), body.readBytes.Load())
}

func TestCallAPIDebugRedactsAuthorization(t *testing.T) {
var gotAuth string
srv := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
gotAuth = r.Header.Get("Authorization")
}))
defer srv.Close()

var logged bytes.Buffer
origOut, origLevel := log.StandardLogger().Out, log.GetLevel()
log.SetOutput(&logged)
log.SetLevel(log.DebugLevel)
defer func() {
log.SetOutput(origOut)
log.SetLevel(origLevel)
}()

req, _ := http.NewRequest(http.MethodGet, srv.URL, nil)
req.Header.Set("Authorization", "Bearer secret-token")
resp, err := CallAPI(srv.Client(), req, true)
assert.NoError(t, err)
resp.Body.Close()

assert.NotContains(t, logged.String(), "secret-token")
assert.Equal(t, "Bearer secret-token", gotAuth) // real request still carries the token
}
17 changes: 10 additions & 7 deletions conf/cdappconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,18 @@
"hostname": "platform",
"name": "webapp-go",
"port": 9001
},
{
"apiPath": "api/patch/v3",
"app": "rbac",
"hostname": "platform",
"name": "",
"port": 9001
}
],
"dependencyEndpoints": {
"v2": {
"rbac": {
"service": {
"uri": "http://platform:9001",
"authenticated": false
}
}
}
},
"privateEndpoints": [
{
"app": "patchman",
Expand Down
61 changes: 51 additions & 10 deletions manager/middlewares/kessel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"strconv"
"strings"
"sync"
"time"

"github.com/gin-gonic/gin"
Expand All @@ -18,19 +19,59 @@ import (
kesselRbacV2 "github.com/project-kessel/kessel-sdk-go/kessel/rbac/v2"
)

var credentials = auth.NewOAuth2ClientCredentials(
utils.CoreCfg.KesselAuthClientID,
utils.CoreCfg.KesselAuthClientSecret,
utils.CoreCfg.KesselAuthOIDCIssuer,
var (
credentialsMu sync.Mutex
credentials *auth.OAuth2ClientCredentials
// bounds OIDC discovery and token calls; gin.Context is never cancelled, and discovery runs under credentialsMu
oidcHTTPClient = &http.Client{Timeout: 10 * time.Second}
)

func setupClient() (kesselv2.KesselInventoryServiceClient, *grpc.ClientConn, error) {
clientBuilder := kesselv2.NewClientBuilder(utils.CoreCfg.KesselURL)
if utils.CoreCfg.KesselAuthEnabled {
clientBuilder = clientBuilder.OAuth2ClientAuthenticated(&credentials, nil)
// getCredentials lazily builds the single OAuth2 client shared by Kessel gRPC and authenticated RBAC calls.
// Failures are not cached so a transient OIDC discovery error is retried on the next call.
func getCredentials(ctx context.Context) (*auth.OAuth2ClientCredentials, error) {
credentialsMu.Lock()
defer credentialsMu.Unlock()
if credentials != nil {
return credentials, nil
}
cfg := &utils.CoreCfg
if cfg.KesselAuthOIDCIssuer == "" || cfg.KesselAuthClientID == "" || cfg.KesselAuthClientSecret == "" {
return nil, errors.New(
"KESSEL_AUTH_OIDC_ISSUER, KESSEL_AUTH_CLIENT_ID and KESSEL_AUTH_CLIENT_SECRET must be set")
}
discovery, err := auth.FetchOIDCDiscovery(
ctx, cfg.KesselAuthOIDCIssuer, auth.FetchOIDCDiscoveryOptions{HttpClient: oidcHTTPClient})
if err != nil {
return nil, errors.Wrap(err, "OIDC discovery failed")
}
creds := auth.NewOAuth2ClientCredentials(cfg.KesselAuthClientID, cfg.KesselAuthClientSecret, discovery.TokenEndpoint)
credentials = &creds
return credentials, nil
}

// getBearerToken returns an SSO service-account access token; the SDK caches and refreshes it.
func getBearerToken(ctx context.Context) (string, error) {
creds, err := getCredentials(ctx)
if err != nil {
return "", err
}
if utils.CoreCfg.KesselInsecure { // insecure TLS
token, err := creds.GetToken(ctx, auth.GetTokenOptions{HttpClient: oidcHTTPClient})
if err != nil {
return "", errors.Wrap(err, "failed to get access token")
}
return token.AccessToken, nil
}

func setupClient(ctx context.Context) (kesselv2.KesselInventoryServiceClient, *grpc.ClientConn, error) {
clientBuilder := kesselv2.NewClientBuilder(utils.CoreCfg.KesselURL)
if utils.CoreCfg.KesselInsecure { // insecure TLS, the SDK drops call credentials here
clientBuilder = clientBuilder.Insecure()
} else if utils.CoreCfg.KesselAuthEnabled {
creds, err := getCredentials(ctx)
if err != nil {
return nil, nil, err
}
clientBuilder = clientBuilder.OAuth2ClientAuthenticated(creds, nil)
}
return clientBuilder.Build()
}
Expand Down Expand Up @@ -91,7 +132,7 @@ func useStreamedListObjects(
}

func hasPermissionKessel(c *gin.Context) {
client, conn, err := setupClient()
client, conn, err := setupClient(c)
if err != nil {
utils.LogError("err", err, "failed to setup Kessel service client")
c.AbortWithStatusJSON(http.StatusInternalServerError, utils.ErrorResponse{
Expand Down
Loading
Loading