-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.go
223 lines (195 loc) · 5.66 KB
/
mapper.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"bufio"
"context"
"fmt"
"os"
"strings"
"github.com/magiconair/properties"
"github.com/zemirco/keycloak"
"golang.org/x/oauth2"
)
type KeycloakSpec struct {
server string
user string
password string
realm string
}
var dryRunOnly = false
var keycloakSpec KeycloakSpec
var ctx context.Context
var k *keycloak.Keycloak
var missingRoles = []string{}
var groupsWithMissingRole = map[string]string{}
func main() {
initProps()
connectToKeycloak()
validateRealm()
prepareMapper()
printMapper()
if !dryRunOnly {
createRolesAndMappings()
} else {
fmt.Printf("\nNote: Disable or remove the %v option in %v to create the missing roles and mappings", PROPS_DRYRUN, PROPS_FILE_NAME)
}
}
const PROPS_FILE_NAME = "mapper.properties"
const PROPS_DRYRUN = "dry.run.only"
const PROPS_URL = "keycloak.url"
const PROPS_USER = "keycloak.user"
const PROPS_PASSWORD = "keycloak.password"
const PROPS_REALM = "keycloak.realm"
func templateProps() {
template := map[string]string{
PROPS_DRYRUN: "false",
PROPS_URL: "http://localhost:8080",
PROPS_USER: "admin",
PROPS_PASSWORD: "password",
PROPS_REALM: "realm",
}
p := properties.LoadMap(template)
f, _ := os.Create(PROPS_FILE_NAME)
w := bufio.NewWriter(f)
p.Write(w, properties.UTF8)
w.Flush()
}
func initProps() {
p, err := properties.LoadFile(PROPS_FILE_NAME, properties.UTF8)
if err != nil {
fmt.Printf("Missing properties file %s. Creating a default template for you\n", PROPS_FILE_NAME)
templateProps()
panic(err)
}
dryRunOnly = p.GetBool(PROPS_DRYRUN, false)
keycloakSpec = KeycloakSpec{}
keycloakSpec.server = p.MustGetString(PROPS_URL)
keycloakSpec.user = p.MustGetString(PROPS_USER)
keycloakSpec.password = p.MustGetString(PROPS_PASSWORD)
keycloakSpec.realm = p.MustGetString(PROPS_REALM)
fmt.Println("*** Running with ***")
fmt.Printf("Dry run only: %v\n", dryRunOnly)
fmt.Printf("Keycloak specs: %v\n", keycloakSpec)
}
func connectToKeycloak() {
config := oauth2.Config{
ClientID: "admin-cli",
Endpoint: oauth2.Endpoint{
TokenURL: keycloakSpec.server + "/auth/realms/master/protocol/openid-connect/token",
},
}
ctx = context.Background()
token, err := config.PasswordCredentialsToken(ctx, keycloakSpec.user, keycloakSpec.password)
if err != nil {
panic(err)
}
client := config.Client(ctx, token)
k, err = keycloak.NewKeycloak(client, keycloakSpec.server+"/auth/")
if err != nil {
panic(err)
}
fmt.Printf("Logged in to %v\n ", keycloakSpec.server)
}
func validateRealm() {
realm, _, err := k.Realms.Get(ctx, keycloakSpec.realm)
if err != nil {
panic(err)
}
if realm.ID == nil {
panic(fmt.Sprintf("Provided realm '%s' is not configured", keycloakSpec.realm))
}
fmt.Printf("Found realm: %v\n", *realm.Realm)
}
func prepareMapper() {
groups, _, err := k.Groups.List(ctx, keycloakSpec.realm)
if err != nil {
panic(err)
}
for _, g := range groups {
prepareMapperForGroup(g)
}
}
func prepareMapperForGroup(group *keycloak.Group) {
fmt.Printf("Preparing mapper for group: %v/%v\n", *group.Name, *group.ID)
g, _, err := k.Groups.Get(ctx, keycloakSpec.realm, *group.ID)
if err != nil {
panic(err)
}
groupMapped := false
for _, r := range g.RealmRoles {
if r == *g.Name {
fmt.Printf("\tRole %v is already mapped\n", *g.Name)
groupMapped = true
break
}
}
if !groupMapped {
fmt.Printf("\tRole mapping is missing for: %v\n", *g.Name)
mappedRole := getRoleGyName(*g.Name)
if mappedRole.ID == nil {
missingRoles = append(missingRoles, *g.Name)
} else {
fmt.Printf("\tMapping role already exists: %v/%v\n", *mappedRole.ID, *mappedRole.Name)
}
groupsWithMissingRole[*g.ID] = *g.Name
}
for _, subGroup := range group.SubGroups {
fmt.Printf("\tIterate on sub-group: %v\n", *subGroup.Name)
prepareMapperForGroup(subGroup)
}
}
func printMapper() {
if anyConfigurationNeeded() {
fmt.Println("*** The following missing roles will be created ***")
for _, roleName := range missingRoles {
fmt.Printf("Role %v\n", roleName)
}
fmt.Println("*** The following mappings will be created ***")
for _, groupName := range groupsWithMissingRole {
fmt.Printf("Group %v to Role %v\n", groupName, groupName)
}
} else {
fmt.Println("*** All roles and mappings are already set, no changes needed ***")
}
}
func anyConfigurationNeeded() bool {
return len(missingRoles) > 0 || len(groupsWithMissingRole) > 0
}
func createRolesAndMappings() {
if anyConfigurationNeeded() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Do you really want to continue? (Y/N): ")
answer, _ := reader.ReadString('\n')
if strings.HasPrefix(strings.ToUpper(strings.TrimSpace(answer)), "Y") {
fmt.Println("*** Creating missing roles ***")
for _, roleName := range missingRoles {
createRoleByName(roleName)
}
fmt.Println("*** Creating missing mappings ***")
for groupID, groupName := range groupsWithMissingRole {
addRoleToGroup(groupID, getRoleGyName(groupName))
}
}
}
}
func createRoleByName(name string) {
role := &keycloak.Role{Name: &name}
fmt.Printf("Creating missing role %v\n", *role.Name)
_, err := k.RealmRoles.Create(ctx, keycloakSpec.realm, role)
if err != nil {
panic(err)
}
}
func getRoleGyName(name string) *keycloak.Role {
role, _, err := k.RealmRoles.GetByName(ctx, keycloakSpec.realm, name)
if err != nil {
panic(err)
}
return role
}
func addRoleToGroup(groupID string, role *keycloak.Role) {
groupName := groupsWithMissingRole[groupID]
mappedRole := getRoleGyName(groupName)
fmt.Printf("Creating mapping between group %v and role %v/%v\n", groupName, *mappedRole.Name, *mappedRole.ID)
var mappedRoles = []*keycloak.Role{mappedRole}
k.Groups.AddRealmRoles(ctx, keycloakSpec.realm, groupID, mappedRoles)
}