Skip to content
This repository has been archived by the owner on Aug 25, 2021. It is now read-only.

Commit

Permalink
Merge pull request #325 from hashicorp/psps
Browse files Browse the repository at this point in the history
Enterprise License Job PodSecurityPolicy
  • Loading branch information
lkysow authored Jan 10, 2020
2 parents 895f0a9 + 13030c4 commit 1e9f22f
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 118 deletions.
1 change: 0 additions & 1 deletion templates/client-podsecuritypolicy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ spec:
hostIPC: false
hostPID: false
runAsUser:
# Require the container to run without root privileges.
rule: 'RunAsAny'
seLinux:
rule: 'RunAsAny'
Expand Down
1 change: 0 additions & 1 deletion templates/client-snapshot-agent-podsecuritypolicy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ spec:
hostIPC: false
hostPID: false
runAsUser:
# Require the container to run without root privileges.
rule: 'RunAsAny'
seLinux:
rule: 'RunAsAny'
Expand Down
1 change: 0 additions & 1 deletion templates/connect-inject-podsecuritypolicy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ spec:
hostIPC: false
hostPID: false
runAsUser:
# Require the container to run without root privileges.
rule: 'RunAsAny'
seLinux:
rule: 'RunAsAny'
Expand Down
14 changes: 12 additions & 2 deletions templates/enterprise-license-clusterrole.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{{- if (or (and (ne (.Values.server.enabled | toString) "-") .Values.server.enabled) (and (eq (.Values.server.enabled | toString) "-") .Values.global.enabled)) }}
{{- if (or (and (ne (.Values.client.enabled | toString) "-") .Values.client.enabled) (and (eq (.Values.client.enabled | toString) "-") .Values.global.enabled)) }}
{{- if .Values.global.bootstrapACLs }}
{{- if (and .Values.server.enterpriseLicense.secretName .Values.server.enterpriseLicense.secretKey) }}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
Expand All @@ -11,7 +9,9 @@ metadata:
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
{{- if or .Values.global.bootstrapACLs .Values.global.enablePodSecurityPolicies }}
rules:
{{- if .Values.global.bootstrapACLs }}
- apiGroups: [""]
resources:
- secrets
Expand All @@ -20,6 +20,16 @@ rules:
verbs:
- get
{{- end }}
{{- if .Values.global.enablePodSecurityPolicies }}
- apiGroups: ["policy"]
resources: ["podsecuritypolicies"]
resourceNames:
- {{ template "consul.fullname" . }}-enterprise-license
verbs:
- use
{{- end }}
{{- else }}
rules: []
{{- end }}
{{- end }}
{{- end }}
4 changes: 0 additions & 4 deletions templates/enterprise-license-clusterrolebinding.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{{- if (or (and (ne (.Values.server.enabled | toString) "-") .Values.server.enabled) (and (eq (.Values.server.enabled | toString) "-") .Values.global.enabled)) }}
{{- if (or (and (ne (.Values.client.enabled | toString) "-") .Values.client.enabled) (and (eq (.Values.client.enabled | toString) "-") .Values.global.enabled)) }}
{{- if .Values.global.bootstrapACLs }}
{{- if (and .Values.server.enterpriseLicense.secretName .Values.server.enterpriseLicense.secretKey) }}
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
Expand All @@ -21,5 +19,3 @@ subjects:
namespace: {{ .Release.Namespace }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
2 changes: 0 additions & 2 deletions templates/enterprise-license-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ spec:
component: license
spec:
restartPolicy: Never
{{- if .Values.global.bootstrapACLs }}
serviceAccountName: {{ template "consul.fullname" . }}-enterprise-license
{{- end }}
containers:
- name: apply-enterprise-license
image: "{{ default .Values.global.image .Values.server.image }}"
Expand Down
37 changes: 37 additions & 0 deletions templates/enterprise-license-podsecuritypolicy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{{- if (or (and (ne (.Values.server.enabled | toString) "-") .Values.server.enabled) (and (eq (.Values.server.enabled | toString) "-") .Values.global.enabled)) }}
{{- if (and .Values.server.enterpriseLicense.secretName .Values.server.enterpriseLicense.secretKey) }}
{{- if .Values.global.enablePodSecurityPolicies }}
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: {{ template "consul.fullname" . }}-enterprise-license
labels:
app: {{ template "consul.name" . }}
chart: {{ template "consul.chart" . }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
spec:
privileged: false
# Allow core volume types.
volumes:
- 'secret'
allowPrivilegeEscalation: false
# This is redundant with non-root + disallow privilege escalation,
# but we can provide it for defense in depth.
requiredDropCapabilities:
- ALL
hostNetwork: false
hostIPC: false
hostPID: false
runAsUser:
rule: 'RunAsAny'
seLinux:
rule: 'RunAsAny'
supplementalGroups:
rule: 'RunAsAny'
fsGroup:
rule: 'RunAsAny'
readOnlyRootFilesystem: false
{{- end }}
{{- end }}
{{- end }}
4 changes: 0 additions & 4 deletions templates/enterprise-license-serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{{- if (or (and (ne (.Values.server.enabled | toString) "-") .Values.server.enabled) (and (eq (.Values.server.enabled | toString) "-") .Values.global.enabled)) }}
{{- if (or (and (ne (.Values.client.enabled | toString) "-") .Values.client.enabled) (and (eq (.Values.client.enabled | toString) "-") .Values.global.enabled)) }}
{{- if .Values.global.bootstrapACLs }}
{{- if (and .Values.server.enterpriseLicense.secretName .Values.server.enterpriseLicense.secretKey) }}
apiVersion: v1
kind: ServiceAccount
Expand All @@ -14,5 +12,3 @@ metadata:
release: {{ .Release.Name }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
1 change: 0 additions & 1 deletion templates/mesh-gateway-podsecuritypolicy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ spec:
hostIPC: false
hostPID: false
runAsUser:
# Require the container to run without root privileges.
rule: 'RunAsAny'
seLinux:
rule: 'RunAsAny'
Expand Down
1 change: 0 additions & 1 deletion templates/sync-catalog-podsecuritypolicy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ spec:
hostIPC: false
hostPID: false
runAsUser:
# Require the container to run without root privileges.
rule: 'RunAsAny'
seLinux:
rule: 'RunAsAny'
Expand Down
55 changes: 35 additions & 20 deletions test/unit/enterprise-license-clusterrole.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,72 +11,87 @@ load _helpers
[ "${actual}" = "false" ]
}

@test "enterpriseLicense/ClusterRole: disabled with global.bootstrapACLs=true" {
@test "enterpriseLicense/ClusterRole: disabled with server=false, ent secret defined" {
cd `chart_dir`
local actual=$(helm template \
-x templates/enterprise-license-clusterrole.yaml \
--set 'global.bootstrapACLs=true' \
--set 'server.enabled=false' \
--set 'server.enterpriseLicense.secretName=foo' \
--set 'server.enterpriseLicense.secretKey=bar' \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}

@test "enterpriseLicense/ClusterRole: disabled with server=false, global.bootstrapACLs=true, ent secret defined" {
@test "enterpriseLicense/ClusterRole: disabled when ent secretName missing" {
cd `chart_dir`
local actual=$(helm template \
-x templates/enterprise-license-clusterrole.yaml \
--set 'global.bootstrapACLs=true' \
--set 'server.enabled=false' \
--set 'server.enterpriseLicense.secretName=foo' \
--set 'server.enterpriseLicense.secretKey=bar' \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}

@test "enterpriseLicense/ClusterRole: disabled with client=false, global.bootstrapACLs=true, ent secret defined" {
@test "enterpriseLicense/ClusterRole: disabled when ent secretKey missing" {
cd `chart_dir`
local actual=$(helm template \
-x templates/enterprise-license-clusterrole.yaml \
--set 'global.bootstrapACLs=true' \
--set 'client.enabled=false' \
--set 'server.enterpriseLicense.secretName=foo' \
--set 'server.enterpriseLicense.secretKey=bar' \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}

@test "enterpriseLicense/ClusterRole: disabled when ent secretName missing" {
@test "enterpriseLicense/ClusterRole: enabled when ent license defined" {
cd `chart_dir`
local actual=$(helm template \
-x templates/enterprise-license-clusterrole.yaml \
--set 'global.bootstrapACLs=true' \
--set 'server.enterpriseLicense.secretName=foo' \
--set 'server.enterpriseLicense.secretKey=bar' \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
[ "${actual}" = "true" ]
}

@test "enterpriseLicense/ClusterRole: disabled when ent secretKey missing" {
@test "enterpriseLicense/ClusterRole: rules are empty if global.bootstrapACLs and global.enablePodSecurityPolicies are false" {
cd `chart_dir`
local actual=$(helm template \
-x templates/enterprise-license-clusterrole.yaml \
--set 'global.bootstrapACLs=true' \
--set 'server.enterpriseLicense.secretName=foo' \
--set 'server.enterpriseLicense.secretKey=bar' \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
yq '.rules | length' | tee /dev/stderr)
[ "${actual}" = "0" ]
}

@test "enterpriseLicense/ClusterRole: can be enabled" {
#--------------------------------------------------------------------
# global.bootstrapACLs

@test "enterpriseLicense/ClusterRole: allows acl token when global.bootstrapACLs is true" {
cd `chart_dir`
local actual=$(helm template \
-x templates/enterprise-license-clusterrole.yaml \
--set 'server.enterpriseLicense.secretName=foo' \
--set 'server.enterpriseLicense.secretKey=bar' \
--set 'global.bootstrapACLs=true' \
. | tee /dev/stderr |
yq -r '.rules | map(select(.resourceNames[0] == "release-name-consul-enterprise-license-acl-token")) | length' | tee /dev/stderr)
[ "${actual}" = "1" ]
}


#--------------------------------------------------------------------
# global.enablePodSecurityPolicies

@test "enterpriseLicense/ClusterRole: allows podsecuritypolicies access with global.enablePodSecurityPolicies=true" {
cd `chart_dir`
local actual=$(helm template \
-x templates/enterprise-license-clusterrole.yaml \
--set 'server.enterpriseLicense.secretName=foo' \
--set 'server.enterpriseLicense.secretKey=bar' \
--set 'global.enablePodSecurityPolicies=true' \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "true" ]
yq -r '.rules | map(select(.resources[0] == "podsecuritypolicies")) | length' | tee /dev/stderr)
[ "${actual}" = "1" ]
}
31 changes: 2 additions & 29 deletions test/unit/enterprise-license-clusterrolebinding.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,10 @@ load _helpers
[ "${actual}" = "false" ]
}

@test "enterpriseLicense/ClusterRoleBinding: disabled with global.bootstrapACLs=true" {
@test "enterpriseLicense/ClusterRoleBinding: disabled with server=false, ent secret defined" {
cd `chart_dir`
local actual=$(helm template \
-x templates/enterprise-license-clusterrolebinding.yaml \
--set 'global.bootstrapACLs=true' \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}

@test "enterpriseLicense/ClusterRoleBinding: disabled with server=false, global.bootstrapACLs=true, ent secret defined" {
cd `chart_dir`
local actual=$(helm template \
-x templates/enterprise-license-clusterrolebinding.yaml \
--set 'global.bootstrapACLs=true' \
--set 'server.enabled=false' \
--set 'server.enterpriseLicense.secretName=foo' \
--set 'server.enterpriseLicense.secretKey=bar' \
Expand All @@ -34,24 +23,10 @@ load _helpers
[ "${actual}" = "false" ]
}

@test "enterpriseLicense/ClusterRoleBinding: disabled with client=false, global.bootstrapACLs=true, ent secret defined" {
cd `chart_dir`
local actual=$(helm template \
-x templates/enterprise-license-clusterrolebinding.yaml \
--set 'global.bootstrapACLs=true' \
--set 'client.enabled=false' \
--set 'server.enterpriseLicense.secretName=foo' \
--set 'server.enterpriseLicense.secretKey=bar' \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}

@test "enterpriseLicense/ClusterRoleBinding: disabled when ent secretName missing" {
cd `chart_dir`
local actual=$(helm template \
-x templates/enterprise-license-clusterrolebinding.yaml \
--set 'global.bootstrapACLs=true' \
--set 'server.enterpriseLicense.secretKey=bar' \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
Expand All @@ -62,18 +37,16 @@ load _helpers
cd `chart_dir`
local actual=$(helm template \
-x templates/enterprise-license-clusterrolebinding.yaml \
--set 'global.bootstrapACLs=true' \
--set 'server.enterpriseLicense.secretName=foo' \
. | tee /dev/stderr |
yq 'length > 0' | tee /dev/stderr)
[ "${actual}" = "false" ]
}

@test "enterpriseLicense/ClusterRoleBinding: can be enabled" {
@test "enterpriseLicense/ClusterRoleBinding: enabled when ent license defined" {
cd `chart_dir`
local actual=$(helm template \
-x templates/enterprise-license-clusterrolebinding.yaml \
--set 'global.bootstrapACLs=true' \
--set 'server.enterpriseLicense.secretName=foo' \
--set 'server.enterpriseLicense.secretKey=bar' \
. | tee /dev/stderr |
Expand Down
23 changes: 0 additions & 23 deletions test/unit/enterprise-license-job.bats
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,3 @@ load _helpers
yq -r '.command | any(contains("consul-k8s acl-init"))' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

@test "server/EnterpriseLicense: no service account specified when global.bootstrapACLS=false" {
cd `chart_dir`
local actual=$(helm template \
-x templates/enterprise-license-job.yaml \
--set 'server.enterpriseLicense.secretName=foo' \
--set 'server.enterpriseLicense.secretKey=bar' \
. | tee /dev/stderr |
yq '.spec.template.spec.serviceAccountName' | tee /dev/stderr)
[ "${actual}" = "null" ]
}

@test "server/EnterpriseLicense: service account specified when global.bootstrapACLS=true" {
cd `chart_dir`
local actual=$(helm template \
-x templates/enterprise-license-job.yaml \
--set 'server.enterpriseLicense.secretName=foo' \
--set 'server.enterpriseLicense.secretKey=bar' \
--set 'global.bootstrapACLs=true' \
. | tee /dev/stderr |
yq '.spec.template.spec.serviceAccountName | contains("consul-enterprise-license")' | tee /dev/stderr)
[ "${actual}" = "true" ]
}
Loading

0 comments on commit 1e9f22f

Please sign in to comment.