Skip to content
Merged
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
1 change: 1 addition & 0 deletions lib/bpf/bpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func New(config *servicecfg.BPFConfig, restrictedSession *servicecfg.RestrictedS
// 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 @@ -52,13 +52,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 @@ -80,7 +86,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 @@ -79,6 +79,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 @@ -1556,6 +1556,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 @@ -1567,6 +1571,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 @@ -32,6 +32,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