this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(toolbox): wire secrets command into cobra cli

+75
+2
toolbox/cmd/root.go
··· 24 24 rootCmd.PersistentFlags().StringVar(&sshUser, "ssh-user", "root", "SSH user") 25 25 rootCmd.PersistentFlags().StringVar(&sshKey, "ssh-key", defaultSSHKey(), "Path to SSH private key") 26 26 rootCmd.PersistentFlags().StringVar(&sshKnownHosts, "ssh-known-hosts", defaultKnownHostsFile(), "Path to SSH known_hosts file") 27 + 28 + rootCmd.AddCommand(secretsCmd) 27 29 } 28 30 29 31 var rootCmd = &cobra.Command{
+73
toolbox/cmd/secrets.go
··· 1 + package cmd 2 + 3 + import ( 4 + "context" 5 + "fmt" 6 + "time" 7 + 8 + "github.com/charmbracelet/log" 9 + "github.com/spf13/cobra" 10 + 11 + "github.com/khuedoan/cloudlab/toolbox/internal/cluster" 12 + "github.com/khuedoan/cloudlab/toolbox/internal/secrets" 13 + ) 14 + 15 + const connectTimeout = 30 * time.Second 16 + 17 + var settingsFile string 18 + 19 + func init() { 20 + secretsCmd.Flags().StringVar(&settingsFile, "settings", "", "Path to settings YAML file") 21 + secretsCmd.MarkFlagRequired("settings") 22 + } 23 + 24 + var secretsCmd = &cobra.Command{ 25 + Use: "secrets", 26 + Short: "Manage secrets in Vault", 27 + PreRunE: func(cmd *cobra.Command, args []string) error { 28 + if hostsFile == "" { 29 + return fmt.Errorf("--hosts-file is required") 30 + } 31 + if host == "" { 32 + return fmt.Errorf("--host is required") 33 + } 34 + return nil 35 + }, 36 + RunE: runSecrets, 37 + } 38 + 39 + func runSecrets(cmd *cobra.Command, args []string) error { 40 + config, err := secrets.LoadConfig(settingsFile) 41 + if err != nil { 42 + return fmt.Errorf("load settings file: %w", err) 43 + } 44 + 45 + entries, err := secrets.ParseAndValidate(config) 46 + if err != nil { 47 + return fmt.Errorf("validate config: %w", err) 48 + } 49 + 50 + connectCtx, cancel := context.WithTimeout(cmd.Context(), connectTimeout) 51 + defer cancel() 52 + 53 + client, err := cluster.NewClient(connectCtx, cluster.ClientConfig{ 54 + HostsFile: hostsFile, 55 + Host: host, 56 + SSHUser: sshUser, 57 + SSHKey: sshKey, 58 + SSHKnownHosts: sshKnownHosts, 59 + }) 60 + if err != nil { 61 + return fmt.Errorf("connect to cluster: %w", err) 62 + } 63 + defer client.Close() 64 + log.Debug("connected to cluster") 65 + 66 + service := secrets.NewService(client.Vault(), secrets.HuhPrompter{}) 67 + if err := service.Run(cmd.Context(), entries); err != nil { 68 + return err 69 + } 70 + 71 + log.Info("all secrets processed successfully") 72 + return nil 73 + }