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

add "vespa auth show" command #32834

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
84 changes: 84 additions & 0 deletions client/go/internal/cli/cmd/auth_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// vespa auth show command
// Author: arnej
package cmd

import (
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/spf13/cobra"
)

func newAuthShowCmd(cli *CLI) *cobra.Command {
cmd := &cobra.Command{
Use: "show",
Short: "Show authenticated user",
Long: `Show which user (if any) is authenticated with "auth login"
`,
Example: "$ vespa auth show",
DisableAutoGenTag: true,
SilenceUsage: true,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if _, err := cli.config.application(); err != nil {
cmd.Flag("application").Value.Set("none.none")
cmd.Flag("application").Changed = true
}
return doAuthShow(cli, args)
},
}
return cmd
}

type userV1 struct {
IsPublic bool `json:"isPublic"`
IsCd bool `json:"isCd"`
User struct {
Email string `json:"email"`
} `json:"user"`
Tenants map[string]struct {
Supported bool `json:"supported"`
Roles []string `json:"roles"`
} `json:"tenants"`
}

func doAuthShow(cli *CLI, args []string) error {
target, err := cli.target(targetOptions{supportedType: cloudTargetOnly})
if err != nil {
return err
}
service, err := target.DeployService()
if err != nil {
return err
}
url := service.BaseURL + "/user/v1/user"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
response, err := service.Do(req, time.Second*3)
if err != nil {
return err
}
defer response.Body.Close()
dec := json.NewDecoder(response.Body)
var userResponse userV1
if err = dec.Decode(&userResponse); err != nil {
return err
}
fmt.Printf("Logged in as: %s\n", userResponse.User.Email)
for tenant, data := range userResponse.Tenants {
fmt.Printf("Available tenant: %s\n", tenant)
for idx, role := range data.Roles {
if idx == 0 {
fmt.Printf(" your roles:")
}
fmt.Printf(" %s", role)
}
fmt.Printf("\n")
}
return nil
}
1 change: 1 addition & 0 deletions client/go/internal/cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ func (c *CLI) configureCommands() {
authCmd.AddCommand(certCmd) // auth cert
authCmd.AddCommand(newAPIKeyCmd(c)) // auth api-key
authCmd.AddCommand(newLoginCmd(c)) // auth login
authCmd.AddCommand(newAuthShowCmd(c)) // auth show
authCmd.AddCommand(newLogoutCmd(c)) // auth logout
rootCmd.AddCommand(authCmd) // auth
rootCmd.AddCommand(newCloneCmd(c)) // clone
Expand Down
Loading