Skip to content

feat(security): allow configurable overide of app rules via helm chart… #4697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions charts/gitops-server/templates/appRulesConfigMap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: wego-app-rules
data:
rules.yaml: |-
{{ toYaml .Values.weaveGitOpsApprules | indent 4 }}
24 changes: 24 additions & 0 deletions charts/gitops-server/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ envVars:
- name: WEAVE_GITOPS_FEATURE_GITOPS_RUNTIME
value: "false"

# This section is used to configure the app rules for the GitOps server.
# The app rules are used to define the permissions that the GitOps server has
# to access resources in the cluster. The rules are defined in a ConfigMap
# that is mounted into the GitOps server pod.
weaveGitOpsApprules:
- apiGroups: [""]
resources: ["pods", "secrets"]
verbs: ["get", "list"]
- apiGroups: [""]
resources: ["events"]
verbs: ["get", "list", "watch"]
- apiGroups: ["apps"]
resources: ["deployments", "replicasets"]
verbs: ["get", "list"]
- apiGroups: ["kustomize.toolkit.fluxcd.io"]
resources: ["kustomizations"]
verbs: ["get", "list"]
- apiGroups: ["helm.toolkit.fluxcd.io"]
resources: ["helmreleases"]
verbs: ["get", "list"]
- apiGroups: ["source.toolkit.fluxcd.io"]
resources: ["buckets", "helmcharts", "helmrepositories", "gitrepositories", "ocirepositories"]
verbs: ["get", "list"]

# -- Annotations to add to the deployment
annotations: {}
# Should the 'oidc-auth' secret be created. For a detailed
Expand Down
2 changes: 1 addition & 1 deletion cmd/gitops-server/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"github.com/weaveworks/weave-gitops/core/clustersmngr/fetcher"
"github.com/weaveworks/weave-gitops/core/logger"
"github.com/weaveworks/weave-gitops/core/nsaccess"
core "github.com/weaveworks/weave-gitops/core/server"

Check failure on line 38 in cmd/gitops-server/cmd/cmd.go

View workflow job for this annotation

GitHub Actions / CI Check Static Checks

could not import github.com/weaveworks/weave-gitops/core/server (-: # github.com/weaveworks/weave-gitops/core/server
"github.com/weaveworks/weave-gitops/pkg/featureflags"
"github.com/weaveworks/weave-gitops/pkg/health"
"github.com/weaveworks/weave-gitops/pkg/kube"
Expand Down Expand Up @@ -244,7 +244,7 @@

fetcher := fetcher.NewSingleClusterFetcher(cl)

clustersManager := clustersmngr.NewClustersManager([]clustersmngr.ClusterFetcher{fetcher}, nsaccess.NewChecker(nsaccess.DefautltWegoAppRules), log)
clustersManager := clustersmngr.NewClustersManager([]clustersmngr.ClusterFetcher{fetcher}, nsaccess.NewChecker(nsaccess.MergeAppRules), log)

Check failure on line 247 in cmd/gitops-server/cmd/cmd.go

View workflow job for this annotation

GitHub Actions / CI Check Static Checks

cannot use nsaccess.MergeAppRules (value of type func(defaultRules []"k8s.io/api/rbac/v1".PolicyRule, configMapRules []"k8s.io/api/rbac/v1".PolicyRule) []"k8s.io/api/rbac/v1".PolicyRule) as []"k8s.io/api/rbac/v1".PolicyRule value in argument to nsaccess.NewChecker (typecheck)
clustersManager.Start(ctx)

healthChecker := health.NewHealthChecker()
Expand Down
57 changes: 55 additions & 2 deletions core/nsaccess/nsaccess.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package nsaccess

import (
"context"
"encoding/json"
"fmt"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"

authorizationv1 "k8s.io/api/authorization/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -14,8 +17,9 @@ import (

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate

// DefautltWegoAppRules is the minimun set of permissions a user will need to use the wego-app in a given namespace
var DefautltWegoAppRules = []rbacv1.PolicyRule{
// BaseWegoAppRules is the minimun set of permissions a user will need to use the wego-app in a given namespace.
// was DefautltWegoAppRules
var BaseWegoAppRules = []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"pods", "secrets"},
Expand Down Expand Up @@ -48,6 +52,55 @@ var DefautltWegoAppRules = []rbacv1.PolicyRule{
},
}

func getWegoAppRulesFromConfigMap() interface{} {

config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}

configMap, err := clientset.CoreV1().ConfigMaps("flux-system").Get(context.TODO(), "wego-app-rules", metav1.GetOptions{})
if err != nil {
panic(err.Error())
}

var rules []rbacv1.PolicyRule
err = json.Unmarshal([]byte(configMap.Data["rules"]), &rules)
if err != nil {
panic(err.Error())
}

return rules

}

// MergeAppRules merges the default rules with the rules from the configMap, overriding defaults with configMap rules
func MergeAppRules(defaultRules, configMapRules []rbacv1.PolicyRule) []rbacv1.PolicyRule {
ruleMap := make(map[string]rbacv1.PolicyRule)

for _, rule := range defaultRules {
key := fmt.Sprintf("%s-%s", rule.APIGroups, rule.Resources)
ruleMap[key] = rule
}

for _, rule := range configMapRules {
key := fmt.Sprintf("%s-%s", rule.APIGroups, rule.Resources)
ruleMap[key] = rule
}

mergedRules := make([]rbacv1.PolicyRule, 0, len(ruleMap))
for _, rule := range ruleMap {
mergedRules = append(mergedRules, rule)
}

return mergedRules
}

// Checker contains methods for validing user access to Kubernetes namespaces, based on a set of PolicyRules
//
//counterfeiter:generate . Checker
Expand Down
2 changes: 1 addition & 1 deletion core/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
log: log.WithName("core-server"),
RestCfg: cfg,
clusterName: clusterName,
NSAccess: nsaccess.NewChecker(nsaccess.DefautltWegoAppRules),
NSAccess: nsaccess.NewChecker(nsaccess.MergeAppRules),

Check failure on line 65 in core/server/server.go

View workflow job for this annotation

GitHub Actions / CI Check Static Checks

cannot use nsaccess.MergeAppRules (value of type func(defaultRules []"k8s.io/api/rbac/v1".PolicyRule, configMapRules []"k8s.io/api/rbac/v1".PolicyRule) []"k8s.io/api/rbac/v1".PolicyRule) as []"k8s.io/api/rbac/v1".PolicyRule value in argument to nsaccess.NewChecker) (typecheck)

Check failure on line 65 in core/server/server.go

View workflow job for this annotation

GitHub Actions / CI Check Static Checks

cannot use nsaccess.MergeAppRules (value of type func(defaultRules []"k8s.io/api/rbac/v1".PolicyRule, configMapRules []"k8s.io/api/rbac/v1".PolicyRule) []"k8s.io/api/rbac/v1".PolicyRule) as []"k8s.io/api/rbac/v1".PolicyRule value in argument to nsaccess.NewChecker (typecheck)

Check failure on line 65 in core/server/server.go

View workflow job for this annotation

GitHub Actions / CI Check Static Checks

cannot use nsaccess.MergeAppRules (value of type func(defaultRules []"k8s.io/api/rbac/v1".PolicyRule, configMapRules []"k8s.io/api/rbac/v1".PolicyRule) []"k8s.io/api/rbac/v1".PolicyRule) as []"k8s.io/api/rbac/v1".PolicyRule value in argument to nsaccess.NewChecker) (typecheck)

Check failure on line 65 in core/server/server.go

View workflow job for this annotation

GitHub Actions / CI Check Static Checks

cannot use nsaccess.MergeAppRules (value of type func(defaultRules []"k8s.io/api/rbac/v1".PolicyRule, configMapRules []"k8s.io/api/rbac/v1".PolicyRule) []"k8s.io/api/rbac/v1".PolicyRule) as []"k8s.io/api/rbac/v1".PolicyRule value in argument to nsaccess.NewChecker) (typecheck)

Check failure on line 65 in core/server/server.go

View workflow job for this annotation

GitHub Actions / CI Check Static Checks

cannot use nsaccess.MergeAppRules (value of type func(defaultRules []"k8s.io/api/rbac/v1".PolicyRule, configMapRules []"k8s.io/api/rbac/v1".PolicyRule) []"k8s.io/api/rbac/v1".PolicyRule) as []"k8s.io/api/rbac/v1".PolicyRule value in argument to nsaccess.NewChecker) (typecheck)
ClustersManager: clustersManager,
PrimaryKinds: kinds,
HealthChecker: healthChecker,
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/crd/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func createClient(k8sEnv *testutils.K8sTestEnv) (clustersmngr.Client, clustersmn

clustersManager := clustersmngr.NewClustersManager(
[]clustersmngr.ClusterFetcher{fetcher},
nsaccess.NewChecker(nsaccess.DefautltWegoAppRules),
nsaccess.NewChecker(nsaccess.MergeAppRules),
log,
)

Expand Down
Loading