From 7c5f941fee000be35ebb9b517864200fd54738c7 Mon Sep 17 00:00:00 2001
From: Zhou Hao <zhouhao@cn.fujitsu.com>
Date: Tue, 27 Mar 2018 14:06:51 +0800
Subject: [PATCH 1/2] generate: add process-username option

Signed-off-by: Zhou Hao <zhouhao@cn.fujitsu.com>
---
 cmd/oci-runtime-tool/generate.go   | 5 +++++
 completions/bash/oci-runtime-tool  | 1 +
 generate/generate.go               | 6 ++++++
 man/oci-runtime-tool-generate.1.md | 3 +++
 4 files changed, 15 insertions(+)

diff --git a/cmd/oci-runtime-tool/generate.go b/cmd/oci-runtime-tool/generate.go
index ad33d4f8a..efe385337 100644
--- a/cmd/oci-runtime-tool/generate.go
+++ b/cmd/oci-runtime-tool/generate.go
@@ -113,6 +113,7 @@ var generateFlags = []cli.Flag{
 	cli.BoolFlag{Name: "process-rlimits-remove-all", Usage: "remove all resource limits for processes inside the container. "},
 	cli.BoolFlag{Name: "process-terminal", Usage: "specifies whether a terminal is attached to the process"},
 	cli.IntFlag{Name: "process-uid", Usage: "uid for the process"},
+	cli.StringFlag{Name: "process-username", Usage: "username for the process"},
 	cli.StringFlag{Name: "rootfs-path", Value: "rootfs", Usage: "path to the root filesystem"},
 	cli.BoolFlag{Name: "rootfs-readonly", Usage: "make the container's rootfs readonly"},
 	cli.StringSliceFlag{Name: "solaris-anet", Usage: "set up networking for Solaris application containers"},
@@ -210,6 +211,10 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
 		g.SetProcessUID(uint32(context.Int("process-uid")))
 	}
 
+	if context.IsSet("process-username") {
+		g.SetProcessUsername(context.String("process-username"))
+	}
+
 	if context.IsSet("process-gid") {
 		g.SetProcessGID(uint32(context.Int("process-gid")))
 	}
diff --git a/completions/bash/oci-runtime-tool b/completions/bash/oci-runtime-tool
index b74423546..cc72c16f7 100755
--- a/completions/bash/oci-runtime-tool
+++ b/completions/bash/oci-runtime-tool
@@ -387,6 +387,7 @@ _oci-runtime-tool_generate() {
 		--process-rlimits-add
 		--process-rlimits-remove
 		--process-uid
+		--process-username
 		--rootfs-path
 		--solaris-anet
 		--solaris-capped-cpu-ncpus
diff --git a/generate/generate.go b/generate/generate.go
index 052340016..88070d07e 100644
--- a/generate/generate.go
+++ b/generate/generate.go
@@ -361,6 +361,12 @@ func (g *Generator) SetProcessUID(uid uint32) {
 	g.spec.Process.User.UID = uid
 }
 
+// SetProcessUsername sets g.spec.Process.User.Username.
+func (g *Generator) SetProcessUsername(username string) {
+	g.initSpecProcess()
+	g.spec.Process.User.Username = username
+}
+
 // SetProcessGID sets g.spec.Process.User.GID.
 func (g *Generator) SetProcessGID(gid uint32) {
 	g.initSpecProcess()
diff --git a/man/oci-runtime-tool-generate.1.md b/man/oci-runtime-tool-generate.1.md
index 56bcf4d34..3c776e023 100644
--- a/man/oci-runtime-tool-generate.1.md
+++ b/man/oci-runtime-tool-generate.1.md
@@ -441,6 +441,9 @@ read the configuration from `config.json`.
 **--process-uid**=UID
   Sets the UID used within the container.
 
+**--process-username**=""
+  Sets the username used within the container.
+
 **--rootfs-path**=ROOTFSPATH
   Path to the rootfs, which can be an absolute path or relative to bundle path.
   e.g the absolute path of rootfs is /to/bundle/rootfs, bundle path is /to/bundle,

From fc1bcf559c3081d6159b13e287b3b8795c9c21fd Mon Sep 17 00:00:00 2001
From: Zhou Hao <zhouhao@cn.fujitsu.com>
Date: Tue, 27 Mar 2018 14:20:36 +0800
Subject: [PATCH 2/2] fix process_user validation

Signed-off-by: Zhou Hao <zhouhao@cn.fujitsu.com>
---
 validation/process_user.go | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/validation/process_user.go b/validation/process_user.go
index 7223bd1bc..6b3bb8954 100644
--- a/validation/process_user.go
+++ b/validation/process_user.go
@@ -1,14 +1,23 @@
 package main
 
 import (
+	"runtime"
+
 	"github.com/opencontainers/runtime-tools/validation/util"
 )
 
 func main() {
 	g := util.GetDefaultGenerator()
-	g.SetProcessUID(10)
-	g.SetProcessGID(10)
-	g.AddProcessAdditionalGid(5)
+
+	switch runtime.GOOS {
+	case "linux", "solaris":
+		g.SetProcessUID(10)
+		g.SetProcessGID(10)
+		g.AddProcessAdditionalGid(5)
+	case "windows":
+		g.SetProcessUsername("test")
+	default:
+	}
 
 	err := util.RuntimeInsideValidate(g, nil)
 	if err != nil {
