-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
135 lines (106 loc) · 3.31 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
package main
import (
"fmt"
"io/ioutil"
"log"
"os/exec"
"strings"
"github.com/go-ldap/ldap/v3"
"gopkg.in/yaml.v2"
)
type Organizations struct {
Organizations []Organization
}
type Organization struct {
Name string `yaml:"name"`
Spaces []Space `yaml:"spaces"`
}
type Space struct {
Space string `yaml:"space"`
Groups []string `yaml:"groups"`
Roles []string `yaml:"roles"`
ExcludeFromSpace []string `yam:"excludefromspace"`
}
func main() {
var err error
config := DefaultConfig()
organizations := Organizations{}
userData, err := ioutil.ReadFile(config.ConfigFile)
if err != nil {
log.Fatalf("Failed to load config file %v", err)
}
if err = yaml.Unmarshal(userData, &organizations); err != nil {
log.Fatalf("Failed to parse config file %v", err)
}
ldapConn, err := ldap.Dial("tcp", config.LdapHost)
if err != nil {
log.Fatalf("Failed to connect %v", err)
}
defer ldapConn.Close()
if err = ldapConn.Bind(config.LdapBindDN, config.LdapPassword); err != nil {
log.Fatalf("ldap: initial bind for user %q failed: %v", config.LdapBindDN, err)
}
loginCFApi(config)
log.Println("Login to PCF")
for _, org := range organizations.Organizations {
for _, space := range org.Spaces {
createPcfUserSetSpaceRole(org.Name, space, config, ldapConn)
if len(space.ExcludeFromSpace) > 0 {
unSetSpaceRole(org.Name, space, config, ldapConn)
}
}
}
}
func getUsersByGroupName(groups []string, config *Config, ldapConn *ldap.Conn) []string {
users := make([]string, 0)
for _, g := range groups {
filter := fmt.Sprintf("(&(objectCategory=user)(memberOf=cn=%s,OU=Groups,%s))", g, config.LdapBaseDN)
result, _ := ldapConn.Search(ldap.NewSearchRequest(
config.LdapBaseDN,
ldap.ScopeWholeSubtree,
ldap.NeverDerefAliases,
0,
0,
false,
filter,
[]string{"sAMAccountName"},
nil,
))
for i := range result.Entries {
users = append(users, result.Entries[i].Attributes[0].Values[0])
}
}
return users
}
func createPcfUserSetSpaceRole(organization string, space Space, config *Config, ldapConn *ldap.Conn) {
users := getUsersByGroupName(space.Groups, config, ldapConn)
for _, user := range users {
cmd := strings.Split(string(fmt.Sprintf("create-user %s --origin ldap", user)), " ")
runCFCli(cmd)
for _, role := range space.Roles {
cmd := strings.Split(string(fmt.Sprintf("set-space-role %s %s %s %s", user, organization, space.Space, role)), " ")
runCFCli(cmd)
}
}
}
func unSetSpaceRole(organization string, space Space, config *Config, ldapConn *ldap.Conn) {
notPermittedUsers := getUsersByGroupName(space.ExcludeFromSpace, config, ldapConn)
for _, user := range notPermittedUsers {
for _, role := range space.Roles {
cmd := strings.Split(string(fmt.Sprintf("unset-space-role %s %s %s %s", user, organization, space.Space, role)), " ")
runCFCli(cmd)
}
}
}
func loginCFApi(config *Config) {
runCFCli(strings.Split(string(fmt.Sprintf("login -a %s -u %s -p %s -o %s -s %s --skip-ssl-validation", config.CFApiURL, config.CFUsername, config.CFPassword, config.CFOrg, config.CFDefaultSpace)), " "))
}
func runCFCli(args []string) {
c := strings.Join(args, " ")
log.Println(fmt.Sprintf("Running Command: cf %s", c))
out, err := exec.Command("cf", args...).Output()
log.Println(string(out))
if err != nil {
log.Println(err)
}
}