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