Skip to content

Commit

Permalink
update secret cmd to externals
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Jul 25, 2018
1 parent e764e2a commit b86760e
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 235 deletions.
94 changes: 45 additions & 49 deletions pkg/oc/cli/secrets/basicauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package secrets
import (
"errors"
"fmt"
"io"
"io/ioutil"

"github.com/spf13/cobra"

api "k8s.io/kubernetes/pkg/apis/core"
kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
coreapiv1 "k8s.io/api/core/v1"
coreclientv1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
kterm "k8s.io/kubernetes/pkg/kubectl/util/term"

"github.com/openshift/origin/pkg/cmd/util/term"
"github.com/openshift/origin/pkg/oc/util/ocscheme"
)

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

// CreateBasicAuthSecretOptions holds the credential needed to authenticate against SCM servers.
type CreateBasicAuthSecretOptions struct {
PrintFlags *genericclioptions.PrintFlags

Printer printers.ResourcePrinter

SecretName string
Username string
Password string
Expand All @@ -52,18 +57,21 @@ type CreateBasicAuthSecretOptions struct {

PromptForPassword bool

Reader io.Reader
Out io.Writer
SecretsInterface coreclientv1.SecretInterface

SecretsInterface kcoreclient.SecretInterface
genericclioptions.IOStreams
}

func NewCreateBasicAuthSecretOptions(streams genericclioptions.IOStreams) *CreateBasicAuthSecretOptions {
return &CreateBasicAuthSecretOptions{
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(ocscheme.PrintingInternalScheme),
IOStreams: streams,
}
}

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

cmd := &cobra.Command{
Use: fmt.Sprintf("%s SECRET --username=USERNAME --password=PASSWORD [--ca-cert=FILENAME] [--gitconfig=FILENAME]", name),
Expand All @@ -81,15 +89,7 @@ func NewCmdCreateBasicAuthSecret(name, fullName string, f kcmdutil.Factory, stre
kcmdutil.CheckErr(kcmdutil.UsageErrorf(c, err.Error()))
}

if len(kcmdutil.GetFlagString(c, "output")) != 0 {
secret, err := o.NewBasicAuthSecret()
kcmdutil.CheckErr(err)

kcmdutil.CheckErr(kcmdutil.PrintObject(c, secret, streams.Out))
return
}

if err := o.CreateBasicAuthSecret(); err != nil {
if err := o.Run(); err != nil {
kcmdutil.CheckErr(err)
}
},
Expand All @@ -103,13 +103,12 @@ func NewCmdCreateBasicAuthSecret(name, fullName string, f kcmdutil.Factory, stre
cmd.MarkFlagFilename("gitconfig")
cmd.Flags().BoolVarP(&o.PromptForPassword, "prompt", "", false, "If true, prompt for password or token")

kcmdutil.AddPrinterFlags(cmd)

o.PrintFlags.AddFlags(cmd)
return cmd
}

// CreateBasicAuthSecret saves created Secret structure and prints the secret name to the output on success.
func (o *CreateBasicAuthSecretOptions) CreateBasicAuthSecret() error {
func (o *CreateBasicAuthSecretOptions) Run() error {
secret, err := o.NewBasicAuthSecret()
if err != nil {
return err
Expand All @@ -119,16 +118,15 @@ func (o *CreateBasicAuthSecretOptions) CreateBasicAuthSecret() error {
return err
}

fmt.Fprintf(o.GetOut(), "secret/%s\n", secret.Name)
return nil
return o.Printer.PrintObj(secret, o.Out)
}

// NewBasicAuthSecret builds up the Secret structure containing secret name, type and data structure
// containing desired credentials.
func (o *CreateBasicAuthSecretOptions) NewBasicAuthSecret() (*api.Secret, error) {
secret := &api.Secret{}
func (o *CreateBasicAuthSecretOptions) NewBasicAuthSecret() (*coreapiv1.Secret, error) {
secret := &coreapiv1.Secret{}
secret.Name = o.SecretName
secret.Type = api.SecretTypeBasicAuth
secret.Type = coreapiv1.SecretTypeBasicAuth
secret.Data = map[string][]byte{}

if len(o.Username) != 0 {
Expand Down Expand Up @@ -170,26 +168,34 @@ func (o *CreateBasicAuthSecretOptions) Complete(f kcmdutil.Factory, args []strin
if len(o.Password) != 0 {
return errors.New("must provide either --prompt or --password flag")
}
if !kterm.IsTerminal(o.Reader) {
if !kterm.IsTerminal(o.In) {
return errors.New("provided reader is not a terminal")
}

o.Password = term.PromptForPasswordString(o.Reader, o.Out, "Password: ")
o.Password = term.PromptForPasswordString(o.In, o.Out, "Password: ")
if len(o.Password) == 0 {
return errors.New("password must be provided")
}
}

if f != nil {
client, err := f.ClientSet()
if err != nil {
return err
}
namespace, _, err := f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
o.SecretsInterface = client.Core().Secrets(namespace)
config, err := f.ToRESTConfig()
if err != nil {
return err
}

clientset, err := coreclientv1.NewForConfig(config)
if err != nil {
return err
}
namespace, _, err := f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
o.SecretsInterface = clientset.Secrets(namespace)

o.Printer, err = o.PrintFlags.ToPrinter()
if err != nil {
return err
}

return nil
Expand All @@ -207,13 +213,3 @@ func (o CreateBasicAuthSecretOptions) Validate() error {

return nil
}

// GetOut check if the CreateBasicAuthSecretOptions Out Writer is set. Returns it if the Writer
// is present, if not returns Writer on which all Write calls succeed without doing anything.
func (o CreateBasicAuthSecretOptions) GetOut() io.Writer {
if o.Out == nil {
return ioutil.Discard
}

return o.Out
}
77 changes: 40 additions & 37 deletions pkg/oc/cli/secrets/dockercfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"strings"

api "k8s.io/kubernetes/pkg/apis/core"
kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
"github.com/spf13/cobra"

"github.com/openshift/origin/pkg/oc/util/ocscheme"
coreapiv1 "k8s.io/api/core/v1"
coreclientv1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/kubernetes/pkg/credentialprovider"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"

"github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
)

const CreateDockerConfigSecretRecommendedName = "new-dockercfg"
Expand Down Expand Up @@ -50,20 +50,31 @@ var (
)

type CreateDockerConfigOptions struct {
PrintFlags *genericclioptions.PrintFlags

Printer printers.ResourcePrinter

SecretName string
RegistryLocation string
Username string
Password string
EmailAddress string

SecretsInterface kcoreclient.SecretInterface
SecretsInterface coreclientv1.SecretInterface

genericclioptions.IOStreams
}

Out io.Writer
func NewCreateDockerConfigOptions(streams genericclioptions.IOStreams) *CreateDockerConfigOptions {
return &CreateDockerConfigOptions{
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(ocscheme.PrintingInternalScheme),
IOStreams: streams,
}
}

// NewCmdCreateDockerConfigSecret creates a command object for making a dockercfg secret
func NewCmdCreateDockerConfigSecret(name, fullName string, f kcmdutil.Factory, streams genericclioptions.IOStreams, newSecretFullName, ocEditFullName string) *cobra.Command {
o := &CreateDockerConfigOptions{Out: streams.Out}
o := NewCreateDockerConfigOptions(streams)

cmd := &cobra.Command{
Use: fmt.Sprintf("%s SECRET --docker-server=DOCKER_REGISTRY_SERVER --docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL", name),
Expand All @@ -81,15 +92,7 @@ func NewCmdCreateDockerConfigSecret(name, fullName string, f kcmdutil.Factory, s
kcmdutil.CheckErr(kcmdutil.UsageErrorf(c, err.Error()))
}

if len(kcmdutil.GetFlagString(c, "output")) != 0 {
secret, err := o.NewDockerSecret()
kcmdutil.CheckErr(err)

kcmdutil.CheckErr(kcmdutil.PrintObject(c, secret, streams.Out))
return
}

if err := o.CreateDockerSecret(); err != nil {
if err := o.Run(); err != nil {
kcmdutil.CheckErr(err)
}

Expand All @@ -100,12 +103,12 @@ func NewCmdCreateDockerConfigSecret(name, fullName string, f kcmdutil.Factory, s
cmd.Flags().StringVar(&o.Password, "docker-password", "", "Password for Docker registry authentication")
cmd.Flags().StringVar(&o.EmailAddress, "docker-email", "", "Email for Docker registry")
cmd.Flags().StringVar(&o.RegistryLocation, "docker-server", "https://index.docker.io/v1/", "Server location for Docker registry")
kcmdutil.AddPrinterFlags(cmd)

o.PrintFlags.AddFlags(cmd)
return cmd
}

func (o CreateDockerConfigOptions) CreateDockerSecret() error {
func (o CreateDockerConfigOptions) Run() error {
secret, err := o.NewDockerSecret()
if err != nil {
return err
Expand All @@ -115,12 +118,10 @@ func (o CreateDockerConfigOptions) CreateDockerSecret() error {
return err
}

fmt.Fprintf(o.GetOut(), "secret/%s\n", secret.Name)

return nil
return o.Printer.PrintObj(secret, o.Out)
}

func (o CreateDockerConfigOptions) NewDockerSecret() (*api.Secret, error) {
func (o CreateDockerConfigOptions) NewDockerSecret() (*coreapiv1.Secret, error) {
dockercfgAuth := credentialprovider.DockerConfigEntry{
Username: o.Username,
Password: o.Password,
Expand All @@ -136,11 +137,11 @@ func (o CreateDockerConfigOptions) NewDockerSecret() (*api.Secret, error) {
return nil, err
}

secret := &api.Secret{}
secret := &coreapiv1.Secret{}
secret.Name = o.SecretName
secret.Type = api.SecretTypeDockerConfigJson
secret.Type = coreapiv1.SecretTypeDockerConfigJson
secret.Data = map[string][]byte{}
secret.Data[api.DockerConfigJsonKey] = dockercfgContent
secret.Data[coreapiv1.DockerConfigJsonKey] = dockercfgContent

return secret, nil
}
Expand All @@ -151,7 +152,12 @@ func (o *CreateDockerConfigOptions) Complete(f kcmdutil.Factory, args []string)
}
o.SecretName = args[0]

client, err := f.ClientSet()
config, err := f.ToRESTConfig()
if err != nil {
return err
}

client, err := coreclientv1.NewForConfig(config)
if err != nil {
return err
}
Expand All @@ -160,7 +166,12 @@ func (o *CreateDockerConfigOptions) Complete(f kcmdutil.Factory, args []string)
return err
}

o.SecretsInterface = client.Core().Secrets(namespace)
o.SecretsInterface = client.Secrets(namespace)

o.Printer, err = o.PrintFlags.ToPrinter()
if err != nil {
return err
}

return nil
}
Expand Down Expand Up @@ -191,11 +202,3 @@ func (o CreateDockerConfigOptions) Validate() error {

return nil
}

func (o CreateDockerConfigOptions) GetOut() io.Writer {
if o.Out == nil {
return ioutil.Discard
}

return o.Out
}
8 changes: 4 additions & 4 deletions pkg/oc/cli/secrets/known_secret_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package secrets
import (
"reflect"

coreapiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
kapi "k8s.io/kubernetes/pkg/apis/core"
)

type KnownSecretType struct {
Type kapi.SecretType
Type coreapiv1.SecretType
RequiredContents sets.String
}

Expand All @@ -22,7 +22,7 @@ func (ks KnownSecretType) Matches(secretContent map[string][]byte) bool {

var (
KnownSecretTypes = []KnownSecretType{
{kapi.SecretTypeDockercfg, sets.NewString(kapi.DockerConfigKey)},
{kapi.SecretTypeDockerConfigJson, sets.NewString(kapi.DockerConfigJsonKey)},
{coreapiv1.SecretTypeDockercfg, sets.NewString(coreapiv1.DockerConfigKey)},
{coreapiv1.SecretTypeDockerConfigJson, sets.NewString(coreapiv1.DockerConfigJsonKey)},
}
)
Loading

0 comments on commit b86760e

Please sign in to comment.