Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for service account token to read from file #242

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions cli/cmd/registry_add_gcr.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"encoding/json"
"io/ioutil"
"strings"

"github.com/pkg/errors"
Expand All @@ -20,8 +22,8 @@ func (r *runners) InitRegistryAddGCR(parent *cobra.Command) {
parent.AddCommand(cmd)

cmd.Flags().StringVar(&r.args.addRegistryEndpoint, "endpoint", "", "The GCR endpoint")
cmd.Flags().StringVar(&r.args.addRegistryServiceAccountKey, "serviceaccountkey", "", "The service account key to authenticate to the registry with")
cmd.Flags().BoolVar(&r.args.addRegistryServiceAccountKeyFromStdIn, "serviceaccountkey-stdin", false, "Take the service account key from stdin")
cmd.Flags().StringVar(&r.args.addRegistryServiceAccountKey, "serviceaccountkey", "", "The service account key to authenticate to the registry with. This is the path to the JSON file.")
cmd.Flags().BoolVar(&r.args.addRegistryServiceAccountKeyFromStdIn, "serviceaccountkey-stdin", false, "Take the service account key content from stdin")

cmd.RunE = r.registryAddGCR
}
Expand All @@ -33,7 +35,7 @@ func (r *runners) registryAddGCR(cmd *cobra.Command, args []string) error {
if err != nil {
return errors.Wrap(err, "read secret service account key from stdin")
}
r.args.addRegistryServiceAccountKey = serviceAccountKey
r.args.addRegistryPassword = serviceAccountKey
}

addRegistryRequest, errs := r.validateRegistryAddGCR()
Expand Down Expand Up @@ -77,10 +79,22 @@ func (r *runners) validateRegistryAddGCR() (kotsclient.AddKOTSRegistryRequest, [
req.Endpoint = r.args.addRegistryEndpoint
}

if r.args.addRegistryServiceAccountKey == "" {
if r.args.addRegistryServiceAccountKey == "" && r.args.addRegistryPassword == "" {
errs = append(errs, errors.New("serviceaccountkey or serviceaccountkey-stdin must be specified"))
} else {
req.Password = r.args.addRegistryServiceAccountKey
if r.args.addRegistryServiceAccountKey != "" {
bytes, err := ioutil.ReadFile(r.args.addRegistryServiceAccountKey)
if err != nil {
errs = append(errs, errors.Wrap(err, "read service account key"))
return req, errs
}
if !json.Valid(bytes) {
errs = append(errs, errors.New("Not valid json key file"))
return req, errs
}
r.args.addRegistryPassword = string(bytes)
}
req.Password = r.args.addRegistryPassword
}

return req, errs
Expand Down
73 changes: 73 additions & 0 deletions cli/cmd/registry_add_gcr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cmd

import (
"os"
"testing"
"text/tabwriter"
)

func TestRegistryAddGCR(t *testing.T) {
tests := []struct {
name string
args []string
wantErr bool
wantErrString string
}{
{
name: "endpoint required",
args: []string{"registry", "add", "gcr"},
wantErr: true,
wantErrString: "endpoint must be specified, serviceaccountkey or serviceaccountkey-stdin must be specified",
},
{
name: "service account key required",
args: []string{"registry", "add", "gcr", "--endpoint", "gcr.io"},
wantErr: true,
wantErrString: "serviceaccountkey or serviceaccountkey-stdin must be specified",
},
{
name: "invalid service account key",
args: []string{"registry", "add", "gcr", "--endpoint", "gcr.io", "--serviceaccountkey", "./testdata/invalid-gcr-service-account-key.json"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should not use relative file paths. We can useembed.FS from the https://pkg.go.dev/embed package

Code should be refactored to validate regardless of whether JSON is coming from a file or from STDIN.

wantErr: true,
wantErrString: "Not valid json key file",
},
{
name: "service account key file not found",
args: []string{"registry", "add", "gcr", "--endpoint", "gcr.io", "--serviceaccountkey", "./testdata/does-not-exist.json"},
wantErr: true,
wantErrString: "read service account key: open ./testdata/does-not-exist.json: no such file or directory",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cmd := GetRootCmd()
w := tabwriter.NewWriter(os.Stdout, minWidth, tabWidth, padding, padChar, tabwriter.TabIndent)
runCmds := &runners{
rootCmd: cmd,
stdin: os.Stdin,
w: w,
}

registryCmd := runCmds.InitRegistryCommand(runCmds.rootCmd)
runCmds.InitRegistryCommand(registryCmd)
registryAddCmd := runCmds.InitRegistryAdd(registryCmd)
runCmds.InitRegistryAddGCR(registryAddCmd)
runCmds.rootCmd.SetArgs(test.args)
err := runCmds.rootCmd.Execute()
if test.wantErr {
if err == nil {
t.Errorf("expected error, got nil")
}
if err.Error() != test.wantErrString {
t.Errorf("expected error string %q, got %q", test.wantErrString, err.Error())
}
} else {
if err != nil {
t.Errorf("expected no error, got %v", err)
}
}

})
}
}
1 change: 1 addition & 0 deletions cli/cmd/testdata/invalid-gcr-service-account-key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is not valid json