github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/step/step_gpg_credentials.go (about)

     1  package step
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/olli-ai/jx/v2/pkg/cmd/opts/step"
    10  
    11  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
    12  
    13  	"github.com/jenkins-x/jx-logging/pkg/log"
    14  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    15  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
    16  	"github.com/olli-ai/jx/v2/pkg/kube"
    17  	"github.com/olli-ai/jx/v2/pkg/util"
    18  	"github.com/spf13/cobra"
    19  	v1 "k8s.io/api/core/v1"
    20  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    21  )
    22  
    23  const (
    24  	optionOutputFile = "output"
    25  )
    26  
    27  // StepGpgCredentialsOptions contains the command line flags
    28  type StepGpgCredentialsOptions struct {
    29  	step.StepOptions
    30  
    31  	OutputDir string
    32  }
    33  
    34  var (
    35  	StepGpgCredentialsLong = templates.LongDesc(`
    36  		This pipeline step generates GPG credentials files from the ` + kube.SecretJenkinsReleaseGPG + ` secret
    37  
    38  `)
    39  
    40  	StepGpgCredentialsExample = templates.Examples(`
    41  		# generate the GPG credentials file in the canonical location
    42  		jx step gpg credentials
    43  
    44  		# generate the git credentials to a output file
    45  		jx step gpg credentials -o /tmp/mycreds
    46  
    47  `)
    48  )
    49  
    50  func NewCmdStepGpgCredentials(commonOpts *opts.CommonOptions) *cobra.Command {
    51  	options := StepGpgCredentialsOptions{
    52  		StepOptions: step.StepOptions{
    53  			CommonOptions: commonOpts,
    54  		},
    55  	}
    56  	cmd := &cobra.Command{
    57  		Use:     "gpg credentials",
    58  		Short:   "Creates the GPG credentials file for GPG signing releases",
    59  		Long:    StepGpgCredentialsLong,
    60  		Example: StepGpgCredentialsExample,
    61  		Run: func(cmd *cobra.Command, args []string) {
    62  			options.Cmd = cmd
    63  			options.Args = args
    64  			err := options.Run()
    65  			helper.CheckErr(err)
    66  		},
    67  	}
    68  	cmd.Flags().StringVarP(&options.OutputDir, optionOutputFile, "o", "", "The output directory")
    69  	return cmd
    70  }
    71  
    72  func (o *StepGpgCredentialsOptions) Run() error {
    73  	kubeClient, curNs, err := o.KubeClientAndNamespace()
    74  	if err != nil {
    75  		return err
    76  	}
    77  	ns, _, err := kube.GetDevNamespace(kubeClient, curNs)
    78  	if err != nil {
    79  		return err
    80  	}
    81  	name := kube.SecretJenkinsReleaseGPG
    82  	secret, err := kubeClient.CoreV1().Secrets(ns).Get(name, metav1.GetOptions{})
    83  	if err != nil {
    84  		if curNs != ns {
    85  			secret2, err2 := kubeClient.CoreV1().Secrets(curNs).Get(name, metav1.GetOptions{})
    86  			if err2 == nil {
    87  				secret = secret2
    88  				err = nil
    89  			} else {
    90  				log.Logger().Warnf("Failed to find secret %s in namespace %s due to: %s", name, curNs, err2)
    91  			}
    92  		}
    93  	}
    94  	if err != nil {
    95  		return fmt.Errorf("Failed to find secret %s in namespace %s due to: %s", name, ns, err)
    96  	}
    97  	return o.GenerateGpgFiles(secret)
    98  }
    99  
   100  func (o *StepGpgCredentialsOptions) GenerateGpgFiles(secret *v1.Secret) error {
   101  	outputDir := o.OutputDir
   102  	if outputDir == "" {
   103  		outputDir = filepath.Join(util.HomeDir(), ".gnupg")
   104  	}
   105  	if outputDir == "" {
   106  		return util.MissingOption(optionOutputFile)
   107  	}
   108  	err := os.MkdirAll(outputDir, util.DefaultWritePermissions)
   109  
   110  	for k, v := range secret.Data {
   111  		fileName := filepath.Join(outputDir, k)
   112  		err = ioutil.WriteFile(fileName, v, util.DefaultWritePermissions)
   113  		if err != nil {
   114  			return err
   115  		}
   116  		log.Logger().Infof("Generated file %s", util.ColorInfo(fileName))
   117  	}
   118  	return nil
   119  }