cli: add --selinux flag to agent/server sub-cmds (#2111)

* cli: add --selinux flag to agent/server sub-cmds

Introduces --selinux flag to affirmatively enable SELinux in containerd.
Deprecates --disable-selinux flag which now defaults to true which
auto-detection of SELinux configuration for containerd is no longer
supported.  Specifying both --selinux and --disable-selinux will result
in an error message encouraging you to pick a side.

* Update pkg/agent/containerd/containerd.go

update log warning message about enabled selinux host but disabled runtime

Co-authored-by: Brad Davidson <brad@oatmail.org>
Signed-off-by: Jacob Blain Christen <jacob@rancher.com>
This commit is contained in:
Jacob Blain Christen 2020-08-11 16:17:32 -07:00 committed by GitHub
parent 4a68698014
commit e2089bea18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 25 deletions

View File

@ -399,7 +399,7 @@ func get(envInfo *cmds.Agent, proxy proxy.Proxy) (*config.Node, error) {
nodeConfig := &config.Node{ nodeConfig := &config.Node{
Docker: envInfo.Docker, Docker: envInfo.Docker,
DisableSELinux: envInfo.DisableSELinux, SELinux: envInfo.EnableSELinux,
ContainerRuntimeEndpoint: envInfo.ContainerRuntimeEndpoint, ContainerRuntimeEndpoint: envInfo.ContainerRuntimeEndpoint,
FlannelBackend: controlConfig.FlannelBackend, FlannelBackend: controlConfig.FlannelBackend,
} }
@ -484,7 +484,6 @@ func get(envInfo *cmds.Agent, proxy proxy.Proxy) (*config.Node, error) {
nodeConfig.AgentConfig.DisableKubeProxy = controlConfig.DisableKubeProxy nodeConfig.AgentConfig.DisableKubeProxy = controlConfig.DisableKubeProxy
nodeConfig.AgentConfig.Rootless = envInfo.Rootless nodeConfig.AgentConfig.Rootless = envInfo.Rootless
nodeConfig.AgentConfig.PodManifests = filepath.Join(envInfo.DataDir, DefaultPodManifestPath) nodeConfig.AgentConfig.PodManifests = filepath.Join(envInfo.DataDir, DefaultPodManifestPath)
nodeConfig.DisableSELinux = envInfo.DisableSELinux
nodeConfig.AgentConfig.ProtectKernelDefaults = envInfo.ProtectKernelDefaults nodeConfig.AgentConfig.ProtectKernelDefaults = envInfo.ProtectKernelDefaults
return nodeConfig, nil return nodeConfig, nil

View File

@ -233,15 +233,10 @@ func setupContainerdConfig(ctx context.Context, cfg *config.Node) error {
if err != nil { if err != nil {
return errors.Wrap(err, "failed to detect selinux") return errors.Wrap(err, "failed to detect selinux")
} }
if cfg.DisableSELinux { switch {
containerdConfig.SELinuxEnabled = false case !cfg.SELinux && selEnabled:
if selEnabled { logrus.Warn("SELinux is enabled on this host, but " + version.Program + " has not been started with --selinux - containerd SELinux support is disabled")
logrus.Warn("SELinux is enabled for system but has been disabled for containerd by override") case cfg.SELinux && !selConfigured:
}
} else {
containerdConfig.SELinuxEnabled = selEnabled
}
if containerdConfig.SELinuxEnabled && !selConfigured {
logrus.Warnf("SELinux is enabled for "+version.Program+" but process is not running in context '%s', "+version.Program+"-selinux policy may need to be applied", SELinuxContextType) logrus.Warnf("SELinux is enabled for "+version.Program+" but process is not running in context '%s', "+version.Program+"-selinux policy may need to be applied", SELinuxContextType)
} }

View File

@ -10,7 +10,6 @@ import (
type ContainerdConfig struct { type ContainerdConfig struct {
NodeConfig *config.Node NodeConfig *config.Node
IsRunningInUserNS bool IsRunningInUserNS bool
SELinuxEnabled bool
PrivateRegistryConfig *Registry PrivateRegistryConfig *Registry
} }
@ -21,7 +20,7 @@ const ContainerdConfigTemplate = `
[plugins.cri] [plugins.cri]
stream_server_address = "127.0.0.1" stream_server_address = "127.0.0.1"
stream_server_port = "10010" stream_server_port = "10010"
enable_selinux = {{ .SELinuxEnabled }} enable_selinux = {{ .NodeConfig.SELinux }}
{{- if .IsRunningInUserNS }} {{- if .IsRunningInUserNS }}
disable_cgroup = true disable_cgroup = true

View File

@ -4,6 +4,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/pkg/errors"
"github.com/rancher/k3s/pkg/version" "github.com/rancher/k3s/pkg/version"
"github.com/rancher/spur/cli" "github.com/rancher/spur/cli"
"github.com/rancher/spur/cli/altsrc" "github.com/rancher/spur/cli/altsrc"
@ -31,7 +32,7 @@ type Agent struct {
Rootless bool Rootless bool
RootlessAlreadyUnshared bool RootlessAlreadyUnshared bool
WithNodeID bool WithNodeID bool
DisableSELinux bool EnableSELinux bool
ExtraKubeletArgs []string ExtraKubeletArgs []string
ExtraKubeProxyArgs []string ExtraKubeProxyArgs []string
Labels []string Labels []string
@ -139,25 +140,47 @@ var (
Destination: &AgentConfig.Labels, Destination: &AgentConfig.Labels,
} }
DisableSELinuxFlag = cli.BoolFlag{ DisableSELinuxFlag = cli.BoolFlag{
Name: "disable-selinux", Name: "disable-selinux",
Usage: "(agent/node) Disable SELinux in containerd if currently enabled", Usage: "(deprecated) Use --selinux to explicitly enable SELinux",
Hidden: true, Hidden: true,
Destination: &AgentConfig.DisableSELinux, Value: true, // disabled by default
} }
ProtectKernelDefaultsFlag = cli.BoolFlag{ ProtectKernelDefaultsFlag = cli.BoolFlag{
Name: "protect-kernel-defaults", Name: "protect-kernel-defaults",
Usage: "(agent/node) Kernel tuning behavior. If set, error if kernel tunables are different than kubelet defaults.", Usage: "(agent/node) Kernel tuning behavior. If set, error if kernel tunables are different than kubelet defaults.",
Destination: &AgentConfig.ProtectKernelDefaults, Destination: &AgentConfig.ProtectKernelDefaults,
} }
SELinuxFlag = cli.BoolFlag{
Name: "selinux",
Usage: "(agent/node) Enable SELinux in containerd",
Hidden: false,
Destination: &AgentConfig.EnableSELinux,
EnvVars: []string{version.ProgramUpper + "_SELINUX"},
}
) )
func CheckSELinuxFlags(ctx *cli.Context) error {
disable, enable := DisableSELinuxFlag.Name, SELinuxFlag.Name
switch {
case ctx.IsSet(disable) && ctx.IsSet(enable):
return errors.Errorf("--%s is deprecated in favor of --%s to affirmatively enable it in containerd", disable, enable)
case ctx.IsSet(disable):
AgentConfig.EnableSELinux = !ctx.Bool(disable)
}
return nil
}
func NewAgentCommand(action func(ctx *cli.Context) error) *cli.Command { func NewAgentCommand(action func(ctx *cli.Context) error) *cli.Command {
return &cli.Command{ return &cli.Command{
Name: "agent", Name: "agent",
Usage: "Run node agent", Usage: "Run node agent",
UsageText: appName + " agent [OPTIONS]", UsageText: appName + " agent [OPTIONS]",
Before: DebugContext(cli.InitAllInputSource(altsrc.NewConfigFromFlag(ConfigFlag.Name))), Before: func(ctx *cli.Context) error {
Action: InitLogging(action), if err := CheckSELinuxFlags(ctx); err != nil {
return err
}
return DebugContext(cli.InitAllInputSource(altsrc.NewConfigFromFlag(ConfigFlag.Name)))(ctx)
},
Action: InitLogging(action),
Flags: []cli.Flag{ Flags: []cli.Flag{
&ConfigFlag, &ConfigFlag,
&DebugFlag, &DebugFlag,
@ -194,7 +217,6 @@ func NewAgentCommand(action func(ctx *cli.Context) error) *cli.Command {
&NodeLabels, &NodeLabels,
&NodeTaints, &NodeTaints,
&DockerFlag, &DockerFlag,
&DisableSELinuxFlag,
&CRIEndpointFlag, &CRIEndpointFlag,
&PauseImageFlag, &PauseImageFlag,
&SnapshotterFlag, &SnapshotterFlag,
@ -212,9 +234,11 @@ func NewAgentCommand(action func(ctx *cli.Context) error) *cli.Command {
Usage: "(experimental) Run rootless", Usage: "(experimental) Run rootless",
Destination: &AgentConfig.Rootless, Destination: &AgentConfig.Rootless,
}, },
&SELinuxFlag,
// Deprecated/hidden below // Deprecated/hidden below
&DisableSELinuxFlag,
&FlannelFlag, &FlannelFlag,
&cli.StringFlag{ &cli.StringFlag{
Name: "cluster-secret", Name: "cluster-secret",

View File

@ -63,8 +63,13 @@ func NewServerCommand(action func(*cli.Context) error) *cli.Command {
Name: "server", Name: "server",
Usage: "Run management server", Usage: "Run management server",
UsageText: appName + " server [OPTIONS]", UsageText: appName + " server [OPTIONS]",
Before: DebugContext(cli.InitAllInputSource(altsrc.NewConfigFromFlag(ConfigFlag.Name))), Before: func(ctx *cli.Context) error {
Action: InitLogging(action), if err := CheckSELinuxFlags(ctx); err != nil {
return err
}
return DebugContext(cli.InitAllInputSource(altsrc.NewConfigFromFlag(ConfigFlag.Name)))(ctx)
},
Action: InitLogging(action),
Flags: []cli.Flag{ Flags: []cli.Flag{
&ConfigFlag, &ConfigFlag,
&DebugFlag, &DebugFlag,
@ -235,7 +240,6 @@ func NewServerCommand(action func(*cli.Context) error) *cli.Command {
&NodeLabels, &NodeLabels,
&NodeTaints, &NodeTaints,
&DockerFlag, &DockerFlag,
&DisableSELinuxFlag,
&CRIEndpointFlag, &CRIEndpointFlag,
&PauseImageFlag, &PauseImageFlag,
&SnapshotterFlag, &SnapshotterFlag,
@ -290,9 +294,11 @@ func NewServerCommand(action func(*cli.Context) error) *cli.Command {
Usage: "(experimental) Enable Secret encryption at rest", Usage: "(experimental) Enable Secret encryption at rest",
Destination: &ServerConfig.EncryptSecrets, Destination: &ServerConfig.EncryptSecrets,
}, },
&SELinuxFlag,
// Hidden/Deprecated flags below // Hidden/Deprecated flags below
&DisableSELinuxFlag,
&FlannelFlag, &FlannelFlag,
&cli.StringSliceFlag{ &cli.StringSliceFlag{
Name: "no-deploy", Name: "no-deploy",

View File

@ -26,7 +26,7 @@ type Node struct {
Docker bool Docker bool
ContainerRuntimeEndpoint string ContainerRuntimeEndpoint string
NoFlannel bool NoFlannel bool
DisableSELinux bool SELinux bool
FlannelBackend string FlannelBackend string
FlannelConf string FlannelConf string
FlannelConfOverride bool FlannelConfOverride bool
@ -46,6 +46,7 @@ type Containerd struct {
Config string Config string
Opt string Opt string
Template string Template string
SELinux bool
} }
type Agent struct { type Agent struct {