Skip to content

Commit

Permalink
support names in privileges and api keys
Browse files Browse the repository at this point in the history
  • Loading branch information
telyn committed Dec 18, 2018
1 parent d638757 commit 8da788b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
7 changes: 4 additions & 3 deletions lib/brain/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
// APIKey represents an api_key in the brain.
type APIKey struct {
// ID must not be set during creation.
ID int `json:"id,omitempty"`
UserID int `json:"user_id,omitempty"`
ID int `json:"id,omitempty"`
UserID int `json:"user_id,omitempty"`
Username string `json:"username,omitempty"`
// Label is a friendly display name for this API key
Label string `json:"label,omitempty"`
// API key is the actual key. To use it, it must be prepended with
Expand All @@ -35,7 +36,7 @@ func (key APIKey) DefaultFields(f output.Format) string {
case output.List:
return "ID, Label, Expired, ExpiresAt, Privileges"
}
return "ID, UserID, Label, Expired, ExpiresAt, Privileges"
return "ID, Username, Label, Expired, ExpiresAt, Privileges"
}

// Expired returns true if ExpiresAt is in the past.
Expand Down
18 changes: 12 additions & 6 deletions lib/brain/privilege.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,16 @@ type Privilege struct {
Username string `json:"username,omitempty"`
// VirtualMachineID is the ID of the virtual machine the privilege is granted on
VirtualMachineID int `json:"virtual_machine_id,omitempty"`
// VirtualMachineName is the name of the virtual machine the privilege is granted on
VirtualMachineName string `json:"virtual_machine_name,omitempty"`
// AccountID is the ID of the account the privilege is granted on
AccountID int `json:"account_id,omitempty"`
// AccountID is the name of the account the privilege is granted on
AccountName string `json:"account_name,omitempty"`
// GroupID is the ID of the group the privilege is granted on
GroupID int `json:"group_id,omitempty"`
// GroupID is the name of the group the privilege is granted on
GroupName string `json:"group_name,omitempty"`
// APIKeyID is the ID of the api key required in order to use this Privilege. If not set, privilege relates to normal logins using auth.
APIKeyID int `json:"api_key_id,omitempty"`
// Level is the PrivilegeLevel they have
Expand All @@ -72,11 +78,11 @@ type Privilege struct {
func (p Privilege) Target() string {
switch p.TargetType() {
case PrivilegeTargetTypeVM:
return fmt.Sprintf("server %d", p.VirtualMachineID)
return fmt.Sprintf("server %s", p.VirtualMachineName)
case PrivilegeTargetTypeGroup:
return fmt.Sprintf("group %d", p.GroupID)
return fmt.Sprintf("group %s", p.GroupName)
case PrivilegeTargetTypeAccount:
return fmt.Sprintf("account %d", p.AccountID)
return fmt.Sprintf("account %s", p.AccountName)
}
return ""

Expand All @@ -96,11 +102,11 @@ func (p Privilege) String() string {
}
switch p.TargetType() {
case PrivilegeTargetTypeVM:
return fmt.Sprintf("%s on VM #%d for %s%s", p.Level, p.VirtualMachineID, p.Username, requiresYubikey)
return fmt.Sprintf("%s on VM %s for %s%s", p.Level, p.VirtualMachineName, p.Username, requiresYubikey)
case PrivilegeTargetTypeGroup:
return fmt.Sprintf("%s on group #%d for %s%s", p.Level, p.GroupID, p.Username, requiresYubikey)
return fmt.Sprintf("%s on group %s for %s%s", p.Level, p.GroupName, p.Username, requiresYubikey)
case PrivilegeTargetTypeAccount:
return fmt.Sprintf("%s on account #%d for %s%s", p.Level, p.AccountID, p.Username, requiresYubikey)
return fmt.Sprintf("%s on account %s for %s%s", p.Level, p.AccountName, p.Username, requiresYubikey)
}
return fmt.Sprintf("%s for %s%s", p.Level, p.Username, requiresYubikey)
}
Expand Down
4 changes: 2 additions & 2 deletions lib/requests/brain/get_api_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
// GetAPIKeys gets all API keys that you can currently see.
// In general this means those for your user, but users with cluster_admin
// will be able to see all API keys on the cluster
func GetAPIKeys(client lib.Client) (apiKeys []brain.APIKey, err error) {
func GetAPIKeys(client lib.Client) (apiKeys brain.APIKeys, err error) {
r, err := client.BuildRequest("GET", lib.BrainEndpoint, "/api_keys?view=overview")
if err != nil {
return
}
_, _, err = r.MarshalAndRun(nil, &apiKeys)
_, _, err = r.Run(nil, &apiKeys)
return
}

0 comments on commit 8da788b

Please sign in to comment.