-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add auth check when listing kinds (#325)
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
- Loading branch information
1 parent
ebc205a
commit 12e67ed
Showing
6 changed files
with
152 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
|
||
authorizationv1client "k8s.io/client-go/kubernetes/typed/authorization/v1" | ||
) | ||
|
||
// AuthResult contains authorization check result | ||
type AuthResult struct { | ||
Allowed bool | ||
Reason string | ||
EvaluationError string | ||
} | ||
|
||
// AuthChecker provides utility to check authorization | ||
type AuthChecker interface { | ||
// Check checks if the caller can perform an operation | ||
Check(ctx context.Context, group, version, resource, subresource, namespace, verb string) (*AuthResult, error) | ||
} | ||
|
||
func NewSelfChecker(client authorizationv1client.SelfSubjectAccessReviewInterface) AuthChecker { | ||
return self{ | ||
client: client, | ||
} | ||
} | ||
|
||
func NewSubjectChecker(client authorizationv1client.SubjectAccessReviewInterface, user string, groups []string) AuthChecker { | ||
return subject{ | ||
client: client, | ||
user: user, | ||
groups: groups, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
func Check(ctx context.Context, checker AuthChecker, group, version, resource, subresource, namespace string, verbs ...string) (bool, error) { | ||
for _, verb := range verbs { | ||
result, err := checker.Check(ctx, group, version, resource, subresource, namespace, verb) | ||
if err != nil { | ||
return false, err | ||
} | ||
if !result.Allowed { | ||
return false, nil | ||
} | ||
} | ||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
|
||
authorizationv1 "k8s.io/api/authorization/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
authorizationv1client "k8s.io/client-go/kubernetes/typed/authorization/v1" | ||
) | ||
|
||
type self struct { | ||
client authorizationv1client.SelfSubjectAccessReviewInterface | ||
} | ||
|
||
func (c self) Check(ctx context.Context, group, version, resource, subresource, namespace, verb string) (*AuthResult, error) { | ||
review := &authorizationv1.SelfSubjectAccessReview{ | ||
Spec: authorizationv1.SelfSubjectAccessReviewSpec{ | ||
ResourceAttributes: &authorizationv1.ResourceAttributes{ | ||
Group: group, | ||
Version: version, | ||
Resource: resource, | ||
Subresource: subresource, | ||
Namespace: namespace, | ||
Verb: verb, | ||
}, | ||
}, | ||
} | ||
resp, err := c.client.Create(ctx, review, metav1.CreateOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &AuthResult{ | ||
Allowed: resp.Status.Allowed, | ||
Reason: resp.Status.Reason, | ||
EvaluationError: resp.Status.EvaluationError, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
|
||
authorizationv1 "k8s.io/api/authorization/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
authorizationv1client "k8s.io/client-go/kubernetes/typed/authorization/v1" | ||
) | ||
|
||
type subject struct { | ||
client authorizationv1client.SubjectAccessReviewInterface | ||
user string | ||
groups []string | ||
} | ||
|
||
func (c subject) Check(ctx context.Context, group, version, resource, subresource, namespace, verb string) (*AuthResult, error) { | ||
review := &authorizationv1.SubjectAccessReview{ | ||
Spec: authorizationv1.SubjectAccessReviewSpec{ | ||
ResourceAttributes: &authorizationv1.ResourceAttributes{ | ||
Group: group, | ||
Version: version, | ||
Resource: resource, | ||
Subresource: subresource, | ||
Namespace: namespace, | ||
Verb: verb, | ||
}, | ||
User: c.user, | ||
Groups: c.groups, | ||
}, | ||
} | ||
resp, err := c.client.Create(ctx, review, metav1.CreateOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &AuthResult{ | ||
Allowed: resp.Status.Allowed, | ||
Reason: resp.Status.Reason, | ||
EvaluationError: resp.Status.EvaluationError, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters