forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_docker_auth.go
146 lines (132 loc) · 3.94 KB
/
create_docker_auth.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package cmd
import (
b64 "encoding/base64"
"encoding/json"
"io"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/AlecAivazis/survey.v1/terminal"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
username = "user"
host = "host"
)
type Config struct {
Auths map[string]*Auth `json:"auths,omitempty"`
}
type Auth struct {
Auth string `json:"auth,omitempty"`
Email string `json:"email,omitempty"`
}
var (
createDockerAuthLong = templates.LongDesc(`
Creates/updates an entry for secret in the Docker config.json for a given user, host
`)
createDockerAuthExample = templates.Examples(`
# Create/update Docker auth entry in the config.json file
jx create docker auth --host "foo.private.docker.registry" --user "foo" --secret "FooDockerHubToken" --email "fakeemail@gmail.com"
`)
)
// CreateDockerAuthOptions the options for the create Docker auth command
type CreateDockerAuthOptions struct {
CreateOptions
Host string
User string
Secret string
Email string
}
// NewCmdCreateIssue creates a command object for the "create" command
func NewCmdCreateDockerAuth(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &CreateDockerAuthOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "docker auth",
Short: "Create/update Docker auth for a given host and user in the config.json file",
Long: createDockerAuthLong,
Example: createDockerAuthExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Host, host, "t", "", "The Docker host")
cmd.Flags().StringVarP(&options.User, username, "u", "", "The user to associate auth component of config.json")
cmd.Flags().StringVarP(&options.Secret, "secret", "s", "", "The secret to associate auth component of config.json")
cmd.Flags().StringVarP(&options.Email, "email", "e", "", "The email to associate auth component of config.json")
options.addCommonFlags(cmd)
return cmd
}
// Run implements the command
func (o *CreateDockerAuthOptions) Run() error {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
if o.Host == "" {
return util.MissingOption(host)
}
if o.User == "" {
return util.MissingOption(username)
}
secret := o.Secret
if secret == "" {
prompt := &survey.Password{
Message: "Please provide secret for the host: " + o.Host + " and user: " + o.User,
}
survey.AskOne(prompt, &secret, nil, surveyOpts)
}
email := o.Email
if email == "" {
prompt := &survey.Input{
Message: "Please provide email ID for the host: " + o.Host + " and user: " + o.User,
}
survey.AskOne(prompt, &email, nil, surveyOpts)
}
kubeClient, currentNs, err := o.KubeClient()
if err != nil {
return err
}
secretFromConfig, err := kubeClient.CoreV1().Secrets(currentNs).Get("jenkins-docker-cfg", metav1.GetOptions{})
if err != nil {
return nil
}
dockerConfig := &Config{}
err = json.Unmarshal(secretFromConfig.Data["config.json"], dockerConfig)
if err != nil {
return err
}
foundAuth := false
for k, v := range dockerConfig.Auths {
if util.StringMatchesPattern(k, o.Host) {
v.Auth = b64.StdEncoding.EncodeToString([]byte(o.User + ":" + o.Secret))
v.Email = email
foundAuth = true
break
}
}
if foundAuth != true {
newConfigData := &Auth{}
newConfigData.Auth = b64.StdEncoding.EncodeToString([]byte(o.User + ":" + o.Secret))
newConfigData.Email = email
if dockerConfig.Auths == nil {
dockerConfig.Auths = map[string]*Auth{}
}
dockerConfig.Auths[o.Host] = newConfigData
}
secretFromConfig.Data["config.json"], err = json.Marshal(dockerConfig)
if err != nil {
return err
}
kubeClient.CoreV1().Secrets(currentNs).Update(secretFromConfig)
return nil
}