Skip to content

Commit 83ae462

Browse files
committed
feat: Narc kan bruke denne for validering og hente bruker
1 parent e2908f4 commit 83ae462

File tree

4 files changed

+16
-15
lines changed

4 files changed

+16
-15
lines changed

cmd/kubeconfigcmd/kubeconfig.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import (
55
"os"
66
"strings"
77

8-
"github.com/nais/cli/pkg/metrics"
9-
108
"github.com/nais/cli/pkg/gcp"
119
"github.com/nais/cli/pkg/kubeconfig"
10+
"github.com/nais/cli/pkg/metrics"
1211
"github.com/nais/cli/pkg/naisdevice"
1312
"github.com/urfave/cli/v2"
1413
)
@@ -68,7 +67,7 @@ gcloud auth login --update-adc`,
6867
},
6968
},
7069
Before: func(context *cli.Context) error {
71-
err := gcp.ValidateUserLogin(context.Context, false)
70+
_, err := gcp.ValidateAndGetUserLogin(context.Context, false)
7271
if err != nil {
7372
return err
7473
}
@@ -90,9 +89,9 @@ gcloud auth login --update-adc`,
9089
return nil
9190
},
9291
Action: func(context *cli.Context) error {
93-
overwrite := context.Bool("overwrite")
9492
clear := context.Bool("clear")
9593
exclude := context.StringSlice("exclude")
94+
overwrite := context.Bool("overwrite")
9695
verbose := context.Bool("verbose")
9796

9897
email, err := gcp.GetActiveUserEmail(context.Context)

cmd/postgrescmd/migratecmd/migrate.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ func Command() *cli.Command {
2121
Name: "migrate",
2222
Usage: "Command used for migrating to a new Postgres instance",
2323
Before: func(context *cli.Context) error {
24-
return gcp.ValidateUserLogin(context.Context, false)
24+
_, err := gcp.ValidateAndGetUserLogin(context.Context, false)
25+
return err
2526
},
2627
Subcommands: []*cli.Command{
2728
setupCommand(),

cmd/postgrescmd/postgres.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ func Command() *cli.Command {
2323
Name: "postgres",
2424
Usage: "Command used for connecting to Postgres",
2525
Before: func(context *cli.Context) error {
26-
return gcp.ValidateUserLogin(context.Context, false)
26+
_, err := gcp.ValidateAndGetUserLogin(context.Context, false)
27+
return err
2728
},
2829
Subcommands: commands,
2930
}

pkg/gcp/auth.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"strings"
1313
)
1414

15-
func ValidateUserLogin(ctx context.Context, enforceNais bool) error {
15+
func ValidateAndGetUserLogin(ctx context.Context, enforceNais bool) (string, error) {
1616
args := []string{
1717
"config",
1818
"list",
@@ -26,26 +26,26 @@ func ValidateUserLogin(ctx context.Context, enforceNais bool) error {
2626
cmd.Stderr = os.Stderr
2727
err := cmd.Run()
2828
if err != nil {
29-
return fmt.Errorf("%v\nerror running '%v' command: %w", buf.String(), cmd.String(), err)
29+
return "", fmt.Errorf("%v\nerror running '%v' command: %w", buf.String(), cmd.String(), err)
3030
}
3131

3232
user := strings.TrimSpace(buf.String())
3333
if user == "" {
34-
return fmt.Errorf("missing active user, have you logged in with 'gcloud auth login --update-adc'")
34+
return "", fmt.Errorf("missing active user, have you logged in with 'gcloud auth login --update-adc'")
3535
}
3636

3737
if enforceNais && !strings.HasSuffix(user, "@nais.io") {
38-
return fmt.Errorf("active gcloud-user is not a nais.io-user: %v", user)
38+
return "", fmt.Errorf("active gcloud-user is not a nais.io-user: %v", user)
3939
}
4040

4141
_, exists := os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS")
4242
if exists {
43-
return nil
43+
return user, nil
4444
}
4545

4646
homedir, err := os.UserHomeDir()
4747
if err != nil {
48-
return err
48+
return "", err
4949
}
5050
homedir += "/.config"
5151

@@ -56,12 +56,12 @@ func ValidateUserLogin(ctx context.Context, enforceNais bool) error {
5656
_, err = os.Stat(filepath.Clean(homedir + "/gcloud/application_default_credentials.json"))
5757
if err != nil {
5858
if errors.Is(err, os.ErrNotExist) {
59-
return fmt.Errorf("you are missing Application Default Credentials, run `gcloud auth login --update-adc` first")
59+
return "", fmt.Errorf("you are missing Application Default Credentials, run `gcloud auth login --update-adc` first")
6060
}
61-
return err
61+
return "", err
6262
}
6363

64-
return nil
64+
return user, nil
6565
}
6666

6767
func GetActiveUserEmail(ctx context.Context) (string, error) {

0 commit comments

Comments
 (0)