|
| 1 | +//go:build linux |
| 2 | + |
| 3 | +// SPDX-FileCopyrightText: 2026 k0s authors |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +package privilegedport |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "context" |
| 11 | + "errors" |
| 12 | + "fmt" |
| 13 | + "html/template" |
| 14 | + "strconv" |
| 15 | + "strings" |
| 16 | + "testing" |
| 17 | + |
| 18 | + "github.com/k0sproject/k0s/inttest/common" |
| 19 | + "github.com/stretchr/testify/suite" |
| 20 | + "golang.org/x/sys/unix" |
| 21 | +) |
| 22 | + |
| 23 | +type PrivilegedPortSuite struct { |
| 24 | + common.BootlooseSuite |
| 25 | +} |
| 26 | + |
| 27 | +const configWithPrivilegedPort = ` |
| 28 | +apiVersion: k0s.k0sproject.io/v1beta1 |
| 29 | +kind: ClusterConfig |
| 30 | +metadata: |
| 31 | + name: k0s |
| 32 | +spec: |
| 33 | + api: |
| 34 | + port: {{ .Port }} |
| 35 | +` |
| 36 | + |
| 37 | +const privilegedPort = 443 |
| 38 | + |
| 39 | +func TestPrivilegedPortSuite(t *testing.T) { |
| 40 | + s := PrivilegedPortSuite{ |
| 41 | + common.BootlooseSuite{ |
| 42 | + ControllerCount: 1, |
| 43 | + WorkerCount: 0, |
| 44 | + KubeAPIExternalPort: privilegedPort, |
| 45 | + }, |
| 46 | + } |
| 47 | + suite.Run(t, &s) |
| 48 | +} |
| 49 | + |
| 50 | +func (s *PrivilegedPortSuite) getControllerConfig() string { |
| 51 | + data := struct { |
| 52 | + Port int |
| 53 | + }{ |
| 54 | + Port: privilegedPort, |
| 55 | + } |
| 56 | + content := bytes.NewBuffer([]byte{}) |
| 57 | + s.Require().NoError(template.Must(template.New("k0s.yaml").Parse(configWithPrivilegedPort)).Execute(content, data), "can't execute k0s.yaml template") |
| 58 | + return content.String() |
| 59 | +} |
| 60 | + |
| 61 | +func (s *PrivilegedPortSuite) TestCapNetBindServiceIsSet() { |
| 62 | + ctx := s.Context() |
| 63 | + |
| 64 | + // Setup k0s with privileged port configuration |
| 65 | + config := s.getControllerConfig() |
| 66 | + s.PutFile(s.ControllerNode(0), "/tmp/k0s.yaml", config) |
| 67 | + |
| 68 | + s.Require().NoError(s.InitController(0, "--config=/tmp/k0s.yaml")) |
| 69 | + |
| 70 | + kc, err := s.KubeClient(s.ControllerNode(0)) |
| 71 | + s.Require().NoError(err) |
| 72 | + |
| 73 | + s.AssertSomeKubeSystemPods(kc) |
| 74 | + |
| 75 | + // Now verify that kube-apiserver has CAP_NET_BIND_SERVICE |
| 76 | + s.Run("kube-apiserver has CAP_NET_BIND_SERVICE", func() { |
| 77 | + s.Require().NoError(s.verifyCapability(ctx, s.ControllerNode(0))) |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +// verifyCapability checks if the kube-apiserver process has CAP_NET_BIND_SERVICE capability |
| 82 | +func (s *PrivilegedPortSuite) verifyCapability(ctx context.Context, node string) error { |
| 83 | + ssh, err := s.SSH(ctx, node) |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("failed to SSH to node: %w", err) |
| 86 | + } |
| 87 | + defer ssh.Disconnect() |
| 88 | + |
| 89 | + // Find the kube-apiserver PID |
| 90 | + pid, err := ssh.ExecWithOutput(ctx, "pidof kube-apiserver") |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("failed to find kube-apiserver process: %w", err) |
| 93 | + } |
| 94 | + pid = strings.TrimSpace(pid) |
| 95 | + if pid == "" { |
| 96 | + return errors.New("kube-apiserver process not found") |
| 97 | + } |
| 98 | + |
| 99 | + s.T().Logf("Found kube-apiserver with PID: %s", pid) |
| 100 | + |
| 101 | + // Read the capability information from /proc/<pid>/status |
| 102 | + // We need to check CapEff (effective capabilities) |
| 103 | + capOutput, err := ssh.ExecWithOutput(ctx, fmt.Sprintf("grep CapEff /proc/%s/status", pid)) |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("failed to read capabilities: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + s.T().Logf("Capability output: %s", capOutput) |
| 109 | + |
| 110 | + // Parse the capability hex value |
| 111 | + // Format is "CapEff:\t0000000000000400" (or similar) |
| 112 | + parts := strings.Fields(capOutput) |
| 113 | + if len(parts) < 2 { |
| 114 | + return fmt.Errorf("unexpected capability format: %s", capOutput) |
| 115 | + } |
| 116 | + |
| 117 | + capHex := parts[1] |
| 118 | + capValue, err := strconv.ParseUint(capHex, 16, 64) |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("failed to parse capability value %s: %w", capHex, err) |
| 121 | + } |
| 122 | + |
| 123 | + // Check if CAP_NET_BIND_SERVICE (bit 10) is set |
| 124 | + // unix.CAP_NET_BIND_SERVICE is defined in golang.org/x/sys/unix |
| 125 | + capNetBindService := uint64(unix.CAP_NET_BIND_SERVICE) |
| 126 | + if capValue&(1<<capNetBindService) == 0 { |
| 127 | + return fmt.Errorf("CAP_NET_BIND_SERVICE (bit %d) is not set in capabilities: 0x%x", capNetBindService, capValue) |
| 128 | + } |
| 129 | + |
| 130 | + s.T().Logf("CAP_NET_BIND_SERVICE is correctly set (capability value: 0x%x, bit %d is set)", capValue, capNetBindService) |
| 131 | + return nil |
| 132 | +} |
0 commit comments