Skip to content

Commit

Permalink
Rename command to 'authority download-cert'
Browse files Browse the repository at this point in the history
  • Loading branch information
royroyee committed Mar 11, 2024
1 parent 0d19c7d commit 1d83f67
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/authority/authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ func init() {
AuthorityCmd.AddCommand(authorityCreateCmd)
AuthorityCmd.AddCommand(authorityListCmd)
AuthorityCmd.AddCommand(authorityDetailCmd)
AuthorityCmd.AddCommand(authorityDownloadCmd)
}
51 changes: 51 additions & 0 deletions cmd/authority/authority_download.go
Original file line number Diff line number Diff line change
@@ -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): ")
}
16 changes: 3 additions & 13 deletions cmd/cert/cert_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -31,26 +30,17 @@ 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)
},
}

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")

}
Expand Down

0 comments on commit 1d83f67

Please sign in to comment.