-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
106 lines (82 loc) · 2.47 KB
/
main.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
package main
import (
"context"
"os"
"sort"
"strings"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
log "github.com/sirupsen/logrus"
l "github.com/michalschott/aws-iam-access-key-disabler/pkg/lambda"
)
type Config struct {
now time.Time
lowThresholdDays int
highThresholdDays int
dryRun bool
}
const (
msgKeyDisabled = "key disabled"
msgKeyToBeDisabled = "days beyond high threshold, key to be disabled"
msgKeyToBeDisabledSoon = "days beyond low threshold, key will be disabled soon"
)
func HandleRequest(ctx context.Context) (int, error) {
var lowThresholdDays = kingpin.Flag("lowTresholdDays", "Show warning for keys older than X days").Default("90").Envar("LOWTHRESHOLDDAYS").Short('l').Int()
var highThresholdDays = kingpin.Flag("highTresholdDays", "Disable keys older than X days").Default("180").Envar("HIGHTHRESHOLDDAYS").Short('h').Int()
var dryRun = kingpin.Flag("dryrun", "").Default("true").Envar("DRYRUN").Bool()
var debug = kingpin.Flag("debug", "").Default("0").Envar("DEBUG").Int()
var whiteList = kingpin.Flag("whitelist", "Comma separated list of users to skip").Default("").Envar("WHITELIST").Short('w').String()
kingpin.Parse()
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
if *debug == 1 {
log.SetLevel(log.DebugLevel)
}
config := Config{time.Now().UTC(), *lowThresholdDays, *highThresholdDays, *dryRun}
whitelistedUsers := strings.Split(*whiteList, ",")
sort.Strings(whitelistedUsers)
log.Info("Ignored users: ", whitelistedUsers)
if *dryRun {
log.Info("Running in dry run mode.")
}
sess, err := session.NewSession()
if err != nil {
return 1, err
}
svc := iam.New(sess)
result, err := svc.ListUsers(&iam.ListUsersInput{})
if err != nil {
return 1, err
}
for _, user := range result.Users {
if user == nil {
continue
}
// skip whitelistedUsers
i := sort.Search(len(whitelistedUsers), func(i int) bool { return whitelistedUsers[i] >= *user.UserName })
if i < len(whitelistedUsers) && whitelistedUsers[i] == *user.UserName {
continue
}
err = parseUser(svc, *user.UserName, config)
if err != nil {
return 1, err
}
}
return 0, nil
}
func main() {
if l.IsLambda() {
lambda.Start(HandleRequest)
} else {
var ret int
var err error
ret, err = HandleRequest(context.Background())
if err != nil {
log.Error(err)
}
os.Exit(ret)
}
}