-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
auth_config.go
147 lines (114 loc) · 4.61 KB
/
auth_config.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
package origin
import (
"crypto/md5"
"fmt"
"net/url"
"github.com/pborman/uuid"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/storage"
"github.com/openshift/origin/pkg/auth/server/session"
osclient "github.com/openshift/origin/pkg/client"
configapi "github.com/openshift/origin/pkg/cmd/server/api"
"github.com/openshift/origin/pkg/cmd/server/api/latest"
identityregistry "github.com/openshift/origin/pkg/user/registry/identity"
identityetcd "github.com/openshift/origin/pkg/user/registry/identity/etcd"
userregistry "github.com/openshift/origin/pkg/user/registry/user"
useretcd "github.com/openshift/origin/pkg/user/registry/user/etcd"
"github.com/openshift/origin/pkg/util/restoptions"
)
type AuthConfig struct {
Options configapi.OAuthConfig
// AssetPublicAddresses contains valid redirectURI prefixes to direct browsers to the web console
AssetPublicAddresses []string
// KubeClient is kubeclient with enough permission for the auth API
KubeClient kclientset.Interface
// OpenShiftClient is osclient with enough permission for the auth API
OpenShiftClient osclient.Interface
// RESTOptionsGetter provides storage and RESTOption lookup
RESTOptionsGetter restoptions.Getter
// EtcdBackends is a list of storage interfaces, each of which talks to a single etcd backend.
// These are only used to ensure newly created tokens are distributed to all backends before returning them for use.
// EtcdHelper should normally be used for storage functions.
EtcdBackends []storage.Interface
UserRegistry userregistry.Registry
IdentityRegistry identityregistry.Registry
SessionAuth *session.Authenticator
HandlerWrapper handlerWrapper
}
func BuildAuthConfig(masterConfig *MasterConfig) (*AuthConfig, error) {
options := masterConfig.Options
osClient, kubeClient := masterConfig.OAuthServerClients()
var sessionAuth *session.Authenticator
var sessionHandlerWrapper handlerWrapper
if options.OAuthConfig.SessionConfig != nil {
secure := isHTTPS(options.OAuthConfig.MasterPublicURL)
auth, wrapper, err := buildSessionAuth(secure, options.OAuthConfig.SessionConfig)
if err != nil {
return nil, err
}
sessionAuth = auth
sessionHandlerWrapper = wrapper
}
// Build the list of valid redirect_uri prefixes for a login using the openshift-web-console client to redirect to
assetPublicURLs := []string{}
if !options.DisabledFeatures.Has(configapi.FeatureWebConsole) {
assetPublicURLs = []string{options.OAuthConfig.AssetPublicURL}
}
userStorage, err := useretcd.NewREST(masterConfig.RESTOptionsGetter)
if err != nil {
return nil, err
}
userRegistry := userregistry.NewRegistry(userStorage)
identityStorage, err := identityetcd.NewREST(masterConfig.RESTOptionsGetter)
if err != nil {
return nil, err
}
identityRegistry := identityregistry.NewRegistry(identityStorage)
ret := &AuthConfig{
Options: *options.OAuthConfig,
KubeClient: kubeClient,
OpenShiftClient: osClient,
AssetPublicAddresses: assetPublicURLs,
RESTOptionsGetter: masterConfig.RESTOptionsGetter,
IdentityRegistry: identityRegistry,
UserRegistry: userRegistry,
SessionAuth: sessionAuth,
HandlerWrapper: sessionHandlerWrapper,
}
return ret, nil
}
func buildSessionAuth(secure bool, config *configapi.SessionConfig) (*session.Authenticator, handlerWrapper, error) {
secrets, err := getSessionSecrets(config.SessionSecretsFile)
if err != nil {
return nil, nil, err
}
sessionStore := session.NewStore(secure, int(config.SessionMaxAgeSeconds), secrets...)
return session.NewAuthenticator(sessionStore, config.SessionName), sessionStore, nil
}
func getSessionSecrets(filename string) ([]string, error) {
// Build secrets list
secrets := []string{}
if len(filename) != 0 {
sessionSecrets, err := latest.ReadSessionSecrets(filename)
if err != nil {
return nil, fmt.Errorf("error reading sessionSecretsFile %s: %v", filename, err)
}
if len(sessionSecrets.Secrets) == 0 {
return nil, fmt.Errorf("sessionSecretsFile %s contained no secrets", filename)
}
for _, s := range sessionSecrets.Secrets {
secrets = append(secrets, s.Authentication)
secrets = append(secrets, s.Encryption)
}
} else {
// Generate random signing and encryption secrets if none are specified in config
secrets = append(secrets, fmt.Sprintf("%x", md5.Sum([]byte(uuid.NewRandom().String()))))
secrets = append(secrets, fmt.Sprintf("%x", md5.Sum([]byte(uuid.NewRandom().String()))))
}
return secrets, nil
}
// isHTTPS returns true if the given URL is a valid https URL
func isHTTPS(u string) bool {
parsedURL, err := url.Parse(u)
return err == nil && parsedURL.Scheme == "https"
}