Skip to content

Commit

Permalink
feat: Disable auto-enroll via environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
codingllama committed Oct 17, 2024
1 parent 22edf09 commit 1fa2a9b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/devicetrust/enroll/auto_enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ package enroll

import (
"context"
"errors"
"os"
"strconv"

"github.com/gravitational/trace"

devicepb "github.com/gravitational/teleport/api/gen/proto/go/teleport/devicetrust/v1"
"github.com/gravitational/teleport/lib/devicetrust/native"
)

// ErrAutoEnrollDisabled signifies that auto-enroll is disabled in the current
// device.
// Setting the TELEPORT_DEVICE_AUTO_ENROLL_DISABLED=1 environment disables
// auto-enroll.
var ErrAutoEnrollDisabled = errors.New("auto-enroll disabled")

// AutoEnrollCeremony is the auto-enrollment version of [Ceremony].
type AutoEnrollCeremony struct {
*Ceremony
Expand All @@ -53,6 +62,11 @@ func AutoEnroll(ctx context.Context, devicesClient devicepb.DeviceTrustServiceCl
// [devicepb.DeviceTrustServiceClient.CreateDeviceEnrollToken] and enrolls the
// device using a regular [Ceremony].
func (c *AutoEnrollCeremony) Run(ctx context.Context, devicesClient devicepb.DeviceTrustServiceClient) (*devicepb.Device, error) {
const autoEnrollDisabledKey = "TELEPORT_DEVICE_AUTO_ENROLL_DISABLED"
if disabled, _ := strconv.ParseBool(os.Getenv(autoEnrollDisabledKey)); disabled {
return nil, trace.Wrap(ErrAutoEnrollDisabled)
}

cd, err := c.CollectDeviceData(native.CollectedDataAlwaysEscalate)
if err != nil {
return nil, trace.Wrap(err, "collecting device data")
Expand Down
8 changes: 8 additions & 0 deletions lib/devicetrust/enroll/auto_enroll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package enroll_test

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -68,3 +69,10 @@ func TestAutoEnrollCeremony_Run(t *testing.T) {
})
}
}

func TestAutoEnroll_disabledByEnv(t *testing.T) {
os.Setenv("TELEPORT_DEVICE_AUTO_ENROLL_DISABLED", "1")

_, err := enroll.AutoEnroll(context.Background(), nil /* devicesClient */)
assert.ErrorIs(t, err, enroll.ErrAutoEnrollDisabled, "AutoEnroll() error mismatch")
}

0 comments on commit 1fa2a9b

Please sign in to comment.