Skip to content

Commit

Permalink
[v15] Add option to change cgroup root path (enhanced recording) (#38394
Browse files Browse the repository at this point in the history
)

* feat(cgroup): add option to customize root path

* test(cgroup): fix loop issue

* chore(config): add comment on default value
  • Loading branch information
gabrielcorado authored Feb 27, 2024
1 parent 2562dd8 commit 821ebfd
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/bpf/bpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func New(config *servicecfg.BPFConfig) (BPF, error) {
// Create a cgroup controller to add/remote cgroups.
cgroup, err := controlgroup.New(&controlgroup.Config{
MountPath: config.CgroupPath,
RootPath: config.RootPath,
})
if err != nil {
return nil, trace.Wrap(err)
Expand Down
8 changes: 7 additions & 1 deletion lib/cgroup/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,19 @@ var log = logrus.WithFields(logrus.Fields{
type Config struct {
// MountPath is where the cgroupv2 hierarchy is mounted.
MountPath string
// RootPath directory where the Teleport managed cgroups are going to be
// placed.
RootPath string
}

// CheckAndSetDefaults checks BPF configuration.
func (c *Config) CheckAndSetDefaults() error {
if c.MountPath == "" {
c.MountPath = defaults.CgroupPath
}
if c.RootPath == "" {
c.RootPath = teleportRoot
}
return nil
}

Expand All @@ -82,7 +88,7 @@ func New(config *Config) (*Service, error) {

s := &Service{
Config: config,
teleportRoot: filepath.Join(config.MountPath, teleportRoot, uuid.New().String()),
teleportRoot: filepath.Join(config.MountPath, config.RootPath, uuid.New().String()),
}

// Mount the cgroup2 filesystem.
Expand Down
47 changes: 47 additions & 0 deletions lib/cgroup/cgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,53 @@ func TestRootCreate(t *testing.T) {
require.NoDirExists(t, service.teleportRoot)
}

// TestRootCreateCustomRootPath given a service configured with a custom root
// path, cgroups must be placed on the correct path.
func TestRootCreateCustomRootPath(t *testing.T) {
// This test must be run as root. Only root can create cgroups.
if !isRoot() {
t.Skip("Tests for package cgroup can only be run as root.")
}

t.Parallel()

for _, rootPath := range []string{
"custom",
"/custom",
"nested/custom",
"/deep/nested/custom",
} {
rootPath := rootPath
t.Run(rootPath, func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
service, err := New(&Config{
MountPath: dir,
RootPath: rootPath,
})
require.NoError(t, err)
defer service.Close(false)

sessionID := uuid.New().String()
err = service.Create(sessionID)
require.NoError(t, err)

cgroupPath := path.Join(service.teleportRoot, sessionID)
require.DirExists(t, cgroupPath)
require.Contains(t, cgroupPath, rootPath)

err = service.Remove(sessionID)
require.NoError(t, err)
require.NoDirExists(t, cgroupPath)

// Teardown
err = service.Close(false)
require.NoError(t, err)
require.NoDirExists(t, service.teleportRoot)
})
}
}

// TestRootCleanup tests the ability for Teleport to remove and cleanup all
// cgroups which is performed upon startup.
func TestRootCleanup(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions lib/config/fileconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,10 @@ type BPF struct {

// CgroupPath controls where cgroupv2 hierarchy is mounted.
CgroupPath string `yaml:"cgroup_path"`

// RootPath root directory for the Teleport cgroups.
// Optional, defaults to /teleport
RootPath string `yaml:"root_path"`
}

// Parse will parse the enhanced session recording configuration.
Expand All @@ -1581,6 +1585,7 @@ func (b *BPF) Parse() *servicecfg.BPFConfig {
DiskBufferSize: b.DiskBufferSize,
NetworkBufferSize: b.NetworkBufferSize,
CgroupPath: b.CgroupPath,
RootPath: b.RootPath,
}
}

Expand Down
3 changes: 3 additions & 0 deletions lib/service/servicecfg/bpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type BPFConfig struct {

// CgroupPath is where the cgroupv2 hierarchy is mounted.
CgroupPath string

// RootPath root directory for the Teleport cgroups.
RootPath string
}

// CheckAndSetDefaults checks BPF configuration.
Expand Down

0 comments on commit 821ebfd

Please sign in to comment.