Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Avoid needless user escalation during auto-enroll #47676

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 10 additions & 9 deletions lib/devicetrust/enroll/auto_enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,18 @@ import (
"github.com/gravitational/trace"

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

// AutoEnrollCeremony is the auto-enrollment version of [Ceremony].
type AutoEnrollCeremony struct {
*Ceremony

CollectDeviceData func(mode native.CollectDataMode) (*devicepb.DeviceCollectedData, error)
}

// NewAutoEnrollCeremony creates a new [AutoEnrollCeremony] based on the regular
// ceremony provided by [NewCeremony].
func NewAutoEnrollCeremony() *AutoEnrollCeremony {
return &AutoEnrollCeremony{
Ceremony: NewCeremony(),
CollectDeviceData: native.CollectDeviceData,
Ceremony: NewCeremony(),
}
}

Expand All @@ -53,18 +49,23 @@ 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) {
cd, err := c.CollectDeviceData(native.CollectedDataAlwaysEscalate)
// 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
// admin credentials, then fail a few steps after the prompt.
init, err := c.EnrollDeviceInit()
if err != nil {
return nil, trace.Wrap(err, "collecting device data")
return nil, trace.Wrap(err)
}

token, err := devicesClient.CreateDeviceEnrollToken(ctx, &devicepb.CreateDeviceEnrollTokenRequest{
DeviceData: cd,
DeviceData: init.DeviceData,
})
if err != nil {
return nil, trace.Wrap(err, "creating auto-token")
}
init.Token = token.Token

dev, err := c.Ceremony.Run(ctx, devicesClient, false, token.Token)
dev, err := c.run(ctx, devicesClient, false /* debug */, init)
return dev, trace.Wrap(err)
}
1 change: 0 additions & 1 deletion lib/devicetrust/enroll/auto_enroll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func TestAutoEnrollCeremony_Run(t *testing.T) {
SignChallenge: test.dev.SignChallenge,
SolveTPMEnrollChallenge: test.dev.SolveTPMEnrollChallenge,
},
CollectDeviceData: test.dev.CollectDeviceData,
}

dev, err := c.Run(ctx, devices)
Expand Down
9 changes: 9 additions & 0 deletions lib/devicetrust/enroll/enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ func (c *Ceremony) Run(ctx context.Context, devicesClient devicepb.DeviceTrustSe
}
init.Token = enrollToken

return c.run(ctx, devicesClient, debug, init)
}

func (c *Ceremony) run(ctx context.Context, devicesClient devicepb.DeviceTrustServiceClient, debug bool, init *devicepb.EnrollDeviceInit) (*devicepb.Device, error) {
// Sanity check.
if init.GetToken() == "" {
return nil, trace.BadParameter("enroll init message lacks enrollment token")
}

stream, err := devicesClient.EnrollDevice(ctx)
if err != nil {
return nil, trace.Wrap(devicetrust.HandleUnimplemented(err))
Expand Down
Loading