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 30, 2024
1 parent 51d6140 commit 0ace110
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 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
8 changes: 4 additions & 4 deletions pkg/transformer/kubernetes/k8sutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"path/filepath"
"reflect"
"sort"
"strconv"
"testing"

"github.com/kubernetes/kompose/pkg/kobject"
Expand Down Expand Up @@ -208,7 +207,7 @@ func TestCreateServiceWithServiceUser(t *testing.T) {
Expose: []string{"expose"}, // not supported
Privileged: true,
Restart: "always",
User: "1234",
User: "1234:5678",
}

komposeObject := kobject.KomposeObject{
Expand All @@ -224,8 +223,9 @@ func TestCreateServiceWithServiceUser(t *testing.T) {
for _, obj := range objects {
if deploy, ok := obj.(*appsv1.Deployment); ok {
uid := *deploy.Spec.Template.Spec.Containers[0].SecurityContext.RunAsUser
if strconv.FormatInt(uid, 10) != service.User {
t.Errorf("User in ServiceConfig is not matching user in PodSpec")
gid := *deploy.Spec.Template.Spec.Containers[0].SecurityContext.RunAsGroup
if fmt.Sprintf("%d:%d", uid, gid) != service.User {
t.Errorf("User and group in ServiceConfig is not matching user in PodSpec")
}
}
}
Expand Down
30 changes: 25 additions & 5 deletions pkg/transformer/kubernetes/podspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kubernetes
import (
"reflect"
"strconv"
"strings"

mapset "github.com/deckarep/golang-set"
"github.com/kubernetes/kompose/pkg/kobject"
Expand Down Expand Up @@ -143,11 +144,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) {
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 0ace110

Please sign in to comment.