Skip to content

Commit

Permalink
fix(cmd): use single field to cache client identity
Browse files Browse the repository at this point in the history
client identity is duplicated between config.ID and
config.PolymorphicID. since config.PolymorphicID supersedes ID, it is
sufficient to only cache the PolymorphicID value.
  • Loading branch information
mxyng committed Mar 16, 2022
1 parent becacb0 commit 567e664
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 28 deletions.
27 changes: 22 additions & 5 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,18 +478,28 @@ func newInfoCmd() *cobra.Command {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight)
defer w.Flush()

if config.PolymorphicID.IsUser() {
id := config.PolymorphicID
if id == "" {
return fmt.Errorf("no active identity")
}

if id.IsUser() {
userID, err := id.ID()
if err != nil {
return err
}

provider, err := client.GetProvider(config.ProviderID)
if err != nil {
return err
}

user, err := client.GetUser(config.ID)
user, err := client.GetUser(userID)
if err != nil {
return err
}

groups, err := client.ListUserGroups(config.ID)
groups, err := client.ListUserGroups(userID)
if err != nil {
return err
}
Expand All @@ -508,8 +518,13 @@ func newInfoCmd() *cobra.Command {
fmt.Fprintf(w, "Identity Provider:\t %s (%s)\n", provider.Name, provider.URL)
fmt.Fprintln(w, "User:\t", user.Email)
fmt.Fprintln(w)
} else if config.PolymorphicID.IsMachine() {
machine, err := client.GetMachine(config.ID)
} else if id.IsMachine() {
machineID, err := id.ID()
if err != nil {
return err
}

machine, err := client.GetMachine(machineID)
if err != nil {
fmt.Fprintln(os.Stderr, "6.3")
return err
Expand All @@ -519,6 +534,8 @@ func newInfoCmd() *cobra.Command {
fmt.Fprintln(w, "Server:\t", config.Host)
fmt.Fprintln(w, "Machine User:\t", machine.Name)
fmt.Fprintln(w)
} else {
return fmt.Errorf("unsupported identity for operation: %s", id)
}

return nil
Expand Down
1 change: 0 additions & 1 deletion internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type ClientConfig struct {

// current: v0.3
type ClientHostConfig struct {
ID uid.ID `json:"id"`
PolymorphicID uid.PolymorphicID `json:"polymorphic-id"`
Name string `json:"name"`
Host string `json:"host"`
Expand Down
34 changes: 23 additions & 11 deletions internal/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ func list() error {
return err
}

if config.ID == 0 {
id := config.PolymorphicID
if id == "" {
return fmt.Errorf("no active identity")
}

destinations, err := client.ListDestinations(api.ListDestinationsRequest{})
if err != nil {
return err
}

var grants []api.Grant
if config.ProviderID != 0 {
if id.IsUser() {
userID, err := id.ID()
if err != nil {
return err
}

grants, err = client.ListUserGrants(config.ID)
grants, err = client.ListUserGrants(userID)
if err != nil {
return err
}

groups, err := client.ListUserGroups(config.ID)
groups, err := client.ListUserGroups(userID)
if err != nil {
return err
}
Expand All @@ -49,11 +49,18 @@ func list() error {

grants = append(grants, groupGrants...)
}
} else {
grants, err = client.ListMachineGrants(config.ID)
} else if id.IsMachine() {
machineID, err := id.ID()
if err != nil {
return err
}

grants, err = client.ListMachineGrants(machineID)
if err != nil {
return err
}
} else {
return fmt.Errorf("unsupported identity for operation: %s", id)
}

gs := make(map[string]mapset.Set)
Expand All @@ -66,6 +73,11 @@ func list() error {
gs[g.Resource].Add(g.Privilege)
}

destinations, err := client.ListDestinations(api.ListDestinationsRequest{})
if err != nil {
return err
}

type row struct {
Name string `header:"RESOURCE"`
Access string `header:"ACCESS"`
Expand Down
6 changes: 0 additions & 6 deletions internal/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,6 @@ func finishLogin(host string, polymorphicID uid.PolymorphicID, name string, acce

var hostConfig ClientHostConfig

id, err := polymorphicID.ID()
if err != nil {
return err
}

hostConfig.ID = id
hostConfig.PolymorphicID = polymorphicID
hostConfig.Current = true
hostConfig.Host = host
Expand Down
18 changes: 13 additions & 5 deletions internal/cmd/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,21 @@ func tokensCreate() error {
return err
}

if config.ID == 0 {
return fmt.Errorf("no active user")
id := config.PolymorphicID
if id == "" {
return fmt.Errorf("no active identity")
}

token, err := client.CreateToken(&api.CreateTokenRequest{
UserID: config.ID,
})
if !id.IsUser() && !id.IsMachine() {
return fmt.Errorf("unsupported identity for operation: %s", id)
}

userID, err := id.ID()
if err != nil {
return err
}

token, err := client.CreateToken(&api.CreateTokenRequest{UserID: userID})
if err != nil {
if errors.Is(err, api.ErrForbidden) {
fmt.Fprintln(os.Stderr, "Session has expired.")
Expand Down

0 comments on commit 567e664

Please sign in to comment.