Skip to content

Commit 19e35e1

Browse files
authored
Global floatingUserId (#577)
1 parent 17c9f0d commit 19e35e1

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
@@ -2341,6 +2341,105 @@ roleRef:
23412341
}),
23422342
)
23432343

2344+
DescribeTable("rendering with GlobalFloatingUserId",
2345+
func(floatingUserId bool) {
2346+
cmd := &Command{
2347+
Chart: &Chart{
2348+
Operators: []Operator{
2349+
{
2350+
Name: "painter",
2351+
Deployment: Deployment{
2352+
Container: Container{
2353+
Image: Image{
2354+
Tag: "v0.0.0",
2355+
Repository: "painter",
2356+
Registry: "quay.io/solo-io",
2357+
PullPolicy: "IfNotPresent",
2358+
},
2359+
},
2360+
},
2361+
GlobalFloatingUserIdPath: ".Values.global.securitySettings.floatingUserId",
2362+
},
2363+
},
2364+
// Because the global override comes from .Values it has to be set here, not on the painter
2365+
Values: map[string]interface{}{
2366+
"global": map[string]interface{}{
2367+
"securitySettings": map[string]interface{}{
2368+
"floatingUserId": floatingUserId,
2369+
},
2370+
},
2371+
},
2372+
Data: Data{
2373+
ApiVersion: "v1",
2374+
Description: "",
2375+
Name: "Painting Operator",
2376+
Version: "v0.0.1",
2377+
Home: "https://docs.solo.io/skv2/latest",
2378+
Sources: []string{
2379+
"https://github.com/solo-io/skv2",
2380+
},
2381+
},
2382+
},
2383+
2384+
ManifestRoot: "codegen/test/chart",
2385+
}
2386+
2387+
err := cmd.Execute()
2388+
Expect(err).NotTo(HaveOccurred())
2389+
2390+
runAsUser := 202020
2391+
runAsGroup := 999
2392+
painterValues := map[string]interface{}{
2393+
"enabled": true,
2394+
"runAsUser": runAsUser,
2395+
"podSecurityContext": map[string]interface{}{
2396+
"runAsUser": runAsUser,
2397+
"fsGroup": runAsGroup,
2398+
},
2399+
}
2400+
2401+
helmValues := map[string]interface{}{"painter": painterValues}
2402+
2403+
renderedManifests := helmTemplate("./codegen/test/chart", helmValues)
2404+
2405+
var renderedDeployment *appsv1.Deployment
2406+
decoder := kubeyaml.NewYAMLOrJSONDecoder(bytes.NewBuffer(renderedManifests), 4096)
2407+
for {
2408+
obj := &unstructured.Unstructured{}
2409+
err := decoder.Decode(obj)
2410+
if err != nil {
2411+
break
2412+
}
2413+
if obj.GetName() != "painter" || obj.GetKind() != "Deployment" {
2414+
continue
2415+
}
2416+
2417+
bytes, err := obj.MarshalJSON()
2418+
Expect(err).NotTo(HaveOccurred())
2419+
renderedDeployment = &appsv1.Deployment{}
2420+
err = json.Unmarshal(bytes, renderedDeployment)
2421+
Expect(err).NotTo(HaveOccurred())
2422+
}
2423+
2424+
Expect(renderedDeployment).NotTo(BeNil())
2425+
renderedRunAsUser := renderedDeployment.Spec.Template.Spec.Containers[0].SecurityContext.RunAsUser
2426+
renderedPodSecurityContext := renderedDeployment.Spec.Template.Spec.SecurityContext
2427+
2428+
// When using the global floatingUserId, the container runAsUser and RunAsUser should not be set
2429+
if floatingUserId {
2430+
Expect(renderedRunAsUser).To(BeNil())
2431+
Expect(renderedPodSecurityContext).To(BeNil())
2432+
} else {
2433+
Expect(*renderedRunAsUser).To(Equal(int64(runAsUser)))
2434+
Expect(*renderedPodSecurityContext.RunAsUser).To(Equal(int64(runAsUser)))
2435+
Expect(*renderedPodSecurityContext.FSGroup).To(Equal(int64(runAsGroup)))
2436+
}
2437+
2438+
},
2439+
Entry("Global floatingUserId is true", true),
2440+
Entry("Global floatingUserId is false", false),
2441+
)
2442+
23442443
Describe("rendering template env vars", func() {
23452444
var tmpDir string
23462445

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 ]]
@@ -201,7 +208,12 @@ spec:
201208
{}
202209
{{- else}}
203210
runAsNonRoot: true
211+
[[- /* if there is a GlobalFloatingUserIdPath add it to the runAsuser logic */ -]]
212+
[[- if $operator.GlobalFloatingUserIdPath ]]
213+
{{- if not (or $[[ $operatorVar ]].floatingUserId [[ $operator.GlobalFloatingUserIdPath ]]) }}
214+
[[- else ]]
204215
{{- if not $[[ $operatorVar ]].floatingUserId }}
216+
[[- end ]]
205217
runAsUser: {{ printf "%.0f" (float64 $[[ $operatorVar ]].runAsUser) }}
206218
{{- end }}
207219
readOnlyRootFilesystem: true

0 commit comments

Comments
 (0)