-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
38 lines (34 loc) · 973 Bytes
/
user.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
package ssos
import (
"context"
"fmt"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ssm"
)
var createUserTmpl = `sudo useradd %s || exit 1
sudo usermod -aG adm,wheel %s
echo "%s ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/%s
`
func CreateUser(ssmCli *ssm.Client, instanceIds []string, user string) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
req := ssmCli.SendCommandRequest(&ssm.SendCommandInput{
InstanceIds: instanceIds,
DocumentName: aws.String("AWS-RunShellScript"),
Comment: aws.String("create user"),
Parameters: map[string][]string{
"commands": {fmt.Sprintf(createUserTmpl, user, user, user, user)},
},
})
res, err := req.Send(ctx)
if err != nil {
return err
}
commandID := res.Command.CommandId
err = WaitCommandInvocations(ctx, ssmCli, commandID)
if err != nil {
return err
}
return PrintCommandOutput(ctx, ssmCli, commandID)
}