Skip to content

Commit

Permalink
fix: security context for ksvc, symlink creation for server
Browse files Browse the repository at this point in the history
  • Loading branch information
Maurice Faber committed Sep 1, 2021
1 parent 262ee5c commit 9a7ad1e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 49 deletions.
6 changes: 3 additions & 3 deletions charts/team-ns/templates/knative-services.yaml
Expand Up @@ -72,9 +72,9 @@ spec:
{{- with $k.resources }}
resources: {{- . | toYaml | nindent 12 }}
{{- end }}
# {{- with $k.securityContext }}
# securityContext: {{- . | toYaml | nindent 12 }}
# {{- end }}
{{- with $k.securityContext }}
securityContext: {{- . | toYaml | nindent 12 }}
{{- end }}
{{- if or $k.files $k.secretMounts }}
volumeMounts:
{{- range $dir, $files := $vols }}
Expand Down
Binary file modified schemas/generated-crd-schemas.tar.gz
Binary file not shown.
32 changes: 14 additions & 18 deletions src/server/index.ts
@@ -1,12 +1,11 @@
/* eslint-disable @typescript-eslint/no-misused-promises, @typescript-eslint/require-await */
import express, { Request, Response } from 'express'
import { existsSync, symlinkSync } from 'fs'
import { existsSync, mkdirSync, symlinkSync } from 'fs'
import { Server } from 'http'
import { commit } from '../cmd/commit'
import { validateValues } from '../cmd/validate-values'
import { decrypt, encrypt } from '../common/crypt'
import { env } from '../common/envalid'
import { rootDir, terminal } from '../common/utils'
import { terminal } from '../common/utils'

const debug = terminal('server')
const app = express()
Expand All @@ -16,20 +15,6 @@ export const stopServer = (): void => {
server?.close()
}

const symlinkEnvDir = (): void => {
const envPath = `${rootDir}/env`
if (!existsSync(env.ENV_DIR)) {
console.warn(`Values at ${env.ENV_DIR} are not mounted yet!`)
return
}
if (!existsSync(envPath)) symlinkSync(env.ENV_DIR, envPath)
}

app.use((req, res, next) => {
symlinkEnvDir()
next()
})

app.get('/', async (req: Request, res: Response): Promise<Response<any>> => {
return res.send({ status: 'ok' })
})
Expand Down Expand Up @@ -72,8 +57,19 @@ app.get('/commit', async (req: Request, res: Response) => {
}
})

export const startServer = (): void => {
export const startServer = async (): Promise<void> => {
server = app.listen(17771, '0.0.0.0')
const k8sEnvDirPath = '/tmp/otomi-values'
const dockerEnvDir = '/home/app/stack/env'
// accomodate k8s deployment with shared values dir, and make symlink to /home/app/stack/env
if (k8sEnvDirPath && !existsSync(k8sEnvDirPath)) {
debug.info('Creating k8s values folder for symlink: ', k8sEnvDirPath)
mkdirSync(k8sEnvDirPath)
if (!existsSync(dockerEnvDir)) {
debug.info(`Creating symlink from ${k8sEnvDirPath} to ${dockerEnvDir}`)
symlinkSync(k8sEnvDirPath, dockerEnvDir)
}
}
debug.log(`Container listening on http://0.0.0.0:17771`)
}

Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/env/teams/jobs.demo.yaml
Expand Up @@ -15,6 +15,11 @@ teamConfig:
runAsNonRoot: true
runAsUser: 1002
runAsGroup: 1002
fsGroup: 1002
securityContext:
runAsNonRoot: true
runAsUser: 1002
runAsGroup: 1002
image:
repository: busybox
tag: latest
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/env/teams/services.demo.yaml
Expand Up @@ -27,6 +27,8 @@ teamConfig:
ownHost: true
auth: true
ksvc:
securityContext:
runAsUser: 1002
image:
repository: otomi/nodejs-helloworld
tag: v1.2.12
Expand Down
68 changes: 40 additions & 28 deletions values-schema.yaml
Expand Up @@ -477,34 +477,33 @@ definitions:
nullable: true
type: object
title: Kubernetes secrets
podSecurityContext:
allOf:
- $ref: '#/definitions/securityContext'
- properties:
fsGroup:
description: Supplementary group ID. Volumes that support ownership management are modified to be owned and writable by this ID.
type: integer
fsGroupChangePolicy:
description:
'Defines behavior for changing ownership and permission of the volume before being exposed inside a Pod. This field only applies to volume types that support fsGroup controlled ownership and permissions.
This field has two possible values:
- OnRootMismatch: Only change permissions and ownership if permission and ownership of root directory does not match with expected permissions of the volume. This could help shorten the time it takes to change ownership and permission of a volume.
- Always: Always change permission and ownership of the volume when volume is mounted.'
type: string
enum:
- Always
- OnRootMismatch
description: Security context for the pod.
title: Pod security context
podSpec:
allOf:
- properties:
annotations:
$ref: '#/definitions/annotations'
title: Pod annotations
podSecurityContext:
allOf:
- $ref: '#/definitions/securityContext'
- properties:
fsGroup:
description: Supplementary group ID. Volumes that support ownership management are modified to be owned and writable by this ID.
type: integer
fsGroupChangePolicy:
description:
'Defines behavior for changing ownership and permission of the volume before being exposed inside a Pod. This field only applies to volume types that support fsGroup controlled ownership and permissions.
This field has two possible values:
- OnRootMismatch: Only change permissions and ownership if permission and ownership of root directory does not match with expected permissions of the volume. This could help shorten the time it takes to change ownership and permission of a volume.
- Always: Always change permission and ownership of the volume when volume is mounted.'
type: string
enum:
- Always
- OnRootMismatch
description: Security context for the pod.
title: Pod security context
required:
- podSecurityContext
type: object
- $ref: '#/definitions/podSecurityContext'
- $ref: '#/definitions/containerSpec'
type: object
portNumber:
Expand Down Expand Up @@ -582,6 +581,11 @@ definitions:
$ref: '#/definitions/resource'
description: Requested resources (guaranteed).
title: Pod resources
runAsUser:
type: integer
minimum": 0
maximum": 65535
default: 1001
scaling:
additionalProperties: false
description: Min and max number of replicas.
Expand Down Expand Up @@ -685,14 +689,11 @@ definitions:
additionalProperties:
uniqueItems: true
properties:
runAsUser:
$ref: '#/definitions/runAsUser'
runAsNonRoot:
type: boolean
default: true
runAsUser:
type: integer
minimum": 0
maximum": 65535
default: 1001
runAsGroup:
type: integer
minimum": 0
Expand Down Expand Up @@ -738,7 +739,18 @@ definitions:
anyOf:
- description: Details for a knative service that will be deployed and operated.
allOf:
- $ref: '#/definitions/podSpec'
- properties:
annotations:
$ref: '#/definitions/annotations'
title: Pod annotations
type: object
- properties:
securityContext:
additionalProperties:
uniqueItems: true
runAsUser:
$ref: '#/definitions/runAsUser'
- $ref: '#/definitions/containerSpec'
- properties:
autoCD:
additionalProperties: false
Expand Down Expand Up @@ -790,7 +802,7 @@ definitions:
description: Scales to zero after 60 seconds and needs approximately 8 seconds to start back up.
title: Scale to zero
type: boolean
- description: Choose this when the ks.vc is already deployed
- description: Choose this when the ksvc is already deployed
properties:
predeployed:
default: true
Expand Down

0 comments on commit 9a7ad1e

Please sign in to comment.