|
| 1 | +package kube |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/base64" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "text/template" |
| 9 | + |
| 10 | + "github.com/akitasoftware/akita-cli/telemetry" |
| 11 | + |
| 12 | + "github.com/akitasoftware/akita-cli/cmd/internal/cmderr" |
| 13 | + "github.com/akitasoftware/akita-cli/printer" |
| 14 | + "github.com/pkg/errors" |
| 15 | + "github.com/spf13/cobra" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + secretFilePathFlag string |
| 20 | + namespaceFlag string |
| 21 | + // Store a parsed representation of /template/akita-secret.tmpl |
| 22 | + secretTemplate *template.Template |
| 23 | +) |
| 24 | + |
| 25 | +var secretCmd = &cobra.Command{ |
| 26 | + Use: "secret", |
| 27 | + Short: "Generate a Kubernetes secret containing the Akita credentials", |
| 28 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 29 | + key, secret, err := cmderr.RequireAPICredentials("Akita API key is required for Kubernetes Secret generation") |
| 30 | + if err != nil { |
| 31 | + return err |
| 32 | + } |
| 33 | + |
| 34 | + output, err := handleSecretGeneration(namespaceFlag, key, secret) |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + |
| 39 | + // If the secret file path flag hasn't been set, print the generated secret to stdout |
| 40 | + if secretFilePathFlag == "" { |
| 41 | + printer.RawOutput(string(output)) |
| 42 | + return nil |
| 43 | + } |
| 44 | + |
| 45 | + // Otherwise, write the generated secret to the given file path |
| 46 | + err = writeSecretFile(output, secretFilePathFlag) |
| 47 | + if err != nil { |
| 48 | + return cmderr.AkitaErr{Err: errors.Wrapf(err, "Failed to write generated secret to %s", output)} |
| 49 | + } |
| 50 | + |
| 51 | + printer.Infof("Successfully generated a Kubernetes Secret file for Akita at %s\n", secretFilePathFlag) |
| 52 | + printer.Infof("To apply, run: kubectl apply -f %s\n", secretFilePathFlag) |
| 53 | + return nil |
| 54 | + }, |
| 55 | + // Override the parent command's PersistentPreRun to prevent any logs from being printed. |
| 56 | + // This is necessary because the secret command is intended to be used in a pipeline |
| 57 | + PersistentPreRun: func(cmd *cobra.Command, args []string) { |
| 58 | + // Initialize the telemetry client, but do not allow any logs to be printed |
| 59 | + telemetry.Init(false) |
| 60 | + }, |
| 61 | +} |
| 62 | + |
| 63 | +// Represents the input used by secretTemplate |
| 64 | +type secretTemplateInput struct { |
| 65 | + Namespace string |
| 66 | + APIKey string |
| 67 | + APISecret string |
| 68 | +} |
| 69 | + |
| 70 | +func initSecretTemplate() error { |
| 71 | + var err error |
| 72 | + |
| 73 | + secretTemplate, err = template.ParseFS(templateFS, "template/akita-secret.tmpl") |
| 74 | + if err != nil { |
| 75 | + return cmderr.AkitaErr{Err: errors.Wrap(err, "failed to parse secret template")} |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +// Generates a Kubernetes secret config file for Akita |
| 82 | +// On success, the generated output is returned as a string. |
| 83 | +func handleSecretGeneration(namespace, key, secret string) ([]byte, error) { |
| 84 | + err := initSecretTemplate() |
| 85 | + if err != nil { |
| 86 | + return nil, cmderr.AkitaErr{Err: errors.Wrap(err, "failed to initialize secret template")} |
| 87 | + } |
| 88 | + |
| 89 | + input := secretTemplateInput{ |
| 90 | + Namespace: namespace, |
| 91 | + APIKey: base64.StdEncoding.EncodeToString([]byte(key)), |
| 92 | + APISecret: base64.StdEncoding.EncodeToString([]byte(secret)), |
| 93 | + } |
| 94 | + |
| 95 | + buf := bytes.NewBuffer([]byte{}) |
| 96 | + |
| 97 | + err = secretTemplate.Execute(buf, input) |
| 98 | + if err != nil { |
| 99 | + return nil, cmderr.AkitaErr{Err: errors.Wrap(err, "failed to generate template")} |
| 100 | + } |
| 101 | + |
| 102 | + return buf.Bytes(), nil |
| 103 | +} |
| 104 | + |
| 105 | +// Writes the generated secret to the given file path |
| 106 | +func writeSecretFile(data []byte, filePath string) error { |
| 107 | + secretFile, err := createSecretFile(filePath) |
| 108 | + if err != nil { |
| 109 | + return cmderr.AkitaErr{ |
| 110 | + Err: cmderr.AkitaErr{ |
| 111 | + Err: errors.Wrapf( |
| 112 | + err, |
| 113 | + "failed to create secret file %s", |
| 114 | + filePath, |
| 115 | + ), |
| 116 | + }, |
| 117 | + } |
| 118 | + } |
| 119 | + defer secretFile.Close() |
| 120 | + |
| 121 | + _, err = secretFile.Write(data) |
| 122 | + if err != nil { |
| 123 | + return cmderr.AkitaErr{Err: errors.Wrap(err, "failed to write generated secret file")} |
| 124 | + } |
| 125 | + |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +// Creates a file at the given path to be used for storing of the generated Secret configuration |
| 130 | +// If the directory provided does not exist, an error will be returned and the file will not be created |
| 131 | +func createSecretFile(path string) (*os.File, error) { |
| 132 | + // Split the output flag value into directory and filename |
| 133 | + outputDir, outputName := filepath.Split(path) |
| 134 | + |
| 135 | + // Get the absolute path of the output directory |
| 136 | + absOutputDir, err := filepath.Abs(outputDir) |
| 137 | + if err != nil { |
| 138 | + return nil, errors.Wrapf(err, "failed to resolve the absolute path of the output directory") |
| 139 | + } |
| 140 | + |
| 141 | + // Check that the output directory exists |
| 142 | + if _, statErr := os.Stat(absOutputDir); os.IsNotExist(statErr) { |
| 143 | + return nil, errors.Errorf("output directory %s does not exist", absOutputDir) |
| 144 | + } |
| 145 | + |
| 146 | + // Check if the output file already exists |
| 147 | + outputFilePath := filepath.Join(absOutputDir, outputName) |
| 148 | + if _, statErr := os.Stat(outputFilePath); statErr == nil { |
| 149 | + return nil, errors.Errorf("output file %s already exists", outputFilePath) |
| 150 | + } |
| 151 | + |
| 152 | + // Create the output file in the output directory |
| 153 | + outputFile, err := os.Create(outputFilePath) |
| 154 | + if err != nil { |
| 155 | + return nil, errors.Wrap(err, "failed to create the output file") |
| 156 | + } |
| 157 | + |
| 158 | + return outputFile, nil |
| 159 | +} |
| 160 | + |
| 161 | +func init() { |
| 162 | + secretCmd.Flags().StringVarP( |
| 163 | + &namespaceFlag, |
| 164 | + "namespace", |
| 165 | + "n", |
| 166 | + "default", |
| 167 | + "The Kubernetes namespace the secret should be applied to", |
| 168 | + ) |
| 169 | + |
| 170 | + secretCmd.Flags().StringVarP( |
| 171 | + &secretFilePathFlag, |
| 172 | + "file", |
| 173 | + "f", |
| 174 | + "", |
| 175 | + "File to output the generated secret. If not set, the secret will be printed to stdout.", |
| 176 | + ) |
| 177 | + |
| 178 | + Cmd.AddCommand(secretCmd) |
| 179 | +} |
0 commit comments