forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ldap_provider.go
213 lines (178 loc) · 6.15 KB
/
ldap_provider.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
package ldap
import (
"context"
"crypto/x509"
"fmt"
"strings"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/auth/providers/common"
"github.com/rancher/rancher/pkg/auth/tokens"
"github.com/rancher/types/apis/management.cattle.io/v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/rancher/types/apis/management.cattle.io/v3public"
"github.com/rancher/types/config"
"github.com/rancher/types/user"
"k8s.io/apimachinery/pkg/runtime"
)
const (
OpenLdapName = "openldap"
FreeIpaName = "freeipa"
OpenLdapUserScope = OpenLdapName + "_user"
OpenLdapGroupScope = OpenLdapName + "_group"
FreeIpaUserScope = FreeIpaName + "_user"
FreeIpaGroupScope = FreeIpaName + "_group"
ObjectClass = "objectClass"
)
var openLdapScopes = []string{OpenLdapUserScope, OpenLdapGroupScope}
var freeIpaScopes = []string{FreeIpaUserScope, FreeIpaGroupScope}
type ldapProvider struct {
ctx context.Context
authConfigs v3.AuthConfigInterface
userMGR user.Manager
tokenMGR *tokens.Manager
certs string
caPool *x509.CertPool
providerName string
testAndApplyInputType string
userScope string
groupScope string
}
func Configure(ctx context.Context, mgmtCtx *config.ScaledContext, userMGR user.Manager, tokenMGR *tokens.Manager, providerName string, testAndApplyInputType string, userScope string, groupScope string) common.AuthProvider {
return &ldapProvider{
ctx: ctx,
authConfigs: mgmtCtx.Management.AuthConfigs(""),
userMGR: userMGR,
tokenMGR: tokenMGR,
providerName: providerName,
testAndApplyInputType: testAndApplyInputType,
userScope: userScope,
groupScope: groupScope}
}
func (p *ldapProvider) GetName() string {
return p.providerName
}
func (p *ldapProvider) CustomizeSchema(schema *types.Schema) {
schema.ActionHandler = p.actionHandler
schema.Formatter = p.formatter
}
func (p *ldapProvider) TransformToAuthProvider(authConfig map[string]interface{}) map[string]interface{} {
ldap := common.TransformToAuthProvider(authConfig)
return ldap
}
func (p *ldapProvider) AuthenticateUser(input interface{}) (v3.Principal, []v3.Principal, string, error) {
login, ok := input.(*v3public.BasicLogin)
if !ok {
return v3.Principal{}, nil, "", errors.New("unexpected input type")
}
config, caPool, err := p.getLDAPConfig()
if err != nil {
return v3.Principal{}, nil, "", errors.New("can't find authprovider")
}
principal, groupPrincipal, err := p.loginUser(login, config, caPool)
if err != nil {
return v3.Principal{}, nil, "", err
}
return principal, groupPrincipal, "", err
}
func (p *ldapProvider) SearchPrincipals(searchKey, principalType string, myToken v3.Token) ([]v3.Principal, error) {
var principals []v3.Principal
var err error
config, caPool, err := p.getLDAPConfig()
if err != nil {
return principals, nil
}
lConn, err := p.ldapConnection(config, caPool)
if err != nil {
return principals, nil
}
defer lConn.Close()
principals, err = p.searchPrincipals(searchKey, principalType, config, lConn)
if err == nil {
for _, principal := range principals {
if principal.PrincipalType == "user" {
if p.isThisUserMe(myToken.UserPrincipal, principal) {
principal.Me = true
}
} else if principal.PrincipalType == "group" {
if p.isMemberOf(myToken.GroupPrincipals, principal) {
principal.MemberOf = true
}
}
}
}
return principals, nil
}
func (p *ldapProvider) GetPrincipal(principalID string, token v3.Token) (v3.Principal, error) {
config, caPool, err := p.getLDAPConfig()
if err != nil {
return v3.Principal{}, nil
}
parts := strings.SplitN(principalID, ":", 2)
if len(parts) != 2 {
return v3.Principal{}, errors.Errorf("invalid id %v", principalID)
}
scope := parts[0]
externalID := strings.TrimPrefix(parts[1], "//")
principal, err := p.getPrincipal(externalID, scope, config, caPool)
if err != nil {
return v3.Principal{}, err
}
if p.isThisUserMe(token.UserPrincipal, *principal) {
principal.Me = true
}
return *principal, err
}
func (p *ldapProvider) isThisUserMe(me v3.Principal, other v3.Principal) bool {
if me.ObjectMeta.Name == other.ObjectMeta.Name && me.LoginName == other.LoginName && me.PrincipalType == other.PrincipalType {
return true
}
return false
}
func (p *ldapProvider) isMemberOf(myGroups []v3.Principal, other v3.Principal) bool {
for _, mygroup := range myGroups {
if mygroup.ObjectMeta.Name == other.ObjectMeta.Name && mygroup.PrincipalType == other.PrincipalType {
return true
}
}
return false
}
func (p *ldapProvider) getLDAPConfig() (*v3.LdapConfig, *x509.CertPool, error) {
// TODO See if this can be simplified. also, this makes an api call everytime. find a better way
authConfigObj, err := p.authConfigs.ObjectClient().UnstructuredClient().Get(p.providerName, metav1.GetOptions{})
if err != nil {
return nil, nil, fmt.Errorf("failed to retrieve %s, error: %v", p.providerName, err)
}
u, ok := authConfigObj.(runtime.Unstructured)
if !ok {
return nil, nil, fmt.Errorf("failed to retrieve %s, cannot read k8s Unstructured data", p.providerName)
}
storedLdapConfigMap := u.UnstructuredContent()
storedLdapConfig := &v3.LdapConfig{}
mapstructure.Decode(storedLdapConfigMap, storedLdapConfig)
metadataMap, ok := storedLdapConfigMap["metadata"].(map[string]interface{})
if !ok {
return nil, nil, fmt.Errorf("failed to retrieve %s metadata, cannot read k8s Unstructured data", p.providerName)
}
objectMeta := &metav1.ObjectMeta{}
mapstructure.Decode(metadataMap, objectMeta)
storedLdapConfig.ObjectMeta = *objectMeta
if p.certs != storedLdapConfig.Certificate || p.caPool == nil {
pool, err := newCAPool(storedLdapConfig.Certificate)
if err != nil {
return nil, nil, err
}
p.certs = storedLdapConfig.Certificate
p.caPool = pool
}
return storedLdapConfig, p.caPool, nil
}
func newCAPool(cert string) (*x509.CertPool, error) {
pool, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
pool.AppendCertsFromPEM([]byte(cert))
return pool, nil
}