mirror of https://github.com/hashicorp/boundary
Add Sessions CLI command and add session cleanup to worker (#388)
parent
bb6ece69b6
commit
33b0021547
@ -0,0 +1,96 @@
|
||||
package sessions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/api/sessions"
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
)
|
||||
|
||||
func generateSessionTableOutput(in *sessions.Session) string {
|
||||
nonAttributeMap := map[string]interface{}{
|
||||
"ID": in.Id,
|
||||
"Target ID": in.TargetId,
|
||||
"Scope ID": in.Scope.Id,
|
||||
"Created Time": in.CreatedTime.Local().Format(time.RFC3339),
|
||||
"Updated Time": in.UpdatedTime.Local().Format(time.RFC3339),
|
||||
"Expiration Time": in.ExpirationTime.Local().Format(time.RFC3339),
|
||||
"Version": in.Version,
|
||||
"Type": in.Type,
|
||||
"Auth Token ID": in.AuthTokenId,
|
||||
"User ID": in.UserId,
|
||||
"Host Set ID": in.HostSetId,
|
||||
"Host ID": in.HostId,
|
||||
"Endpoint": in.Endpoint,
|
||||
"Status": in.Status,
|
||||
}
|
||||
|
||||
maxLength := 0
|
||||
for k := range nonAttributeMap {
|
||||
if len(k) > maxLength {
|
||||
maxLength = len(k)
|
||||
}
|
||||
}
|
||||
|
||||
var statesMaps []map[string]interface{}
|
||||
if len(in.States) > 0 {
|
||||
for _, state := range in.States {
|
||||
m := map[string]interface{}{
|
||||
"Status": state.Status,
|
||||
"Start Time": state.StartTime.Local().Format(time.RFC3339),
|
||||
"End Time": state.EndTime.Local().Format(time.RFC3339),
|
||||
}
|
||||
statesMaps = append(statesMaps, m)
|
||||
}
|
||||
if l := len("Start Time"); l > maxLength {
|
||||
maxLength = l
|
||||
}
|
||||
}
|
||||
|
||||
var workerInfoMaps []map[string]interface{}
|
||||
if len(in.WorkerInfo) > 0 {
|
||||
for _, wi := range in.WorkerInfo {
|
||||
m := map[string]interface{}{
|
||||
"Address": wi.Address,
|
||||
}
|
||||
workerInfoMaps = append(workerInfoMaps, m)
|
||||
}
|
||||
if l := len("Address"); l > maxLength {
|
||||
maxLength = l
|
||||
}
|
||||
}
|
||||
|
||||
ret := []string{"", "Session information:"}
|
||||
|
||||
ret = append(ret,
|
||||
// We do +2 because there is another +2 offset for host sets below
|
||||
base.WrapMap(2, maxLength+2, nonAttributeMap),
|
||||
)
|
||||
|
||||
if len(in.States) > 0 {
|
||||
ret = append(ret,
|
||||
fmt.Sprintf(" States: %s", ""),
|
||||
)
|
||||
for _, m := range statesMaps {
|
||||
ret = append(ret,
|
||||
base.WrapMap(4, maxLength, m),
|
||||
"",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if len(in.WorkerInfo) > 0 {
|
||||
ret = append(ret,
|
||||
fmt.Sprintf(" Worker Info: %s", ""),
|
||||
)
|
||||
for _, m := range workerInfoMaps {
|
||||
ret = append(ret,
|
||||
base.WrapMap(4, maxLength, m),
|
||||
"",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return base.WrapForHelpText(ret)
|
||||
}
|
||||
@ -0,0 +1,177 @@
|
||||
package sessions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/api/sessions"
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/hashicorp/boundary/internal/cmd/common"
|
||||
"github.com/hashicorp/boundary/internal/types/resource"
|
||||
"github.com/hashicorp/boundary/sdk/strutil"
|
||||
"github.com/kr/pretty"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/posener/complete"
|
||||
)
|
||||
|
||||
var _ cli.Command = (*Command)(nil)
|
||||
var _ cli.CommandAutocomplete = (*Command)(nil)
|
||||
|
||||
type Command struct {
|
||||
*base.Command
|
||||
|
||||
Func string
|
||||
}
|
||||
|
||||
func (c *Command) Synopsis() string {
|
||||
return common.SynopsisFunc(c.Func, "session")
|
||||
}
|
||||
|
||||
var flagsMap = map[string][]string{
|
||||
"read": {"id"},
|
||||
"cancel": {"id"},
|
||||
"list": {"scope-id"},
|
||||
}
|
||||
|
||||
func (c *Command) Help() string {
|
||||
helpMap := common.HelpMap("session")
|
||||
if c.Func == "" {
|
||||
return helpMap["base"]()
|
||||
}
|
||||
return helpMap[c.Func]() + c.Flags().Help()
|
||||
}
|
||||
|
||||
func (c *Command) Flags() *base.FlagSets {
|
||||
set := c.FlagSet(base.FlagSetHTTP | base.FlagSetClient | base.FlagSetOutputFormat)
|
||||
|
||||
if len(flagsMap[c.Func]) > 0 {
|
||||
f := set.NewFlagSet("Command Options")
|
||||
common.PopulateCommonFlags(c.Command, f, resource.Session.String(), flagsMap[c.Func])
|
||||
}
|
||||
|
||||
return set
|
||||
}
|
||||
|
||||
func (c *Command) AutocompleteArgs() complete.Predictor {
|
||||
return complete.PredictAnything
|
||||
}
|
||||
|
||||
func (c *Command) AutocompleteFlags() complete.Flags {
|
||||
return c.Flags().Completions()
|
||||
}
|
||||
|
||||
func (c *Command) Run(args []string) int {
|
||||
if c.Func == "" {
|
||||
return cli.RunResultHelp
|
||||
}
|
||||
|
||||
f := c.Flags()
|
||||
|
||||
if err := f.Parse(args); err != nil {
|
||||
c.UI.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
|
||||
if strutil.StrListContains(flagsMap[c.Func], "id") && c.FlagId == "" {
|
||||
c.UI.Error("ID is required but not passed in via -id")
|
||||
return 1
|
||||
}
|
||||
if strutil.StrListContains(flagsMap[c.Func], "scope-id") && c.FlagScopeId == "" {
|
||||
c.UI.Error("Scope ID must be passed in via -scope-id")
|
||||
return 1
|
||||
}
|
||||
|
||||
client, err := c.Client()
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Sprintf("Error creating API client: %s", err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
sessionClient := sessions.NewClient(client)
|
||||
|
||||
var result api.GenericResult
|
||||
var listResult api.GenericListResult
|
||||
var apiErr *api.Error
|
||||
|
||||
switch c.Func {
|
||||
case "read":
|
||||
result, apiErr, err = sessionClient.Read(c.Context, c.FlagId)
|
||||
case "cancel":
|
||||
result, apiErr, err = sessionClient.Cancel(c.Context, c.FlagId, 0, sessions.WithAutomaticVersioning(true))
|
||||
case "list":
|
||||
listResult, apiErr, err = sessionClient.List(c.Context, c.FlagScopeId)
|
||||
}
|
||||
|
||||
plural := "session"
|
||||
if c.Func == "list" {
|
||||
plural = "sessions"
|
||||
}
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
|
||||
return 2
|
||||
}
|
||||
if apiErr != nil {
|
||||
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
|
||||
return 1
|
||||
}
|
||||
|
||||
switch c.Func {
|
||||
case "list":
|
||||
listedSessions := listResult.GetItems().([]*sessions.Session)
|
||||
switch base.Format(c.UI) {
|
||||
case "json":
|
||||
if len(listedSessions) == 0 {
|
||||
c.UI.Output("null")
|
||||
return 0
|
||||
}
|
||||
b, err := base.JsonFormatter{}.Format(listedSessions)
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Errorf("Error formatting as JSON: %w", err).Error())
|
||||
return 1
|
||||
}
|
||||
c.UI.Output(string(b))
|
||||
|
||||
case "table":
|
||||
if len(listedSessions) == 0 {
|
||||
c.UI.Output("No auth tokens found")
|
||||
return 0
|
||||
}
|
||||
var output []string
|
||||
output = []string{
|
||||
"",
|
||||
"Session information:",
|
||||
}
|
||||
for i, t := range listedSessions {
|
||||
if i > 0 {
|
||||
output = append(output, "")
|
||||
}
|
||||
output = append(output,
|
||||
fmt.Sprintf(" ID: %s", t.Id),
|
||||
fmt.Sprintf(" Created Time: %s", t.CreatedTime.Local().Format(time.RFC3339)),
|
||||
fmt.Sprintf(" Expiration Time: %s", t.ExpirationTime.Local().Format(time.RFC3339)),
|
||||
fmt.Sprintf(" Updated Time: %s", t.UpdatedTime.Local().Format(time.RFC3339)),
|
||||
fmt.Sprintf(" User ID: %s", t.UserId),
|
||||
fmt.Sprintf(" Target ID: %s", t.UserId),
|
||||
)
|
||||
}
|
||||
c.UI.Output(base.WrapForHelpText(output))
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
sess := result.GetItem().(*sessions.Session)
|
||||
switch base.Format(c.UI) {
|
||||
case "table":
|
||||
c.UI.Output(generateSessionTableOutput(sess))
|
||||
case "json":
|
||||
b, err := base.JsonFormatter{}.Format(sess)
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Errorf("Error formatting as JSON: %w", err).Error())
|
||||
return 1
|
||||
}
|
||||
c.UI.Output(string(b))
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
Loading…
Reference in new issue