diff --git a/README.md b/README.md index 6abdd3f..09b4adb 100644 --- a/README.md +++ b/README.md @@ -330,6 +330,9 @@ $ alpacon authority ls # Get detailed information about a specific Certificate Authority. $ alpacon authority describe [AUTHORITY ID] +# Download a root Certificate by authority's ID and save it to the specified file path. +$ alpacon authority download-crt [AUTHOIRY ID] --out=/path/to/root.crt + # Generate a new Certificate Signing Request (CSR) $ alpacon csr create @@ -357,9 +360,6 @@ $ alpacon cert describe [CERT ID] # Download a specific Certificate by its ID and save it to the specified file path. $ alpacon cert download [CERT ID] --out=/path/to/certificate.crt - -# Download a root Certificate by authority's ID and save it to the specified file path. -$ alpacon cert download [AUTHOIRY ID] --root --out=/path/to/root.crt ``` ### Contributing diff --git a/cmd/authority/authority.go b/cmd/authority/authority.go index da0f182..1439a25 100644 --- a/cmd/authority/authority.go +++ b/cmd/authority/authority.go @@ -21,4 +21,5 @@ func init() { AuthorityCmd.AddCommand(authorityCreateCmd) AuthorityCmd.AddCommand(authorityListCmd) AuthorityCmd.AddCommand(authorityDetailCmd) + AuthorityCmd.AddCommand(authorityDownloadCmd) } diff --git a/cmd/authority/authority_download.go b/cmd/authority/authority_download.go new file mode 100644 index 0000000..a839247 --- /dev/null +++ b/cmd/authority/authority_download.go @@ -0,0 +1,51 @@ +package authority + +import ( + "github.com/alpacanetworks/alpacon-cli/api/cert" + "github.com/alpacanetworks/alpacon-cli/client" + "github.com/alpacanetworks/alpacon-cli/utils" + "github.com/spf13/cobra" +) + +var authorityDownloadCmd = &cobra.Command{ + Use: "download-crt [AUTHORITY ID]", + Aliases: []string{"download-cert"}, + Short: "Download a root certificate", + Long: ` + Download a root certificate from the server and save it to a specified file path. + The path argument should include the file name and extension where the certificate will be stored. + For example, '/path/to/root.crt'. The recommended file extension for certificates is '.crt'.`, + Example: ` + alpacon authority download-crt [AUTHORITY ID] --out=/path/to/root.crt + `, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + authorityId := args[0] + filePath, _ := cmd.Flags().GetString("out") + if filePath == "" { + filePath = promptForCertificate() + } + + alpaconClient, err := client.NewAlpaconAPIClient() + if err != nil { + utils.CliError("Connection to Alpacon API failed: %s. Consider re-logging.", err) + } + + err = cert.DownloadRootCertificate(alpaconClient, authorityId, filePath) + if err != nil { + utils.CliError("Failed to download the root certificate from authority: %s", err) + } + + utils.CliInfo("Root certificate downloaded successfully: '%s'", filePath) + }, +} + +func init() { + var filePath string + authorityDownloadCmd.Flags().StringVarP(&filePath, "out", "o", "", "path where root certificate should be stored") + +} + +func promptForCertificate() string { + return utils.PromptForRequiredInput("Path to root certificate (e.g., /path/to/root.crt, recommended extension: .crt): ") +} diff --git a/cmd/cert/cert_download.go b/cmd/cert/cert_download.go index ab5d9e8..3a7d76c 100644 --- a/cmd/cert/cert_download.go +++ b/cmd/cert/cert_download.go @@ -20,7 +20,6 @@ var certDownloadCmd = &cobra.Command{ Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { csrId := args[0] - isRoot, _ := cmd.Flags().GetBool("root") filePath, _ := cmd.Flags().GetString("out") if filePath == "" { filePath = promptForCertificate() @@ -31,16 +30,9 @@ var certDownloadCmd = &cobra.Command{ utils.CliError("Connection to Alpacon API failed: %s. Consider re-logging.", err) } - if isRoot { - err = cert.DownloadRootCertificate(alpaconClient, csrId, filePath) // csrId refers to the authorityId. - if err != nil { - utils.CliError("Failed to download the certificate from authority: %s", err) - } - } else { - err = cert.DownloadCertificate(alpaconClient, csrId, filePath) - if err != nil { - utils.CliError("Failed to download the certificate from authority: %s", err) - } + err = cert.DownloadCertificate(alpaconClient, csrId, filePath) + if err != nil { + utils.CliError("Failed to download the certificate from authority: %s", err) } utils.CliInfo("Certificate downloaded successfully: '%s'", filePath) @@ -48,9 +40,7 @@ var certDownloadCmd = &cobra.Command{ } func init() { - var root bool var filePath string - certDownloadCmd.Flags().BoolVar(&root, "root", false, "Download the root certificate") certDownloadCmd.Flags().StringVarP(&filePath, "out", "o", "", "path where certificate should be stored") }