Skip to content

Commit 4751b9c

Browse files
committed
Added X-Openrun-Rbac-Enabled header to indicate whether RBAC is enabled
1 parent 8d0f6c7 commit 4751b9c

File tree

5 files changed

+40
-29
lines changed

5 files changed

+40
-29
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ This project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Support for passing `X-Openrun-Rbac-Enabled` header to the proxied downstream service. Value is true if RBAC is enabled for app, false otherwise.
13+
1014
## [0.15.9] - 2025-10-22
1115

1216
### Added

internal/app/setup.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,7 @@ func (a *App) addProxyConfig(count int, router *chi.Mux, proxyDef *starlarkstruc
860860
}
861861
}
862862

863+
// Add X-Openrun- headers to request
863864
customPerms := make([]string, 0)
864865
if a.rbacApi != nil {
865866
customPerms, err = a.rbacApi.GetCustomPermissions(r.Context())
@@ -875,6 +876,11 @@ func (a *App) addProxyConfig(count int, router *chi.Mux, proxyDef *starlarkstruc
875876
}
876877
}
877878
r.Header.Set(types.OPENRUN_HEADER_USER, userId)
879+
appRBACEnabled := false
880+
if a.rbacApi != nil {
881+
appRBACEnabled = a.rbacApi.IsAppRBACEnabled(r.Context())
882+
}
883+
r.Header.Set(types.OPENRUN_HEADER_APP_RBAC_ENABLED, strconv.FormatBool(appRBACEnabled))
878884

879885
// Set the response headers
880886
for key, value := range responseHeaders {

internal/app/tests/proxy_test.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ func (t *testRBAC) GetCustomPermissions(ctx context.Context) ([]string, error) {
3232
return t.perms, nil
3333
}
3434

35+
func (t *testRBAC) IsAppRBACEnabled(ctx context.Context) bool {
36+
return true
37+
}
3538
func TestProxyBasics(t *testing.T) {
3639
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3740
if r.URL.Path != "/abc" {
@@ -676,9 +679,11 @@ func TestProxyUserAndPermsHeaders(t *testing.T) {
676679
// Test that X-Openrun-User and X-Openrun-Perms headers are passed to proxied endpoint
677680
var receivedUser string
678681
var receivedPerms string
682+
var receivedRBACEnabled string
679683
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
680684
receivedUser = r.Header.Get("X-Openrun-User")
681685
receivedPerms = r.Header.Get("X-Openrun-Perms")
686+
receivedRBACEnabled = r.Header.Get("X-Openrun-Rbac-Enabled")
682687
io.WriteString(w, "test contents") //nolint:errcheck
683688
}))
684689

@@ -694,19 +699,6 @@ permissions=[
694699
)`, testServer.URL),
695700
}
696701

697-
/*
698-
// Create custom authorizer and perms func
699-
_ := func(ctx context.Context, permissions []string) (bool, error) {
700-
// Always allow
701-
return true, nil
702-
}
703-
704-
_ := func(ctx context.Context) ([]string, error) {
705-
// Return custom permissions
706-
return []string{"read:data", "write:data", "admin"}, nil
707-
}
708-
*/
709-
710702
a, _, err := CreateTestAppAuthorizer(logger, fileData, []string{"proxy.in"},
711703
[]types.Permission{
712704
{Plugin: "proxy.in", Method: "config"},
@@ -728,6 +720,7 @@ permissions=[
728720
// Verify the headers were passed to the proxied endpoint
729721
testutil.AssertEqualsString(t, "X-Openrun-User", types.ANONYMOUS_USER, receivedUser)
730722
testutil.AssertEqualsString(t, "X-Openrun-Perms", "read:data,write:data,admin", receivedPerms)
723+
testutil.AssertEqualsString(t, "X-Openrun-Rbac-Enabled", "true", receivedRBACEnabled)
731724
}
732725

733726
func TestProxyUserHeaderWithAuthentication(t *testing.T) {
@@ -752,19 +745,6 @@ permissions=[
752745
)`, testServer.URL),
753746
}
754747

755-
/*
756-
// Create custom authorizer that sets a user in context
757-
authorizer := func(ctx context.Context, permissions []string) (bool, error) {
758-
// Always allow
759-
return true, nil
760-
}
761-
762-
customPermsFunc := func(ctx context.Context) ([]string, error) {
763-
// Return empty custom permissions
764-
return []string{}, nil
765-
}
766-
*/
767-
768748
a, _, err := CreateTestAppAuthorizer(logger, fileData, []string{"proxy.in"},
769749
[]types.Permission{
770750
{Plugin: "proxy.in", Method: "config"},

internal/rbac/rbac_api.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package rbac
55

66
import (
77
"context"
8+
"strings"
89

910
"github.com/openrundev/openrun/internal/types"
1011
)
@@ -46,10 +47,29 @@ func (h *RBACManager) GetCustomPermissions(ctx context.Context) ([]string, error
4647
return h.GetCustomPermissionsInt(userId, appPathDomain, appAuth, groups)
4748
}
4849

50+
// IsAppRBACEnabled checks if the RBAC is enabled for the current app
51+
func (h *RBACManager) IsAppRBACEnabled(ctx context.Context) bool {
52+
h.mu.RLock()
53+
defer h.mu.RUnlock()
54+
if !h.RbacConfig.Enabled {
55+
// rbac is not enabled at the config level
56+
return false
57+
}
58+
59+
appAuth := string(ctx.Value(types.APP_AUTH).(types.AppAuthnType))
60+
if !strings.HasPrefix(appAuth, RBAC_AUTH_PREFIX) {
61+
// app auth does not have rbac enabled
62+
return false
63+
}
64+
65+
return true
66+
}
67+
4968
type RBACAPI interface {
5069
AuthorizeAny(ctx context.Context, permissions []string) (bool, error)
5170
Authorize(ctx context.Context, permission types.RBACPermission, isAppLevelPermission bool) (bool, error)
5271
GetCustomPermissions(ctx context.Context) ([]string, error)
72+
IsAppRBACEnabled(ctx context.Context) bool
5373
}
5474

5575
var _ RBACAPI = (*RBACManager)(nil)

internal/types/types.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,8 @@ const (
744744

745745
const (
746746
// OpenRun headers are used to pass information to the downstream service
747-
OPENRUN_HEADER_PREFIX = "X-Openrun-"
748-
OPENRUN_HEADER_USER = OPENRUN_HEADER_PREFIX + "User"
749-
OPENRUN_HEADER_PERMS = OPENRUN_HEADER_PREFIX + "Perms"
747+
OPENRUN_HEADER_PREFIX = "X-Openrun-"
748+
OPENRUN_HEADER_USER = OPENRUN_HEADER_PREFIX + "User"
749+
OPENRUN_HEADER_PERMS = OPENRUN_HEADER_PREFIX + "Perms"
750+
OPENRUN_HEADER_APP_RBAC_ENABLED = OPENRUN_HEADER_PREFIX + "Rbac-Enabled"
750751
)

0 commit comments

Comments
 (0)