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

Split out get, describe, and delete keypairs commands #11820

Merged
merged 2 commits into from
Jun 20, 2021
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
4 changes: 3 additions & 1 deletion cmd/kops/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions cmd/kops/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type DeleteOptions struct {

var (
deleteLong = templates.LongDesc(i18n.T(`
Delete Kubernetes clusters, instancegroups, instances, and secrets, or a combination of the before mentioned.
Delete Kubernetes clusters, instancegroups, instances, keypairs, and secrets, or a combination of the before mentioned.
`))

deleteExample = templates.Examples(i18n.T(`
Expand All @@ -63,7 +63,7 @@ var (
kops delete ig --name=k8s-cluster.example.com node-example --yes
`))

deleteShort = i18n.T("Delete clusters,instancegroups, instances, or secrets.")
deleteShort = i18n.T("Delete clusters, instancegroups, instances, keypairs, or secrets.")
)

func NewCmdDelete(f *util.Factory, out io.Writer) *cobra.Command {
Expand Down Expand Up @@ -92,6 +92,7 @@ func NewCmdDelete(f *util.Factory, out io.Writer) *cobra.Command {
// create subcommands
cmd.AddCommand(NewCmdDeleteCluster(f, out))
cmd.AddCommand(NewCmdDeleteInstanceGroup(f, out))
cmd.AddCommand(NewCmdDeleteKeypair(f, out))
cmd.AddCommand(NewCmdDeleteSecret(f, out))
cmd.AddCommand(NewCmdDeleteInstance(f, out))

Expand Down
140 changes: 140 additions & 0 deletions cmd/kops/delete_keypair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"context"
"fmt"
"io"

"github.com/spf13/cobra"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)

var (
deleteKeypairLong = templates.LongDesc(i18n.T(`
Delete a keypair.`))

deleteKeypairExample = templates.Examples(i18n.T(`
# Syntax: kops delete keypair KEYSET ID
kops delete keypair ca 5938372002934847

`))

deleteKeypairShort = i18n.T(`Delete a keypair.`)
)

type DeleteKeypairOptions struct {
ClusterName string
Keyset string
KeypairID string
}

func NewCmdDeleteKeypair(f *util.Factory, out io.Writer) *cobra.Command {
options := &DeleteKeypairOptions{}

cmd := &cobra.Command{
Use: "keypair",
Short: deleteKeypairShort,
Long: deleteKeypairLong,
Example: deleteKeypairExample,
Run: func(cmd *cobra.Command, args []string) {
ctx := context.TODO()

if len(args) != 2 && len(args) != 3 {
exitWithError(fmt.Errorf("Syntax: <keyset> <id>"))
}

options.Keyset = args[0]
options.KeypairID = args[1]

options.ClusterName = rootCommand.ClusterName()

err := RunDeleteKeypair(ctx, f, out, options)
if err != nil {
exitWithError(err)
}
},
}

return cmd
}

func RunDeleteKeypair(ctx context.Context, f *util.Factory, out io.Writer, options *DeleteKeypairOptions) error {
if options.ClusterName == "" {
return fmt.Errorf("ClusterName is required")
}
if options.Keyset == "" {
return fmt.Errorf("Keyset is required")
}
if options.KeypairID == "" {
return fmt.Errorf("KeypairID is required")
}

clientset, err := f.Clientset()
if err != nil {
return err
}

cluster, err := GetCluster(ctx, f, options.ClusterName)
if err != nil {
return err
}

keyStore, err := clientset.KeyStore(cluster)
if err != nil {
return err
}

keypairs, err := listKeypairs(keyStore, []string{options.Keyset})
if err != nil {
return err
}

{
var matches []*fi.KeystoreItem
for _, s := range keypairs {
if s.ID == options.KeypairID {
matches = append(matches, s)
}
}
keypairs = matches
}

if len(keypairs) == 0 {
return fmt.Errorf("keypair not found")
}

if len(keypairs) != 1 {
// TODO: it would be friendly to print the matching keys
return fmt.Errorf("found multiple matching keypairs; specify the id of the key")
}

keyset := &kops.Keyset{}
keyset.Name = keypairs[0].Name
keyset.Spec.Type = keypairs[0].Type
err = keyStore.DeleteKeysetItem(keyset, keypairs[0].ID)
if err != nil {
return fmt.Errorf("error deleting keypair: %v", err)
}

return nil
}
12 changes: 1 addition & 11 deletions cmd/kops/delete_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,6 @@ func RunDeleteSecret(ctx context.Context, f *util.Factory, out io.Writer, option
return err
}

keyStore, err := clientset.KeyStore(cluster)
if err != nil {
return err
}

secretStore, err := clientset.SecretStore(cluster)
if err != nil {
return err
Expand All @@ -119,7 +114,7 @@ func RunDeleteSecret(ctx context.Context, f *util.Factory, out io.Writer, option
return err
}

secrets, err := listSecrets(keyStore, secretStore, sshCredentialStore, options.SecretType, []string{options.SecretName})
secrets, err := listSecrets(secretStore, sshCredentialStore, options.SecretType, []string{options.SecretName})
if err != nil {
return err
}
Expand Down Expand Up @@ -153,11 +148,6 @@ func RunDeleteSecret(ctx context.Context, f *util.Factory, out io.Writer, option
sshCredential.Spec.PublicKey = string(secrets[0].Data)
}
err = sshCredentialStore.DeleteSSHCredential(sshCredential)
default:
keyset := &kops.Keyset{}
keyset.Name = secrets[0].Name
keyset.Spec.Type = secrets[0].Type
err = keyStore.DeleteKeysetItem(keyset, secrets[0].ID)
}
if err != nil {
return fmt.Errorf("error deleting secret: %v", err)
Expand Down
77 changes: 20 additions & 57 deletions cmd/kops/describe_secrets.go → cmd/kops/describe_keypairs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,53 +27,49 @@ import (
"text/tabwriter"

"github.com/spf13/cobra"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/pki"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)

var (
describeSecretLong = templates.LongDesc(i18n.T(`
Get additional information about cluster secrets.
describeKeypairLong = templates.LongDesc(i18n.T(`
Get additional information about keypairs.
`))

describeSecretExample = templates.Examples(i18n.T(`
# Describe a secret
kops describe secrets admin
describeKeypairExample = templates.Examples(i18n.T(`
# Describe a keypair
kops describe keypairs ca
`))
describeSecretShort = i18n.T(`Describe a cluster secret`)
describeKeypairShort = i18n.T(`Describe a cluster keypair`)
)

type DescribeSecretsCommand struct {
Type string
type DescribeKeypairsCommand struct {
}

var describeSecretsCommand DescribeSecretsCommand
var describeKeypairsCommand DescribeKeypairsCommand

func init() {
cmd := &cobra.Command{
Use: "secrets",
Aliases: []string{"secret"},
Short: describeSecretShort,
Long: describeSecretLong,
Example: describeSecretExample,
Use: "keypairs",
Aliases: []string{"keypair"},
Short: describeKeypairShort,
Long: describeKeypairLong,
Example: describeKeypairExample,
Run: func(cmd *cobra.Command, args []string) {
ctx := context.TODO()
err := describeSecretsCommand.Run(ctx, args)
err := describeKeypairsCommand.Run(ctx, args)
if err != nil {
exitWithError(err)
}
},
}

describeCmd.cobraCommand.AddCommand(cmd)

cmd.Flags().StringVarP(&describeSecretsCommand.Type, "type", "", "", "Filter by secret type")
}

func (c *DescribeSecretsCommand) Run(ctx context.Context, args []string) error {
func (c *DescribeKeypairsCommand) Run(ctx context.Context, args []string) error {
cluster, err := rootCommand.Cluster(ctx)
if err != nil {
return err
Expand All @@ -89,23 +85,13 @@ func (c *DescribeSecretsCommand) Run(ctx context.Context, args []string) error {
return err
}

secretStore, err := clientset.SecretStore(cluster)
if err != nil {
return err
}

sshCredentialStore, err := clientset.SSHCredentialStore(cluster)
if err != nil {
return err
}

items, err := listSecrets(keyStore, secretStore, sshCredentialStore, c.Type, args)
items, err := listKeypairs(keyStore, args)
if err != nil {
return err
}

if len(items) == 0 {
fmt.Fprintf(os.Stderr, "No secrets found\n")
fmt.Fprintf(os.Stderr, "No keypairs found\n")

return nil
}
Expand All @@ -121,24 +107,9 @@ func (c *DescribeSecretsCommand) Run(ctx context.Context, args []string) error {
fmt.Fprintf(w, "Type:\t%s\n", i.Type)
fmt.Fprintf(w, "Id:\t%s\n", i.ID)

switch i.Type {
case kops.SecretTypeKeypair:
err = describeKeypair(keyStore, i, &b)
if err != nil {
return err
}

case SecretTypeSSHPublicKey:
err = describeSSHPublicKey(i, &b)
if err != nil {
return err
}

case kops.SecretTypeSecret:
err = describeSecret(i, &b)
if err != nil {
return err
}
err = describeKeypair(keyStore, i, &b)
if err != nil {
return err
}

b.WriteString("\n")
Expand Down Expand Up @@ -200,11 +171,3 @@ func describeKeypair(keyStore fi.CAStore, item *fi.KeystoreItem, w *bytes.Buffer

return nil
}

func describeSecret(item *fi.KeystoreItem, w *bytes.Buffer) error {
return nil
}

func describeSSHPublicKey(item *fi.KeystoreItem, w *bytes.Buffer) error {
return nil
}
1 change: 1 addition & 0 deletions cmd/kops/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func NewCmdGet(f *util.Factory, out io.Writer) *cobra.Command {
cmd.AddCommand(NewCmdGetAssets(f, out, options))
cmd.AddCommand(NewCmdGetCluster(f, out, options))
cmd.AddCommand(NewCmdGetInstanceGroups(f, out, options))
cmd.AddCommand(NewCmdGetKeypairs(f, out, options))
cmd.AddCommand(NewCmdGetSecrets(f, out, options))
cmd.AddCommand(NewCmdGetInstances(f, out, options))

Expand Down
Loading