This repository has been archived by the owner on Sep 14, 2022. It is now read-only.
forked from UKHomeOffice/aws_usersync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
178 lines (158 loc) · 4.8 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/uswitch/aws_usersync/log"
"github.com/uswitch/aws_usersync/sync_iam"
"github.com/uswitch/aws_usersync/sync_users"
)
const (
version = "0.0.1"
)
// custom type for maps to userData
type userMap map[string]*userData
// structure to hold the user data for users
type userData struct {
group string
keys []string
}
// Define variables flag and standard
var (
keyEncoding = flag.String("e", "SSH", "SSH Key encoding type ssh-rsa or pem, defaults to SSH")
sudoGroup = flag.String("S", "sudo", "Group for the users to be part of for sudo, defaults to sudo group")
groups = flag.String("g", "", "Comma separated list of Group names in AWS")
versionShow = flag.Bool("v", false, "Display the version")
interval = flag.Int("i", 30, "The frequency to poll in Minutes, for updates from the cloud provider")
ignoreusers = flag.String("I", "root,core", "Specify comma separated list of users to ignore on the system so they wont be attempted to be removed")
onetime = flag.Bool("o", true, "One time run as oppose polling and daemonizing")
logLevel = flag.String("L", "", "Set the log level: Error, Info, Debug")
region = flag.String("r", "eu-west-1", "AWS Region, defaults to eu-west-1")
binName = "coreos_awsusermgt"
grpList []string
)
// Split the group list into an array
func splitString(g string) []string {
glist := strings.Split(strings.Replace(g, " ", "", -1), ",")
return glist
}
// Set the key in the structure for the user fetched from iam or delete user from
// structure if the user hasn't set a key
func (u userMap) setKey(svc *iam.IAM) error {
for user, struc := range u {
keys, err := sync_iam.GetKeys(user, svc)
if err != nil {
log.Error(fmt.Sprintf("Error occurred getting keys: %v", err))
return err
}
if len(keys) == 0 {
log.Debug(fmt.Sprintf("No active keys for %v. Not adding user [get them to add their key]", user))
delete(u, user)
} else {
struc.keys = keys
}
}
return nil
}
// Set the IAM users
func (u userMap) setIamUsers(svc *iam.IAM, g []string) error {
for _, grp := range g {
resp, err := svc.GetGroup(&iam.GetGroupInput{GroupName: aws.String(grp)})
if err != nil {
log.Error(fmt.Sprintf("Error getting Group: %v, %v", grp, err))
return err
}
for _, user := range sync_iam.GetIamUsers(resp) {
u[user] = &userData{group: grp}
}
}
return nil
}
// Take in the channels and loop continuously through until we need to break
// syncing uses with whatever the time interval supplied is
func (u userMap) process(grp []string, doneChan chan bool, stopChan chan bool, interval int) {
defer close(doneChan)
for {
u.userSync(grp)
select {
case <-stopChan:
break
case <-time.After(time.Duration(interval) * time.Minute):
continue
}
}
}
// Loop through all the users and add user locally to main which will call sync
// we need to call dokeys on if the user exists or not so need to check the error message
// for whether the user exists or not and run it anyway unless some other error
func (u userMap) userSync(grp []string) error {
// set region
cfg := &aws.Config{Region: aws.String(*region)}
// set Iam service
iamsvc := sync_iam.GetIamClient(cfg)
// Fetch all iam users from group and assign to userMap type
if err := u.setIamUsers(iamsvc, grp); err != nil {
return err
}
// Set all the keys for users
if err := u.setKey(iamsvc); err != nil {
return err
}
var IamUsers []string
for userStr, data := range u {
IamUsers = append(IamUsers, userStr)
luser := sync_users.New(userStr, data.group, *sudoGroup, data.keys)
if err := luser.Sync(); err != nil {
log.Error(fmt.Sprintf("Error syncing users: %v", err))
return err
}
}
ignored := splitString(*ignoreusers)
userCmp, err := sync_users.CmpNew(IamUsers, ignored)
if err != nil {
return err
}
if err := userCmp.Cleanup(); err != nil {
return err
}
return nil
}
// function main call out into validate code
func main() {
// Make and initaize the map for structure
umap := make(userMap)
// Check the flag options
flagOptions()
// Get a list of the groups
grpList = splitString(*groups)
if *onetime {
if err := umap.userSync(grpList); err != nil {
os.Exit(1)
}
os.Exit(0)
}
// Set the channels
stopChan := make(chan bool)
doneChan := make(chan bool)
errChan := make(chan error, 10)
go umap.process(grpList, doneChan, stopChan, *interval)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case err := <-errChan:
log.Error(fmt.Sprintf("Error captured: %v", err.Error()))
case s := <-signalChan:
log.Info(fmt.Sprintf("Captured %v. Exiting...", s))
close(doneChan)
case <-doneChan:
os.Exit(0)
}
}
}