-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32834 from vespa-engine/arnej/add-auth-show
add "vespa auth show" command
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// 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 ( | ||
"bytes" | ||
"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 | ||
} | ||
var output bytes.Buffer | ||
fmt.Fprintf(&output, "Logged in as: %s", userResponse.User.Email) | ||
for tenant, data := range userResponse.Tenants { | ||
fmt.Fprintf(&output, "\nAvailable tenant: %s", tenant) | ||
for idx, role := range data.Roles { | ||
if idx == 0 { | ||
fmt.Fprintf(&output, "\n your roles:") | ||
} | ||
fmt.Fprintf(&output, " %s", role) | ||
} | ||
} | ||
cli.printSuccess(output.String()) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. | ||
// Author: mpolden | ||
|
||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/vespa-engine/vespa/client/go/internal/mock" | ||
) | ||
|
||
func TestAuthShow(t *testing.T) { | ||
t.Run("auth show", func(t *testing.T) { | ||
testAuthShow(t, []string{"auth", "show"}) | ||
}) | ||
} | ||
|
||
func testAuthShow(t *testing.T, subcommand []string) { | ||
cli, stdout, stderr := newTestCLI(t) | ||
|
||
err := cli.Run("config", "set", "target", "cloud") | ||
assert.Nil(t, err) | ||
err = cli.Run("config", "set", "application", "t1.a1") | ||
assert.Nil(t, err) | ||
err = cli.Run("auth", "api-key") | ||
assert.Nil(t, err) | ||
stdout.Reset() | ||
stderr.Reset() | ||
|
||
httpClient := &mock.HTTPClient{} | ||
httpClient.NextResponseString(200, `{"user":{"email":"foo@bar"}}`) | ||
cli.httpClient = httpClient | ||
|
||
err = cli.Run(subcommand...) | ||
assert.Nil(t, err) | ||
assert.Contains(t, stderr.String(), "Authenticating with API key") | ||
assert.Contains(t, stdout.String(), "Logged in as: foo@bar\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters