2019-03-21 16:09:14 +00:00
|
|
|
package config
|
2019-03-21 13:57:30 +00:00
|
|
|
|
2019-11-18 19:41:37 +00:00
|
|
|
import (
|
2022-05-04 16:05:03 +00:00
|
|
|
"context"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
2020-07-01 21:50:49 +00:00
|
|
|
|
2022-04-01 06:59:44 +00:00
|
|
|
osutils "github.com/kyverno/kyverno/pkg/utils/os"
|
2022-08-25 05:23:01 +00:00
|
|
|
wildcard "github.com/kyverno/kyverno/pkg/utils/wildcard"
|
2022-05-17 14:14:31 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2022-05-04 16:05:03 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
2019-11-18 19:41:37 +00:00
|
|
|
)
|
2019-05-30 19:28:56 +00:00
|
|
|
|
2020-01-24 20:05:53 +00:00
|
|
|
// These constants MUST be equal to the corresponding names in service definition in definitions/install.yaml
|
2019-03-21 13:57:30 +00:00
|
|
|
const (
|
2022-05-04 16:05:03 +00:00
|
|
|
// MutatingWebhookConfigurationName default resource mutating webhook configuration name
|
2020-01-24 20:05:53 +00:00
|
|
|
MutatingWebhookConfigurationName = "kyverno-resource-mutating-webhook-cfg"
|
2022-05-04 16:05:03 +00:00
|
|
|
// MutatingWebhookConfigurationDebugName default resource mutating webhook configuration name for debug mode
|
2019-09-04 20:43:12 +00:00
|
|
|
MutatingWebhookConfigurationDebugName = "kyverno-resource-mutating-webhook-cfg-debug"
|
2022-05-04 16:05:03 +00:00
|
|
|
// MutatingWebhookName default resource mutating webhook name
|
2020-11-27 00:07:06 +00:00
|
|
|
MutatingWebhookName = "mutate.kyverno.svc"
|
2022-05-04 16:05:03 +00:00
|
|
|
// ValidatingWebhookConfigurationName ...
|
|
|
|
ValidatingWebhookConfigurationName = "kyverno-resource-validating-webhook-cfg"
|
|
|
|
// ValidatingWebhookConfigurationDebugName ...
|
2020-01-15 14:59:02 +00:00
|
|
|
ValidatingWebhookConfigurationDebugName = "kyverno-resource-validating-webhook-cfg-debug"
|
2022-05-04 16:05:03 +00:00
|
|
|
// ValidatingWebhookName ...
|
|
|
|
ValidatingWebhookName = "validate.kyverno.svc"
|
2022-05-17 06:19:03 +00:00
|
|
|
// VerifyMutatingWebhookConfigurationName default verify mutating webhook configuration name
|
2020-01-24 20:05:53 +00:00
|
|
|
VerifyMutatingWebhookConfigurationName = "kyverno-verify-mutating-webhook-cfg"
|
2022-05-17 06:19:03 +00:00
|
|
|
// VerifyMutatingWebhookConfigurationDebugName default verify mutating webhook configuration name for debug mode
|
2019-10-30 20:39:19 +00:00
|
|
|
VerifyMutatingWebhookConfigurationDebugName = "kyverno-verify-mutating-webhook-cfg-debug"
|
2022-05-17 06:19:03 +00:00
|
|
|
// VerifyMutatingWebhookName default verify mutating webhook name
|
2020-11-27 00:07:06 +00:00
|
|
|
VerifyMutatingWebhookName = "monitor-webhooks.kyverno.svc"
|
2022-05-17 06:19:03 +00:00
|
|
|
// PolicyValidatingWebhookConfigurationName default policy validating webhook configuration name
|
2020-01-24 20:05:53 +00:00
|
|
|
PolicyValidatingWebhookConfigurationName = "kyverno-policy-validating-webhook-cfg"
|
2022-05-17 06:19:03 +00:00
|
|
|
// PolicyValidatingWebhookConfigurationDebugName default policy validating webhook configuration name for debug mode
|
2019-08-27 21:59:17 +00:00
|
|
|
PolicyValidatingWebhookConfigurationDebugName = "kyverno-policy-validating-webhook-cfg-debug"
|
2022-05-17 06:19:03 +00:00
|
|
|
// PolicyValidatingWebhookName default policy validating webhook name
|
2020-11-27 00:07:06 +00:00
|
|
|
PolicyValidatingWebhookName = "validate-policy.kyverno.svc"
|
2022-05-17 06:19:03 +00:00
|
|
|
// PolicyMutatingWebhookConfigurationName default policy mutating webhook configuration name
|
2020-01-24 20:05:53 +00:00
|
|
|
PolicyMutatingWebhookConfigurationName = "kyverno-policy-mutating-webhook-cfg"
|
2022-05-17 06:19:03 +00:00
|
|
|
// PolicyMutatingWebhookConfigurationDebugName default policy mutating webhook configuration name for debug mode
|
2019-08-27 21:52:56 +00:00
|
|
|
PolicyMutatingWebhookConfigurationDebugName = "kyverno-policy-mutating-webhook-cfg-debug"
|
2022-05-17 06:19:03 +00:00
|
|
|
// PolicyMutatingWebhookName default policy mutating webhook name
|
2020-11-27 00:07:06 +00:00
|
|
|
PolicyMutatingWebhookName = "mutate-policy.kyverno.svc"
|
2019-03-25 13:44:53 +00:00
|
|
|
// Due to kubernetes issue, we must use next literal constants instead of deployment TypeMeta fields
|
2019-05-14 14:58:59 +00:00
|
|
|
// Issue: https://github.com/kubernetes/kubernetes/pull/63972
|
|
|
|
// When the issue is closed, we should use TypeMeta struct instead of this constants
|
2021-11-03 06:44:32 +00:00
|
|
|
// ClusterRoleAPIVersion define the default clusterrole resource apiVersion
|
|
|
|
ClusterRoleAPIVersion = "rbac.authorization.k8s.io/v1"
|
|
|
|
// ClusterRoleKind define the default clusterrole resource kind
|
|
|
|
ClusterRoleKind = "ClusterRole"
|
2022-05-17 06:19:03 +00:00
|
|
|
// MutatingWebhookServicePath is the path for mutation webhook
|
2020-01-24 20:05:53 +00:00
|
|
|
MutatingWebhookServicePath = "/mutate"
|
2022-05-17 06:19:03 +00:00
|
|
|
// ValidatingWebhookServicePath is the path for validation webhook
|
2020-01-24 20:05:53 +00:00
|
|
|
ValidatingWebhookServicePath = "/validate"
|
2022-05-17 06:19:03 +00:00
|
|
|
// PolicyValidatingWebhookServicePath is the path for policy validation webhook(used to validate policy resource)
|
2019-07-03 01:42:07 +00:00
|
|
|
PolicyValidatingWebhookServicePath = "/policyvalidate"
|
2022-05-17 06:19:03 +00:00
|
|
|
// PolicyMutatingWebhookServicePath is the path for policy mutation webhook(used to default)
|
2020-01-24 20:05:53 +00:00
|
|
|
PolicyMutatingWebhookServicePath = "/policymutate"
|
2022-05-17 06:19:03 +00:00
|
|
|
// VerifyMutatingWebhookServicePath is the path for verify webhook(used to veryfing if admission control is enabled and active)
|
2020-01-24 20:05:53 +00:00
|
|
|
VerifyMutatingWebhookServicePath = "/verifymutate"
|
2020-05-27 01:03:32 +00:00
|
|
|
// LivenessServicePath is the path for check liveness health
|
|
|
|
LivenessServicePath = "/health/liveness"
|
2020-07-01 21:50:49 +00:00
|
|
|
// ReadinessServicePath is the path for check readness health
|
2020-05-27 01:03:32 +00:00
|
|
|
ReadinessServicePath = "/health/readiness"
|
2019-03-25 13:44:53 +00:00
|
|
|
)
|
2019-05-30 19:28:56 +00:00
|
|
|
|
2022-04-01 06:59:44 +00:00
|
|
|
var (
|
2022-05-11 06:14:30 +00:00
|
|
|
// kyvernoNamespace is the Kyverno namespace
|
|
|
|
kyvernoNamespace = osutils.GetEnvWithFallback("KYVERNO_NAMESPACE", "kyverno")
|
|
|
|
// kyvernoDeploymentName is the Kyverno deployment name
|
|
|
|
kyvernoDeploymentName = osutils.GetEnvWithFallback("KYVERNO_DEPLOYMENT", "kyverno")
|
|
|
|
// kyvernoServiceName is the Kyverno service name
|
|
|
|
kyvernoServiceName = osutils.GetEnvWithFallback("KYVERNO_SVC", "kyverno-svc")
|
|
|
|
// kyvernoPodName is the Kyverno pod name
|
|
|
|
kyvernoPodName = osutils.GetEnvWithFallback("KYVERNO_POD_NAME", "kyverno")
|
|
|
|
// kyvernoConfigMapName is the Kyverno configmap name
|
|
|
|
kyvernoConfigMapName = osutils.GetEnvWithFallback("INIT_CONFIG", "kyverno")
|
2022-05-04 16:05:03 +00:00
|
|
|
// defaultExcludeGroupRole ...
|
|
|
|
defaultExcludeGroupRole []string = []string{"system:serviceaccounts:kube-system", "system:nodes", "system:kube-scheduler"}
|
2022-04-01 06:59:44 +00:00
|
|
|
)
|
|
|
|
|
2022-05-11 06:14:30 +00:00
|
|
|
func KyvernoNamespace() string {
|
|
|
|
return kyvernoNamespace
|
|
|
|
}
|
|
|
|
|
|
|
|
func KyvernoDeploymentName() string {
|
|
|
|
return kyvernoDeploymentName
|
|
|
|
}
|
|
|
|
|
|
|
|
func KyvernoServiceName() string {
|
|
|
|
return kyvernoServiceName
|
|
|
|
}
|
|
|
|
|
|
|
|
func KyvernoPodName() string {
|
|
|
|
return kyvernoPodName
|
|
|
|
}
|
|
|
|
|
|
|
|
func KyvernoConfigMapName() string {
|
|
|
|
return kyvernoConfigMapName
|
|
|
|
}
|
|
|
|
|
2022-05-04 16:05:03 +00:00
|
|
|
// Configuration to be used by consumer to check filters
|
|
|
|
type Configuration interface {
|
|
|
|
// ToFilter checks if the given resource is set to be filtered in the configuration
|
|
|
|
ToFilter(kind, namespace, name string) bool
|
|
|
|
// GetExcludeGroupRole return exclude roles
|
|
|
|
GetExcludeGroupRole() []string
|
|
|
|
// GetExcludeUsername return exclude username
|
|
|
|
GetExcludeUsername() []string
|
|
|
|
// GetGenerateSuccessEvents return if should generate success events
|
|
|
|
GetGenerateSuccessEvents() bool
|
|
|
|
// RestrictDevelopmentUsername return exclude development username
|
|
|
|
RestrictDevelopmentUsername() []string
|
|
|
|
// FilterNamespaces filters exclude namespace
|
|
|
|
FilterNamespaces(namespaces []string) []string
|
|
|
|
// GetWebhooks returns the webhook configs
|
|
|
|
GetWebhooks() []WebhookConfig
|
|
|
|
// Load loads configuration from a configmap
|
2022-05-17 14:14:31 +00:00
|
|
|
Load(cm *corev1.ConfigMap)
|
2022-05-04 16:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// configuration stores the configuration
|
|
|
|
type configuration struct {
|
|
|
|
mux sync.RWMutex
|
|
|
|
filters []filter
|
|
|
|
excludeGroupRole []string
|
|
|
|
excludeUsername []string
|
|
|
|
restrictDevelopmentUsername []string
|
|
|
|
webhooks []WebhookConfig
|
|
|
|
generateSuccessEvents bool
|
|
|
|
reconcilePolicyReport chan<- bool
|
|
|
|
updateWebhookConfigurations chan<- bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfiguration ...
|
|
|
|
func NewConfiguration(client kubernetes.Interface, reconcilePolicyReport, updateWebhookConfigurations chan<- bool) (Configuration, error) {
|
|
|
|
cd := &configuration{
|
|
|
|
reconcilePolicyReport: reconcilePolicyReport,
|
|
|
|
updateWebhookConfigurations: updateWebhookConfigurations,
|
|
|
|
restrictDevelopmentUsername: []string{"minikube-user", "kubernetes-admin"},
|
|
|
|
excludeGroupRole: defaultExcludeGroupRole,
|
|
|
|
}
|
2022-05-11 06:14:30 +00:00
|
|
|
if cm, err := client.CoreV1().ConfigMaps(kyvernoNamespace).Get(context.TODO(), kyvernoConfigMapName, metav1.GetOptions{}); err != nil {
|
2022-05-04 16:05:03 +00:00
|
|
|
if !errors.IsNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cd.load(cm)
|
|
|
|
}
|
|
|
|
return cd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cd *configuration) ToFilter(kind, namespace, name string) bool {
|
|
|
|
cd.mux.RLock()
|
|
|
|
defer cd.mux.RUnlock()
|
|
|
|
for _, f := range cd.filters {
|
|
|
|
if wildcard.Match(f.Kind, kind) && wildcard.Match(f.Namespace, namespace) && wildcard.Match(f.Name, name) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if kind == "Namespace" {
|
|
|
|
// [Namespace,kube-system,*] || [*,kube-system,*]
|
|
|
|
if (f.Kind == "Namespace" || f.Kind == "*") && wildcard.Match(f.Namespace, name) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cd *configuration) GetExcludeGroupRole() []string {
|
|
|
|
cd.mux.RLock()
|
|
|
|
defer cd.mux.RUnlock()
|
|
|
|
return cd.excludeGroupRole
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cd *configuration) RestrictDevelopmentUsername() []string {
|
|
|
|
cd.mux.RLock()
|
|
|
|
defer cd.mux.RUnlock()
|
|
|
|
return cd.restrictDevelopmentUsername
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cd *configuration) GetExcludeUsername() []string {
|
|
|
|
cd.mux.RLock()
|
|
|
|
defer cd.mux.RUnlock()
|
|
|
|
return cd.excludeUsername
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cd *configuration) GetGenerateSuccessEvents() bool {
|
|
|
|
cd.mux.RLock()
|
|
|
|
defer cd.mux.RUnlock()
|
|
|
|
return cd.generateSuccessEvents
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cd *configuration) FilterNamespaces(namespaces []string) []string {
|
|
|
|
var results []string
|
|
|
|
for _, ns := range namespaces {
|
|
|
|
if !cd.ToFilter("", ns, "") {
|
|
|
|
results = append(results, ns)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cd *configuration) GetWebhooks() []WebhookConfig {
|
|
|
|
cd.mux.RLock()
|
|
|
|
defer cd.mux.RUnlock()
|
|
|
|
return cd.webhooks
|
|
|
|
}
|
|
|
|
|
2022-05-17 14:14:31 +00:00
|
|
|
func (cd *configuration) Load(cm *corev1.ConfigMap) {
|
2022-05-04 16:05:03 +00:00
|
|
|
reconcilePolicyReport, updateWebhook := true, true
|
|
|
|
if cm != nil {
|
|
|
|
logger.Info("load config", "name", cm.Name, "namespace", cm.Namespace)
|
|
|
|
reconcilePolicyReport, updateWebhook = cd.load(cm)
|
|
|
|
} else {
|
|
|
|
logger.Info("unload config")
|
|
|
|
cd.unload()
|
|
|
|
}
|
|
|
|
if reconcilePolicyReport {
|
|
|
|
logger.Info("resource filters changed, sending reconcile signal to the policy controller")
|
|
|
|
cd.reconcilePolicyReport <- true
|
2021-12-08 13:03:07 +00:00
|
|
|
}
|
2022-05-04 16:05:03 +00:00
|
|
|
if updateWebhook {
|
|
|
|
logger.Info("webhook configurations changed, updating webhook configurations")
|
|
|
|
cd.updateWebhookConfigurations <- true
|
2021-12-08 13:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 14:14:31 +00:00
|
|
|
func (cd *configuration) load(cm *corev1.ConfigMap) (reconcilePolicyReport, updateWebhook bool) {
|
2022-05-04 16:05:03 +00:00
|
|
|
logger := logger.WithValues("name", cm.Name, "namespace", cm.Namespace)
|
|
|
|
if cm.Data == nil {
|
|
|
|
logger.V(4).Info("configuration: No data defined in ConfigMap")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cd.mux.Lock()
|
|
|
|
defer cd.mux.Unlock()
|
|
|
|
filters, ok := cm.Data["resourceFilters"]
|
|
|
|
if !ok {
|
|
|
|
logger.V(4).Info("configuration: No resourceFilters defined in ConfigMap")
|
|
|
|
} else {
|
|
|
|
newFilters := parseKinds(filters)
|
|
|
|
if reflect.DeepEqual(newFilters, cd.filters) {
|
|
|
|
logger.V(4).Info("resourceFilters did not change")
|
|
|
|
} else {
|
|
|
|
logger.V(2).Info("Updated resource filters", "oldFilters", cd.filters, "newFilters", newFilters)
|
|
|
|
cd.filters = newFilters
|
|
|
|
reconcilePolicyReport = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
excludeGroupRole, ok := cm.Data["excludeGroupRole"]
|
|
|
|
if !ok {
|
|
|
|
logger.V(4).Info("configuration: No excludeGroupRole defined in ConfigMap")
|
|
|
|
}
|
|
|
|
newExcludeGroupRoles := parseRbac(excludeGroupRole)
|
|
|
|
newExcludeGroupRoles = append(newExcludeGroupRoles, defaultExcludeGroupRole...)
|
|
|
|
if reflect.DeepEqual(newExcludeGroupRoles, cd.excludeGroupRole) {
|
|
|
|
logger.V(4).Info("excludeGroupRole did not change")
|
|
|
|
} else {
|
|
|
|
logger.V(2).Info("Updated resource excludeGroupRoles", "oldExcludeGroupRole", cd.excludeGroupRole, "newExcludeGroupRole", newExcludeGroupRoles)
|
|
|
|
cd.excludeGroupRole = newExcludeGroupRoles
|
|
|
|
reconcilePolicyReport = true
|
|
|
|
}
|
|
|
|
excludeUsername, ok := cm.Data["excludeUsername"]
|
|
|
|
if !ok {
|
|
|
|
logger.V(4).Info("configuration: No excludeUsername defined in ConfigMap")
|
|
|
|
} else {
|
|
|
|
excludeUsernames := parseRbac(excludeUsername)
|
|
|
|
if reflect.DeepEqual(excludeUsernames, cd.excludeUsername) {
|
|
|
|
logger.V(4).Info("excludeGroupRole did not change")
|
|
|
|
} else {
|
|
|
|
logger.V(2).Info("Updated resource excludeUsernames", "oldExcludeUsername", cd.excludeUsername, "newExcludeUsername", excludeUsernames)
|
|
|
|
cd.excludeUsername = excludeUsernames
|
|
|
|
reconcilePolicyReport = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
webhooks, ok := cm.Data["webhooks"]
|
|
|
|
if !ok {
|
|
|
|
if len(cd.webhooks) > 0 {
|
|
|
|
cd.webhooks = nil
|
|
|
|
updateWebhook = true
|
|
|
|
logger.V(4).Info("configuration: Setting namespaceSelector to empty in the webhook configurations")
|
|
|
|
} else {
|
|
|
|
logger.V(4).Info("configuration: No webhook configurations defined in ConfigMap")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cfgs, err := parseWebhooks(webhooks)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "unable to parse webhooks configurations")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if reflect.DeepEqual(cfgs, cd.webhooks) {
|
|
|
|
logger.V(4).Info("webhooks did not change")
|
|
|
|
} else {
|
|
|
|
logger.Info("Updated webhooks configurations", "oldWebhooks", cd.webhooks, "newWebhookd", cfgs)
|
|
|
|
cd.webhooks = cfgs
|
|
|
|
updateWebhook = true
|
|
|
|
}
|
2019-11-18 19:41:37 +00:00
|
|
|
}
|
2022-05-04 16:05:03 +00:00
|
|
|
generateSuccessEvents, ok := cm.Data["generateSuccessEvents"]
|
|
|
|
if !ok {
|
|
|
|
logger.V(4).Info("configuration: No generateSuccessEvents defined in ConfigMap")
|
|
|
|
} else {
|
|
|
|
generateSuccessEvents, err := strconv.ParseBool(generateSuccessEvents)
|
|
|
|
if err != nil {
|
|
|
|
logger.V(4).Info("configuration: generateSuccessEvents must be either true/false")
|
|
|
|
} else if generateSuccessEvents == cd.generateSuccessEvents {
|
|
|
|
logger.V(4).Info("generateSuccessEvents did not change")
|
|
|
|
} else {
|
|
|
|
logger.V(2).Info("Updated generateSuccessEvents", "oldGenerateSuccessEvents", cd.generateSuccessEvents, "newGenerateSuccessEvents", generateSuccessEvents)
|
|
|
|
cd.generateSuccessEvents = generateSuccessEvents
|
|
|
|
reconcilePolicyReport = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cd *configuration) unload() {
|
|
|
|
cd.mux.Lock()
|
|
|
|
defer cd.mux.Unlock()
|
|
|
|
cd.filters = []filter{}
|
|
|
|
cd.excludeGroupRole = []string{}
|
|
|
|
cd.excludeGroupRole = append(cd.excludeGroupRole, defaultExcludeGroupRole...)
|
|
|
|
cd.excludeUsername = []string{}
|
|
|
|
cd.generateSuccessEvents = false
|
2019-11-18 19:41:37 +00:00
|
|
|
}
|