Skip to content

Commit b98b2e4

Browse files
authored
Global floatingUserId (#577) (#578)
* Global floatingUserId (#577) * Global floatingUserId (#577)
1 parent 01eb817 commit b98b2e4

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
changelog:
2+
- type: NEW_FEATURE
3+
issueLink: https://github.com/solo-io/gloo/issues/5034
4+
resolvesIssue: false
5+
description: |
6+
Adds the ability to render deployments templates with a reference global floatingUserId field. This field is used to globally unset
7+
the runAsUser field in container securityContexts (like the painter's floatingUserId) and supresses the rendering of the
8+
pod's securityContext. This feature is enabled by setting the GlobalFloatingUserIdPath in the Operator to the path of the global field,
9+
and defaults to an empty string.

codegen/cmd_test.go

+99
Original file line numberDiff line numberDiff line change
@@ -2134,6 +2134,105 @@ roleRef:
21342134
}),
21352135
)
21362136

2137+
DescribeTable("rendering with GlobalFloatingUserId",
2138+
func(floatingUserId bool) {
2139+
cmd := &Command{
2140+
Chart: &Chart{
2141+
Operators: []Operator{
2142+
{
2143+
Name: "painter",
2144+
Deployment: Deployment{
2145+
Container: Container{
2146+
Image: Image{
2147+
Tag: "v0.0.0",
2148+
Repository: "painter",
2149+
Registry: "quay.io/solo-io",
2150+
PullPolicy: "IfNotPresent",
2151+
},
2152+
},
2153+
},
2154+
GlobalFloatingUserIdPath: ".Values.global.securitySettings.floatingUserId",
2155+
},
2156+
},
2157+
// Because the global override comes from .Values it has to be set here, not on the painter
2158+
Values: map[string]interface{}{
2159+
"global": map[string]interface{}{
2160+
"securitySettings": map[string]interface{}{
2161+
"floatingUserId": floatingUserId,
2162+
},
2163+
},
2164+
},
2165+
Data: Data{
2166+
ApiVersion: "v1",
2167+
Description: "",
2168+
Name: "Painting Operator",
2169+
Version: "v0.0.1",
2170+
Home: "https://docs.solo.io/skv2/latest",
2171+
Sources: []string{
2172+
"https://github.com/solo-io/skv2",
2173+
},
2174+
},
2175+
},
2176+
2177+
ManifestRoot: "codegen/test/chart",
2178+
}
2179+
2180+
err := cmd.Execute()
2181+
Expect(err).NotTo(HaveOccurred())
2182+
2183+
runAsUser := 202020
2184+
runAsGroup := 999
2185+
painterValues := map[string]interface{}{
2186+
"enabled": true,
2187+
"runAsUser": runAsUser,
2188+
"podSecurityContext": map[string]interface{}{
2189+
"runAsUser": runAsUser,
2190+
"fsGroup": runAsGroup,
2191+
},
2192+
}
2193+
2194+
helmValues := map[string]interface{}{"painter": painterValues}
2195+
2196+
renderedManifests := helmTemplate("./codegen/test/chart", helmValues)
2197+
2198+
var renderedDeployment *appsv1.Deployment
2199+
decoder := kubeyaml.NewYAMLOrJSONDecoder(bytes.NewBuffer(renderedManifests), 4096)
2200+
for {
2201+
obj := &unstructured.Unstructured{}
2202+
err := decoder.Decode(obj)
2203+
if err != nil {
2204+
break
2205+
}
2206+
if obj.GetName() != "painter" || obj.GetKind() != "Deployment" {
2207+
continue
2208+
}
2209+
2210+
bytes, err := obj.MarshalJSON()
2211+
Expect(err).NotTo(HaveOccurred())
2212+
renderedDeployment = &appsv1.Deployment{}
2213+
err = json.Unmarshal(bytes, renderedDeployment)
2214+
Expect(err).NotTo(HaveOccurred())
2215+
}
2216+
2217+
Expect(renderedDeployment).NotTo(BeNil())
2218+
renderedRunAsUser := renderedDeployment.Spec.Template.Spec.Containers[0].SecurityContext.RunAsUser
2219+
renderedPodSecurityContext := renderedDeployment.Spec.Template.Spec.SecurityContext
2220+
2221+
// When using the global floatingUserId, the container runAsUser and RunAsUser should not be set
2222+
if floatingUserId {
2223+
Expect(renderedRunAsUser).To(BeNil())
2224+
Expect(renderedPodSecurityContext).To(BeNil())
2225+
} else {
2226+
Expect(*renderedRunAsUser).To(Equal(int64(runAsUser)))
2227+
Expect(*renderedPodSecurityContext.RunAsUser).To(Equal(int64(runAsUser)))
2228+
Expect(*renderedPodSecurityContext.FSGroup).To(Equal(int64(runAsGroup)))
2229+
}
2230+
2231+
},
2232+
Entry("Global floatingUserId is true", true),
2233+
Entry("Global floatingUserId is false", false),
2234+
)
2235+
21372236
Describe("rendering template env vars", func() {
21382237
var tmpDir string
21392238

codegen/model/chart.go

+3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ type Operator struct {
9898
//
9999
// E.g: `and (.Values.operator.customValueA) (.Values.operator.customValueB)`
100100
CustomEnableCondition string
101+
102+
// Optional: if specified, will use this path in rendering template logic
103+
GlobalFloatingUserIdPath string
101104
}
102105

103106
func (o Operator) FormattedName() string {

codegen/templates/chart/operator-deployment.yamltmpl

+12
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ spec:
8787
spec:
8888
serviceAccountName: [[ $operator.Name ]]
8989
{{- /* Override the default podSecurityContext config if it is set. */}}
90+
[[- /* the GlobalFloatingUserId is expected to disable the pod security context */ -]]
91+
[[- if $operator.GlobalFloatingUserIdPath ]]
92+
{{- if not [[ $operator.GlobalFloatingUserIdPath ]] }}
93+
[[- end ]]
9094
{{- if or ([[ (opVar $operator) ]].podSecurityContext) (eq "map[]" (printf "%v" [[ (opVar $operator) ]].podSecurityContext)) }}
9195
securityContext:
9296
{{ toYaml [[ (opVar $operator) ]].podSecurityContext | indent 8 }}
@@ -96,6 +100,9 @@ spec:
96100
[[ toYaml $podSecurityContext | indent 8 ]]
97101
[[- end ]]
98102
{{- end }}
103+
[[- if $operator.GlobalFloatingUserIdPath ]] [[/* end the "if" if GlobalFloatingUserId is being checked */]]
104+
{{- end }}
105+
[[- end ]]
99106
[[- if $volumes ]]
100107
volumes:
101108
[[ toYaml $volumes | indent 6 ]]
@@ -192,7 +199,12 @@ spec:
192199
{}
193200
{{- else}}
194201
runAsNonRoot: true
202+
[[- /* if there is a GlobalFloatingUserIdPath add it to the runAsuser logic */ -]]
203+
[[- if $operator.GlobalFloatingUserIdPath ]]
204+
{{- if not (or $[[ $operatorVar ]].floatingUserId [[ $operator.GlobalFloatingUserIdPath ]]) }}
205+
[[- else ]]
195206
{{- if not $[[ $operatorVar ]].floatingUserId }}
207+
[[- end ]]
196208
runAsUser: {{ printf "%.0f" (float64 $[[ $operatorVar ]].runAsUser) }}
197209
{{- end }}
198210
readOnlyRootFilesystem: true

0 commit comments

Comments
 (0)