Skip to content

Commit 66d70ce

Browse files
Update User Permitted in Namespace Logic
With this change the "user permitted in namespace" logic now properly indicates what namespaces a user is allowed to access and which they can't (i.e. because the install doesn't have access to that namespace, or the user themselves does not have permissions to access that namespace). Therefore, commands like 'pgo show namespace' now return the proper results.
1 parent 0dee4c7 commit 66d70ce

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

apiserver/namespaceservice/namespaceimpl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func ShowNamespace(clientset *kubernetes.Clientset, username string, request *ms
7272
if err != nil {
7373
resp.Status.Code = msgs.Error
7474
resp.Status.Msg = fmt.Sprintf("Error when determining whether user [%s] is allowed "+
75-
"access to namespace [%s]: %w", username, nsList[i], err)
75+
"access to namespace [%s]: %s", username, nsList[i], err.Error())
7676
return resp
7777
}
7878
r := msgs.NamespaceResult{

apiserver/root.go

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func GetNamespace(clientset *kubernetes.Clientset, username, requestedNS string)
286286
iAccess, uAccess, err := UserIsPermittedInNamespace(username, requestedNS)
287287
if err != nil {
288288
return requestedNS, fmt.Errorf("Error when determining whether user [%s] is allowed access to "+
289-
"namespace [%s]: %w", username, requestedNS, err)
289+
"namespace [%s]: %s", username, requestedNS, err.Error())
290290
}
291291
if iAccess == false {
292292
errMsg := fmt.Sprintf("namespace [%s] is not part of the Operator installation", requestedNS)
@@ -367,38 +367,45 @@ func ValidateNodeLabel(nodeLabel string) error {
367367
// UserIsPermittedInNamespace returns installation access and user access.
368368
// Installation access means a namespace belongs to this Operator installation.
369369
// User access means this user has access to a namespace.
370-
func UserIsPermittedInNamespace(username, requestedNS string) (iAccess, uAccess bool, err error) {
370+
func UserIsPermittedInNamespace(username, requestedNS string) (bool, bool, error) {
371371

372-
if err = ns.ValidateNamespacesWatched(Clientset, NamespaceOperatingMode(), InstallationName,
373-
requestedNS); err != nil && !errors.Is(err, ns.ErrNamespaceNotWatched) {
374-
return
375-
}
376-
iAccess = true
372+
var iAccess, uAccess bool
377373

378-
//get the pgouser Secret for this username
379-
userSecretName := "pgouser-" + username
380-
userSecret, err := kubeapi.GetSecret(Clientset, userSecretName, PgoNamespace)
381-
if err != nil {
382-
log.Errorf("could not get pgouser secret %s: %s", username, err.Error())
383-
return
374+
if err := ns.ValidateNamespacesWatched(Clientset, NamespaceOperatingMode(), InstallationName,
375+
requestedNS); err != nil {
376+
if !errors.Is(err, ns.ErrNamespaceNotWatched) {
377+
return false, false, err
378+
}
379+
} else {
380+
iAccess = true
384381
}
385382

386-
// handle the case of a user in pgouser with "" (all) namespaces, otherwise check the
387-
// namespaces config in the user secret
388-
nsstring := string(userSecret.Data["namespaces"])
389-
if nsstring == "" {
390-
uAccess = true
391-
} else {
392-
nsList := strings.Split(nsstring, ",")
393-
for _, v := range nsList {
394-
ns := strings.TrimSpace(v)
395-
if ns == requestedNS {
396-
uAccess = true
383+
if iAccess {
384+
//get the pgouser Secret for this username
385+
userSecretName := "pgouser-" + username
386+
userSecret, err := kubeapi.GetSecret(Clientset, userSecretName, PgoNamespace)
387+
if err != nil {
388+
log.Errorf("could not get pgouser secret %s: %s", username, err.Error())
389+
return false, false, err
390+
}
391+
392+
// handle the case of a user in pgouser with "" (all) namespaces, otherwise check the
393+
// namespaces config in the user secret
394+
nsstring := string(userSecret.Data["namespaces"])
395+
if nsstring == "" {
396+
uAccess = true
397+
} else {
398+
nsList := strings.Split(nsstring, ",")
399+
for _, v := range nsList {
400+
ns := strings.TrimSpace(v)
401+
if ns == requestedNS {
402+
uAccess = true
403+
}
397404
}
398405
}
399406
}
400407

401-
return
408+
return iAccess, uAccess, nil
402409
}
403410

404411
// WriteTLSCert is a legacy method that writes the server certificate and key to

0 commit comments

Comments
 (0)