-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.go
212 lines (183 loc) · 5.51 KB
/
authentication.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
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"strings"
"github.com/go-ldap/ldap/v3"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"net/http"
"os"
)
var ldapURL string
// TokenReview - token review request struct from k8s
type TokenReview struct {
Kind string `json:"kind"`
ApiVersion string `json:"apiVersion"`
Metadata map[string]interface{} `json:"metadata"`
Spec map[string]string `json:"spec"`
Status map[string]interface{} `json:"status"`
}
func main() {
app := &cli.App{
Name: "k8s-ldap",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "key",
Usage: "key.pem to use for ssl",
Required: true,
EnvVars: []string{"K8S_LDAP_KEY"},
},
&cli.StringFlag{
Name: "cert",
Usage: "cert.pem to use for ssl",
Required: true,
EnvVars: []string{"K8S_LDAP_CERT"},
},
&cli.StringFlag{
Name: "url",
Usage: "ldap server url",
Required: true,
EnvVars: []string{"K8S_LDAP_URL"},
},
&cli.StringFlag{
Name: "config",
Usage: "k8s-ldap config file",
Required: true,
EnvVars: []string{"K8S_LDAP_CONFIG"},
},
},
Action: func(c *cli.Context) error {
RunServer(c)
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal("app.Run failed: ", err)
}
}
func RunServer(c *cli.Context) {
loadConfig(c)
ldapURL = "ldaps://" + c.String("url")
http.HandleFunc("/authenticate", AuthenticationHandler)
http.HandleFunc("/authorize", AuthorizationHandler)
log.Info("Starting Server ...")
log.Info("Using ", c.String("key"), "", c.String("cert"))
log.Fatal(http.ListenAndServeTLS(":443", c.String("cert"), c.String("key"), nil))
}
// AuthenticationHandler - handles authentication request from k8s api.
func AuthenticationHandler(w http.ResponseWriter, r *http.Request) {
var req *TokenReview
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&req)
if err != nil {
log.Error("Error decoding auth request ", err)
http.Error(w, "Error decoding auth request", 400)
return
}
if !(strings.Contains(req.Spec["token"], ":")) {
http.Error(w, "Error: no username:password combination in request", 400)
return
}
up := strings.SplitN(req.Spec["token"], ":", 2)
username := up[0]
password := up[1]
log.WithFields(log.Fields{"User": username}).Info("Authenticating user with LDAP server.")
groups, err := getADGroups(username, password)
if err != nil {
// Auth failed.
req.Status = map[string]interface{}{"authenticated": false}
json.NewEncoder(w).Encode(req)
return
}
config := GetConfig()
// first look for user in userRoles as that takes precedence.
if _, ok := config.UserRoles[username]; ok {
// user exists with permission so auth ok
req.Status = map[string]interface{}{"authenticated": true}
req.Status["user"] = map[string]interface{}{"username": username, "uid": "314", "groups": groups}
json.NewEncoder(w).Encode(req)
return
}
// now just check if one of user's group is allowed
for _, group := range groups {
if _, ok := config.GroupRoles[group]; ok {
// user exists with permission so auth ok
req.Status["authenticated"] = true
req.Status["user"] = map[string]interface{}{"username": username, "uid": "314", "groups": groups}
json.NewEncoder(w).Encode(req)
return
}
}
log.WithFields(log.Fields{"User": username}).Info("No matching group or user attribute. Authentication rejected.")
req.Status["authenticated"] = false
json.NewEncoder(w).Encode(req)
return
}
// getADGroups - bind the user to ldap, and return its groups
func getADGroups(username, password string) ([]string, error) {
// groups holds all the groups the user belongs to
groups := []string{}
// TODO: make this config var
config := &tls.Config{InsecureSkipVerify: true}
ldapConn, err := ldap.DialURL(ldapURL, ldap.DialWithTLSConfig(config))
if err != nil {
log.Error("Error getting ldap connection : ", err)
return groups, err
}
defer ldapConn.Close()
userConfig := GetConfig()
// for specific cases where AD server binds with @domain.com
// if such domain is defined in the config, we append it to the
// username and try to bind with that.
binduser := username
if userConfig.BindDomain != "" {
binduser = username + "@" + userConfig.BindDomain
}
log.WithFields(log.Fields{"User": binduser}).Info("Attempting to bind user.")
err = ldapConn.Bind(binduser, password)
if err != nil {
log.Error("Error binding user to ldap server : ", err)
return groups, err
}
log.WithFields(log.Fields{"User": username}).Info("Serching user membership.")
searchString := fmt.Sprintf("(&(objectCategory=person)(objectClass=user)(samAccountName=%s))", username)
if userConfig.Filter != "" {
searchString = fmt.Sprintf(userConfig.Filter, username)
}
memberSearchAttribute := "memberOf"
if userConfig.MemberSearchAttribute != "" {
memberSearchAttribute = userConfig.MemberSearchAttribute
}
searchRequest := ldap.NewSearchRequest(
userConfig.BaseDN,
ldap.ScopeWholeSubtree,
ldap.NeverDerefAliases,
0,
0,
false,
searchString,
[]string{memberSearchAttribute},
nil,
)
sr, err := ldapConn.Search(searchRequest)
if err != nil {
log.Error("Error searching user properties : ", err)
return groups, err
}
entry := sr.Entries[0]
for _, i := range entry.Attributes {
for _, attr := range i.Values {
groupList := strings.Split(attr, ",")
for _, g := range groupList {
if strings.HasPrefix(g, "CN=") {
group := strings.Split(g, "=")
groups = append(groups, group[1])
}
}
}
}
return groups, nil
}