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

Add incus remote generate-certificate #560

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions cmd/incus/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func (c *cmdRemote) Command() *cobra.Command {
remoteAddCmd := cmdRemoteAdd{global: c.global, remote: c}
cmd.AddCommand(remoteAddCmd.Command())

// Generate certificate
remoteGenerateCertificateCmd := cmdRemoteGenerateCertificate{global: c.global, remote: c}
cmd.AddCommand(remoteGenerateCertificateCmd.Command())

// Get default
remoteGetDefaultCmd := cmdRemoteGetDefault{global: c.global, remote: c}
cmd.AddCommand(remoteGetDefaultCmd.Command())
Expand Down Expand Up @@ -612,6 +616,51 @@ func (c *cmdRemoteAdd) Run(cmd *cobra.Command, args []string) error {
return conf.SaveConfig(c.global.confPath)
}

// Generate certificate.
type cmdRemoteGenerateCertificate struct {
global *cmdGlobal
remote *cmdRemote
}

func (c *cmdRemoteGenerateCertificate) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("generate-certificate")
cmd.Short = i18n.G("Generate the client certificate")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Manually trigger the generation of a client certificate`))

cmd.RunE = c.Run

return cmd
}

func (c *cmdRemoteGenerateCertificate) Run(cmd *cobra.Command, args []string) error {
conf := c.global.conf

// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 0, 0)
if exit {
return err
}

// Check if we already have a certificate.
if conf.HasClientCertificate() {
return fmt.Errorf(i18n.G("A client certificate is already present"))
}

// Generate the certificate.
if !c.global.flagQuiet {
fmt.Fprintf(os.Stderr, i18n.G("Generating a client certificate. This may take a minute...")+"\n")
}

err = conf.GenerateClientCertificate()
if err != nil {
return err
}

return nil
}

// Get default.
type cmdRemoteGetDefault struct {
global *cmdGlobal
Expand Down
Loading
Loading