Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds SnapRefreshRPC and node token authentication #636

Merged
merged 10 commits into from
Sep 6, 2024
23 changes: 23 additions & 0 deletions src/k8s/cmd/k8s/k8s_x_capi.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package k8s

import (
"os"

apiv1 "github.com/canonical/k8s-snap-api/api/v1"
cmdutil "github.com/canonical/k8s/cmd/util"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -34,6 +36,26 @@ func newXCAPICmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
}
},
}
setNodeToken := &cobra.Command{
Use: "set-node-token <token>",
Short: "Set the node token to authenticate with per-node k8sd endpoints",
Args: cmdutil.ExactArgs(env, 1),
Run: func(cmd *cobra.Command, args []string) {
token := args[0]
if token == "" {
berkayoz marked this conversation as resolved.
Show resolved Hide resolved
cmd.PrintErrf("Error: The token must be provided.\n")
env.Exit(1)
return
}

err := os.WriteFile(env.Snap.NodeTokenFile(), []byte(token), 0600)
if err != nil {
berkayoz marked this conversation as resolved.
Show resolved Hide resolved
cmd.PrintErrf("Error: Failed to write the node token to file.\n\nThe error was: %v\n", err)
env.Exit(1)
return
}
},
}

cmd := &cobra.Command{
Use: "x-capi",
Expand All @@ -42,6 +64,7 @@ func newXCAPICmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
}

cmd.AddCommand(setAuthTokenCmd)
cmd.AddCommand(setNodeToken)

return cmd
}
Loading