Skip to content

Commit

Permalink
[v14] feat: Disable auto-enroll via environment variable (#47718)
Browse files Browse the repository at this point in the history
* feat: Disable auto-enroll via environment variable

* Fix TestAutoEnroll_disabledByEnv flakiness (#47723)

* Fix TestAutoEnroll_disabledByEnv flakiness

* Use t.Setenv

Co-authored-by: rosstimothy <[email protected]>

---------

Co-authored-by: rosstimothy <[email protected]>

---------

Co-authored-by: rosstimothy <[email protected]>
  • Loading branch information
codingllama and rosstimothy authored Oct 18, 2024
1 parent 2c5f0c0 commit 6f2fdf7
Show file tree
Hide file tree
Showing 2 changed files with 21 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 @@ -16,12 +16,21 @@ package enroll

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

"github.com/gravitational/trace"

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

// 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 @@ -45,6 +54,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)
}

// Creating the init message straight away aborts the process cleanly if the
// device cannot create the device key (for example, if it lacks a TPM).
// This avoids a situation where we ask for escalation, like a sudo prompt or
Expand Down
7 changes: 7 additions & 0 deletions lib/devicetrust/enroll/auto_enroll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,10 @@ func TestAutoEnrollCeremony_Run(t *testing.T) {
})
}
}

func TestAutoEnroll_disabledByEnv(t *testing.T) {
t.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 6f2fdf7

Please sign in to comment.