|
@@ -45,12 +45,24 @@ const (
|
|
|
|
|
|
|
|
// Config options for the autocert admission controller.
|
|
// Config options for the autocert admission controller.
|
|
|
type Config struct {
|
|
type Config struct {
|
|
|
- LogFormat string `yaml:"logFormat"`
|
|
|
|
|
- CaURL string `yaml:"caUrl"`
|
|
|
|
|
- CertLifetime string `yaml:"certLifetime"`
|
|
|
|
|
- Bootstrapper corev1.Container `yaml:"bootstrapper"`
|
|
|
|
|
- Renewer corev1.Container `yaml:"renewer"`
|
|
|
|
|
- CertsVolume corev1.Volume `yaml:"certsVolume"`
|
|
|
|
|
|
|
+ LogFormat string `yaml:"logFormat"`
|
|
|
|
|
+ CaURL string `yaml:"caUrl"`
|
|
|
|
|
+ CertLifetime string `yaml:"certLifetime"`
|
|
|
|
|
+ Bootstrapper corev1.Container `yaml:"bootstrapper"`
|
|
|
|
|
+ Renewer corev1.Container `yaml:"renewer"`
|
|
|
|
|
+ CertsVolume corev1.Volume `yaml:"certsVolume"`
|
|
|
|
|
+ RestrictCertificatesToNamespace bool `yaml:"restrictCertificatesToNamespace"`
|
|
|
|
|
+ ClusterDomain string `yaml:"clusterDomain"`
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// GetClusterDomain returns the Kubernetes cluster domain, defaults to
|
|
|
|
|
+// "cluster.local" if not specified in the configuration.
|
|
|
|
|
+func (c Config) GetClusterDomain() string {
|
|
|
|
|
+ if c.ClusterDomain != "" {
|
|
|
|
|
+ return c.ClusterDomain
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return "cluster.local"
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// PatchOperation represents a RFC6902 JSONPatch Operation
|
|
// PatchOperation represents a RFC6902 JSONPatch Operation
|
|
@@ -216,6 +228,7 @@ func mkBootstrapper(config *Config, commonName string, namespace string, provisi
|
|
|
Name: "COMMON_NAME",
|
|
Name: "COMMON_NAME",
|
|
|
Value: commonName,
|
|
Value: commonName,
|
|
|
})
|
|
})
|
|
|
|
|
+
|
|
|
b.Env = append(b.Env, corev1.EnvVar{
|
|
b.Env = append(b.Env, corev1.EnvVar{
|
|
|
Name: "STEP_TOKEN",
|
|
Name: "STEP_TOKEN",
|
|
|
ValueFrom: &corev1.EnvVarSource{
|
|
ValueFrom: &corev1.EnvVarSource{
|
|
@@ -357,7 +370,8 @@ func addAnnotations(existing, new map[string]string) (ops []PatchOperation) {
|
|
|
func patch(pod *corev1.Pod, namespace string, config *Config, provisioner Provisioner) ([]byte, error) {
|
|
func patch(pod *corev1.Pod, namespace string, config *Config, provisioner Provisioner) ([]byte, error) {
|
|
|
var ops []PatchOperation
|
|
var ops []PatchOperation
|
|
|
|
|
|
|
|
- commonName := pod.ObjectMeta.GetAnnotations()[admissionWebhookAnnotationKey]
|
|
|
|
|
|
|
+ annotations := pod.ObjectMeta.GetAnnotations()
|
|
|
|
|
+ commonName := annotations[admissionWebhookAnnotationKey]
|
|
|
renewer := mkRenewer(config)
|
|
renewer := mkRenewer(config)
|
|
|
bootstrapper, err := mkBootstrapper(config, commonName, namespace, provisioner)
|
|
bootstrapper, err := mkBootstrapper(config, commonName, namespace, provisioner)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -376,7 +390,10 @@ func patch(pod *corev1.Pod, namespace string, config *Config, provisioner Provis
|
|
|
// shouldMutate checks whether a pod is subject to mutation by this admission controller. A pod
|
|
// shouldMutate checks whether a pod is subject to mutation by this admission controller. A pod
|
|
|
// is subject to mutation if it's annotated with the `admissionWebhookAnnotationKey` and if it
|
|
// is subject to mutation if it's annotated with the `admissionWebhookAnnotationKey` and if it
|
|
|
// has not already been processed (indicated by `admissionWebhookStatusKey` set to `injected`).
|
|
// has not already been processed (indicated by `admissionWebhookStatusKey` set to `injected`).
|
|
|
-func shouldMutate(metadata *metav1.ObjectMeta) bool {
|
|
|
|
|
|
|
+// If the pod requests a certificate with a subject matching a namespace other than its own
|
|
|
|
|
+// and restrictToNamespace is true, then shouldMutate will return a validation error
|
|
|
|
|
+// that should be returned to the client.
|
|
|
|
|
+func shouldMutate(metadata *metav1.ObjectMeta, namespace string, clusterDomain string, restrictToNamespace bool) (bool, error) {
|
|
|
annotations := metadata.GetAnnotations()
|
|
annotations := metadata.GetAnnotations()
|
|
|
if annotations == nil {
|
|
if annotations == nil {
|
|
|
annotations = map[string]string{}
|
|
annotations = map[string]string{}
|
|
@@ -385,10 +402,26 @@ func shouldMutate(metadata *metav1.ObjectMeta) bool {
|
|
|
// Only mutate if the object is annotated appropriately (annotation key set) and we haven't
|
|
// Only mutate if the object is annotated appropriately (annotation key set) and we haven't
|
|
|
// mutated already (status key isn't set).
|
|
// mutated already (status key isn't set).
|
|
|
if annotations[admissionWebhookAnnotationKey] == "" || annotations[admissionWebhookStatusKey] == "injected" {
|
|
if annotations[admissionWebhookAnnotationKey] == "" || annotations[admissionWebhookStatusKey] == "injected" {
|
|
|
- return false
|
|
|
|
|
|
|
+ return false, nil
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if !restrictToNamespace {
|
|
|
|
|
+ return true, nil
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ subject := strings.Trim(annotations[admissionWebhookAnnotationKey], ".")
|
|
|
|
|
+
|
|
|
|
|
+ err := fmt.Errorf("subject \"%s\" matches a namespace other than \"%s\" and is not permitted. This check can be disabled by setting restrictCertificatesToNamespace to false in the autocert-config ConfigMap", subject, namespace)
|
|
|
|
|
+
|
|
|
|
|
+ if strings.HasSuffix(subject, ".svc") && !strings.HasSuffix(subject, fmt.Sprintf(".%s.svc", namespace)) {
|
|
|
|
|
+ return false, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return true
|
|
|
|
|
|
|
+ if strings.HasSuffix(subject, fmt.Sprintf(".svc.%s", clusterDomain)) && !strings.HasSuffix(subject, fmt.Sprintf(".%s.svc.%s", namespace, clusterDomain)) {
|
|
|
|
|
+ return false, err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return true, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// mutate takes an `AdmissionReview`, determines whether it is subject to mutation, and returns
|
|
// mutate takes an `AdmissionReview`, determines whether it is subject to mutation, and returns
|
|
@@ -418,7 +451,20 @@ func mutate(review *v1beta1.AdmissionReview, config *Config, provisioner Provisi
|
|
|
"user": request.UserInfo,
|
|
"user": request.UserInfo,
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- if !shouldMutate(&pod.ObjectMeta) {
|
|
|
|
|
|
|
+ mutationAllowed, validationErr := shouldMutate(&pod.ObjectMeta, request.Namespace, config.GetClusterDomain(), config.RestrictCertificatesToNamespace)
|
|
|
|
|
+
|
|
|
|
|
+ if validationErr != nil {
|
|
|
|
|
+ ctxLog.WithField("error", validationErr).Info("Validation error")
|
|
|
|
|
+ return &v1beta1.AdmissionResponse{
|
|
|
|
|
+ Allowed: false,
|
|
|
|
|
+ UID: request.UID,
|
|
|
|
|
+ Result: &metav1.Status{
|
|
|
|
|
+ Message: validationErr.Error(),
|
|
|
|
|
+ },
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if !mutationAllowed {
|
|
|
ctxLog.WithField("annotations", pod.Annotations).Info("Skipping mutation")
|
|
ctxLog.WithField("annotations", pod.Annotations).Info("Skipping mutation")
|
|
|
return &v1beta1.AdmissionResponse{
|
|
return &v1beta1.AdmissionResponse{
|
|
|
Allowed: true,
|
|
Allowed: true,
|