github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/cmd/orbctl/readsecret.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/caos/orbos/internal/secret/operators"
    10  	"github.com/caos/orbos/pkg/kubernetes/cli"
    11  	"github.com/caos/orbos/pkg/secret"
    12  )
    13  
    14  func ReadSecretCommand(getRv GetRootValues) *cobra.Command {
    15  
    16  	return &cobra.Command{
    17  		Use:   "readsecret [path]",
    18  		Short: "Print a secrets decrypted value to stdout",
    19  		Long:  "Print a secrets decrypted value to stdout.\nIf no path is provided, a secret can interactively be chosen from a list of all possible secrets",
    20  		Args:  cobra.MaximumNArgs(1),
    21  		Example: `orbctl readsecret
    22  orbctl readsecret orbiter.k8s.kubeconfig.encrypted
    23  orbctl readsecret orbiter.k8s.kubeconfig.encrypted > ~/.kube/config`,
    24  		RunE: func(cmd *cobra.Command, args []string) (err error) {
    25  
    26  			path := ""
    27  			if len(args) > 0 {
    28  				path = args[0]
    29  			}
    30  
    31  			rv := getRv("readsecret", "", map[string]interface{}{"path": path})
    32  			defer rv.ErrFunc(err)
    33  
    34  			k8sClient, err := cli.Init(monitor, rv.OrbConfig, rv.GitClient, rv.Kubeconfig, rv.Gitops, rv.Gitops, rv.Gitops)
    35  			if err != nil && (!rv.Gitops || !errors.Is(err, cli.ErrNotInitialized)) {
    36  				return err
    37  			}
    38  
    39  			value, err := secret.Read(
    40  				k8sClient,
    41  				path,
    42  				operators.GetAllSecretsFunc(monitor, path == "", rv.Gitops, rv.GitClient, k8sClient, rv.OrbConfig),
    43  			)
    44  			if err != nil {
    45  				return err
    46  			}
    47  			if _, err := os.Stdout.Write([]byte(value)); err != nil {
    48  				panic(err)
    49  			}
    50  			return nil
    51  		},
    52  	}
    53  }