Skip to content

Commit

Permalink
rbac: narrow permissions (#613)
Browse files Browse the repository at this point in the history
* fix(rbac): narrow permissions

Signed-off-by: Pedro Tôrres <[email protected]>

* feat(rbac): generate manifests with kubebuilder

Signed-off-by: Pedro Tôrres <[email protected]>

---------

Signed-off-by: Pedro Tôrres <[email protected]>
Signed-off-by: Pedro Tôrres <[email protected]>
  • Loading branch information
t0rr3sp3dr0 committed Mar 2, 2023
1 parent 3bd29c2 commit 653ddb6
Show file tree
Hide file tree
Showing 20 changed files with 210 additions and 57 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This changelog keeps track of work items that have been completed and are ready
### Improvements

- **General**: Automatically tag Docker image with commit SHA ([#567](https://github.com/kedacore/http-add-on/issues/567))
- **RBAC**: Introduce fine-grained permissions per component and reduce required permissions ([#612](https://github.com/kedacore/http-add-on/issues/612))

### Fixes

Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ publish-multiarch: publish-operator-multiarch publish-interceptor-multiarch publ
# Development

manifests: controller-gen ## Generate ClusterRole and CustomResourceDefinition objects for core componenets.
$(CONTROLLER_GEN) crd:crdVersions=v1 rbac:roleName=keda-http-add-on paths="./..." output:crd:artifacts:config=config/crd/bases
$(CONTROLLER_GEN) crd:crdVersions=v1 rbac:roleName=keda-http-add-on paths="./operator/..." output:crd:artifacts:config=config/crd/bases
$(CONTROLLER_GEN) crd:crdVersions=v1 rbac:roleName=keda-http-add-on-scaler paths="./scaler/..." output:rbac:artifacts:config=config/scaler
$(CONTROLLER_GEN) crd:crdVersions=v1 rbac:roleName=keda-http-add-on-interceptor paths="./interceptor/..." output:rbac:artifacts:config=config/interceptor

verify-manifests:
./hack/verify-manifests.sh
Expand Down
2 changes: 1 addition & 1 deletion config/interceptor/interceptor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ spec:
spec:
securityContext:
runAsNonRoot: true
serviceAccountName: keda-http-add-on
serviceAccountName: keda-http-add-on-interceptor
containers:
- name: interceptor
image: ghcr.io/kedacore/http-add-on-interceptor:latest
Expand Down
3 changes: 3 additions & 0 deletions config/interceptor/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ resources:
- interceptor.yaml
- service-admin.yaml
- service-proxy.yaml
- role.yaml
- role_binding.yaml
- service_account.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
Expand Down
31 changes: 31 additions & 0 deletions config/interceptor/role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
creationTimestamp: null
name: keda-http-add-on-interceptor
rules:
- apiGroups:
- apps
resources:
- deployments
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
creationTimestamp: null
name: keda-http-add-on-interceptor
namespace: keda
rules:
- apiGroups:
- ""
resources:
- configmaps
verbs:
- get
- list
- watch
26 changes: 26 additions & 0 deletions config/interceptor/role_binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: keda-http-add-on-interceptor
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: keda-http-add-on-interceptor
subjects:
- kind: ServiceAccount
name: keda-http-add-on-interceptor
namespace: keda
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: keda-http-add-on-interceptor
namespace: keda
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: keda-http-add-on-interceptor
subjects:
- kind: ServiceAccount
name: keda-http-add-on-interceptor
namespace: keda
9 changes: 9 additions & 0 deletions config/interceptor/service_account.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
app.kubernetes.io/name: keda-http-add-on
app.kubernetes.io/version: latest
app.kubernetes.io/part-of: keda-http-add-on
name: keda-http-add-on-interceptor
namespace: keda
77 changes: 31 additions & 46 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,23 @@ metadata:
name: keda-http-add-on
rules:
- apiGroups:
- ""
resources:
- configmaps
- configmaps/status
- endpoint
- endpoints
- events
- pods
- services
verbs:
- create
- delete
- get
- list
- watch
- apiGroups:
- apps
resources:
- deployments
verbs:
- create
- delete
- get
- list
- watch
- apiGroups:
- coordination.k8s.io
- http.keda.sh
resources:
- leases
- httpscaledobjects
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- http.keda.sh
resources:
- httpscaledobjects
- httpscaledobjects/finalizers
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- http.keda.sh
resources:
Expand All @@ -73,16 +43,6 @@ rules:
- patch
- update
- watch
- apiGroups:
- networking
resources:
- ingresses
verbs:
- create
- delete
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
Expand All @@ -96,4 +56,29 @@ rules:
resources:
- configmaps
verbs:
- '*'
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
- events
verbs:
- create
- patch
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
3 changes: 3 additions & 0 deletions config/scaler/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
resources:
- scaler.yaml
- service.yaml
- role.yaml
- role_binding.yaml
- service_account.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
Expand Down
39 changes: 39 additions & 0 deletions config/scaler/role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
creationTimestamp: null
name: keda-http-add-on-scaler
rules:
- apiGroups:
- ""
resources:
- endpoints
verbs:
- get
- list
- watch
- apiGroups:
- apps
resources:
- deployments
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
creationTimestamp: null
name: keda-http-add-on-scaler
namespace: keda
rules:
- apiGroups:
- ""
resources:
- configmaps
verbs:
- get
- list
- watch
26 changes: 26 additions & 0 deletions config/scaler/role_binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: keda-http-add-on-scaler
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: keda-http-add-on-scaler
subjects:
- kind: ServiceAccount
name: keda-http-add-on-scaler
namespace: keda
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: keda-http-add-on-scaler
namespace: keda
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: keda-http-add-on-scaler
subjects:
- kind: ServiceAccount
name: keda-http-add-on-scaler
namespace: keda
2 changes: 1 addition & 1 deletion config/scaler/scaler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ spec:
spec:
securityContext:
runAsNonRoot: true
serviceAccountName: keda-http-add-on
serviceAccountName: keda-http-add-on-scaler
containers:
- name: external-scaler
image: ghcr.io/kedacore/http-add-on-scaler:latest
Expand Down
9 changes: 9 additions & 0 deletions config/scaler/service_account.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
app.kubernetes.io/name: keda-http-add-on
app.kubernetes.io/version: latest
app.kubernetes.io/part-of: keda-http-add-on
name: keda-http-add-on-scaler
namespace: keda
4 changes: 4 additions & 0 deletions interceptor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func init() {
rand.Seed(time.Now().UnixNano())
}

// +kubebuilder:rbac:groups="",namespace=keda,resources=configmaps,verbs=get;list;watch
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch

func main() {
lggr, err := pkglog.NewZapr()
if err != nil {
Expand Down Expand Up @@ -90,6 +93,7 @@ func main() {
lggr,
cl,
servingCfg.ConfigMapCacheRsyncPeriod,
servingCfg.CurrentNamespace,
)

lggr.Info(
Expand Down
2 changes: 0 additions & 2 deletions operator/controllers/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"github.com/kedacore/http-add-on/pkg/routing"
)

// +kubebuilder:rbac:groups="",namespace=keda,resources=configmaps,verbs="*"

func removeApplicationResources(
ctx context.Context,
logger logr.Logger,
Expand Down
7 changes: 2 additions & 5 deletions operator/controllers/httpscaledobject_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,10 @@ type HTTPScaledObjectReconciler struct {
RoutingTable *routing.Table
}

// +kubebuilder:rbac:groups=keda.sh,resources=scaledobjects,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=http.keda.sh,resources=httpscaledobjects,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=http.keda.sh,resources=httpscaledobjects/status,verbs=get;update;patch
// +kubebuilder:rbac:groups="",resources=pods;services;configmaps;configmaps/status;events;endpoints;endpoint,verbs=get;list;watch;create;delete
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;delete
// +kubebuilder:rbac:groups=networking,resources=ingresses,verbs=get;list;watch;create;delete
// +kubebuilder:rbac:groups=coordination.k8s.io,resources=leases,verbs=get;list;create;update;delete
// +kubebuilder:rbac:groups=http.keda.sh,resources=httpscaledobjects/finalizers,verbs=update
// +kubebuilder:rbac:groups=keda.sh,resources=scaledobjects,verbs=get;list;watch;create;update;patch;delete

// Reconcile reconciles a newly created, deleted, or otherwise changed
// HTTPScaledObject
Expand Down
12 changes: 12 additions & 0 deletions operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ import (
"github.com/go-logr/logr"
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
"golang.org/x/sync/errgroup"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

httpv1alpha1 "github.com/kedacore/http-add-on/operator/api/v1alpha1"
Expand All @@ -56,6 +58,10 @@ func init() {
// +kubebuilder:scaffold:scheme
}

// +kubebuilder:rbac:groups="",namespace=keda,resources=events,verbs=create;patch
// +kubebuilder:rbac:groups="",namespace=keda,resources=configmaps,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=coordination.k8s.io,namespace=keda,resources=leases,verbs=get;list;watch;create;update;patch;delete

func main() {
ctx := ctrl.SetupSignalHandler()
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
Expand Down Expand Up @@ -117,6 +123,12 @@ func main() {
LeaderElectionID: "f8508ff1.keda.sh",
// will be empty to indicate all namespaces
Namespace: baseConfig.WatchNamespace,
// TODO(pedrotorres): remove this when we stop relying on ConfigMaps for the routing table
// workaround for using the same K8s client for both the routing table and the HTTPScaledObject
// this was already broken if the operator was running only for a single namespace
ClientDisableCacheFor: []client.Object{
&corev1.ConfigMap{},
},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down
Loading

0 comments on commit 653ddb6

Please sign in to comment.