Lorenz Brun | a9b455f | 2021-12-07 03:53:22 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
Lorenz Brun | a9b455f | 2021-12-07 03:53:22 +0100 | [diff] [blame] | 5 | "encoding/pem" |
| 6 | "log" |
| 7 | "net" |
| 8 | "os" |
Lorenz Brun | 20d1dd1 | 2022-07-01 12:21:42 +0000 | [diff] [blame] | 9 | "os/exec" |
Lorenz Brun | a9b455f | 2021-12-07 03:53:22 +0100 | [diff] [blame] | 10 | "path/filepath" |
| 11 | |
Lorenz Brun | a9b455f | 2021-12-07 03:53:22 +0100 | [diff] [blame] | 12 | "github.com/spf13/cobra" |
Lorenz Brun | 20d1dd1 | 2022-07-01 12:21:42 +0000 | [diff] [blame] | 13 | clientauthentication "k8s.io/client-go/pkg/apis/clientauthentication/v1" |
| 14 | "k8s.io/client-go/tools/clientcmd" |
| 15 | clientapi "k8s.io/client-go/tools/clientcmd/api" |
Lorenz Brun | a9b455f | 2021-12-07 03:53:22 +0100 | [diff] [blame] | 16 | |
Mateusz Zalega | 18a67b0 | 2022-08-02 13:37:50 +0200 | [diff] [blame^] | 17 | "source.monogon.dev/metropolis/cli/metroctl/core" |
Lorenz Brun | a9b455f | 2021-12-07 03:53:22 +0100 | [diff] [blame] | 18 | clicontext "source.monogon.dev/metropolis/cli/pkg/context" |
| 19 | "source.monogon.dev/metropolis/node" |
| 20 | "source.monogon.dev/metropolis/node/core/rpc" |
Serge Bazanski | dc1bec4 | 2021-12-16 17:38:53 +0100 | [diff] [blame] | 21 | apb "source.monogon.dev/metropolis/proto/api" |
Lorenz Brun | a9b455f | 2021-12-07 03:53:22 +0100 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | var takeownershipCommand = &cobra.Command{ |
Mateusz Zalega | b1e7ee4 | 2022-07-08 12:19:02 +0200 | [diff] [blame] | 25 | Use: "takeownership", |
Lorenz Brun | a9b455f | 2021-12-07 03:53:22 +0100 | [diff] [blame] | 26 | Short: "Takes ownership of a new Metropolis cluster", |
| 27 | Long: `This takes ownership of a new Metropolis cluster by asking the new |
| 28 | cluster to issue an owner certificate to for the owner key generated by a |
Mateusz Zalega | b1e7ee4 | 2022-07-08 12:19:02 +0200 | [diff] [blame] | 29 | previous invocation of metroctl install on this machine. A single cluster |
| 30 | endpoint must be provided with the --endpoints parameter.`, |
| 31 | Args: cobra.ExactArgs(0), |
| 32 | Run: doTakeOwnership, |
Lorenz Brun | a9b455f | 2021-12-07 03:53:22 +0100 | [diff] [blame] | 33 | } |
| 34 | |
Mateusz Zalega | b1e7ee4 | 2022-07-08 12:19:02 +0200 | [diff] [blame] | 35 | func doTakeOwnership(cmd *cobra.Command, _ []string) { |
| 36 | if len(flags.clusterEndpoints) != 1 { |
| 37 | log.Fatalf("takeownership requires a single cluster endpoint to be provided with the --endpoints parameter.") |
| 38 | } |
Mateusz Zalega | b1e7ee4 | 2022-07-08 12:19:02 +0200 | [diff] [blame] | 39 | |
Mateusz Zalega | 1846450 | 2022-07-14 16:18:26 +0200 | [diff] [blame] | 40 | // Retrieve the cluster owner's private key, and use it to construct |
| 41 | // ephemeral credentials. Then, dial the cluster. |
| 42 | opk, err := getOwnerKey() |
| 43 | if err == noCredentialsError { |
Lorenz Brun | a9b455f | 2021-12-07 03:53:22 +0100 | [diff] [blame] | 44 | log.Fatalf("Owner key does not exist. takeownership needs to be executed on the same system that has previously installed the cluster using metroctl install.") |
Lorenz Brun | a9b455f | 2021-12-07 03:53:22 +0100 | [diff] [blame] | 45 | } |
Mateusz Zalega | 1846450 | 2022-07-14 16:18:26 +0200 | [diff] [blame] | 46 | if err != nil { |
| 47 | log.Fatalf("Couldn't get owner's key: %v", err) |
Lorenz Brun | a9b455f | 2021-12-07 03:53:22 +0100 | [diff] [blame] | 48 | } |
Mateusz Zalega | 1846450 | 2022-07-14 16:18:26 +0200 | [diff] [blame] | 49 | ctx := clicontext.WithInterrupt(context.Background()) |
Mateusz Zalega | 18a67b0 | 2022-08-02 13:37:50 +0200 | [diff] [blame^] | 50 | cc, err := core.DialCluster(ctx, opk, nil, flags.proxyAddr, flags.clusterEndpoints, rpcLogger) |
Mateusz Zalega | 1846450 | 2022-07-14 16:18:26 +0200 | [diff] [blame] | 51 | if err != nil { |
| 52 | log.Fatalf("While dialing the cluster: %v", err) |
Lorenz Brun | a9b455f | 2021-12-07 03:53:22 +0100 | [diff] [blame] | 53 | } |
Mateusz Zalega | 1846450 | 2022-07-14 16:18:26 +0200 | [diff] [blame] | 54 | aaa := apb.NewAAAClient(cc) |
Lorenz Brun | a9b455f | 2021-12-07 03:53:22 +0100 | [diff] [blame] | 55 | |
Mateusz Zalega | 1846450 | 2022-07-14 16:18:26 +0200 | [diff] [blame] | 56 | ownerCert, err := rpc.RetrieveOwnerCertificate(ctx, aaa, opk) |
Lorenz Brun | a9b455f | 2021-12-07 03:53:22 +0100 | [diff] [blame] | 57 | if err != nil { |
| 58 | log.Fatalf("Failed to retrive owner certificate from cluster: %v", err) |
| 59 | } |
| 60 | ownerCertPEM := pem.Block{ |
| 61 | Type: "CERTIFICATE", |
| 62 | Bytes: ownerCert.Certificate[0], |
| 63 | } |
Mateusz Zalega | 8234c16 | 2022-07-08 17:05:50 +0200 | [diff] [blame] | 64 | if err := os.WriteFile(filepath.Join(flags.configPath, "owner.pem"), pem.EncodeToMemory(&ownerCertPEM), 0644); err != nil { |
Lorenz Brun | a9b455f | 2021-12-07 03:53:22 +0100 | [diff] [blame] | 65 | log.Printf("Failed to store retrieved owner certificate: %v", err) |
| 66 | log.Fatalln("Sorry, the cluster has been lost as taking ownership cannot be repeated. Fix the reason the file couldn't be written and reinstall the node.") |
| 67 | } |
Lorenz Brun | 20d1dd1 | 2022-07-01 12:21:42 +0000 | [diff] [blame] | 68 | log.Print("Successfully retrieved owner credentials! You now own this cluster. Setting up kubeconfig now...") |
| 69 | |
| 70 | ca := clientcmd.NewDefaultPathOptions() |
| 71 | config, err := ca.GetStartingConfig() |
| 72 | if err != nil { |
| 73 | log.Fatalf("Failed to get initial kubeconfig to add Metropolis cluster: %v", err) |
| 74 | } |
| 75 | // If the user has metroctl in their path, use the metroctl from path as |
| 76 | // a credential plugin. Otherwise use the path to the currently-running |
| 77 | // metroctl. |
| 78 | metroctlPath := "metroctl" |
| 79 | if _, err := exec.LookPath("metroctl"); err != nil { |
| 80 | metroctlPath, err = os.Executable() |
| 81 | if err != nil { |
| 82 | log.Fatalf("Failed to create kubectl entry as metroctl is neither in PATH nor can its absolute path be determined: %v", err) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | config.AuthInfos["metropolis"] = &clientapi.AuthInfo{ |
| 87 | Exec: &clientapi.ExecConfig{ |
| 88 | APIVersion: clientauthentication.SchemeGroupVersion.String(), |
| 89 | Command: metroctlPath, |
| 90 | Args: []string{k8scredpluginCmd.Use}, |
| 91 | InstallHint: `Authenticating to Metropolis clusters requires metroctl to be present. |
| 92 | Running metroctl takeownership creates this entry and either points to metroctl as a command in |
| 93 | PATH if metroctl is in PATH at that time or to the absolute path to metroctl at that time. |
| 94 | If you moved metroctl afterwards or want to switch to PATH resolution, edit $HOME/.kube/config and |
| 95 | change users.metropolis.exec.command to the required path (or just metroctl if using PATH resolution).`, |
| 96 | InteractiveMode: clientapi.NeverExecInteractiveMode, |
| 97 | }, |
| 98 | } |
| 99 | |
| 100 | config.Clusters["metropolis"] = &clientapi.Cluster{ |
| 101 | // MVP: This is insecure, but making this work would be wasted effort |
| 102 | // as all of it will be replaced by the identity system. |
Mateusz Zalega | 1846450 | 2022-07-14 16:18:26 +0200 | [diff] [blame] | 103 | // TODO(issues/144): adjust cluster endpoints once have functioning roles |
| 104 | // implemented. |
Lorenz Brun | 20d1dd1 | 2022-07-01 12:21:42 +0000 | [diff] [blame] | 105 | InsecureSkipTLSVerify: true, |
Mateusz Zalega | 1846450 | 2022-07-14 16:18:26 +0200 | [diff] [blame] | 106 | Server: "https://" + net.JoinHostPort(flags.clusterEndpoints[0], node.KubernetesAPIWrappedPort.PortString()), |
Lorenz Brun | 20d1dd1 | 2022-07-01 12:21:42 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | config.Contexts["metropolis"] = &clientapi.Context{ |
| 110 | AuthInfo: "metropolis", |
| 111 | Cluster: "metropolis", |
| 112 | Namespace: "default", |
| 113 | } |
| 114 | |
| 115 | // Only set us as the current context if no other exists. Changing that |
| 116 | // unprompted would be kind of rude. |
| 117 | if config.CurrentContext == "" { |
| 118 | config.CurrentContext = "metropolis" |
| 119 | } |
| 120 | |
| 121 | if err := clientcmd.ModifyConfig(ca, *config, true); err != nil { |
| 122 | log.Fatalf("Failed to modify kubeconfig to add Metropolis cluster: %v", err) |
| 123 | } |
| 124 | log.Println("Success! kubeconfig is set up. You can now run kubectl --context=metropolis ... to access the Kubernetes cluster.") |
Lorenz Brun | a9b455f | 2021-12-07 03:53:22 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | func init() { |
| 128 | rootCmd.AddCommand(takeownershipCommand) |
| 129 | } |