| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Mateusz Zalega | c437dc4 | 2022-07-07 13:01:43 +0200 | [diff] [blame] | 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "fmt" |
| Mateusz Zalega | c437dc4 | 2022-07-07 13:01:43 +0200 | [diff] [blame] | 9 | "log" |
| Tim Windelschmidt | b765f24 | 2024-05-08 01:40:02 +0200 | [diff] [blame] | 10 | "os" |
| 11 | "os/signal" |
| Mateusz Zalega | c437dc4 | 2022-07-07 13:01:43 +0200 | [diff] [blame] | 12 | |
| 13 | "github.com/spf13/cobra" |
| 14 | |
| Mateusz Zalega | 18a67b0 | 2022-08-02 13:37:50 +0200 | [diff] [blame] | 15 | "source.monogon.dev/metropolis/cli/metroctl/core" |
| Mateusz Zalega | c437dc4 | 2022-07-07 13:01:43 +0200 | [diff] [blame] | 16 | "source.monogon.dev/metropolis/proto/api" |
| 17 | ) |
| 18 | |
| 19 | var approveCmd = &cobra.Command{ |
| 20 | Short: "Approves a candidate node, if specified; lists nodes pending approval otherwise.", |
| 21 | Use: "approve [node-id]", |
| Tim Windelschmidt | fc6e1cf | 2024-09-18 17:34:07 +0200 | [diff] [blame] | 22 | Args: PrintUsageOnWrongArgs(cobra.MaximumNArgs(1)), // One positional argument: node ID |
| Tim Windelschmidt | 0b4fb8c | 2024-09-18 17:34:23 +0200 | [diff] [blame] | 23 | RunE: func(cmd *cobra.Command, args []string) error { |
| 24 | ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt) |
| 25 | cc, err := dialAuthenticated(ctx) |
| 26 | if err != nil { |
| 27 | return fmt.Errorf("while dialing node: %w", err) |
| 28 | } |
| 29 | mgmt := api.NewManagementClient(cc) |
| 30 | |
| 31 | // Get a list of all nodes pending approval by calling Management.GetNodes. |
| 32 | // We need this list regardless of whether we're actually approving nodes, or |
| 33 | // just listing them. |
| 34 | nodes, err := core.GetNodes(ctx, mgmt, "node.state == NODE_STATE_NEW") |
| 35 | if err != nil { |
| 36 | log.Fatalf("While fetching a list of nodes pending approval: %v", err) |
| 37 | } |
| 38 | |
| 39 | if len(args) == 0 { |
| 40 | // If no id was given, just list the nodes pending approval. |
| 41 | if len(nodes) != 0 { |
| 42 | for _, n := range nodes { |
| 43 | fmt.Println(n.Id) |
| 44 | } |
| 45 | } else { |
| 46 | log.Print("There are no nodes pending approval at this time.") |
| 47 | } |
| 48 | } else { |
| 49 | // Otherwise, try to approve the nodes matching the supplied ids. |
| 50 | for _, tgtNodeId := range args { |
| 51 | n := nodeById(nodes, tgtNodeId) |
| 52 | if n == nil { |
| 53 | return fmt.Errorf("couldn't find a new node matching id %s", tgtNodeId) |
| 54 | } |
| 55 | // nolint:SA5011 |
| 56 | _, err := mgmt.ApproveNode(ctx, &api.ApproveNodeRequest{ |
| 57 | Pubkey: n.Pubkey, |
| 58 | }) |
| 59 | if err != nil { |
| 60 | return fmt.Errorf("while approving node %s: %w", tgtNodeId, err) |
| 61 | } |
| 62 | log.Printf("Approved node %s.", tgtNodeId) |
| 63 | } |
| 64 | } |
| 65 | return nil |
| 66 | }, |
| Mateusz Zalega | c437dc4 | 2022-07-07 13:01:43 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | func init() { |
| 70 | rootCmd.AddCommand(approveCmd) |
| 71 | } |
| 72 | |
| Mateusz Zalega | c437dc4 | 2022-07-07 13:01:43 +0200 | [diff] [blame] | 73 | // nodeById returns the node matching id, if it exists within nodes. |
| 74 | func nodeById(nodes []*api.Node, id string) *api.Node { |
| 75 | for _, n := range nodes { |
| Jan Schär | 39d9c24 | 2024-09-24 13:49:55 +0200 | [diff] [blame] | 76 | if n.Id == id { |
| Mateusz Zalega | c437dc4 | 2022-07-07 13:01:43 +0200 | [diff] [blame] | 77 | return n |
| 78 | } |
| 79 | } |
| 80 | return nil |
| 81 | } |