forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
providers.go
160 lines (137 loc) · 5.61 KB
/
providers.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
package providers
import (
"context"
"fmt"
"sync"
"github.com/rancher/rancher/pkg/auth/providers/activedirectory"
"github.com/rancher/rancher/pkg/auth/providers/azure"
"github.com/rancher/rancher/pkg/auth/providers/common"
"github.com/rancher/rancher/pkg/auth/providers/github"
"github.com/rancher/rancher/pkg/auth/providers/ldap"
"github.com/rancher/rancher/pkg/auth/providers/local"
"github.com/rancher/rancher/pkg/auth/providers/saml"
"github.com/rancher/rancher/pkg/auth/tokens"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/client/management/v3"
publicclient "github.com/rancher/types/client/management/v3public"
"github.com/rancher/types/config"
)
var (
ProviderNames = make(map[string]bool)
ProvidersWithSecrets = make(map[string]bool)
UnrefreshableProviders = make(map[string]bool)
providers = make(map[string]common.AuthProvider)
localProvider = "local"
providersByType = make(map[string]common.AuthProvider)
confMu sync.Mutex
)
func GetProvider(providerName string) (common.AuthProvider, error) {
if provider, ok := providers[providerName]; ok {
if provider != nil {
return provider, nil
}
}
return nil, fmt.Errorf("No such provider '%s'", providerName)
}
func GetProviderByType(configType string) common.AuthProvider {
return providersByType[configType]
}
func Configure(ctx context.Context, mgmt *config.ScaledContext) {
confMu.Lock()
defer confMu.Unlock()
userMGR := mgmt.UserManager
tokenMGR := tokens.NewManager(ctx, mgmt)
var p common.AuthProvider
p = local.Configure(ctx, mgmt, tokenMGR)
ProviderNames[local.Name] = true
providers[local.Name] = p
providersByType[client.LocalConfigType] = p
providersByType[publicclient.LocalProviderType] = p
p = github.Configure(ctx, mgmt, userMGR, tokenMGR)
ProviderNames[github.Name] = true
ProvidersWithSecrets[github.Name] = true
providers[github.Name] = p
providersByType[client.GithubConfigType] = p
providersByType[publicclient.GithubProviderType] = p
p = azure.Configure(ctx, mgmt, userMGR, tokenMGR)
ProviderNames[azure.Name] = true
ProvidersWithSecrets[azure.Name] = true
providers[azure.Name] = p
providersByType[client.AzureADConfigType] = p
providersByType[publicclient.AzureADProviderType] = p
p = activedirectory.Configure(ctx, mgmt, userMGR, tokenMGR)
ProviderNames[activedirectory.Name] = true
providers[activedirectory.Name] = p
providersByType[client.ActiveDirectoryConfigType] = p
providersByType[publicclient.ActiveDirectoryProviderType] = p
p = ldap.Configure(ctx, mgmt, userMGR, tokenMGR, ldap.OpenLdapName)
ProviderNames[ldap.OpenLdapName] = true
providers[ldap.OpenLdapName] = p
providersByType[client.OpenLdapConfigType] = p
providersByType[publicclient.OpenLdapProviderType] = p
p = ldap.Configure(ctx, mgmt, userMGR, tokenMGR, ldap.FreeIpaName)
ProviderNames[ldap.FreeIpaName] = true
providers[ldap.FreeIpaName] = p
providersByType[client.FreeIpaConfigType] = p
providersByType[publicclient.FreeIpaProviderType] = p
p = saml.Configure(ctx, mgmt, userMGR, tokenMGR, saml.PingName)
ProviderNames[saml.PingName] = true
UnrefreshableProviders[saml.PingName] = true
providers[saml.PingName] = p
providersByType[client.PingConfigType] = p
providersByType[publicclient.PingProviderType] = p
p = saml.Configure(ctx, mgmt, userMGR, tokenMGR, saml.ADFSName)
ProviderNames[saml.ADFSName] = true
UnrefreshableProviders[saml.ADFSName] = true
providers[saml.ADFSName] = p
providersByType[client.ADFSConfigType] = p
providersByType[publicclient.ADFSProviderType] = p
p = saml.Configure(ctx, mgmt, userMGR, tokenMGR, saml.KeyCloakName)
ProviderNames[saml.KeyCloakName] = true
UnrefreshableProviders[saml.KeyCloakName] = true
providers[saml.KeyCloakName] = p
providersByType[client.KeyCloakConfigType] = p
providersByType[publicclient.KeyCloakProviderType] = p
p = saml.Configure(ctx, mgmt, userMGR, tokenMGR, saml.OKTAName)
ProviderNames[saml.OKTAName] = true
UnrefreshableProviders[saml.OKTAName] = true
providers[saml.OKTAName] = p
providersByType[client.OKTAConfigType] = p
providersByType[publicclient.OKTAProviderType] = p
}
func AuthenticateUser(input interface{}, providerName string) (v3.Principal, []v3.Principal, string, error) {
return providers[providerName].AuthenticateUser(input)
}
func GetPrincipal(principalID string, myToken v3.Token) (v3.Principal, error) {
principal, err := providers[myToken.AuthProvider].GetPrincipal(principalID, myToken)
if err != nil && myToken.AuthProvider != localProvider {
p2, e2 := providers[localProvider].GetPrincipal(principalID, myToken)
if e2 == nil {
return p2, nil
}
}
return principal, err
}
func SearchPrincipals(name, principalType string, myToken v3.Token) ([]v3.Principal, error) {
principals, err := providers[myToken.AuthProvider].SearchPrincipals(name, principalType, myToken)
if err != nil {
return principals, err
}
if myToken.AuthProvider != localProvider {
lp := providers[localProvider]
if lpDedupe, _ := lp.(*local.Provider); lpDedupe != nil {
localPrincipals, err := lpDedupe.SearchPrincipalsDedupe(name, principalType, myToken, principals)
if err != nil {
return principals, err
}
principals = append(principals, localPrincipals...)
}
}
return principals, err
}
func CanAccessWithGroupProviders(providerName string, userPrincipalID string, groups []v3.Principal) (bool, error) {
return providers[providerName].CanAccessWithGroupProviders(userPrincipalID, groups)
}
func RefetchGroupPrincipals(principalID string, providerName string, secret string) ([]v3.Principal, error) {
return providers[providerName].RefetchGroupPrincipals(principalID, secret)
}