forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rancher#27336 from prachidamle/fwd_port_cis_svc_accnt
[Forwardport] Default serviceaccounts of system namespaces should not be used
- Loading branch information
Showing
4 changed files
with
111 additions
and
2 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
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,71 @@ | ||
package nsserviceaccount | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/rancher/norman/types/slice" | ||
"github.com/rancher/rancher/pkg/settings" | ||
rv1 "github.com/rancher/types/apis/core/v1" | ||
"github.com/rancher/types/config" | ||
"github.com/sirupsen/logrus" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
type defaultSvcAccountHandler struct { | ||
serviceAccounts rv1.ServiceAccountInterface | ||
} | ||
|
||
func Register(ctx context.Context, cluster *config.UserContext) { | ||
logrus.Debugf("Registering defaultSvcAccountHandler for checking default service account of system namespaces") | ||
nsh := &defaultSvcAccountHandler{ | ||
serviceAccounts: cluster.Core.ServiceAccounts(""), | ||
} | ||
cluster.Core.ServiceAccounts("").AddHandler(ctx, "defaultSvcAccountHandler", nsh.Sync) | ||
} | ||
|
||
func (nsh *defaultSvcAccountHandler) Sync(key string, sa *corev1.ServiceAccount) (runtime.Object, error) { | ||
if sa == nil || sa.DeletionTimestamp != nil { | ||
return nil, nil | ||
} | ||
logrus.Debugf("defaultSvcAccountHandler: Sync service account: key=%v", key) | ||
//handle default svcAccount of system namespaces only | ||
if err := nsh.handleIfSystemNSDefaultSA(sa); err != nil { | ||
logrus.Errorf("defaultSvcAccountHandler: Sync: error handling default ServiceAccount of namespace key=%v, err=%v", key, err) | ||
} | ||
return nil, nil | ||
} | ||
|
||
func (nsh *defaultSvcAccountHandler) handleIfSystemNSDefaultSA(defSvcAccnt *corev1.ServiceAccount) error { | ||
//check if sa is "default" | ||
if defSvcAccnt.Name != "default" { | ||
return nil | ||
} | ||
//check if ns is a system-ns | ||
namespace := defSvcAccnt.Namespace | ||
if namespace == "kube-system" || namespace == "default" || !nsh.isSystemNS(namespace) { | ||
return nil | ||
} | ||
if defSvcAccnt.AutomountServiceAccountToken != nil && *defSvcAccnt.AutomountServiceAccountToken == false { | ||
return nil | ||
} | ||
automountServiceAccountToken := false | ||
defSvcAccnt.AutomountServiceAccountToken = &automountServiceAccountToken | ||
logrus.Debugf("defaultSvcAccountHandler: updating default service account key=%v", defSvcAccnt) | ||
_, err := nsh.serviceAccounts.Update(defSvcAccnt) | ||
if err != nil { | ||
logrus.Errorf("defaultSvcAccountHandler: error updating default service account flag for namespace: %v, err=%+v", namespace, err) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (nsh *defaultSvcAccountHandler) isSystemNS(namespace string) bool { | ||
systemNamespacesStr := settings.SystemNamespaces.Get() | ||
if systemNamespacesStr == "" { | ||
return false | ||
} | ||
systemNamespaces := strings.Split(systemNamespacesStr, ",") | ||
return slice.ContainsString(systemNamespaces, namespace) | ||
} |
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