Skip to content

Commit

Permalink
Merge pull request #461 from flatcar/tormath1/cilium-selinux
Browse files Browse the repository at this point in the history
kubeadm: add logic to enforce SELinux for Cilium CNI in Flatcar >= 3745
  • Loading branch information
tormath1 authored Oct 10, 2023
2 parents fbb5dee + daead39 commit 96843d2
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 15 deletions.
34 changes: 32 additions & 2 deletions kola/tests/kubeadm/kubeadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ var (
_ = c.MustSSH(controller, "/opt/bin/cilium uninstall")
version := params["CiliumVersion"].(string)
cidr := params["PodSubnet"].(string)
cmd := fmt.Sprintf("/opt/bin/cilium install --config enable-endpoint-routes=true --config cluster-pool-ipv4-cidr=%s --version=%s --encryption=ipsec --wait --wait-duration 1m", cidr, version)
_ = c.MustSSH(controller, cmd)
cmd := fmt.Sprintf("/opt/bin/cilium install --config enable-endpoint-routes=true --config cluster-pool-ipv4-cidr=%s --version=%s --encryption=ipsec --wait=false --restart-unmanaged-pods=false --rollback=false", cidr, version)
_, _ = c.SSH(controller, cmd)
patch := `{ grep -q svirt_lxc_file_t /etc/selinux/mcs/contexts/lxc_contexts && /opt/bin/kubectl --namespace kube-system patch daemonset/cilium -p '{"spec":{"template":{"spec":{"containers":[{"name":"cilium-agent","securityContext":{"seLinuxOptions":{"level":"s0","type":"unconfined_t"}}}],"initContainers":[{"name":"mount-cgroup","securityContext":{"seLinuxOptions":{"level":"s0","type":"unconfined_t"}}},{"name":"apply-sysctl-overwrites","securityContext":{"seLinuxOptions":{"level":"s0","type":"unconfined_t"}}},{"name":"clean-cilium-state","securityContext":{"seLinuxOptions":{"level":"s0","type":"unconfined_t"}}}]}}}}'; } || true`
_ = c.MustSSH(controller, patch)
status := "/opt/bin/cilium status --wait --wait-duration 1m"
_ = c.MustSSH(controller, status)
},
},
},
Expand Down Expand Up @@ -368,6 +372,32 @@ func setup(c cluster.TestCluster, params map[string]interface{}) (platform.Machi
return nil, fmt.Errorf("unable to create etcd node: %w", err)
}

v := string(c.MustSSH(etcdNode, `set -euo pipefail; grep -m 1 "^VERSION=" /usr/lib/os-release | cut -d = -f 2`))
if v == "" {
c.Fatalf("Assertion for version string failed")
}

version, err := semver.NewVersion(v)
if err != nil {
c.Fatalf("unable to create semver version from %s: %v", version, err)
}

// For Cilium CNI, we enforce SELinux only for version >= 3745 because the SELinux policies update (container_t/spc_t) is not yet
// propagated through all the channels.
// The etcd node will run with enforced SELinux anyway but we want to test SELinux on the worker / master nodes.
cni, ok := params["CNI"]
if !ok {
c.Fatal("unable to get CNI value")
}

if cni == "cilium" && version.LessThan(semver.Version{Major: 3745}) {
r := c.RuntimeConf()
if r != nil {
plog.Infof("Setting SELinux to permissive mode")
r.NoEnableSelinux = true
}
}

if err := etcd.GetClusterHealth(c, etcdNode, 1); err != nil {
return nil, fmt.Errorf("unable to get etcd node health: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions kola/tests/kubeadm/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ EOF
--config enable-endpoint-routes=true \
--config cluster-pool-ipv4-cidr={{ .PodSubnet }} \
--version={{ .CiliumVersion }} 2>&1 | iconv --from-code utf-8 --to-code ascii//TRANSLIT
{ grep -q svirt_lxc_file_t /etc/selinux/mcs/contexts/lxc_contexts && kubectl --namespace kube-system patch daemonset/cilium -p '{"spec":{"template":{"spec":{"containers":[{"name":"cilium-agent","securityContext":{"seLinuxOptions":{"level":"s0","type":"unconfined_t"}}}],"initContainers":[{"name":"mount-cgroup","securityContext":{"seLinuxOptions":{"level":"s0","type":"unconfined_t"}}},{"name":"apply-sysctl-overwrites","securityContext":{"seLinuxOptions":{"level":"s0","type":"unconfined_t"}}},{"name":"clean-cilium-state","securityContext":{"seLinuxOptions":{"level":"s0","type":"unconfined_t"}}}]}}}}'; } || true
# --wait will wait for status to report success
/opt/bin/cilium status --wait 2>&1 | iconv --from-code utf-8 --to-code ascii//TRANSLIT
{{ end }}
Expand Down
1 change: 1 addition & 0 deletions kola/tests/kubeadm/testdata/master-cilium-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ EOF
--config enable-endpoint-routes=true \
--config cluster-pool-ipv4-cidr=192.168.0.0/17 \
--version=v0.11.1 2>&1 | iconv --from-code utf-8 --to-code ascii//TRANSLIT
{ grep -q svirt_lxc_file_t /etc/selinux/mcs/contexts/lxc_contexts && kubectl --namespace kube-system patch daemonset/cilium -p '{"spec":{"template":{"spec":{"containers":[{"name":"cilium-agent","securityContext":{"seLinuxOptions":{"level":"s0","type":"unconfined_t"}}}],"initContainers":[{"name":"mount-cgroup","securityContext":{"seLinuxOptions":{"level":"s0","type":"unconfined_t"}}},{"name":"apply-sysctl-overwrites","securityContext":{"seLinuxOptions":{"level":"s0","type":"unconfined_t"}}},{"name":"clean-cilium-state","securityContext":{"seLinuxOptions":{"level":"s0","type":"unconfined_t"}}}]}}}}'; } || true
# --wait will wait for status to report success
/opt/bin/cilium status --wait 2>&1 | iconv --from-code utf-8 --to-code ascii//TRANSLIT

Expand Down
4 changes: 2 additions & 2 deletions platform/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ func (bc *BaseCluster) Name() string {
return bc.name
}

func (bc *BaseCluster) RuntimeConf() RuntimeConfig {
return *bc.rconf
func (bc *BaseCluster) RuntimeConf() *RuntimeConfig {
return bc.rconf
}

func (bc *BaseCluster) ConsoleOutput() map[string]string {
Expand Down
2 changes: 1 addition & 1 deletion platform/machine/aws/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (am *machine) PrivateIP() string {
return *am.mach.PrivateIpAddress
}

func (am *machine) RuntimeConf() platform.RuntimeConfig {
func (am *machine) RuntimeConf() *platform.RuntimeConfig {
return am.cluster.RuntimeConf()
}

Expand Down
2 changes: 1 addition & 1 deletion platform/machine/azure/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (am *machine) PrivateIP() string {
return am.mach.PrivateIPAddress
}

func (am *machine) RuntimeConf() platform.RuntimeConfig {
func (am *machine) RuntimeConf() *platform.RuntimeConfig {
return am.cluster.RuntimeConf()
}

Expand Down
2 changes: 1 addition & 1 deletion platform/machine/do/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (dm *machine) PrivateIP() string {
return dm.privateIP
}

func (dm *machine) RuntimeConf() platform.RuntimeConfig {
func (dm *machine) RuntimeConf() *platform.RuntimeConfig {
return dm.cluster.RuntimeConf()
}

Expand Down
2 changes: 1 addition & 1 deletion platform/machine/equinixmetal/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (pm *machine) PrivateIP() string {
return pm.privateIP
}

func (pm *machine) RuntimeConf() platform.RuntimeConfig {
func (pm *machine) RuntimeConf() *platform.RuntimeConfig {
return pm.cluster.RuntimeConf()
}

Expand Down
2 changes: 1 addition & 1 deletion platform/machine/esx/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (em *machine) PrivateIP() string {
return em.mach.IPAddress
}

func (em *machine) RuntimeConf() platform.RuntimeConfig {
func (em *machine) RuntimeConf() *platform.RuntimeConfig {
return em.cluster.RuntimeConf()
}

Expand Down
2 changes: 1 addition & 1 deletion platform/machine/external/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (pm *machine) PrivateIP() string {
return pm.ipAddr
}

func (pm *machine) RuntimeConf() platform.RuntimeConfig {
func (pm *machine) RuntimeConf() *platform.RuntimeConfig {
return pm.cluster.RuntimeConf()
}

Expand Down
2 changes: 1 addition & 1 deletion platform/machine/gcloud/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (gm *machine) PrivateIP() string {
return gm.intIP
}

func (gm *machine) RuntimeConf() platform.RuntimeConfig {
func (gm *machine) RuntimeConf() *platform.RuntimeConfig {
return gm.gc.RuntimeConf()
}

Expand Down
2 changes: 1 addition & 1 deletion platform/machine/openstack/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (om *machine) PrivateIP() string {
return om.IP()
}

func (om *machine) RuntimeConf() platform.RuntimeConfig {
func (om *machine) RuntimeConf() *platform.RuntimeConfig {
return om.cluster.RuntimeConf()
}

Expand Down
2 changes: 1 addition & 1 deletion platform/machine/qemu/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (m *machine) PrivateIP() string {
return m.netif.DHCPv4[0].IP.String()
}

func (m *machine) RuntimeConf() platform.RuntimeConfig {
func (m *machine) RuntimeConf() *platform.RuntimeConfig {
return m.qc.RuntimeConf()
}

Expand Down
2 changes: 1 addition & 1 deletion platform/machine/unprivqemu/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (m *machine) PrivateIP() string {
return m.privateAddr
}

func (m *machine) RuntimeConf() platform.RuntimeConfig {
func (m *machine) RuntimeConf() *platform.RuntimeConfig {
return m.qc.RuntimeConf()
}

Expand Down
5 changes: 4 additions & 1 deletion platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Machine interface {
PrivateIP() string

// RuntimeConf returns the cluster's runtime configuration.
RuntimeConf() RuntimeConfig
RuntimeConf() *RuntimeConfig

// SSHClient establishes a new SSH connection to the machine.
SSHClient() (*ssh.Client, error)
Expand Down Expand Up @@ -113,6 +113,9 @@ type Cluster interface {
// IgnitionVersion returns the version of Ignition supported by the
// cluster
IgnitionVersion() string

// RuntimeConf returns a pointer to the runtime configuration.
RuntimeConf() *RuntimeConfig
}

// Flight represents a group of Clusters within a single platform.
Expand Down

0 comments on commit 96843d2

Please sign in to comment.