Skip to content

Commit 3ba183c

Browse files
author
Nikolas De Giorgis
authored
add user and missing package to agent (#382)
1 parent 2c0e35e commit 3ba183c

File tree

4 files changed

+57
-20
lines changed

4 files changed

+57
-20
lines changed

agent/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ARG tools_version
66
RUN apt-get -qq update \
77
&& apt-get -y -qq install \
88
curl \
9+
libnss-wrapper \
910
&& apt-get upgrade -y -qq \
1011
&& apt-get dist-upgrade -y -qq \
1112
&& rm -rf /var/lib/apt/lists/*
@@ -32,4 +33,5 @@ RUN curl --fail --retry 3 --silent https://downloads.mongodb.org/tools/db/mongod
3233
&& tar xfz mongodb-tools.tgz --directory /var/lib/mongodb-mms-automation/ \
3334
&& rm mongodb-tools.tgz
3435

36+
USER 2000
3537
CMD ["agent/mongodb-agent", "-cluster=/var/lib/automation/config/automation-config.json"]

controllers/construct/mongodbstatefulset.go

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ const (
4343
headlessAgentEnv = "HEADLESS_AGENT"
4444
podNamespaceEnv = "POD_NAMESPACE"
4545
automationConfigEnv = "AUTOMATION_CONFIG_MAP"
46+
47+
automationconfFilePath = "/data/automation-mongod.conf"
48+
keyfileFilePath = "/var/lib/mongodb-mms-automation/authentication/keyfile"
4649
)
4750

4851
// MongoDBStatefulSetOwner is an interface which any resource which generates a MongoDB StatefulSet should implement.
@@ -112,6 +115,7 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
112115
statefulset.WithVolumeClaim(logVolumeName, logsPvc()),
113116
statefulset.WithPodSpecTemplate(
114117
podtemplatespec.Apply(
118+
podtemplatespec.WithSecurityContext(podtemplatespec.DefaultPodSecurityContext()),
115119
podtemplatespec.WithPodLabels(labels),
116120
podtemplatespec.WithVolume(healthStatusVolume),
117121
podtemplatespec.WithVolume(hooksVolume),
@@ -128,23 +132,34 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
128132
}
129133

130134
func mongodbAgentContainer(automationConfigSecretName string, volumeMounts []corev1.VolumeMount) container.Modification {
135+
agentCommand := strings.Join([]string{
136+
"agent/mongodb-agent",
137+
"-cluster=" + clusterFilePath,
138+
"-skipMongoStart",
139+
"-noDaemonize",
140+
"-healthCheckFilePath=" + agentHealthStatusFilePathValue,
141+
"-serveStatusPort=5000",
142+
"-useLocalMongoDbTools"}, " ")
131143
return container.Apply(
132144
container.WithName(AgentName),
133145
container.WithImage(os.Getenv(AgentImageEnv)),
134146
container.WithImagePullPolicy(corev1.PullAlways),
135147
container.WithReadinessProbe(DefaultReadiness()),
136148
container.WithResourceRequirements(resourcerequirements.Defaults()),
137149
container.WithVolumeMounts(volumeMounts),
138-
container.WithCommand([]string{
139-
"agent/mongodb-agent",
140-
"-cluster=" + clusterFilePath,
141-
"-skipMongoStart",
142-
"-noDaemonize",
143-
"-healthCheckFilePath=" + agentHealthStatusFilePathValue,
144-
"-serveStatusPort=5000",
145-
"-useLocalMongoDbTools",
146-
},
147-
),
150+
container.WithSecurityContext(container.DefaultSecurityContext()),
151+
container.WithCommand([]string{"/bin/bash", "-c", `current_uid=$(id -u)
152+
echo $current_uid
153+
declare -r current_uid
154+
if ! grep -q "${current_uid}" /etc/passwd ; then
155+
sed -e "s/^mongodb:/builder:/" /etc/passwd > /tmp/passwd
156+
echo "mongodb:x:$(id -u):$(id -g):,,,:/:/bin/bash" >> /tmp/passwd
157+
cat /tmp/passwd
158+
export NSS_WRAPPER_PASSWD=/tmp/passwd
159+
export LD_PRELOAD=libnss_wrapper.so
160+
export NSS_WRAPPER_GROUP=/etc/group
161+
fi
162+
` + agentCommand}),
148163
container.WithEnvs(
149164
corev1.EnvVar{
150165
Name: headlessAgentEnv,
@@ -227,32 +242,37 @@ func getMongoDBImage(version string) string {
227242
}
228243

229244
func mongodbContainer(version string, volumeMounts []corev1.VolumeMount) container.Modification {
230-
mongoDbCommand := []string{
231-
"/bin/sh",
232-
"-c",
233-
`
234-
# run post-start hook to handle version changes
245+
mongoDbCommand := fmt.Sprintf(`
246+
#run post-start hook to handle version changes
235247
/hooks/version-upgrade
236248
237-
# wait for config to be created by the agent
238-
while [ ! -f /data/automation-mongod.conf ]; do sleep 3 ; done ; sleep 2 ;
249+
# wait for config and keyfile to be created by the agent
250+
while ! [ -f %s -a -f %s ]; do sleep 3 ; done ; sleep 2 ;
251+
239252
240253
# start mongod with this configuration
241-
exec mongod -f /data/automation-mongod.conf ;
242-
`,
254+
exec mongod -f %s;
255+
`, automationconfFilePath, keyfileFilePath, automationconfFilePath)
256+
257+
containerCommand := []string{
258+
"/bin/sh",
259+
"-c",
260+
mongoDbCommand,
243261
}
244262

245263
return container.Apply(
246264
container.WithName(MongodbName),
247265
container.WithImage(getMongoDBImage(version)),
248266
container.WithResourceRequirements(resourcerequirements.Defaults()),
249-
container.WithCommand(mongoDbCommand),
267+
container.WithCommand(containerCommand),
250268
container.WithEnvs(
251269
corev1.EnvVar{
252270
Name: agentHealthStatusFilePathEnv,
253271
Value: "/healthstatus/agent-health-status.json",
254272
},
255273
),
256274
container.WithVolumeMounts(volumeMounts),
275+
276+
container.WithSecurityContext(container.DefaultSecurityContext()),
257277
)
258278
}

pkg/kube/container/containers.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,12 @@ func WithSecurityContext(context corev1.SecurityContext) Modification {
183183
container.SecurityContext = &context
184184
}
185185
}
186+
187+
// DefaultSecurityContext returns the default security context for containers.
188+
// It sets RunAsUser = 2000 and RunAsNonRoot = true
189+
func DefaultSecurityContext() corev1.SecurityContext {
190+
runAsNonRoot := true
191+
runAsUser := int64(2000)
192+
193+
return corev1.SecurityContext{RunAsUser: &runAsUser, RunAsNonRoot: &runAsNonRoot}
194+
}

pkg/kube/podtemplatespec/podspec_template.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ func WithSecurityContext(securityContext corev1.PodSecurityContext) Modification
144144
}
145145
}
146146

147+
// DefaultPodSecurityContext returns the default pod security context with FsGroup = 2000
148+
func DefaultPodSecurityContext() corev1.PodSecurityContext {
149+
fsGroup := int64(2000)
150+
return corev1.PodSecurityContext{FSGroup: &fsGroup}
151+
}
152+
147153
// WithImagePullSecrets adds an ImagePullSecrets local reference with the given name
148154
func WithImagePullSecrets(name string) Modification {
149155
return func(podTemplateSpec *corev1.PodTemplateSpec) {

0 commit comments

Comments
 (0)