Skip to content

Commit e7f9e86

Browse files
committed
chore: add ReadonlyRootfs and SecurityOpt option
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 2fa691c commit e7f9e86

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

api/handlers/container/create.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
154154
groupAdd = req.HostConfig.GroupAdd
155155
}
156156

157+
securityOpt := []string{}
158+
if req.HostConfig.SecurityOpt != nil {
159+
securityOpt = req.HostConfig.SecurityOpt
160+
}
161+
157162
globalOpt := ncTypes.GlobalCommandOptions(*h.Config)
158163
createOpt := ncTypes.ContainerCreateOptions{
159164
Stdout: nil,
@@ -205,7 +210,7 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
205210
// #endregion
206211

207212
// #region for security flags
208-
SecurityOpt: []string{}, // nerdctl default.
213+
SecurityOpt: securityOpt, // nerdctl default.
209214
CapAdd: capAdd,
210215
CapDrop: capDrop,
211216
Privileged: req.HostConfig.Privileged,
@@ -246,6 +251,9 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
246251
Stderr: nil,
247252
},
248253
// #endregion
254+
255+
// #region for rootfs flags
256+
ReadOnly: req.HostConfig.ReadonlyRootfs, // Is the container root filesystem in read-only
249257
}
250258

251259
portMappings, err := translatePortMappings(req.HostConfig.PortBindings)

api/handlers/container/create_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,29 @@ var _ = Describe("Container Create API ", func() {
652652
Expect(rr.Body).Should(MatchJSON(jsonResponse))
653653
})
654654

655+
It("should set ReadonlyRootfs and SecurityOpt option", func() {
656+
body := []byte(`{
657+
"Image": "test-image",
658+
"HostConfig": {
659+
"ReadonlyRootfs": true,
660+
"SecurityOpt": [ "seccomp=/path/to/custom_seccomp.json", "apparmor=unconfined"]
661+
}
662+
}`)
663+
req, _ := http.NewRequest(http.MethodPost, "/containers/create", bytes.NewReader(body))
664+
665+
// expected create options
666+
createOpt.ReadOnly = true
667+
createOpt.SecurityOpt = []string{"seccomp=/path/to/custom_seccomp.json", "apparmor=unconfined"}
668+
669+
service.EXPECT().Create(gomock.Any(), "test-image", nil, equalTo(createOpt), equalTo(netOpt)).Return(
670+
cid, nil)
671+
672+
// handler should return success message with 201 status code.
673+
h.create(rr, req)
674+
Expect(rr).Should(HaveHTTPStatus(http.StatusCreated))
675+
Expect(rr.Body).Should(MatchJSON(jsonResponse))
676+
})
677+
655678
It("should return 404 if the image was not found", func() {
656679
body := []byte(`{"Image": "test-image"}`)
657680
req, _ := http.NewRequest(http.MethodPost, "/containers/create", bytes.NewReader(body))

api/types/container_types.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ type ContainerHostConfig struct {
8181
IpcMode string // IPC namespace to use for the container
8282
// TODO: Cgroup CgroupSpec // Cgroup to use for the container
8383
// TODO: Links []string // List of links (in the name:alias form)
84-
OomKillDisable bool // specifies whether to disable OOM Killer
85-
OomScoreAdj int // specifies the tune container’s OOM preferences (-1000 to 1000, rootless: 100 to 1000)
86-
PidMode string // PID namespace to use for the container
87-
Privileged bool // Is the container in privileged mode
84+
OomKillDisable bool // specifies whether to disable OOM Killer
85+
OomScoreAdj int // specifies the tune container’s OOM preferences (-1000 to 1000, rootless: 100 to 1000)
86+
PidMode string // PID namespace to use for the container
87+
Privileged bool // Is the container in privileged mode
88+
ReadonlyRootfs bool // Is the container root filesystem in read-only
89+
SecurityOpt []string // List of string values to customize labels for MLS systems, such as SELinux. (["key=value"])
8890
// TODO: PublishAllPorts bool // Should docker publish all exposed port for the container
89-
// TODO: ReadonlyRootfs bool // Is the container root filesystem in read-only
90-
// TODO: SecurityOpt []string // List of string values to customize labels for MLS systems, such as SELinux.
9191
// TODO: StorageOpt map[string]string `json:",omitempty"` // Storage driver options per container.
9292
// TODO: Tmpfs map[string]string `json:",omitempty"` // List of tmpfs (mounts) used for the container
9393
// TODO: UTSMode UTSMode // UTS namespace to use for the container

0 commit comments

Comments
 (0)