Skip to content

Commit

Permalink
fix(rbac): narrow permissions
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Tôrres <[email protected]>
  • Loading branch information
t0rr3sp3dr0 committed Feb 25, 2023
1 parent ab7ce09 commit 569dbc0
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ This changelog keeps track of work items that have been completed and are ready

### Fixes

- **General**: TODO ([#TODO](https://github.com/kedacore/http-add-on/issues/TODO))
- **RBAC**: Introduce fine-grained permissions per component and reduce required permissions ([#612](https://github.com/kedacore/http-add-on/issues/612))

### Deprecations

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
1 change: 1 addition & 0 deletions interceptor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,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 @@ -25,11 +25,13 @@ import (

"github.com/go-logr/logr"
"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 @@ -54,6 +56,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 @@ -115,6 +121,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
4 changes: 3 additions & 1 deletion pkg/k8s/config_map_cache_informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,12 @@ func NewInformerConfigMapUpdater(
lggr logr.Logger,
cl kubernetes.Interface,
defaultResync time.Duration,
namespace string,
) *InformerConfigMapUpdater {
factory := informers.NewSharedInformerFactory(
factory := informers.NewSharedInformerFactoryWithOptions(
cl,
defaultResync,
informers.WithNamespace(namespace),
)
cmInformer := factory.Core().V1().ConfigMaps()
ret := &InformerConfigMapUpdater{
Expand Down
1 change: 1 addition & 0 deletions pkg/routing/config_map_updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func TestStartUpdateLoop(t *testing.T) {
lggr,
fakeGetter,
time.Second*1,
ns,
)

grp, ctx := errgroup.WithContext(ctx)
Expand Down
Loading

0 comments on commit 569dbc0

Please sign in to comment.