Skip to content
Open
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
61 changes: 61 additions & 0 deletions cmd/kms/key/formatting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package key

import (
"fmt"
"os"
"strconv"
"strings"

"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/table"
v3 "github.com/exoscale/egoscale/v3"
)

type successResponseOutput v3.SuccessResponse

func (o *successResponseOutput) ToJSON() { output.JSON(o) }
func (o *successResponseOutput) ToText() { output.Text(o) }
func (o *successResponseOutput) ToTable() {
t := table.NewTable(os.Stdout)
defer t.Render()

t.SetHeader([]string{
"STATUS",
})

t.Append([]string{
string(o.Status),
})
}

func formatKeyRotationConfig(s *v3.KeyRotationConfig) string {
if s == nil {
return ""
}
return fmt.Sprintf("auto: %s\ncount: %d\nnextAt: %s\nrotationPeriod: %d",
strconv.FormatBool(*s.Automatic),
s.ManualCount,
s.NextAT,
s.RotationPeriod)
}

func formatKeyMaterial(s *v3.KeyMaterial) string {
if s == nil {
return "-"
}
return fmt.Sprintf("auto: %s\ncreatedAt: %s\nversion: %d",
strconv.FormatBool(*s.Automatic),
s.CreatedAT,
s.Version)
}

func formatReplicaStatus(s []v3.ReplicaState) string {
if len(s) == 0 {
return "-"
}
var res []string
for _, r := range s {
res = append(res, r.Zone)
}
return strings.Join(res, ", ")
}
15 changes: 15 additions & 0 deletions cmd/kms/key/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package key

import (
"github.com/exoscale/cli/cmd/kms"
"github.com/spf13/cobra"
)

var keyCmd = &cobra.Command{
Use: "key",
Short: "KMS key",
}

func init() {
kms.KMSCmd.AddCommand(keyCmd)
}
61 changes: 61 additions & 0 deletions cmd/kms/key/key_cancel_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package key

import (
exocmd "github.com/exoscale/cli/cmd"
"github.com/exoscale/cli/pkg/globalstate"
v3 "github.com/exoscale/egoscale/v3"
"github.com/spf13/cobra"
)

type keyCancelDeleteCmd struct {
exocmd.CliCommandSettings `cli-cmd:"-"`

_ bool `cli-cmd:"cancel-delete"`

Key string `cli-arg:"#" cli-usage:"ID"`

Zone v3.ZoneName `cli-short:"z" cli-flag:"zone" cli-usage:"key zone"`
}

func (c *keyCancelDeleteCmd) CmdAliases() []string { return nil }

func (c *keyCancelDeleteCmd) CmdShort() string {
return "Cancels the scheduled deletion of a KMS key."
}

func (c *keyCancelDeleteCmd) CmdLong() string {
return "Cancels the scheduled deletion of a KMS key."
}

func (c *keyCancelDeleteCmd) CmdPreRun(cmd *cobra.Command, args []string) error {
exocmd.CmdSetZoneFlagFromDefault(cmd)
return exocmd.CliCommandDefaultPreRun(c, cmd, args)
}

func (c *keyCancelDeleteCmd) CmdRun(_ *cobra.Command, _ []string) error {
ctx := exocmd.GContext
client, err := exocmd.SwitchClientZoneV3(ctx, globalstate.EgoscaleV3Client, c.Zone)
if err != nil {
return err
}

_, err = client.CancelKmsKeyDeletion(ctx, v3.UUID(c.Key))
if err != nil {
return err
}

if !globalstate.Quiet {
return (&KeyShowCmd{
CliCommandSettings: c.CliCommandSettings,
Key: c.Key,
}).CmdRun(nil, nil)
}

return nil
}

func init() {
cobra.CheckErr(exocmd.RegisterCLICommand(keyCmd, &keyCancelDeleteCmd{
CliCommandSettings: exocmd.DefaultCLICmdSettings(),
}))
}
71 changes: 71 additions & 0 deletions cmd/kms/key/key_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package key

import (
exocmd "github.com/exoscale/cli/cmd"
"github.com/exoscale/cli/pkg/globalstate"
v3 "github.com/exoscale/egoscale/v3"
"github.com/spf13/cobra"
)

type keyCreateCmd struct {
exocmd.CliCommandSettings `cli-cmd:"-"`

_ bool `cli-cmd:"create"`

Name string `cli-arg:"#" cli-usage:"NAME"`

Description string `cli-short:"d" cli-flag:"description" cli-usage:"key description"`
Usage string `cli-short:"u" cli-flag:"usage" cli-usage:"key usage [encrypt-decrypt]"`
Multizone bool `cli-short:"m" cli-flag:"multizone" cli-usage:"allow replication accross zones (default: false)"`
Zone v3.ZoneName `cli-short:"z" cli-flag:"zone" cli-usage:"key zone"`
}

func (c *keyCreateCmd) CmdAliases() []string { return nil }

func (c *keyCreateCmd) CmdShort() string {
return "Creates a new KMS key."
}

func (c *keyCreateCmd) CmdLong() string {
return "Creates a new KMS key."
}

func (c *keyCreateCmd) CmdPreRun(cmd *cobra.Command, args []string) error {
exocmd.CmdSetZoneFlagFromDefault(cmd)
return exocmd.CliCommandDefaultPreRun(c, cmd, args)
}

func (c *keyCreateCmd) CmdRun(_ *cobra.Command, _ []string) error {
ctx := exocmd.GContext
client, err := exocmd.SwitchClientZoneV3(ctx, globalstate.EgoscaleV3Client, c.Zone)
if err != nil {
return err
}

req := v3.CreateKmsKeyRequest{
Name: c.Name,
Description: c.Description,
Usage: v3.CreateKmsKeyRequestUsage(c.Usage),
MultiZone: &c.Multizone,
}

resp, err := client.CreateKmsKey(ctx, req)
if err != nil {
return err
}

if !globalstate.Quiet {
return (&KeyShowCmd{
CliCommandSettings: c.CliCommandSettings,
Key: resp.ID.String(),
}).CmdRun(nil, nil)
}

return nil
}

func init() {
cobra.CheckErr(exocmd.RegisterCLICommand(keyCmd, &keyCreateCmd{
CliCommandSettings: exocmd.DefaultCLICmdSettings(),
}))
}
97 changes: 97 additions & 0 deletions cmd/kms/key/key_decrypt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package key

import (
"encoding/base64"
"os"

exocmd "github.com/exoscale/cli/cmd"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/table"
v3 "github.com/exoscale/egoscale/v3"
"github.com/spf13/cobra"
)

type keyDecryptOutput struct {
Plaintext string `json:"plaintext"`
}

func (o *keyDecryptOutput) ToJSON() { output.JSON(o) }
func (o *keyDecryptOutput) ToText() { output.Text(o) }
func (o *keyDecryptOutput) ToTable() {
t := table.NewTable(os.Stdout)
defer t.Render()

t.SetHeader([]string{
"PLAINTEXT",
})

t.Append([]string{
o.Plaintext,
})
}

type keyDecryptCmd struct {
exocmd.CliCommandSettings `cli-cmd:"-"`

_ bool `cli-cmd:"decrypt"`

Key string `cli-arg:"#" cli-usage:"ID"`
Ciphertext string `cli-arg:"#" cli-usage:"CIPHERTEXT"`

EncryptionContext string `cli-short:"e" cli-flag:"encryption-context" cli-usage:"encryption context to use for decryption"`
Zone v3.ZoneName `cli-short:"z" cli-flag:"zone" cli-usage:"key zone"`
}

func (c *keyDecryptCmd) CmdAliases() []string { return nil }

func (c *keyDecryptCmd) CmdShort() string {
return "Decrypts data using a KMS key."
}

func (c *keyDecryptCmd) CmdLong() string {
return "Decrypts data using a KMS key."
}

func (c *keyDecryptCmd) CmdPreRun(cmd *cobra.Command, args []string) error {
exocmd.CmdSetZoneFlagFromDefault(cmd)
return exocmd.CliCommandDefaultPreRun(c, cmd, args)
}

func (c *keyDecryptCmd) CmdRun(_ *cobra.Command, _ []string) error {
ctx := exocmd.GContext
client, err := exocmd.SwitchClientZoneV3(ctx, globalstate.EgoscaleV3Client, c.Zone)
if err != nil {
return err
}

ec := []byte(c.EncryptionContext)
decoded, err := base64.StdEncoding.DecodeString(c.Ciphertext)
if err != nil {
return err
}
req := v3.DecryptRequest{
Ciphertext: decoded,
EncryptionContext: &ec,
}

resp, err := client.Decrypt(ctx, v3.UUID(c.Key), req)
if err != nil {
return err
}

if !globalstate.Quiet {
out := keyDecryptOutput{
Plaintext: base64.StdEncoding.EncodeToString(resp.Plaintext),
}
return c.OutputFunc(&out, nil)
}

return nil
}

func init() {
cobra.CheckErr(exocmd.RegisterCLICommand(keyCmd, &keyDecryptCmd{
CliCommandSettings: exocmd.DefaultCLICmdSettings(),
}))
}
78 changes: 78 additions & 0 deletions cmd/kms/key/key_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package key

import (
"fmt"
"strconv"

exocmd "github.com/exoscale/cli/cmd"
"github.com/exoscale/cli/pkg/globalstate"
v3 "github.com/exoscale/egoscale/v3"
"github.com/spf13/cobra"
)

type keyDeleteCmd struct {
exocmd.CliCommandSettings `cli-cmd:"-"`

_ bool `cli-cmd:"delete"`

Key string `cli-arg:"#" cli-usage:"ID"`

DelayDays string `cli-short:"d" cli-flag:"delay-days" cli-usage:"number of days before deletion (7 - 30, default: 30)"`
Zone v3.ZoneName `cli-short:"z" cli-flag:"zone" cli-usage:"key zone"`
}

func (c *keyDeleteCmd) CmdAliases() []string { return nil }

func (c *keyDeleteCmd) CmdShort() string {
return "Deletes a KMS key."
}

func (c *keyDeleteCmd) CmdLong() string {
return "Deletes a KMS key."
}

func (c *keyDeleteCmd) CmdPreRun(cmd *cobra.Command, args []string) error {
exocmd.CmdSetZoneFlagFromDefault(cmd)
return exocmd.CliCommandDefaultPreRun(c, cmd, args)
}

func (c *keyDeleteCmd) CmdRun(_ *cobra.Command, _ []string) error {
ctx := exocmd.GContext
client, err := exocmd.SwitchClientZoneV3(ctx, globalstate.EgoscaleV3Client, c.Zone)
if err != nil {
return err
}

var delayDays int
if c.DelayDays != "" {
n, err := strconv.Atoi(c.DelayDays)
if err != nil {
return fmt.Errorf("invalid delay days: %v", err)
}
delayDays = n
}

req := v3.ScheduleKmsKeyDeletionRequest{
DelayDays: delayDays,
}

_, err = client.ScheduleKmsKeyDeletion(ctx, v3.UUID(c.Key), req)
if err != nil {
return err
}

if !globalstate.Quiet {
return (&KeyShowCmd{
CliCommandSettings: c.CliCommandSettings,
Key: c.Key,
}).CmdRun(nil, nil)
}

return nil
}

func init() {
cobra.CheckErr(exocmd.RegisterCLICommand(keyCmd, &keyDeleteCmd{
CliCommandSettings: exocmd.DefaultCLICmdSettings(),
}))
}
Loading
Loading