-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
584 lines (483 loc) · 18.9 KB
/
helpers.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package api
import (
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/http"
"strings"
"time"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/client/restclient"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
kubeletclient "k8s.io/kubernetes/pkg/kubelet/client"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/sets"
"github.com/openshift/origin/pkg/client"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
)
var (
// Maps lower-cased feature flag names and aliases to their canonical names.
knownOpenShiftFeatureSet map[string]string
)
func init() {
knownOpenShiftFeatureSet = make(map[string]string, len(KnownOpenShiftFeatures))
for _, feature := range KnownOpenShiftFeatures {
knownOpenShiftFeatureSet[strings.ToLower(feature)] = feature
}
for alias, feature := range FeatureAliases {
knownOpenShiftFeatureSet[strings.ToLower(alias)] = feature
}
}
// NormalizeOpenShiftFeature returns canonical name for given OpenShift feature
// flag or an alias if known. Otherwise lower-cased name is returned.
func NormalizeOpenShiftFeature(name string) (string, bool) {
name = strings.ToLower(name)
if feature, ok := knownOpenShiftFeatureSet[name]; ok {
return feature, true
}
return name, false
}
// Add extends feature list with given valid items. They are appended
// unless already present.
func (fl *FeatureList) Add(items ...string) error {
unknown := []string{}
toAppend := make([]string, 0, len(items))
for _, item := range items {
feature, known := NormalizeOpenShiftFeature(item)
if !known {
unknown = append(unknown, item)
continue
}
if fl.Has(feature) {
continue
}
toAppend = append(toAppend, feature)
}
if len(unknown) > 0 {
return fmt.Errorf("unknown features: %s", strings.Join(unknown, ", "))
}
*fl = append(*fl, toAppend...)
return nil
}
// Delete removes given items from feature list while keeping its original
// order.
func (fl *FeatureList) Delete(items ...string) {
toDelete := FeatureList(items)
newList := []string{}
for _, item := range *fl {
if !toDelete.Has(item) {
newList = append(newList, item)
}
}
*fl = newList
}
// Has returns true if given feature exists in feature list. The check is
// case-insensitive.
func (fl FeatureList) Has(feature string) bool {
normalized, _ := NormalizeOpenShiftFeature(feature)
for _, item := range fl {
itemNormalized, _ := NormalizeOpenShiftFeature(item)
if normalized == itemNormalized {
return true
}
}
return false
}
// ParseNamespaceAndName returns back the namespace and name (empty if something goes wrong), for a given string.
// This is useful when pointing to a particular resource inside of our config.
func ParseNamespaceAndName(in string) (string, string, error) {
if len(in) == 0 {
return "", "", nil
}
tokens := strings.Split(in, "/")
if len(tokens) != 2 {
return "", "", fmt.Errorf("expected input in the form <namespace>/<resource-name>, not: %v", in)
}
return tokens[0], tokens[1], nil
}
func RelativizeMasterConfigPaths(config *MasterConfig, base string) error {
return cmdutil.RelativizePathWithNoBacksteps(GetMasterFileReferences(config), base)
}
func ResolveMasterConfigPaths(config *MasterConfig, base string) error {
return cmdutil.ResolvePaths(GetMasterFileReferences(config), base)
}
func GetMasterFileReferences(config *MasterConfig) []*string {
refs := []*string{}
refs = append(refs, &config.ServingInfo.ServerCert.CertFile)
refs = append(refs, &config.ServingInfo.ServerCert.KeyFile)
refs = append(refs, &config.ServingInfo.ClientCA)
for i := range config.ServingInfo.NamedCertificates {
refs = append(refs, &config.ServingInfo.NamedCertificates[i].CertFile)
refs = append(refs, &config.ServingInfo.NamedCertificates[i].KeyFile)
}
refs = append(refs, &config.EtcdClientInfo.ClientCert.CertFile)
refs = append(refs, &config.EtcdClientInfo.ClientCert.KeyFile)
refs = append(refs, &config.EtcdClientInfo.CA)
refs = append(refs, &config.KubeletClientInfo.ClientCert.CertFile)
refs = append(refs, &config.KubeletClientInfo.ClientCert.KeyFile)
refs = append(refs, &config.KubeletClientInfo.CA)
if config.EtcdConfig != nil {
refs = append(refs, &config.EtcdConfig.ServingInfo.ServerCert.CertFile)
refs = append(refs, &config.EtcdConfig.ServingInfo.ServerCert.KeyFile)
refs = append(refs, &config.EtcdConfig.ServingInfo.ClientCA)
for i := range config.EtcdConfig.ServingInfo.NamedCertificates {
refs = append(refs, &config.EtcdConfig.ServingInfo.NamedCertificates[i].CertFile)
refs = append(refs, &config.EtcdConfig.ServingInfo.NamedCertificates[i].KeyFile)
}
refs = append(refs, &config.EtcdConfig.PeerServingInfo.ServerCert.CertFile)
refs = append(refs, &config.EtcdConfig.PeerServingInfo.ServerCert.KeyFile)
refs = append(refs, &config.EtcdConfig.PeerServingInfo.ClientCA)
for i := range config.EtcdConfig.PeerServingInfo.NamedCertificates {
refs = append(refs, &config.EtcdConfig.PeerServingInfo.NamedCertificates[i].CertFile)
refs = append(refs, &config.EtcdConfig.PeerServingInfo.NamedCertificates[i].KeyFile)
}
refs = append(refs, &config.EtcdConfig.StorageDir)
}
if config.OAuthConfig != nil {
if config.OAuthConfig.MasterCA != nil {
refs = append(refs, config.OAuthConfig.MasterCA)
}
if config.OAuthConfig.SessionConfig != nil {
refs = append(refs, &config.OAuthConfig.SessionConfig.SessionSecretsFile)
}
for _, identityProvider := range config.OAuthConfig.IdentityProviders {
switch provider := identityProvider.Provider.(type) {
case (*RequestHeaderIdentityProvider):
refs = append(refs, &provider.ClientCA)
case (*HTPasswdPasswordIdentityProvider):
refs = append(refs, &provider.File)
case (*LDAPPasswordIdentityProvider):
refs = append(refs, &provider.CA)
refs = append(refs, GetStringSourceFileReferences(&provider.BindPassword)...)
case (*BasicAuthPasswordIdentityProvider):
refs = append(refs, &provider.RemoteConnectionInfo.CA)
refs = append(refs, &provider.RemoteConnectionInfo.ClientCert.CertFile)
refs = append(refs, &provider.RemoteConnectionInfo.ClientCert.KeyFile)
case (*KeystonePasswordIdentityProvider):
refs = append(refs, &provider.RemoteConnectionInfo.CA)
refs = append(refs, &provider.RemoteConnectionInfo.ClientCert.CertFile)
refs = append(refs, &provider.RemoteConnectionInfo.ClientCert.KeyFile)
case (*GitLabIdentityProvider):
refs = append(refs, &provider.CA)
refs = append(refs, GetStringSourceFileReferences(&provider.ClientSecret)...)
case (*OpenIDIdentityProvider):
refs = append(refs, &provider.CA)
refs = append(refs, GetStringSourceFileReferences(&provider.ClientSecret)...)
case (*GoogleIdentityProvider):
refs = append(refs, GetStringSourceFileReferences(&provider.ClientSecret)...)
case (*GitHubIdentityProvider):
refs = append(refs, GetStringSourceFileReferences(&provider.ClientSecret)...)
}
}
if config.OAuthConfig.Templates != nil {
refs = append(refs, &config.OAuthConfig.Templates.Login)
refs = append(refs, &config.OAuthConfig.Templates.ProviderSelection)
refs = append(refs, &config.OAuthConfig.Templates.Error)
}
}
if config.AssetConfig != nil {
refs = append(refs, &config.AssetConfig.ServingInfo.ServerCert.CertFile)
refs = append(refs, &config.AssetConfig.ServingInfo.ServerCert.KeyFile)
refs = append(refs, &config.AssetConfig.ServingInfo.ClientCA)
for i := range config.AssetConfig.ServingInfo.NamedCertificates {
refs = append(refs, &config.AssetConfig.ServingInfo.NamedCertificates[i].CertFile)
refs = append(refs, &config.AssetConfig.ServingInfo.NamedCertificates[i].KeyFile)
}
for i := range config.AssetConfig.ExtensionScripts {
refs = append(refs, &config.AssetConfig.ExtensionScripts[i])
}
for i := range config.AssetConfig.ExtensionStylesheets {
refs = append(refs, &config.AssetConfig.ExtensionStylesheets[i])
}
for i := range config.AssetConfig.Extensions {
refs = append(refs, &config.AssetConfig.Extensions[i].SourceDirectory)
}
}
if config.KubernetesMasterConfig != nil {
refs = append(refs, &config.KubernetesMasterConfig.SchedulerConfigFile)
refs = append(refs, &config.KubernetesMasterConfig.ProxyClientInfo.CertFile)
refs = append(refs, &config.KubernetesMasterConfig.ProxyClientInfo.KeyFile)
}
refs = append(refs, &config.ServiceAccountConfig.MasterCA)
refs = append(refs, &config.ServiceAccountConfig.PrivateKeyFile)
for i := range config.ServiceAccountConfig.PublicKeyFiles {
refs = append(refs, &config.ServiceAccountConfig.PublicKeyFiles[i])
}
refs = append(refs, &config.MasterClients.OpenShiftLoopbackKubeConfig)
refs = append(refs, &config.MasterClients.ExternalKubernetesKubeConfig)
refs = append(refs, &config.PolicyConfig.BootstrapPolicyFile)
if config.ControllerConfig.ServiceServingCert.Signer != nil {
refs = append(refs, &config.ControllerConfig.ServiceServingCert.Signer.CertFile)
refs = append(refs, &config.ControllerConfig.ServiceServingCert.Signer.KeyFile)
}
refs = append(refs, &config.AuditConfig.AuditFilePath)
return refs
}
func RelativizeNodeConfigPaths(config *NodeConfig, base string) error {
return cmdutil.RelativizePathWithNoBacksteps(GetNodeFileReferences(config), base)
}
func ResolveNodeConfigPaths(config *NodeConfig, base string) error {
return cmdutil.ResolvePaths(GetNodeFileReferences(config), base)
}
func GetNodeFileReferences(config *NodeConfig) []*string {
refs := []*string{}
refs = append(refs, &config.ServingInfo.ServerCert.CertFile)
refs = append(refs, &config.ServingInfo.ServerCert.KeyFile)
refs = append(refs, &config.ServingInfo.ClientCA)
for i := range config.ServingInfo.NamedCertificates {
refs = append(refs, &config.ServingInfo.NamedCertificates[i].CertFile)
refs = append(refs, &config.ServingInfo.NamedCertificates[i].KeyFile)
}
refs = append(refs, &config.MasterKubeConfig)
refs = append(refs, &config.VolumeDirectory)
if config.PodManifestConfig != nil {
refs = append(refs, &config.PodManifestConfig.Path)
}
return refs
}
// SetProtobufClientDefaults sets the appropriate content types for defaulting to protobuf
// client communications and increases the default QPS and burst. This is used to override
// defaulted config supporting versions older than 1.3 for new configurations generated in 1.3+.
func SetProtobufClientDefaults(overrides *ClientConnectionOverrides) {
overrides.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json"
overrides.ContentType = "application/vnd.kubernetes.protobuf"
overrides.QPS *= 2
overrides.Burst *= 2
}
// TODO: clients should be copied and instantiated from a common client config, tweaked, then
// given to individual controllers and other infrastructure components.
func GetKubeClient(kubeConfigFile string, overrides *ClientConnectionOverrides) (*kclient.Client, *restclient.Config, error) {
loadingRules := &clientcmd.ClientConfigLoadingRules{}
loadingRules.ExplicitPath = kubeConfigFile
loader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})
kubeConfig, err := loader.ClientConfig()
if err != nil {
return nil, nil, err
}
applyClientConnectionOverrides(overrides, kubeConfig)
kubeConfig.WrapTransport = DefaultClientTransport
kubeClient, err := kclient.New(kubeConfig)
if err != nil {
return nil, nil, err
}
return kubeClient, kubeConfig, nil
}
// TODO: clients should be copied and instantiated from a common client config, tweaked, then
// given to individual controllers and other infrastructure components. Overrides are optional
// and may alter the default configuration.
func GetOpenShiftClient(kubeConfigFile string, overrides *ClientConnectionOverrides) (*client.Client, *restclient.Config, error) {
loadingRules := &clientcmd.ClientConfigLoadingRules{}
loadingRules.ExplicitPath = kubeConfigFile
loader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})
kubeConfig, err := loader.ClientConfig()
if err != nil {
return nil, nil, err
}
applyClientConnectionOverrides(overrides, kubeConfig)
kubeConfig.WrapTransport = DefaultClientTransport
openshiftClient, err := client.New(kubeConfig)
if err != nil {
return nil, nil, err
}
return openshiftClient, kubeConfig, nil
}
// applyClientConnectionOverrides updates a kubeConfig with the overrides from the config.
func applyClientConnectionOverrides(overrides *ClientConnectionOverrides, kubeConfig *restclient.Config) {
if overrides == nil {
return
}
kubeConfig.QPS = overrides.QPS
kubeConfig.Burst = int(overrides.Burst)
kubeConfig.ContentConfig.AcceptContentTypes = overrides.AcceptContentTypes
kubeConfig.ContentConfig.ContentType = overrides.ContentType
}
// DefaultClientTransport sets defaults for a client Transport that are suitable
// for use by infrastructure components.
func DefaultClientTransport(rt http.RoundTripper) http.RoundTripper {
transport := rt.(*http.Transport)
// TODO: this should be configured by the caller, not in this method.
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}
transport.Dial = dialer.Dial
// Hold open more internal idle connections
// TODO: this should be configured by the caller, not in this method.
transport.MaxIdleConnsPerHost = 100
return transport
}
func UseTLS(servingInfo ServingInfo) bool {
return len(servingInfo.ServerCert.CertFile) > 0
}
// GetAPIClientCertCAPool returns the cert pool used to validate client certificates to the API server
func GetAPIClientCertCAPool(options MasterConfig) (*x509.CertPool, error) {
return cmdutil.CertPoolFromFile(options.ServingInfo.ClientCA)
}
// GetNamedCertificateMap returns a map of strings to *tls.Certificate, suitable for use in tls.Config#NamedCertificates
// Returns an error if any of the certs cannot be loaded, or do not match the configured name
// Returns nil if len(namedCertificates) == 0
func GetNamedCertificateMap(namedCertificates []NamedCertificate) (map[string]*tls.Certificate, error) {
if len(namedCertificates) == 0 {
return nil, nil
}
namedCerts := map[string]*tls.Certificate{}
for _, namedCertificate := range namedCertificates {
cert, err := tls.LoadX509KeyPair(namedCertificate.CertFile, namedCertificate.KeyFile)
if err != nil {
return nil, err
}
for _, name := range namedCertificate.Names {
namedCerts[name] = &cert
}
}
return namedCerts, nil
}
// GetClientCertCAPool returns a cert pool containing all client CAs that could be presented (union of API and OAuth)
func GetClientCertCAPool(options MasterConfig) (*x509.CertPool, error) {
roots := x509.NewCertPool()
// Add CAs for OAuth
certs, err := getOAuthClientCertCAs(options)
if err != nil {
return nil, err
}
for _, root := range certs {
roots.AddCert(root)
}
// Add CAs for API
certs, err = getAPIClientCertCAs(options)
if err != nil {
return nil, err
}
for _, root := range certs {
roots.AddCert(root)
}
return roots, nil
}
func getOAuthClientCertCAs(options MasterConfig) ([]*x509.Certificate, error) {
if !UseTLS(options.ServingInfo.ServingInfo) {
return nil, nil
}
allCerts := []*x509.Certificate{}
if options.OAuthConfig != nil {
for _, identityProvider := range options.OAuthConfig.IdentityProviders {
switch provider := identityProvider.Provider.(type) {
case (*RequestHeaderIdentityProvider):
caFile := provider.ClientCA
if len(caFile) == 0 {
continue
}
certs, err := cmdutil.CertificatesFromFile(caFile)
if err != nil {
return nil, fmt.Errorf("Error reading %s: %s", caFile, err)
}
allCerts = append(allCerts, certs...)
}
}
}
return allCerts, nil
}
func getAPIClientCertCAs(options MasterConfig) ([]*x509.Certificate, error) {
if !UseTLS(options.ServingInfo.ServingInfo) {
return nil, nil
}
return cmdutil.CertificatesFromFile(options.ServingInfo.ClientCA)
}
func GetKubeletClientConfig(options MasterConfig) *kubeletclient.KubeletClientConfig {
config := &kubeletclient.KubeletClientConfig{
Port: options.KubeletClientInfo.Port,
}
if len(options.KubeletClientInfo.CA) > 0 {
config.EnableHttps = true
config.CAFile = options.KubeletClientInfo.CA
}
if len(options.KubeletClientInfo.ClientCert.CertFile) > 0 {
config.EnableHttps = true
config.CertFile = options.KubeletClientInfo.ClientCert.CertFile
config.KeyFile = options.KubeletClientInfo.ClientCert.KeyFile
}
return config
}
func IsPasswordAuthenticator(provider IdentityProvider) bool {
switch provider.Provider.(type) {
case
(*BasicAuthPasswordIdentityProvider),
(*AllowAllPasswordIdentityProvider),
(*DenyAllPasswordIdentityProvider),
(*HTPasswdPasswordIdentityProvider),
(*LDAPPasswordIdentityProvider),
(*KeystonePasswordIdentityProvider):
return true
}
return false
}
func IsIdentityProviderType(provider runtime.Object) bool {
switch provider.(type) {
case
(*RequestHeaderIdentityProvider),
(*BasicAuthPasswordIdentityProvider),
(*AllowAllPasswordIdentityProvider),
(*DenyAllPasswordIdentityProvider),
(*HTPasswdPasswordIdentityProvider),
(*LDAPPasswordIdentityProvider),
(*KeystonePasswordIdentityProvider),
(*OpenIDIdentityProvider),
(*GitHubIdentityProvider),
(*GitLabIdentityProvider),
(*GoogleIdentityProvider):
return true
}
return false
}
func IsOAuthIdentityProvider(provider IdentityProvider) bool {
switch provider.Provider.(type) {
case
(*OpenIDIdentityProvider),
(*GitHubIdentityProvider),
(*GitLabIdentityProvider),
(*GoogleIdentityProvider):
return true
}
return false
}
func HasOpenShiftAPILevel(config MasterConfig, apiLevel string) bool {
apiLevelSet := sets.NewString(config.APILevels...)
return apiLevelSet.Has(apiLevel)
}
// GetEnabledAPIVersionsForGroup returns the list of API Versions that are enabled for that group
func GetEnabledAPIVersionsForGroup(config KubernetesMasterConfig, apiGroup string) []string {
allowedVersions := KubeAPIGroupsToAllowedVersions[apiGroup]
blacklist := sets.NewString(config.DisabledAPIGroupVersions[apiGroup]...)
if blacklist.Has(AllVersions) {
return []string{}
}
enabledVersions := []string{}
for _, currVersion := range allowedVersions {
if !blacklist.Has(currVersion) {
enabledVersions = append(enabledVersions, currVersion)
}
}
return enabledVersions
}
// GetDisabledAPIVersionsForGroup returns the list of API Versions that are disabled for that group
func GetDisabledAPIVersionsForGroup(config KubernetesMasterConfig, apiGroup string) []string {
allowedVersions := sets.NewString(KubeAPIGroupsToAllowedVersions[apiGroup]...)
enabledVersions := sets.NewString(GetEnabledAPIVersionsForGroup(config, apiGroup)...)
return allowedVersions.Difference(enabledVersions).List()
}
func HasKubernetesAPIVersion(config KubernetesMasterConfig, groupVersion unversioned.GroupVersion) bool {
enabledVersions := GetEnabledAPIVersionsForGroup(config, groupVersion.Group)
return sets.NewString(enabledVersions...).Has(groupVersion.Version)
}
func CIDRsOverlap(cidr1, cidr2 string) bool {
_, ipNet1, err := net.ParseCIDR(cidr1)
if err != nil {
return false
}
_, ipNet2, err := net.ParseCIDR(cidr2)
if err != nil {
return false
}
return ipNet1.Contains(ipNet2.IP) || ipNet2.Contains(ipNet1.IP)
}