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

IPsec key status command implementation. #2287

Merged
merged 1 commit into from
Feb 13, 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
48 changes: 48 additions & 0 deletions encrypt/ipsec_key_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium

package encrypt

import (
"context"
"encoding/json"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/cilium/cilium-cli/defaults"
"github.com/cilium/cilium-cli/status"
)

// IPsecKeyStatus displays IPsec key.
func (s *Encrypt) IPsecKeyStatus(ctx context.Context) error {
ctx, cancelFn := context.WithTimeout(ctx, s.params.WaitDuration)
defer cancelFn()

secret, err := s.client.GetSecret(ctx, s.params.CiliumNamespace, defaults.EncryptionSecretName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to fetch IPsec secret: %s", err)
}

if key, ok := secret.Data["keys"]; ok {
return printIPsecKey(string(key), s.params.Output)
}
return fmt.Errorf("IPsec keys not found in the secret: %s", defaults.EncryptionSecretName)
}

type ipsecKeyStatus struct {
Key string `json:"ipsec-key"`
}

func printIPsecKey(key string, format string) error {
if format == status.OutputJSON {
js, err := json.MarshalIndent(ipsecKeyStatus{Key: key}, "", " ")
if err != nil {
return err
}
_, err = fmt.Println(string(js))
return err
}
_, err := fmt.Printf("IPsec key: %s\n", key)
return err
}
21 changes: 21 additions & 0 deletions internal/cli/cmd/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func newCmdEncrypt() *cobra.Command {
}
cmd.AddCommand(newCmdEncryptStatus())
cmd.AddCommand(newCmdIPsecRotateKey())
cmd.AddCommand(newCmdIPsecKeyStatus())
return cmd
}

Expand Down Expand Up @@ -65,6 +66,26 @@ func newCmdIPsecRotateKey() *cobra.Command {
},
}
cmd.Flags().DurationVar(&params.WaitDuration, "wait-duration", 1*time.Minute, "Maximum time to wait for result, default 1 minute")
return cmd
}

func newCmdIPsecKeyStatus() *cobra.Command {
params := encrypt.Parameters{}
cmd := &cobra.Command{
Use: "key-status",
Aliases: []string{"ks"},
Short: "Display IPsec key",
Long: "This command displays IPsec encryption key",
RunE: func(cmd *cobra.Command, args []string) error {
params.CiliumNamespace = namespace
s := encrypt.NewEncrypt(k8sClient, params)
if err := s.IPsecKeyStatus(context.Background()); err != nil {
fatalf("Unable to display IPsec key: %s", err)
}
return nil
},
}
cmd.Flags().DurationVar(&params.WaitDuration, "wait-duration", 1*time.Minute, "Maximum time to wait for result, default 1 minute")
cmd.Flags().StringVarP(&params.Output, "output", "o", status.OutputSummary, "Output format. One of: json, summary")
return cmd
}
Loading