From 0803c407a9fd25719644d993b927d7dff61edb8b Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 8 Nov 2021 20:49:03 +0000 Subject: [PATCH] Fix Reusable typo, add tests for Augustines scenario --- cmd/headscale/cli/nodes.go | 2 +- cmd/headscale/cli/preauthkeys.go | 10 +++- grpcv1.go | 3 +- integration_cli_test.go | 91 ++++++++++++++++++++++++++++++++ integration_test.go | 1 + 5 files changed, 103 insertions(+), 4 deletions(-) diff --git a/cmd/headscale/cli/nodes.go b/cmd/headscale/cli/nodes.go index 60d2c2f8..9b3f4243 100644 --- a/cmd/headscale/cli/nodes.go +++ b/cmd/headscale/cli/nodes.go @@ -97,7 +97,7 @@ var registerNodeCmd = &cobra.Command{ response, err := client.RegisterMachine(ctx, request) if err != nil { - ErrorOutput(err, fmt.Sprintf("Cannot register machine: %s\n", err), output) + ErrorOutput(err, fmt.Sprintf("Cannot register machine: %s\n", status.Convert(err).Message()), output) return } diff --git a/cmd/headscale/cli/preauthkeys.go b/cmd/headscale/cli/preauthkeys.go index 06f61974..a07e9610 100644 --- a/cmd/headscale/cli/preauthkeys.go +++ b/cmd/headscale/cli/preauthkeys.go @@ -75,7 +75,7 @@ var listPreAuthKeys = &cobra.Command{ if k.GetEphemeral() { reusable = "N/A" } else { - reusable = fmt.Sprintf("%v", k.GetResuable()) + reusable = fmt.Sprintf("%v", k.GetReusable()) } d = append(d, []string{ @@ -112,9 +112,15 @@ var createPreAuthKeyCmd = &cobra.Command{ reusable, _ := cmd.Flags().GetBool("reusable") ephemeral, _ := cmd.Flags().GetBool("ephemeral") + log.Trace(). + Bool("reusable", reusable). + Bool("ephemeral", ephemeral). + Str("namespace", namespace). + Msg("Preparing to create preauthkey") + request := &v1.CreatePreAuthKeyRequest{ Namespace: namespace, - Resuable: reusable, + Reusable: reusable, Ephemeral: ephemeral, } diff --git a/grpcv1.go b/grpcv1.go index dfb86425..998f0c0e 100644 --- a/grpcv1.go +++ b/grpcv1.go @@ -106,7 +106,7 @@ func (api headscaleV1APIServer) CreatePreAuthKey( preAuthKey, err := api.h.CreatePreAuthKey( request.GetNamespace(), - request.GetResuable(), + request.GetReusable(), request.GetEphemeral(), &expiration, ) @@ -155,6 +155,7 @@ func (api headscaleV1APIServer) RegisterMachine( ctx context.Context, request *v1.RegisterMachineRequest, ) (*v1.RegisterMachineResponse, error) { + log.Trace().Str("namespace", request.GetNamespace()).Str("machine_key", request.GetKey()).Msg("Registering machine") machine, err := api.h.RegisterMachine( request.GetKey(), request.GetNamespace(), diff --git a/integration_cli_test.go b/integration_cli_test.go index 20b6ce34..e1b16727 100644 --- a/integration_cli_test.go +++ b/integration_cli_test.go @@ -396,6 +396,97 @@ func (s *IntegrationCLITestSuite) TestPreAuthKeyCommandWithoutExpiry() { assert.True(s.T(), time.Time{}.Equal(listedPreAuthKeys[0].Expiration.AsTime())) } +func (s *IntegrationCLITestSuite) TestPreAuthKeyCommandReusableEphemeral() { + namespace, err := s.createNamespace("pre-auth-key-reus-ephm-namespace") + assert.Nil(s.T(), err) + + preAuthReusableResult, err := ExecuteCommand( + &s.headscale, + []string{ + "headscale", + "preauthkeys", + "--namespace", + namespace.Name, + "create", + "--reusable=true", + "--output", + "json", + }, + []string{}, + ) + assert.Nil(s.T(), err) + + var preAuthReusableKey v1.PreAuthKey + err = json.Unmarshal([]byte(preAuthReusableResult), &preAuthReusableKey) + assert.Nil(s.T(), err) + + assert.True(s.T(), preAuthReusableKey.GetReusable()) + assert.False(s.T(), preAuthReusableKey.GetEphemeral()) + + preAuthEphemeralResult, err := ExecuteCommand( + &s.headscale, + []string{ + "headscale", + "preauthkeys", + "--namespace", + namespace.Name, + "create", + "--ephemeral=true", + "--output", + "json", + }, + []string{}, + ) + assert.Nil(s.T(), err) + + var preAuthEphemeralKey v1.PreAuthKey + err = json.Unmarshal([]byte(preAuthEphemeralResult), &preAuthEphemeralKey) + assert.Nil(s.T(), err) + + assert.True(s.T(), preAuthEphemeralKey.GetEphemeral()) + assert.False(s.T(), preAuthEphemeralKey.GetReusable()) + + // TODO(kradalby): Evaluate if we need a case to test for reusable and ephemeral + // preAuthReusableAndEphemeralResult, err := ExecuteCommand( + // &s.headscale, + // []string{ + // "headscale", + // "preauthkeys", + // "--namespace", + // namespace.Name, + // "create", + // "--ephemeral", + // "--reusable", + // "--output", + // "json", + // }, + // []string{}, + // ) + // assert.NotNil(s.T(), err) + + // Test list of keys + listResult, err := ExecuteCommand( + &s.headscale, + []string{ + "headscale", + "preauthkeys", + "--namespace", + namespace.Name, + "list", + "--output", + "json", + }, + []string{}, + ) + assert.Nil(s.T(), err) + + var listedPreAuthKeys []v1.PreAuthKey + err = json.Unmarshal([]byte(listResult), &listedPreAuthKeys) + assert.Nil(s.T(), err) + + assert.Len(s.T(), listedPreAuthKeys, 2) +} + func (s *IntegrationCLITestSuite) TestNodeCommand() { namespace, err := s.createNamespace("machine-namespace") assert.Nil(s.T(), err) diff --git a/integration_test.go b/integration_test.go index 9c572583..c4f6bb41 100644 --- a/integration_test.go +++ b/integration_test.go @@ -269,6 +269,7 @@ func (s *IntegrationTestSuite) SetupSuite() { var preAuthKey v1.PreAuthKey err = json.Unmarshal([]byte(preAuthResult), &preAuthKey) assert.Nil(s.T(), err) + assert.True(s.T(), preAuthKey.Reusable) headscaleEndpoint := "http://headscale:8080"