Skip to content

Commit

Permalink
When creating a preauthkey, the default expiration was passed through as
Browse files Browse the repository at this point in the history
a nil value, instead of the default value (24h). This resulted in the
preauthkey being created with expiration key '0001-01-01 00:00:00',
which meant the key would not work, because it was already expired.

This commit applies the default expiration time (24h) when a preauthkey
is created without a specific expiration. It also adds an integration
test to make sure this bug does not reoccur.
  • Loading branch information
cure committed Nov 26, 2021
1 parent a52a4d4 commit b4d89b0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
10 changes: 4 additions & 6 deletions cmd/headscale/cli/preauthkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,12 @@ var createPreAuthKeyCmd = &cobra.Command{
Ephemeral: ephemeral,
}

if cmd.Flags().Changed("expiration") {
duration, _ := cmd.Flags().GetDuration("expiration")
expiration := time.Now().UTC().Add(duration)
duration, _ := cmd.Flags().GetDuration("expiration")
expiration := time.Now().UTC().Add(duration)

log.Trace().Dur("expiration", duration).Msg("expiration has been set")
log.Trace().Dur("expiration", duration).Msg("expiration has been set")

request.Expiration = timestamppb.New(expiration)
}
request.Expiration = timestamppb.New(expiration)

ctx, client, conn, cancel := getHeadscaleCLIClient()
defer cancel()
Expand Down
7 changes: 6 additions & 1 deletion integration_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,12 @@ func (s *IntegrationCLITestSuite) TestPreAuthKeyCommandWithoutExpiry() {
assert.Nil(s.T(), err)

assert.Len(s.T(), listedPreAuthKeys, 1)
assert.True(s.T(), time.Time{}.Equal(listedPreAuthKeys[0].Expiration.AsTime()))

assert.True(s.T(), listedPreAuthKeys[0].Expiration.AsTime().After(time.Now()))
assert.True(
s.T(),
listedPreAuthKeys[0].Expiration.AsTime().Before(time.Now().Add(time.Hour*25)),
)
}

func (s *IntegrationCLITestSuite) TestPreAuthKeyCommandReusableEphemeral() {
Expand Down

0 comments on commit b4d89b0

Please sign in to comment.