/
main.go
179 lines (159 loc) · 4.77 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
179
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/UKHomeOffice/aws_usersync/pkg/log"
iam "github.com/UKHomeOffice/aws_usersync/pkg/sync_iam"
"github.com/UKHomeOffice/aws_usersync/pkg/sync_users"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
)
var (
// version for the build
version = "0.1.2"
// GitSHA for the build
GitSHA = "unknown"
)
// 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")
group = flag.String("g", "", "The group in AWS that contains the users")
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
)
func init() {
// set region
cfg := &aws.Config{Region: aws.String(*region)}
sess := session.Must(session.NewSession())
// set Iam service
iam.NewIAM(sess, cfg)
}
// 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() error {
for user, umap := range u {
keys, err := iam.IAMsvc.GetKeys(user)
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 {
umap.keys = keys
}
}
return nil
}
//Compare and delete users that may nolonger be there
// Set the IAM users
func (u userMap) setIamUsers(g string) error {
r := iam.IAMsvc.FetchGroup(g)
for _, user := range iam.IAMsvc.GetIamUsers(r) {
u[user] = &userData{group: g}
}
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 {
// reset userMap map so it's empty
u = make(map[string]*userData)
// Fetch all iam users from group and assign to userMap type
if err := u.setIamUsers(grp); err != nil {
return err
}
// Set all the keys for users
if err := u.setKey(); 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()
if *onetime {
if err := umap.userSync(*group); 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(*group, 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)
}
}
}