Skip to content

Commit d67e50f

Browse files
committed
permissions preflight: kubernetes rbac code modifications
Signed-off-by: Joe Lanford <[email protected]>
1 parent 8223f44 commit d67e50f

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

internal/operator-controller/authorization/internal/kubernetes/pkg/registry/rbac/validation/policy_compact.go

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"reflect"
2121

2222
rbacv1 "k8s.io/api/rbac/v1"
23+
"k8s.io/apimachinery/pkg/util/sets"
2324
)
2425

2526
type simpleResource struct {
@@ -54,6 +55,12 @@ func CompactRules(rules []rbacv1.PolicyRule) ([]rbacv1.PolicyRule, error) {
5455

5556
// Once we've consolidated the simple resource rules, add them to the compacted list
5657
for _, simpleRule := range simpleRules {
58+
verbSet := sets.New[string](simpleRule.Verbs...)
59+
if verbSet.Has("*") {
60+
simpleRule.Verbs = []string{"*"}
61+
} else {
62+
simpleRule.Verbs = sets.List(verbSet)
63+
}
5764
compacted = append(compacted, *simpleRule)
5865
}
5966

internal/operator-controller/authorization/internal/kubernetes/pkg/registry/rbac/validation/rule.go

+29-13
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ import (
2222
"fmt"
2323
"strings"
2424

25-
"k8s.io/klog/v2"
26-
27-
rbacv1helpers "github.com/operator-framework/operator-controller/internal/operator-controller/authorization/internal/kubernetes/pkg/apis/rbac/v1"
2825
rbacv1 "k8s.io/api/rbac/v1"
2926
utilerrors "k8s.io/apimachinery/pkg/util/errors"
3027
"k8s.io/apimachinery/pkg/util/sets"
3128
"k8s.io/apiserver/pkg/authentication/serviceaccount"
3229
"k8s.io/apiserver/pkg/authentication/user"
3330
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
3431
"k8s.io/component-helpers/auth/rbac/validation"
32+
"k8s.io/klog/v2"
33+
34+
rbacv1helpers "github.com/operator-framework/operator-controller/internal/operator-controller/authorization/internal/kubernetes/pkg/apis/rbac/v1"
3535
)
3636

3737
type AuthorizationRuleResolver interface {
@@ -49,6 +49,27 @@ type AuthorizationRuleResolver interface {
4949
VisitRulesFor(ctx context.Context, user user.Info, namespace string, visitor func(source fmt.Stringer, rule *rbacv1.PolicyRule, err error) bool)
5050
}
5151

52+
type PrivilegeEscalationError struct {
53+
User user.Info
54+
Namespace string
55+
MissingRules []rbacv1.PolicyRule
56+
RuleResolutionErrors []error
57+
}
58+
59+
func (e *PrivilegeEscalationError) Error() string {
60+
missingDescriptions := sets.NewString()
61+
for _, missing := range e.MissingRules {
62+
missingDescriptions.Insert(rbacv1helpers.CompactString(missing))
63+
}
64+
65+
msg := fmt.Sprintf("user %q (groups=%q) is attempting to grant RBAC permissions not currently held:\n%s", e.User.GetName(), e.User.GetGroups(), strings.Join(missingDescriptions.List(), "\n"))
66+
if len(e.RuleResolutionErrors) > 0 {
67+
msg = msg + fmt.Sprintf("; resolution errors: %v", e.RuleResolutionErrors)
68+
}
69+
70+
return msg
71+
}
72+
5273
// ConfirmNoEscalation determines if the roles for a given user in a given namespace encompass the provided role.
5374
func ConfirmNoEscalation(ctx context.Context, ruleResolver AuthorizationRuleResolver, rules []rbacv1.PolicyRule) error {
5475
ruleResolutionErrors := []error{}
@@ -73,17 +94,12 @@ func ConfirmNoEscalation(ctx context.Context, ruleResolver AuthorizationRuleReso
7394
compactMissingRights = compact
7495
}
7596

76-
missingDescriptions := sets.NewString()
77-
for _, missing := range compactMissingRights {
78-
missingDescriptions.Insert(rbacv1helpers.CompactString(missing))
79-
}
80-
81-
msg := fmt.Sprintf("user %q (groups=%q) is attempting to grant RBAC permissions not currently held:\n%s", user.GetName(), user.GetGroups(), strings.Join(missingDescriptions.List(), "\n"))
82-
if len(ruleResolutionErrors) > 0 {
83-
msg = msg + fmt.Sprintf("; resolution errors: %v", ruleResolutionErrors)
97+
return &PrivilegeEscalationError{
98+
User: user,
99+
Namespace: namespace,
100+
MissingRules: compactMissingRights,
101+
RuleResolutionErrors: ruleResolutionErrors,
84102
}
85-
86-
return errors.New(msg)
87103
}
88104
return nil
89105
}

0 commit comments

Comments
 (0)