Skip to content

Commit

Permalink
POC set fsGroup when function is non-root
Browse files Browse the repository at this point in the history
**What**
- Set the PodSecurityGroup fsGroup value when the function is non-root.
  This is needed in AWS EKS to enable the container access to the
  ServiceAccount token. This token, in AWS, will be an IAM API token
  that can be used to access AWS services.  Non-root functions will
  otherwise not be able to open the token file, preventing integration
  with AWS services via IAM.

Resovles openfaas#598

Signed-off-by: Lucas Roesler <[email protected]>
  • Loading branch information
LucasRoesler committed Mar 21, 2020
1 parent 1e9ca85 commit 254e10d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions k8s/securityContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import (
// value >10000 per the suggestion from https://kubesec.io/basics/containers-securitycontext-runasuser/
const SecurityContextUserID = int64(12000)

// SecurityContextFSGroup is the arbitrary FSGroup ID value we will set when the pod is configured
// to run as non-root. This is useful in AWS EKS to allow the container to access the service
// account token, per these docs
// https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts-technical-overview.html
const SecurityContextFSGroup = int64(12000)

// ConfigureContainerUserID sets the UID to 12000 for the function Container. Defaults to user
// specified in image metadata if `SetNonRootUser` is `false`. Root == 0.
func (f *FunctionFactory) ConfigureContainerUserID(deployment *appsv1.Deployment) {
Expand All @@ -23,6 +29,15 @@ func (f *FunctionFactory) ConfigureContainerUserID(deployment *appsv1.Deployment
functionUser = &userID
}

if deployment.Spec.Template.Spec.SecurityContext == nil {
deployment.Spec.Template.Spec.SecurityContext = &corev1.PodSecurityContext{}
}

if f.Config.SetNonRootUser {
groupID := SecurityContextFSGroup
deployment.Spec.Template.Spec.SecurityContext.FSGroup = &groupID
}

if deployment.Spec.Template.Spec.Containers[0].SecurityContext == nil {
deployment.Spec.Template.Spec.Containers[0].SecurityContext = &corev1.SecurityContext{}
}
Expand Down

0 comments on commit 254e10d

Please sign in to comment.