Skip to content

Commit

Permalink
Add and update PodSecurityPolicy to match SecurityContext
Browse files Browse the repository at this point in the history
This changeset add and update PodSecurityPolicy for open source and
enterprise components to match the recent work for SecurityContext in [1].

To summarize the changes:
* Update Elasticsearch and Fluentd capabilities in PodSecurityPolicy to
  match SecurityContext.
* Add missing PSPs for all enterprise components. Enterprise components
  like packetcapture, esgateway, l7-log-collector, etc. are able to be
  deployed without creating PodSecurityPolicy manually.
* Manage Prometheus operator ClusterRole and ClusterRoleBinding in
  operator.
* use `UsePSP` as the only flag to determine whether we need to render
  PodSecurityPolicy or not.

[1] tigera#2433
  • Loading branch information
hjiawei committed Feb 24, 2023
1 parent 481bbcb commit d274784
Show file tree
Hide file tree
Showing 36 changed files with 802 additions and 423 deletions.
1 change: 1 addition & 0 deletions pkg/controller/apiserver/apiserver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ func (r *ReconcileAPIServer) Reconcile(ctx context.Context, request reconcile.Re
ClusterDomain: r.clusterDomain,
ManagementClusterConnection: managementClusterConnection,
TrustedBundle: trustedBundle,
UsePSP: r.usePSP,
}
pc := render.PacketCaptureAPI(packetCaptureApiCfg)
pcPolicy = render.PacketCaptureAPIPolicy(packetCaptureApiCfg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func newReconciler(mgr manager.Manager, opts options.AddOptions, licenseAPIReady
status: status.New(mgr.GetClient(), "applicationlayer", opts.KubernetesVersion),
clusterDomain: opts.ClusterDomain,
licenseAPIReady: licenseAPIReady,
usePSP: opts.UsePSP,
}
r.status.Run(opts.ShutdownContext)
return r
Expand Down Expand Up @@ -162,6 +163,7 @@ type ReconcileApplicationLayer struct {
status status.StatusManager
clusterDomain string
licenseAPIReady *utils.ReadyFlag
usePSP bool
}

// Reconcile reads that state of the cluster for a ApplicationLayer object and makes changes
Expand Down Expand Up @@ -280,6 +282,7 @@ func (r *ReconcileApplicationLayer) Reconcile(ctx context.Context, request recon
LogRequestsPerInterval: lcSpec.LogRequestsPerInterval,
LogIntervalSeconds: lcSpec.LogIntervalSeconds,
ModSecurityConfigMap: modSecurityRuleSet,
UsePSP: r.usePSP,
}
component := applicationlayer.ApplicationLayer(config)

Expand Down
12 changes: 5 additions & 7 deletions pkg/controller/egressgateway/egressgateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,12 @@ func (r *ReconcileEgressGateway) Reconcile(ctx context.Context, request reconcil
// If there are no Egress Gateway resources, return.
ch := utils.NewComponentHandler(log, r.client, r.scheme, nil)
if len(egws) == 0 {
objects := []client.Object{}

var objects []client.Object
if r.provider == operatorv1.ProviderOpenShift {
scc := egressgateway.SecurityContextConstraints()
objects = append(objects, scc)
} else if r.usePSP {
psp := egressgateway.PodSecurityPolicy()
objects = append(objects, psp)
objects = append(objects, egressgateway.SecurityContextConstraints())
}
if r.usePSP {
objects = append(objects, egressgateway.PodSecurityPolicy())
}
err := ch.CreateOrUpdateOrDelete(ctx, render.NewDeletionPassthrough(objects...), r.status)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion pkg/controller/installation/core_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,6 @@ func (r *ReconcileInstallation) Reconcile(ctx context.Context, request reconcile
csiCfg := render.CSIConfiguration{
Installation: &instance.Spec,
Terminating: terminating,
Openshift: r.autoDetectedProvider == operator.ProviderOpenShift,
UsePSP: r.usePSP,
}
components = append(components, render.CSI(&csiCfg))
Expand Down
14 changes: 8 additions & 6 deletions pkg/controller/logstorage/esgateway.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023 Tigera, Inc. All rights reserved.
// Copyright (c) 2021-2023 Tigera, Inc. All rights reserved.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -18,21 +18,21 @@ import (
"context"

"github.com/go-logr/logr"
"github.com/tigera/operator/pkg/controller/certificatemanager"
"github.com/tigera/operator/pkg/dns"
rcertificatemanagement "github.com/tigera/operator/pkg/render/certificatemanagement"
"github.com/tigera/operator/pkg/render/monitor"
"github.com/tigera/operator/pkg/tls/certificatemanagement"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

operatorv1 "github.com/tigera/operator/api/v1"
"github.com/tigera/operator/pkg/common"
"github.com/tigera/operator/pkg/controller/certificatemanager"
lscommon "github.com/tigera/operator/pkg/controller/logstorage/common"
"github.com/tigera/operator/pkg/controller/utils"
"github.com/tigera/operator/pkg/controller/utils/imageset"
"github.com/tigera/operator/pkg/dns"
"github.com/tigera/operator/pkg/render"
rcertificatemanagement "github.com/tigera/operator/pkg/render/certificatemanagement"
"github.com/tigera/operator/pkg/render/logstorage/esgateway"
"github.com/tigera/operator/pkg/render/monitor"
"github.com/tigera/operator/pkg/tls/certificatemanagement"
)

func (r *ReconcileLogStorage) createEsGateway(
Expand All @@ -44,6 +44,7 @@ func (r *ReconcileLogStorage) createEsGateway(
reqLogger logr.Logger,
ctx context.Context,
certificateManager certificatemanager.CertificateManager,
usePSP bool,
) (reconcile.Result, certificatemanagement.TrustedBundle, bool, error) {
svcDNSNames := dns.GetServiceDNSNames(render.ElasticsearchServiceName, render.ElasticsearchNamespace, r.clusterDomain)
svcDNSNames = append(svcDNSNames, dns.GetServiceDNSNames(esgateway.ServiceName, render.ElasticsearchNamespace, r.clusterDomain)...)
Expand Down Expand Up @@ -113,6 +114,7 @@ func (r *ReconcileLogStorage) createEsGateway(
ClusterDomain: r.clusterDomain,
EsAdminUserName: esAdminUserName,
ESGatewayKeyPair: gatewayKeyPair,
UsePSP: usePSP,
}

esGatewayComponent := esgateway.EsGateway(cfg)
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/logstorage/esmetrics.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023 Tigera, Inc. All rights reserved.
// Copyright (c) 2021-2023 Tigera, Inc. All rights reserved.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,6 +45,7 @@ func (r *ReconcileLogStorage) createEsMetrics(
hdler utils.ComponentHandler,
clusterDomain string,
trustedBundle certificatemanagement.TrustedBundle,
usePSP bool,
) (reconcile.Result, bool, error) {
esMetricsSecret, err := utils.GetSecret(context.Background(), r.client, esmetrics.ElasticsearchMetricsSecret, common.OperatorNamespace())
if err != nil {
Expand Down Expand Up @@ -80,6 +81,7 @@ func (r *ReconcileLogStorage) createEsMetrics(
ClusterDomain: r.clusterDomain,
ServerTLS: serverTLS,
TrustedBundle: trustedBundle,
UsePSP: usePSP,
}
esMetricsComponent := esmetrics.ElasticsearchMetrics(esMetricsCfg)
components := []render.Component{esMetricsComponent,
Expand Down
25 changes: 13 additions & 12 deletions pkg/controller/logstorage/logstorage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@ import (
"fmt"
"time"

"github.com/tigera/operator/pkg/render/common/networkpolicy"
"github.com/tigera/operator/pkg/render/kubecontrollers"
"github.com/tigera/operator/pkg/tls/certificatemanagement"
"k8s.io/client-go/kubernetes"

v3 "github.com/tigera/api/pkg/apis/projectcalico/v3"

esv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/elasticsearch/v1"
kbv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/kibana/v1"
"github.com/tigera/operator/pkg/controller/certificatemanager"
"github.com/elastic/cloud-on-k8s/v2/pkg/utils/stringsutil"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
Expand All @@ -44,30 +39,34 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/elastic/cloud-on-k8s/v2/pkg/utils/stringsutil"
v3 "github.com/tigera/api/pkg/apis/projectcalico/v3"
operatorv1 "github.com/tigera/operator/api/v1"
"github.com/tigera/operator/pkg/common"
"github.com/tigera/operator/pkg/controller/certificatemanager"
logstoragecommon "github.com/tigera/operator/pkg/controller/logstorage/common"
"github.com/tigera/operator/pkg/controller/options"
"github.com/tigera/operator/pkg/controller/status"
"github.com/tigera/operator/pkg/controller/utils"
"github.com/tigera/operator/pkg/controller/utils/imageset"
"github.com/tigera/operator/pkg/render"
relasticsearch "github.com/tigera/operator/pkg/render/common/elasticsearch"
"github.com/tigera/operator/pkg/render/common/networkpolicy"
rsecret "github.com/tigera/operator/pkg/render/common/secret"
"github.com/tigera/operator/pkg/render/kubecontrollers"
"github.com/tigera/operator/pkg/render/logstorage/esgateway"
"github.com/tigera/operator/pkg/render/logstorage/esmetrics"
"github.com/tigera/operator/pkg/render/monitor"
"github.com/tigera/operator/pkg/tls/certificatemanagement"
)

const ResourceName = "log-storage"

var log = logf.Log.WithName("controller_logstorage")

const (
defaultEckOperatorMemorySetting = "512Mi"
DefaultElasticsearchStorageClass = "tigera-elasticsearch"
LogStorageFinalizer = "tigera.io/eck-cleanup"
ResourceName = "log-storage"

defaultEckOperatorMemorySetting = "512Mi"
)

// Add creates a new LogStorage Controller and adds it to the Manager. The Manager will set fields on the Controller
Expand Down Expand Up @@ -620,6 +619,7 @@ func (r *ReconcileLogStorage) Reconcile(ctx context.Context, request reconcile.R
reqLogger,
ctx,
certificateManager,
r.usePSP,
)
if err != nil || !proceed {
return result, err
Expand All @@ -645,6 +645,7 @@ func (r *ReconcileLogStorage) Reconcile(ctx context.Context, request reconcile.R
hdler,
r.clusterDomain,
trustedBundle,
r.usePSP,
)
if err != nil || !proceed {
return result, err
Expand Down
5 changes: 4 additions & 1 deletion pkg/controller/monitor/monitor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func newReconciler(mgr manager.Manager, opts options.AddOptions, prometheusReady
prometheusReady: prometheusReady,
tierWatchReady: tierWatchReady,
clusterDomain: opts.ClusterDomain,
usePSP: opts.UsePSP,
}

r.status.AddStatefulSets([]types.NamespacedName{
Expand Down Expand Up @@ -180,6 +181,7 @@ type ReconcileMonitor struct {
prometheusReady *utils.ReadyFlag
tierWatchReady *utils.ReadyFlag
clusterDomain string
usePSP bool
}

func (r *ReconcileMonitor) getMonitor(ctx context.Context) (*operatorv1.Monitor, error) {
Expand Down Expand Up @@ -260,7 +262,7 @@ func (r *ReconcileMonitor) Reconcile(ctx context.Context, request reconcile.Requ
r.status.SetDegraded(operatorv1.ResourceCreateError, "Unable to create the Tigera CA", err, reqLogger)
return reconcile.Result{}, err
}
serverTLSSecret, err := certificateManager.GetOrCreateKeyPair(r.client, monitor.PrometheusTLSSecretName, common.OperatorNamespace(), dns.GetServiceDNSNames(monitor.PrometheusHTTPAPIServiceName, common.TigeraPrometheusNamespace, r.clusterDomain))
serverTLSSecret, err := certificateManager.GetOrCreateKeyPair(r.client, monitor.PrometheusTLSSecretName, common.OperatorNamespace(), dns.GetServiceDNSNames(monitor.PrometheusServiceServiceName, common.TigeraPrometheusNamespace, r.clusterDomain))
if err != nil {
r.status.SetDegraded(operatorv1.ResourceCreateError, "Error creating TLS certificate", err, reqLogger)
return reconcile.Result{}, err
Expand Down Expand Up @@ -352,6 +354,7 @@ func (r *ReconcileMonitor) Reconcile(ctx context.Context, request reconcile.Requ
TrustedCertBundle: trustedBundle,
Openshift: r.provider == operatorv1.ProviderOpenShift,
KubeControllerPort: kubeControllersMetricsPort,
UsePSP: r.usePSP,
}

// Render prometheus component
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/utils/discovery.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022 Tigera, Inc. All rights reserved.
// Copyright (c) 2020-2023 Tigera, Inc. All rights reserved.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -195,7 +195,7 @@ func isRKE2(ctx context.Context, c kubernetes.Interface) (bool, error) {
}

// SupportsPodSecurityPolicies returns true if the cluster contains the policy/v1beta1 PodSecurityPolicy API,
// and false otherwise. This API is scheuled to be removed in Kubernetes v1.25, but should still be used
// and false otherwise. This API is scheduled to be removed in Kubernetes v1.25, but should still be used
// in earlier Kubernetes versions.
func SupportsPodSecurityPolicies(c kubernetes.Interface) (bool, error) {
resources, err := c.Discovery().ServerResourcesForGroupVersion("policy/v1beta1")
Expand Down
15 changes: 5 additions & 10 deletions pkg/render/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
operatorv1 "github.com/tigera/operator/api/v1"
"github.com/tigera/operator/pkg/components"
"github.com/tigera/operator/pkg/controller/k8sapi"
"github.com/tigera/operator/pkg/ptr"
rcomp "github.com/tigera/operator/pkg/render/common/components"
rmeta "github.com/tigera/operator/pkg/render/common/meta"
"github.com/tigera/operator/pkg/render/common/networkpolicy"
Expand Down Expand Up @@ -186,7 +185,7 @@ func (c *apiServerComponent) Objects() ([]client.Object, []client.Object) {
globalObjects, objsToDelete = populateLists(globalObjects, objsToDelete, c.authReaderRoleBinding)
globalObjects, objsToDelete = populateLists(globalObjects, objsToDelete, c.webhookReaderClusterRole)
globalObjects, objsToDelete = populateLists(globalObjects, objsToDelete, c.webhookReaderClusterRoleBinding)
if !c.cfg.Openshift && c.cfg.UsePSP {
if c.cfg.UsePSP {
globalObjects, objsToDelete = populateLists(globalObjects, objsToDelete, c.apiServerPodSecurityPolicy)
}

Expand Down Expand Up @@ -524,7 +523,7 @@ func (c *apiServerComponent) calicoCustomResourcesClusterRole() *rbacv1.ClusterR
},
},
}
if !c.cfg.Openshift {
if c.cfg.UsePSP {
// Allow access to the pod security policy in case this is enforced on the cluster
rules = append(rules, rbacv1.PolicyRule{
APIGroups: []string{"policy"},
Expand Down Expand Up @@ -1125,15 +1124,11 @@ func (c *apiServerComponent) apiServerPodSecurityPolicy() (client.Object, client
nameToDelete = enterpriseName
}

psp := podsecuritypolicy.NewBasePolicy()
psp.GetObjectMeta().SetName(name)
psp.Spec.Privileged = false
psp.Spec.AllowPrivilegeEscalation = ptr.BoolToPtr(false)
psp := podsecuritypolicy.NewBasePolicy(name)
psp.Spec.Volumes = append(psp.Spec.Volumes, policyv1beta1.HostPath)
psp.Spec.RunAsUser.Rule = policyv1beta1.RunAsUserStrategyRunAsAny

pspToDelete := podsecuritypolicy.NewBasePolicy()
pspToDelete.GetObjectMeta().SetName(nameToDelete)
pspToDelete := podsecuritypolicy.NewBasePolicy(nameToDelete)

return psp, pspToDelete
}
Expand Down Expand Up @@ -1211,7 +1206,7 @@ func (c *apiServerComponent) tigeraCustomResourcesClusterRole() *rbacv1.ClusterR
},
},
}
if !c.cfg.Openshift {
if c.cfg.UsePSP {
// Allow access to the pod security policy in case this is enforced on the cluster
rules = append(rules, rbacv1.PolicyRule{
APIGroups: []string{"policy"},
Expand Down
Loading

0 comments on commit d274784

Please sign in to comment.