Skip to content

Commit 3cce5c1

Browse files
juanvallejosoltysh
authored andcommitted
update secret cmd to externals
1 parent 779a3be commit 3cce5c1

11 files changed

+498
-398
lines changed

pkg/oc/cli/secrets/basicauth.go

+51-61
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ package secrets
33
import (
44
"errors"
55
"fmt"
6-
"io"
76
"io/ioutil"
87

98
"github.com/spf13/cobra"
109

11-
api "k8s.io/kubernetes/pkg/apis/core"
12-
kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
10+
corev1 "k8s.io/api/core/v1"
11+
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
1312
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
1413
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
1514
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
15+
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
1616
kterm "k8s.io/kubernetes/pkg/kubectl/util/term"
1717

1818
"github.com/openshift/origin/pkg/cmd/util/term"
19+
"github.com/openshift/origin/pkg/oc/util/ocscheme"
1920
)
2021

2122
// CreateBasicAuthSecretRecommendedCommandName represents name of subcommand for `oc secrets` command
@@ -44,6 +45,10 @@ var (
4445

4546
// CreateBasicAuthSecretOptions holds the credential needed to authenticate against SCM servers.
4647
type CreateBasicAuthSecretOptions struct {
48+
PrintFlags *genericclioptions.PrintFlags
49+
50+
Printer printers.ResourcePrinter
51+
4752
SecretName string
4853
Username string
4954
Password string
@@ -52,18 +57,21 @@ type CreateBasicAuthSecretOptions struct {
5257

5358
PromptForPassword bool
5459

55-
Reader io.Reader
56-
Out io.Writer
60+
SecretsInterface corev1client.SecretInterface
5761

58-
SecretsInterface kcoreclient.SecretInterface
62+
genericclioptions.IOStreams
63+
}
64+
65+
func NewCreateBasicAuthSecretOptions(streams genericclioptions.IOStreams) *CreateBasicAuthSecretOptions {
66+
return &CreateBasicAuthSecretOptions{
67+
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(ocscheme.PrintingInternalScheme),
68+
IOStreams: streams,
69+
}
5970
}
6071

6172
// NewCmdCreateBasicAuthSecret implements the OpenShift cli secrets new-basicauth subcommand
6273
func NewCmdCreateBasicAuthSecret(name, fullName string, f kcmdutil.Factory, streams genericclioptions.IOStreams, newSecretFullName, ocEditFullName string) *cobra.Command {
63-
o := &CreateBasicAuthSecretOptions{
64-
Out: streams.Out,
65-
Reader: streams.In,
66-
}
74+
o := NewCreateBasicAuthSecretOptions(streams)
6775

6876
cmd := &cobra.Command{
6977
Use: fmt.Sprintf("%s SECRET --username=USERNAME --password=PASSWORD [--ca-cert=FILENAME] [--gitconfig=FILENAME]", name),
@@ -73,25 +81,9 @@ func NewCmdCreateBasicAuthSecret(name, fullName string, f kcmdutil.Factory, stre
7381
Deprecated: "use oc create secret",
7482
Hidden: true,
7583
Run: func(c *cobra.Command, args []string) {
76-
if err := o.Complete(f, args); err != nil {
77-
kcmdutil.CheckErr(kcmdutil.UsageErrorf(c, err.Error()))
78-
}
79-
80-
if err := o.Validate(); err != nil {
81-
kcmdutil.CheckErr(kcmdutil.UsageErrorf(c, err.Error()))
82-
}
83-
84-
if len(kcmdutil.GetFlagString(c, "output")) != 0 {
85-
secret, err := o.NewBasicAuthSecret()
86-
kcmdutil.CheckErr(err)
87-
88-
kcmdutil.CheckErr(kcmdutil.PrintObject(c, secret, streams.Out))
89-
return
90-
}
91-
92-
if err := o.CreateBasicAuthSecret(); err != nil {
93-
kcmdutil.CheckErr(err)
94-
}
84+
kcmdutil.CheckErr(o.Complete(f, args))
85+
kcmdutil.CheckErr(o.Validate())
86+
kcmdutil.CheckErr(o.Run())
9587
},
9688
}
9789

@@ -103,13 +95,12 @@ func NewCmdCreateBasicAuthSecret(name, fullName string, f kcmdutil.Factory, stre
10395
cmd.MarkFlagFilename("gitconfig")
10496
cmd.Flags().BoolVarP(&o.PromptForPassword, "prompt", "", false, "If true, prompt for password or token")
10597

106-
kcmdutil.AddPrinterFlags(cmd)
107-
98+
o.PrintFlags.AddFlags(cmd)
10899
return cmd
109100
}
110101

111102
// CreateBasicAuthSecret saves created Secret structure and prints the secret name to the output on success.
112-
func (o *CreateBasicAuthSecretOptions) CreateBasicAuthSecret() error {
103+
func (o *CreateBasicAuthSecretOptions) Run() error {
113104
secret, err := o.NewBasicAuthSecret()
114105
if err != nil {
115106
return err
@@ -119,16 +110,15 @@ func (o *CreateBasicAuthSecretOptions) CreateBasicAuthSecret() error {
119110
return err
120111
}
121112

122-
fmt.Fprintf(o.GetOut(), "secret/%s\n", secret.Name)
123-
return nil
113+
return o.Printer.PrintObj(secret, o.Out)
124114
}
125115

126116
// NewBasicAuthSecret builds up the Secret structure containing secret name, type and data structure
127117
// containing desired credentials.
128-
func (o *CreateBasicAuthSecretOptions) NewBasicAuthSecret() (*api.Secret, error) {
129-
secret := &api.Secret{}
118+
func (o *CreateBasicAuthSecretOptions) NewBasicAuthSecret() (*corev1.Secret, error) {
119+
secret := &corev1.Secret{}
130120
secret.Name = o.SecretName
131-
secret.Type = api.SecretTypeBasicAuth
121+
secret.Type = corev1.SecretTypeBasicAuth
132122
secret.Data = map[string][]byte{}
133123

134124
if len(o.Username) != 0 {
@@ -164,32 +154,38 @@ func (o *CreateBasicAuthSecretOptions) Complete(f kcmdutil.Factory, args []strin
164154
if len(args) != 1 {
165155
return errors.New("must have exactly one argument: secret name")
166156
}
157+
167158
o.SecretName = args[0]
168159

169160
if o.PromptForPassword {
170-
if len(o.Password) != 0 {
171-
return errors.New("must provide either --prompt or --password flag")
172-
}
173-
if !kterm.IsTerminal(o.Reader) {
161+
if !kterm.IsTerminal(o.In) {
174162
return errors.New("provided reader is not a terminal")
175163
}
176164

177-
o.Password = term.PromptForPasswordString(o.Reader, o.Out, "Password: ")
165+
o.Password = term.PromptForPasswordString(o.In, o.Out, "Password: ")
178166
if len(o.Password) == 0 {
179167
return errors.New("password must be provided")
180168
}
181169
}
182170

183-
if f != nil {
184-
client, err := f.ClientSet()
185-
if err != nil {
186-
return err
187-
}
188-
namespace, _, err := f.ToRawKubeConfigLoader().Namespace()
189-
if err != nil {
190-
return err
191-
}
192-
o.SecretsInterface = client.Core().Secrets(namespace)
171+
config, err := f.ToRESTConfig()
172+
if err != nil {
173+
return err
174+
}
175+
176+
clientset, err := corev1client.NewForConfig(config)
177+
if err != nil {
178+
return err
179+
}
180+
namespace, _, err := f.ToRawKubeConfigLoader().Namespace()
181+
if err != nil {
182+
return err
183+
}
184+
o.SecretsInterface = clientset.Secrets(namespace)
185+
186+
o.Printer, err = o.PrintFlags.ToPrinter()
187+
if err != nil {
188+
return err
193189
}
194190

195191
return nil
@@ -205,15 +201,9 @@ func (o CreateBasicAuthSecretOptions) Validate() error {
205201
return errors.New("must provide basic authentication credentials")
206202
}
207203

208-
return nil
209-
}
210-
211-
// GetOut check if the CreateBasicAuthSecretOptions Out Writer is set. Returns it if the Writer
212-
// is present, if not returns Writer on which all Write calls succeed without doing anything.
213-
func (o CreateBasicAuthSecretOptions) GetOut() io.Writer {
214-
if o.Out == nil {
215-
return ioutil.Discard
204+
if o.PromptForPassword && len(o.Password) > 0 {
205+
return errors.New("must provide either --prompt or --password flag")
216206
}
217207

218-
return o.Out
208+
return nil
219209
}

pkg/oc/cli/secrets/basicauth_test.go

+45-36
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,87 @@ package secrets
33
import (
44
"testing"
55

6-
api "k8s.io/kubernetes/pkg/apis/core"
6+
corev1 "k8s.io/api/core/v1"
7+
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
78
)
89

910
func TestValidateBasicAuth(t *testing.T) {
1011
tests := []struct {
1112
testName string
12-
args []string
13-
params CreateBasicAuthSecretOptions
13+
options func(genericclioptions.IOStreams) *CreateBasicAuthSecretOptions
1414
expErr bool
1515
}{
1616
{
1717
testName: "validArgs",
18-
args: []string{"testSecret"},
19-
params: CreateBasicAuthSecretOptions{
20-
Username: "testUser",
21-
Password: "testPassword",
18+
options: func(streams genericclioptions.IOStreams) *CreateBasicAuthSecretOptions {
19+
o := NewCreateBasicAuthSecretOptions(streams)
20+
o.Username = "testUser"
21+
o.Password = "testPassword"
22+
o.SecretName = "testSecret"
23+
return o
2224
},
2325
expErr: false,
2426
},
2527
{
2628
testName: "validArgsWithCertificate",
27-
args: []string{"testSecret"},
28-
params: CreateBasicAuthSecretOptions{
29-
Username: "testUser",
30-
Password: "testPassword",
31-
CertificatePath: "./bsFixtures/valid/ca.crt",
29+
options: func(streams genericclioptions.IOStreams) *CreateBasicAuthSecretOptions {
30+
o := NewCreateBasicAuthSecretOptions(streams)
31+
o.Username = "testUser"
32+
o.Password = "testPassword"
33+
o.SecretName = "testSecret"
34+
o.CertificatePath = "./bsFixtures/valid/ca.crt"
35+
return o
3236
},
3337
expErr: false,
3438
},
3539
{
3640
testName: "validArgsWithGitconfig",
37-
args: []string{"testSecret"},
38-
params: CreateBasicAuthSecretOptions{
39-
Username: "testUser",
40-
Password: "testPassword",
41-
GitConfigPath: "./bsFixtures/leadingdot/.gitconfig",
41+
options: func(streams genericclioptions.IOStreams) *CreateBasicAuthSecretOptions {
42+
o := NewCreateBasicAuthSecretOptions(streams)
43+
o.Username = "testUser"
44+
o.Password = "testPassword"
45+
o.SecretName = "testSecret"
46+
o.GitConfigPath = "./bsFixtures/leadingdot/.gitconfig"
47+
return o
4248
},
4349
expErr: false,
4450
},
4551
{
4652
testName: "noName",
47-
args: []string{},
48-
params: CreateBasicAuthSecretOptions{
49-
Username: "testUser",
50-
Password: "testPassword",
53+
options: func(streams genericclioptions.IOStreams) *CreateBasicAuthSecretOptions {
54+
o := NewCreateBasicAuthSecretOptions(streams)
55+
o.Username = "testUser"
56+
o.Password = "testPassword"
57+
return o
5158
},
5259
expErr: true, //"Must have exactly one argument: secret name"
5360
},
5461
{
5562
testName: "noParams",
56-
args: []string{"testSecret"},
57-
params: CreateBasicAuthSecretOptions{},
58-
expErr: true, //"Must provide basic authentication credentials"
63+
options: func(streams genericclioptions.IOStreams) *CreateBasicAuthSecretOptions {
64+
o := NewCreateBasicAuthSecretOptions(streams)
65+
o.SecretName = "testSecret"
66+
return o
67+
},
68+
expErr: true, //"Must provide basic authentication credentials"
5969
},
6070
{
6171
testName: "passwordAndPrompt",
62-
args: []string{"testSecret"},
63-
params: CreateBasicAuthSecretOptions{
64-
Username: "testUser",
65-
Password: "testPassword",
66-
PromptForPassword: true,
72+
options: func(streams genericclioptions.IOStreams) *CreateBasicAuthSecretOptions {
73+
o := NewCreateBasicAuthSecretOptions(streams)
74+
o.Username = "testUser"
75+
o.Password = "testPassword"
76+
o.SecretName = "testSecret"
77+
o.PromptForPassword = true
78+
return o
6779
},
6880
expErr: true, //"Must provide either --prompt or --password flag"
6981
},
7082
}
7183

7284
for _, test := range tests {
73-
options := test.params
74-
err := options.Complete(nil, test.args)
75-
if err == nil {
76-
err = options.Validate()
77-
}
78-
85+
options := test.options(genericclioptions.NewTestIOStreamsDiscard())
86+
err := options.Validate()
7987
if test.expErr {
8088
if err == nil {
8189
t.Errorf("%s: unexpected error: %v", test.testName, err)
@@ -85,13 +93,14 @@ func TestValidateBasicAuth(t *testing.T) {
8593

8694
if err != nil {
8795
t.Errorf("%s: unexpected error: %v", test.testName, err)
96+
continue
8897
}
8998

9099
secret, err := options.NewBasicAuthSecret()
91100
if err != nil {
92101
t.Errorf("%s: unexpected error: %v", test.testName, err)
93102
}
94-
if secret.Type != api.SecretTypeBasicAuth {
103+
if secret.Type != corev1.SecretTypeBasicAuth {
95104
t.Errorf("%s: unexpected secret.Type: %v", test.testName, secret.Type)
96105
}
97106
}

0 commit comments

Comments
 (0)