Skip to content

Commit

Permalink
feature: support UID:GID in the user key
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Aug 28, 2024
1 parent 51d6140 commit cb63129
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
29 changes: 24 additions & 5 deletions pkg/transformer/kubernetes/k8sutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 +641,30 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
securityContext.Privileged = &service.Privileged
}
if service.User != "" {
uid, err := strconv.ParseInt(service.User, 10, 64)
if err != nil {
log.Warn("Ignoring user directive. User to be specified as a UID (numeric).")
} else {
securityContext.RunAsUser = &uid
switch userparts := strings.Split(service.User, ":"); len(userparts) {
default:
log.Warn("Ignoring ill-formed user directive. Must be in format UID or UID:GID.")
case 1:
uid, err := strconv.ParseInt(userparts[0], 10, 64)
if err != nil {
log.Warn("Ignoring user directive. User to be specified as a UID (numeric).")
} else {
securityContext.RunAsUser = &uid
}
case 2:
uid, err := strconv.ParseInt(userparts[0], 10, 64)
if err != nil {
log.Warn("Ignoring user name in user directive. User to be specified as a UID (numeric).")
} else {
securityContext.RunAsUser = &uid
}

gid, err := strconv.ParseInt(userparts[1], 10, 64)
if err != nil {
log.Warn("Ignoring group name in user directive. Group to be specified as a GID (numeric).")
} else {
securityContext.RunAsGroup = &gid
}
}
}

Expand Down
29 changes: 24 additions & 5 deletions pkg/transformer/kubernetes/podspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,30 @@ func SecurityContext(name string, service kobject.ServiceConfig) PodSpecOption {
securityContext.Privileged = &service.Privileged
}
if service.User != "" {
uid, err := strconv.ParseInt(service.User, 10, 64)
if err != nil {
log.Warn("Ignoring user directive. User to be specified as a UID (numeric).")
} else {
securityContext.RunAsUser = &uid
switch userparts := strings.Split(service.User, ":"); len(userparts) {

Check failure on line 146 in pkg/transformer/kubernetes/podspec.go

View workflow job for this annotation

GitHub Actions / Build

undefined: strings

Check failure on line 146 in pkg/transformer/kubernetes/podspec.go

View workflow job for this annotation

GitHub Actions / Test with 1.21 and CROSS_COMPILE=true

undefined: strings

Check failure on line 146 in pkg/transformer/kubernetes/podspec.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

undefined: strings

Check failure on line 146 in pkg/transformer/kubernetes/podspec.go

View workflow job for this annotation

GitHub Actions / Test with 1.21 and CROSS_COMPILE=false

undefined: strings

Check failure on line 146 in pkg/transformer/kubernetes/podspec.go

View workflow job for this annotation

GitHub Actions / lint (1.22)

undefined: strings

Check failure on line 146 in pkg/transformer/kubernetes/podspec.go

View workflow job for this annotation

GitHub Actions / Test with 1.22 and CROSS_COMPILE=true

undefined: strings

Check failure on line 146 in pkg/transformer/kubernetes/podspec.go

View workflow job for this annotation

GitHub Actions / Test with 1.22 and CROSS_COMPILE=false

undefined: strings
default:
log.Warn("Ignoring ill-formed user directive. Must be in format UID or UID:GID.")
case 1:
uid, err := strconv.ParseInt(userparts[0], 10, 64)
if err != nil {
log.Warn("Ignoring user directive. User to be specified as a UID (numeric).")
} else {
securityContext.RunAsUser = &uid
}
case 2:
uid, err := strconv.ParseInt(userparts[0], 10, 64)
if err != nil {
log.Warn("Ignoring user name in user directive. User to be specified as a UID (numeric).")
} else {
securityContext.RunAsUser = &uid
}

gid, err := strconv.ParseInt(userparts[1], 10, 64)
if err != nil {
log.Warn("Ignoring group name in user directive. Group to be specified as a GID (numeric).")
} else {
securityContext.RunAsGroup = &gid
}
}
}

Expand Down

0 comments on commit cb63129

Please sign in to comment.