Skip to content

Commit ae941c4

Browse files
committed
Review fixes
* Add constants for CRUD actions * Add plural for experiments and suggestions as constants * Add GetUsername logic under IsAuthorized and handle errors properly * Have APP_DISABLE_AUTH by default as true, since currently Katib doesn't support it in standalone mode. Signed-off-by: Apostolos Gerakaris <[email protected]>
1 parent 9e28398 commit ae941c4

File tree

6 files changed

+113
-174
lines changed

6 files changed

+113
-174
lines changed

pkg/controller.v1beta1/consts/const.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,23 @@ import (
2626

2727
const (
2828

29+
// ActionTypeCreate is the create CRUD action
30+
ActionTypeCreate = "create"
31+
// ActionTypeList is the list CRUD action
32+
ActionTypeList = "list"
33+
// ActionTypeGet is the get CRUD action
34+
ActionTypeGet = "get"
35+
// ActionTypeUpdate is the update CRUD action
36+
ActionTypeUpdate = "update"
37+
// ActionTypeDelete is the delete CRUD action
38+
ActionTypeDelete = "delete"
39+
2940
// PluralTrial is the plural for Trial object
3041
PluralTrial = "trials"
42+
// PluralExperiment is the plural for Experiment object
43+
PluralExperiment = "experiments"
44+
// PluralSuggestion is the plural for Suggestion object
45+
PluralSuggestion = "suggestions"
3146

3247
// ConfigExperimentSuggestionName is the config name of the
3348
// suggestion client implementation in experiment controller.

pkg/new-ui/v1beta1/authzn.go

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,9 @@ import (
1818
var (
1919
USER_HEADER = env.GetEnvOrDefault("USERID_HEADER", "kubeflow-userid")
2020
USER_PREFIX = env.GetEnvOrDefault("USERID_PREFIX", ":")
21-
DISABLE_AUTH = env.GetEnvOrDefault("APP_DISABLE_AUTH", "false")
21+
DISABLE_AUTH = env.GetEnvOrDefault("APP_DISABLE_AUTH", "true")
2222
)
2323

24-
func GetUsername(r *http.Request) (string, error) {
25-
var username string
26-
if DISABLE_AUTH == "true" {
27-
log.Printf("APP_DISABLE_AUTH set to True. Skipping authentication check")
28-
return "", nil
29-
}
30-
31-
if r.Header.Get(USER_HEADER) == "" {
32-
return "", errors.New("user header not present")
33-
}
34-
35-
user := r.Header.Get(USER_HEADER)
36-
username = strings.Replace(user, USER_PREFIX, "", 1)
37-
38-
return username, nil
39-
}
40-
4124
// Function for constructing SubjectAccessReviews (SAR) objects
4225
func CreateSAR(user, verb, namespace, resource, subresource, name string, schema schema.GroupVersion) *v1.SubjectAccessReview {
4326

@@ -58,28 +41,34 @@ func CreateSAR(user, verb, namespace, resource, subresource, name string, schema
5841
return sar
5942
}
6043

61-
func IsAuthorized(user, verb, namespace, resource, subresource, name string, schema schema.GroupVersion, client client.Client) error {
44+
func IsAuthorized(verb, namespace, resource, subresource, name string, schema schema.GroupVersion, client client.Client, r *http.Request) (string, error) {
6245

63-
// Skip authz when admin is explicity requested it
46+
// We disable authn/authz checks when in standalone mode.
6447
if DISABLE_AUTH == "true" {
65-
log.Printf("APP_DISABLE_AUTH set to True. Skipping authorization check")
66-
return nil
48+
log.Printf("APP_DISABLE_AUTH set to True. Skipping authentication/authorization checks")
49+
return "", nil
6750
}
51+
// Check if an incoming request is from an authenticated user (kubeflow mode: kubeflow-userid header)
52+
if r.Header.Get(USER_HEADER) == "" {
53+
return "", errors.New("user header not present")
54+
}
55+
user := r.Header.Get(USER_HEADER)
56+
user = strings.Replace(user, USER_PREFIX, "", 1)
6857

58+
// Check if the user is authorized to perform a given action on katib/k8s resources.
6959
sar := CreateSAR(user, verb, namespace, resource, subresource, name, schema)
70-
7160
err := client.Create(context.TODO(), sar)
7261
if err != nil {
7362
log.Printf("Error submitting SubjectAccessReview: %v, %s", sar, err.Error())
74-
return err
63+
return user, err
7564
}
7665

7766
if sar.Status.Allowed {
78-
return nil
67+
return user, nil
7968
}
8069

8170
msg := generateUnauthorizedMessage(user, verb, namespace, resource, subresource, schema, sar)
82-
return errors.New(msg)
71+
return user, errors.New(msg)
8372
}
8473

8574
func generateUnauthorizedMessage(user, verb, namespace, resource, subresource string, schema schema.GroupVersion, sar *v1.SubjectAccessReview) string {

0 commit comments

Comments
 (0)