This repository has been archived by the owner on Feb 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
list.go
148 lines (130 loc) · 3.82 KB
/
list.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
package main
/*
* OneLogin AWS Role
* Copyright (c) 2020-2021 Aaron Turner <aturner at synfin dot net>
*
* 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"
"os"
"reflect"
log "github.com/sirupsen/logrus"
"github.com/synfinatic/onelogin-aws-role/aws"
"github.com/synfinatic/onelogin-aws-role/utils"
)
/*
* This command just does a pretty print of our YAML config file basically
*/
type ListCmd struct {
Fields []string `kong:"optional,arg,enum='AccountId,AccountName,AppId,AppName,AppAlias,Arn,Expires,Profile,Region',help='Fields to display (default: AppAlias AccountName RoleAlias Arn Expires)'"`
ListFields bool `kong:"optional,short='f',help='List available fields'"`
}
// Fields match those in FlatConfig. Used when user doesn't have the `fields` in
// their YAML config file or provided list on the CLI
var defaultFields = []string{
"AppAlias",
"AccountName",
"Profile",
"Arn",
"Expires",
}
func (cc *ListCmd) Run(ctx *RunContext) error {
cli := *ctx.Cli
// If `-f` then print our fields and exit
fcList := ctx.Config.GetFlatConfig()
if cli.List.ListFields {
listFlatConfigFields(fcList[0])
os.Exit(1)
}
// List our AWS account aliases by abusing the FlatConfig struct
accounts := []FlatConfig{}
if ctx.Config.Accounts != nil {
for k, v := range *ctx.Config.Accounts {
accounts = append(accounts, FlatConfig{
AccountId: k,
AccountName: v,
})
}
accountList := []string{
"AccountId",
"AccountName",
}
// manually convert our []FlatConfig into []TableStruct because Go is lame
ts := []utils.TableStruct{}
for _, x := range accounts {
ts = append(ts, x)
}
utils.GenerateTable(ts, accountList)
fmt.Printf("\n\n")
} else {
// Skip AccountName if we don't know it
defaultFields = []string{
"AppAlias",
"Profile",
"Arn",
"Expires",
}
}
kr, err := OpenKeyring(nil)
if err != nil {
log.WithError(err).Warn("Unable to retrieve STS Sessions from Keychain")
kr = nil
}
// manually convert our []FlatConfig into []TableStruct because Go is lame
ts := []utils.TableStruct{}
for _, fc := range fcList {
if kr != nil {
session := aws.STSSession{}
err := kr.GetSTSSession(fc.Profile, &session)
if err == nil {
fc.Expires = session.GetExpireTimeString()
}
}
if fc.Expires == "" {
fc.Expires = "Expired"
}
ts = append(ts, fc)
}
// List our configured Roles
if len(cli.List.Fields) > 0 {
utils.GenerateTable(ts, cli.List.Fields)
} else if ctx.Config.Fields != nil && len(*ctx.Config.Fields) > 0 {
utils.GenerateTable(ts, *ctx.Config.Fields)
} else {
utils.GenerateTable(ts, defaultFields)
}
return nil
}
type ConfigFieldNames struct {
Field string `header:"Field"`
Description string `header:"Description"`
}
func (cfn ConfigFieldNames) GetHeader(fieldName string) (string, error) {
v := reflect.ValueOf(cfn)
return utils.GetHeaderTag(v, fieldName)
}
func listFlatConfigFields(fc FlatConfig) {
t := reflect.TypeOf(fc)
ts := []utils.TableStruct{}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
ts = append(ts, ConfigFieldNames{
Field: field.Name,
Description: field.Tag.Get(utils.TABLE_HEADER_TAG),
})
}
fields := []string{"Field", "Description"}
utils.GenerateTable(ts, fields)
}