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