Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: allow setting security_opt option #382

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## UNRELEASED

IMPROVEMENTS:

* config: Allow setting `security_opt` option.

## 0.6.1 (July 15, 2024)

BUG FIXES:
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,16 @@ config {
}
```

* **security_opt** - (Optional) A list of security-related options that are set in the container.

```hcl
config {
security_opt = [
"no-new-privileges"
]
}
```

* **selinux_opts** - (Optional) A list of process labels the container will use.

```
Expand Down
14 changes: 13 additions & 1 deletion api/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ type ContainerBasicConfig struct {
// container.
// Optional.
Env map[string]string `json:"env,omitempty"`
// Labels nested indicates whether or not the container is allowed
// to run fully nested containers including SELinux labelling.
// Optional.
LabelsNested bool `json:"labels_nested,omitempty"`
// Labels are key-value pairs that are used to add metadata to
// containers.
// Optional.
Expand Down Expand Up @@ -298,7 +302,15 @@ type ContainerSecurityConfig struct {
// privileges flag on create, which disables gaining additional
// privileges (e.g. via setuid) in the container.
NoNewPrivileges bool `json:"no_new_privileges,omitempty"`

// Mask is the path we want to mask in the container. This masks the paths
// given in addition to the default list.
// Optional
Mask []string `json:"mask,omitempty"`
// Unmask a path in the container. Some paths are masked by default,
// preventing them from being accessed within the container; this undoes that masking.
// If ALL is passed, all paths will be unmasked.
// Optional.
Unmask []string `json:"unmask,omitempty"`
// IDMappings are UID and GID mappings that will be used by user
// namespaces.
// Required if UserNS is private.
Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ var (
"readonly_rootfs": hclspec.NewAttr("readonly_rootfs", "bool", false),
"userns": hclspec.NewAttr("userns", "string", false),
"shm_size": hclspec.NewAttr("shm_size", "string", false),
"security_opt": hclspec.NewAttr("security_opt", "list(string)", false),
})
)

Expand Down Expand Up @@ -224,4 +225,5 @@ type TaskConfig struct {
ReadOnlyRootfs bool `codec:"readonly_rootfs"`
UserNS string `codec:"userns"`
ShmSize string `codec:"shm_size"`
SecurityOpt []string `codec:"security_opt"`
}
60 changes: 60 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,11 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
createOpts.ContainerSecurityConfig.ReadOnlyFilesystem = driverConfig.ReadOnlyRootfs
createOpts.ContainerSecurityConfig.ApparmorProfile = driverConfig.ApparmorProfile

// add security_opt if configured
if securiyOptsErr := parseSecurityOpt(driverConfig.SecurityOpt, &createOpts); securiyOptsErr != nil {
return nil, nil, fmt.Errorf("failed to parse security_opt configuration: %v", securiyOptsErr)
}

// Populate --userns mode only if configured
if driverConfig.UserNS != "" {
userns := strings.SplitN(driverConfig.UserNS, ":", 2)
Expand Down Expand Up @@ -1526,3 +1531,58 @@ func setExtraHosts(hosts []string, createOpts *api.SpecGenerator) error {
createOpts.ContainerNetworkConfig.HostAdd = slices.Clone(hosts)
return nil
}

func parseSecurityOpt(securityOpt []string, createOpts *api.SpecGenerator) error {
labelMap := make(map[string]string)
for _, opt := range securityOpt {
con := strings.SplitN(opt, "=", 2)
if len(con) == 1 && con[0] != "no-new-privileges" {
if strings.Contains(opt, ":") {
con = strings.SplitN(opt, ":", 2)
} else {
return fmt.Errorf("invalid security_opt: %q", opt)
}
}

switch con[0] {
case "no-new-privileges":
createOpts.ContainerSecurityConfig.NoNewPrivileges = true
continue
case "label":
if con[1] == "nested" {
createOpts.ContainerBasicConfig.LabelsNested = true
continue
}
labelValue := strings.SplitN(con[1], ":", 2)
if len(labelValue) == 2 {
labelMap[labelValue[0]] = labelValue[1]
createOpts.ContainerBasicConfig.Labels = labelMap
}
case "apparmor":
createOpts.ContainerSecurityConfig.ApparmorProfile = con[1]
case "seccomp":
if con[1] != "empty" && con[1] != "default" && con[1] != "image" {
createOpts.ContainerSecurityConfig.SeccompProfilePath = con[1]
continue
}
// For empty, default, image profile
createOpts.ContainerSecurityConfig.SeccompPolicy = con[1]
case "proc-opts":
procOptsList := strings.Split(con[1], ",")
createOpts.ContainerSecurityConfig.ProcOpts = procOptsList
case "mask":
maskList := strings.Split(con[1], ":")
createOpts.ContainerSecurityConfig.Mask = maskList
case "unmask":
if con[1] == "ALL" {
createOpts.ContainerSecurityConfig.Unmask = []string{con[1]}
continue
}
unmaskList := strings.Split(con[1], ":")
createOpts.ContainerSecurityConfig.Unmask = unmaskList
default:
return fmt.Errorf("invalid security_opt: %q", opt)
}
}
return nil
}
10 changes: 10 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,16 @@ func TestPodmanDriver_Caps(t *testing.T) {
}
}

// check security_opt option
func TestPodmanDriver_SecurityOpt(t *testing.T) {
taskCfg := newTaskConfig("", busyboxLongRunningCmd)
// add a security_opt
taskCfg.SecurityOpt = []string{"no-new-privileges"}
inspectData := startDestroyInspect(t, taskCfg, "securityopt")
// and compare it
must.SliceContains(t, inspectData.HostConfig.SecurityOpt, "no-new-privileges")
}

// check enabled tty option
func TestPodmanDriver_Tty(t *testing.T) {
ci.Parallel(t)
Expand Down