-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathecs_client_cmd.go
153 lines (124 loc) · 4.66 KB
/
ecs_client_cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
/*
* AWS SSO CLI
* Copyright (c) 2021-2023 Aaron Turner <synfinatic at gmail dot com>
*
* This program is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or with the authors permission any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import (
"fmt"
"sort"
"strings"
"github.com/davecgh/go-spew/spew"
"github.com/synfinatic/aws-sso-cli/internal/ecs"
"github.com/synfinatic/aws-sso-cli/internal/ecs/client"
"github.com/synfinatic/aws-sso-cli/internal/utils"
"github.com/synfinatic/aws-sso-cli/sso"
"github.com/synfinatic/gotable"
)
type EcsListCmd struct{}
type EcsLoadCmd struct {
// AWS Params
Arn string `kong:"short='a',help='ARN of role to assume',env='AWS_SSO_ROLE_ARN',predictor='arn'"`
AccountId int64 `kong:"name='account',short='A',help='AWS AccountID of role to assume',env='AWS_SSO_ACCOUNT_ID',predictor='accountId',xor='account'"`
Role string `kong:"short='R',help='Name of AWS Role to assume',env='AWS_SSO_ROLE_NAME',predictor='role',xor='role'"`
Profile string `kong:"short='p',help='Name of AWS Profile to assume',predictor='profile',xor='account,role'"`
// Other params
Port int `kong:"help='TCP port of aws-sso ECS Server',env='AWS_SSO_ECS_PORT',default=4144"` // SEE ECS_PORT in ecs_cmd.go
Slotted bool `kong:"short='s',help='Load credentials in a unique slot using the ProfileName as the key'"`
}
type EcsProfileCmd struct {
Port int `kong:"help='TCP port of aws-sso ECS Server',env='AWS_SSO_ECS_PORT',default=4144"`
}
type EcsUnloadCmd struct {
Port int `kong:"help='TCP port of aws-sso ECS Server',env='AWS_SSO_ECS_PORT',default=4144"`
Profile string `kong:"short='p',help='Name of AWS Profile to unload',predictor='profile'"`
}
func (cc *EcsLoadCmd) Run(ctx *RunContext) error {
sci := NewSelectCliArgs(ctx.Cli.Ecs.Load.Arn, ctx.Cli.Ecs.Load.AccountId, ctx.Cli.Ecs.Load.Role, ctx.Cli.Ecs.Load.Profile)
if awssso, err := sci.Update(ctx); err == nil {
// successful lookup?
return ecsLoadCmd(ctx, awssso, sci.AccountId, sci.RoleName)
}
return ctx.PromptExec(ecsLoadCmd)
}
func (cc *EcsProfileCmd) Run(ctx *RunContext) error {
c := client.NewECSClient(ctx.Cli.Ecs.Profile.Port)
profile, err := c.GetProfile()
if err != nil {
return err
}
if profile.ProfileName == "" {
return fmt.Errorf("No profile loaded in ECS Server.")
}
profiles := []ecs.ListProfilesResponse{
profile,
}
return listProfiles(profiles)
}
func (cc *EcsUnloadCmd) Run(ctx *RunContext) error {
c := client.NewECSClient(ctx.Cli.Ecs.Unload.Port)
return c.Delete(ctx.Cli.Ecs.Unload.Profile)
}
// Loads our AWS API creds into the ECS Server
func ecsLoadCmd(ctx *RunContext, awssso *sso.AWSSSO, accountId int64, role string) error {
creds := GetRoleCredentials(ctx, awssso, accountId, role)
cache := ctx.Settings.Cache.GetSSO() // ctx.Settings.Cache.Refresh(awssso, ssoConfig, ctx.Cli.SSO)
rFlat, err := cache.Roles.GetRole(accountId, role)
if err != nil {
return err
}
// generate our ProfileName if necessary
p, err := rFlat.ProfileName(ctx.Settings)
if err == nil {
rFlat.Profile = p
}
// save history
ctx.Settings.Cache.AddHistory(utils.MakeRoleARN(rFlat.AccountId, rFlat.RoleName))
if err := ctx.Settings.Cache.Save(false); err != nil {
log.WithError(err).Warnf("Unable to update cache")
}
// do something
c := client.NewECSClient(ctx.Cli.Ecs.Load.Port)
log.Debugf("%s", spew.Sdump(rFlat))
return c.SubmitCreds(creds, rFlat.Profile, ctx.Cli.Ecs.Load.Slotted)
}
func (cc *EcsListCmd) Run(ctx *RunContext) error {
c := client.NewECSClient(ctx.Cli.Ecs.Profile.Port)
profiles, err := c.ListProfiles()
if err != nil {
return err
}
if len(profiles) == 0 {
fmt.Printf("No profiles are stored in any named slots.\n")
return nil
}
return listProfiles(profiles)
}
func listProfiles(profiles []ecs.ListProfilesResponse) error {
// sort our results
sort.Slice(profiles, func(i, j int) bool {
return strings.Compare(profiles[i].ProfileName, profiles[j].ProfileName) < 0
})
tr := []gotable.TableStruct{}
for _, row := range profiles {
tr = append(tr, row)
}
fields := []string{"ProfileName", "AccountIdPad", "RoleName", "Expires"}
err := gotable.GenerateTable(tr, fields)
if err != nil {
fmt.Printf("\n")
}
return err
}