From 8be9e9655c567e70cb1f2ccc244abb6381a165bd Mon Sep 17 00:00:00 2001 From: Samuel Lock Date: Tue, 10 May 2022 20:51:14 +1000 Subject: [PATCH 1/3] fixed issue #360 --- cmd/headscale/cli/routes.go | 45 ++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/cmd/headscale/cli/routes.go b/cmd/headscale/cli/routes.go index dc060fba..e1deb79e 100644 --- a/cmd/headscale/cli/routes.go +++ b/cmd/headscale/cli/routes.go @@ -24,6 +24,8 @@ func init() { enableRouteCmd.Flags(). StringSliceP("route", "r", []string{}, "List (or repeated flags) of routes to enable") enableRouteCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)") + enableRouteCmd.Flags().BoolP("all", "a", false, "All routes from host") + err = enableRouteCmd.MarkFlagRequired("identifier") if err != nil { log.Fatalf(err.Error()) @@ -125,21 +127,42 @@ omit the route you do not want to enable. return } - routes, err := cmd.Flags().GetStringSlice("route") - if err != nil { - ErrorOutput( - err, - fmt.Sprintf("Error getting routes from flag: %s", err), - output, - ) - - return - } - ctx, client, conn, cancel := getHeadscaleCLIClient() defer cancel() defer conn.Close() + routes := []string{} + + isAll, _ := cmd.Flags().GetBool("all") + if isAll == true { + // x := v1.NewHeadscaleServiceClient(conn) + // machine, err := x.GetMachineByID(machineID) + response, err := client.GetMachineRoute(ctx, &v1.GetMachineRouteRequest{ + MachineId: machineID, + }) + if err != nil { + ErrorOutput( + err, + fmt.Sprintf( + "Cannot get machine routes: %s\n", + status.Convert(err).Message(), + ), + output, + ) + } + routes = response.GetRoutes().GetAdvertisedRoutes() + } else { + routes, err = cmd.Flags().GetStringSlice("route") + if err != nil { + ErrorOutput( + err, + fmt.Sprintf("Error getting routes from flag: %s", err), + output, + ) + return + } + } + request := &v1.EnableMachineRoutesRequest{ MachineId: machineID, Routes: routes, From c26280c3315b42399d3a780559e0ed067c739d8e Mon Sep 17 00:00:00 2001 From: Samuel Lock Date: Wed, 11 May 2022 09:31:24 +1000 Subject: [PATCH 2/3] modified code to satisfy golangci-lint and added integration test --- cmd/headscale/cli/routes.go | 9 +++++---- integration_cli_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/cmd/headscale/cli/routes.go b/cmd/headscale/cli/routes.go index e1deb79e..105175a1 100644 --- a/cmd/headscale/cli/routes.go +++ b/cmd/headscale/cli/routes.go @@ -131,12 +131,10 @@ omit the route you do not want to enable. defer cancel() defer conn.Close() - routes := []string{} + var routes []string isAll, _ := cmd.Flags().GetBool("all") - if isAll == true { - // x := v1.NewHeadscaleServiceClient(conn) - // machine, err := x.GetMachineByID(machineID) + if isAll { response, err := client.GetMachineRoute(ctx, &v1.GetMachineRouteRequest{ MachineId: machineID, }) @@ -149,6 +147,8 @@ omit the route you do not want to enable. ), output, ) + + return } routes = response.GetRoutes().GetAdvertisedRoutes() } else { @@ -159,6 +159,7 @@ omit the route you do not want to enable. fmt.Sprintf("Error getting routes from flag: %s", err), output, ) + return } } diff --git a/integration_cli_test.go b/integration_cli_test.go index 29bca647..05bca29f 100644 --- a/integration_cli_test.go +++ b/integration_cli_test.go @@ -1076,6 +1076,35 @@ func (s *IntegrationCLITestSuite) TestRouteCommand() { string(failEnableNonAdvertisedRoute), "route (route-machine) is not available on node", ) + + // Enable all routes on host + enableAllRouteResult, err := ExecuteCommand( + &s.headscale, + []string{ + "headscale", + "routes", + "enable", + "--output", + "json", + "--identifier", + "0", + "--all", + }, + []string{}, + ) + assert.Nil(s.T(), err) + + var enableAllRoute v1.Routes + err = json.Unmarshal([]byte(enableAllRouteResult), &enableAllRoute) + assert.Nil(s.T(), err) + + assert.Len(s.T(), enableAllRoute.AdvertisedRoutes, 2) + assert.Contains(s.T(), enableAllRoute.AdvertisedRoutes, "10.0.0.0/8") + assert.Contains(s.T(), enableAllRoute.AdvertisedRoutes, "192.168.1.0/24") + + assert.Len(s.T(), enableAllRoute.EnabledRoutes, 2) + assert.Contains(s.T(), enableAllRoute.EnabledRoutes, "10.0.0.0/8") + assert.Contains(s.T(), enableAllRoute.EnabledRoutes, "192.168.1.0/24") } func (s *IntegrationCLITestSuite) TestApiKeyCommand() { From 614c003704cb7a6bec6d07288686be514de30dee Mon Sep 17 00:00:00 2001 From: Samuel Lock Date: Sat, 14 May 2022 22:36:04 +1000 Subject: [PATCH 3/3] updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30043c8d..f59bf919 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - Fixed spurious calls to setLastStateChangeToNow from ephemeral nodes [#566](https://github.com/juanfont/headscale/pull/566) - Add command for moving nodes between namespaces [#362](https://github.com/juanfont/headscale/issues/362) - Added more configuration parameters for OpenID Connect (scopes, free-form paramters, domain and user allowlist) +- Add --all (-a) flag to enable routes command [#360](https://github.com/juanfont/headscale/issues/360) ## 0.15.0 (2022-03-20)