-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassume.go
75 lines (60 loc) · 1.85 KB
/
assume.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
package plugins
import (
"flag"
"github.com/RyanJarv/liquidswards/lib/types"
"github.com/RyanJarv/liquidswards/lib/utils"
)
import (
"fmt"
"github.com/RyanJarv/liquidswards/lib/creds"
"strings"
)
var noAssume = flag.Bool("no-assume", false, "do not attempt to assume discovered roles")
func NewAssume(ctx utils.Context, args types.GlobalPluginArgs) types.Plugin {
return &Assume{
GlobalPluginArgs: args,
}
}
type Assume struct {
types.GlobalPluginArgs
// For mocking assumeRole which gets set in Register
AssumeRole func(ctx utils.Context, cfg *creds.Config, role types.Role)
}
func (a *Assume) Name() string { return "Assume" }
func (a *Assume) Enabled() (bool, string) {
if *noAssume {
return false, "assuming roles is disabled because -no-assume was used"
} else {
return true, "assuming roles discovered by the scanner"
}
}
func (a *Assume) Run(ctx utils.Context) {
a.Access.Walk(func(cfg *creds.Config) {
ctx.Debug.Println("assume:", strings.Join(cfg.IdentityPath(), " -> "))
verifyScope(a.Scope, cfg.Arn())
if cfg.IsRecursive() {
ctx.Debug.Println("assume: skipping recursive chain:", cfg.Arn())
return
}
a.FoundRoles.Walk(func(role types.Role) {
ctx.Debug.Printf("assume: testing: %s -> %s", cfg.Id(), role.Id())
if ctx.IsDone("Finished assuming Items, exiting...") {
return
}
verifyScope(a.Scope, *role.Arn)
newCfg, err := cfg.Assume(ctx, *role.Arn)
if err != nil {
ctx.Debug.Println(err)
return
}
a.Access.Add(newCfg)
ctx.Info.Println(strings.Join(newCfg.IdentityPath(), utils.Arrow))
})
})
}
func verifyScope(scope []string, arn string) {
if scope != nil && !utils.ArnInScope(scope, arn) {
// Senders should check if the ARN is in scope, exit to avoid traversing into out of scope accounts.
panic(fmt.Errorf("assume roles: exiting due out of scope arn (this is a bug): %s\n", arn))
}
}