forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.go
517 lines (427 loc) · 16.6 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
package config
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"net/http"
"strconv"
"strings"
"time"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
)
// 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)
}
}
for k := range config.AdmissionConfig.PluginConfig {
refs = append(refs, &config.AdmissionConfig.PluginConfig[k].Location)
}
refs = append(refs, &config.KubernetesMasterConfig.SchedulerConfigFile)
refs = append(refs, &config.KubernetesMasterConfig.ProxyClientInfo.CertFile)
refs = append(refs, &config.KubernetesMasterConfig.ProxyClientInfo.KeyFile)
refs = appendFlagsWithFileExtensions(refs, config.KubernetesMasterConfig.APIServerArguments)
refs = appendFlagsWithFileExtensions(refs, config.KubernetesMasterConfig.SchedulerArguments)
refs = appendFlagsWithFileExtensions(refs, config.KubernetesMasterConfig.ControllerArguments)
if config.AuthConfig.RequestHeader != nil {
refs = append(refs, &config.AuthConfig.RequestHeader.ClientCA)
}
for k := range config.AuthConfig.WebhookTokenAuthenticators {
refs = append(refs, &config.AuthConfig.WebhookTokenAuthenticators[k].ConfigFile)
}
if len(config.AuthConfig.OAuthMetadataFile) > 0 {
refs = append(refs, &config.AuthConfig.OAuthMetadataFile)
}
refs = append(refs, &config.AggregatorConfig.ProxyClientInfo.CertFile)
refs = append(refs, &config.AggregatorConfig.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)
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)
refs = append(refs, &config.AuditConfig.PolicyFile)
return refs
}
func appendFlagsWithFileExtensions(refs []*string, args ExtendedArguments) []*string {
for key, s := range args {
if len(s) == 0 {
continue
}
if !strings.HasSuffix(key, "-file") && !strings.HasSuffix(key, "-dir") {
continue
}
for i := range s {
refs = append(refs, &s[i])
}
}
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.DNSRecursiveResolvConf)
refs = append(refs, &config.MasterKubeConfig)
refs = append(refs, &config.VolumeDirectory)
if config.PodManifestConfig != nil {
refs = append(refs, &config.PodManifestConfig.Path)
}
refs = appendFlagsWithFileExtensions(refs, config.KubeletArguments)
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
}
// GetKubeConfigOrInClusterConfig loads in-cluster config if kubeConfigFile is empty or the file if not,
// then applies overrides.
func GetKubeConfigOrInClusterConfig(kubeConfigFile string, overrides *ClientConnectionOverrides) (*restclient.Config, error) {
if len(kubeConfigFile) > 0 {
return GetClientConfig(kubeConfigFile, overrides)
}
clientConfig, err := restclient.InClusterConfig()
if err != nil {
return nil, err
}
applyClientConnectionOverrides(overrides, clientConfig)
clientConfig.WrapTransport = DefaultClientTransport
return clientConfig, nil
}
func GetClientConfig(kubeConfigFile string, overrides *ClientConnectionOverrides) (*restclient.Config, error) {
kubeConfigBytes, err := ioutil.ReadFile(kubeConfigFile)
if err != nil {
return nil, err
}
kubeConfig, err := clientcmd.NewClientConfigFromBytes(kubeConfigBytes)
if err != nil {
return nil, err
}
clientConfig, err := kubeConfig.ClientConfig()
if err != nil {
return nil, err
}
applyClientConnectionOverrides(overrides, clientConfig)
clientConfig.WrapTransport = DefaultClientTransport
return clientConfig, 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, ok := rt.(*http.Transport)
if !ok {
return rt
}
// 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
}
// 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
}
func GetOAuthClientCertCAs(options MasterConfig) ([]*x509.Certificate, error) {
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 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
}
const kubeAPIEnablementFlag = "runtime-config"
// GetKubeAPIServerFlagAPIEnablement parses the available flag at the groupVersion level
// with no support for individual resources and no support for the legacy API.
func GetKubeAPIServerFlagAPIEnablement(flagValue []string) map[schema.GroupVersion]bool {
versions := map[schema.GroupVersion]bool{}
for _, val := range flagValue {
// skip bad flags
if strings.HasPrefix(val, "api/") {
continue
}
val = strings.TrimPrefix(val, "apis/")
tokens := strings.Split(val, "=")
if len(tokens) != 2 {
continue
}
gv, err := schema.ParseGroupVersion(tokens[0])
if err != nil {
continue
}
enabled, _ := strconv.ParseBool(tokens[1])
versions[gv] = enabled
}
return versions
}
// GetEnabledAPIVersionsForGroup returns the list of API Versions that are enabled for that group.
// It respects the extended args which are used to enable and disable versions in kube too.
func GetEnabledAPIVersionsForGroup(config KubernetesMasterConfig, apiGroup string) []string {
allowedVersions := KubeAPIGroupsToAllowedVersions[apiGroup]
blacklist := sets.NewString(config.DisabledAPIGroupVersions[apiGroup]...)
if blacklist.Has(AllVersions) {
return []string{}
}
flagVersions := GetKubeAPIServerFlagAPIEnablement(config.APIServerArguments[kubeAPIEnablementFlag])
enabledVersions := sets.String{}
for _, currVersion := range allowedVersions {
if blacklist.Has(currVersion) {
continue
}
gv := schema.GroupVersion{Group: apiGroup, Version: currVersion}
// if this was explicitly disabled via flag, skip it
if enabled, ok := flagVersions[gv]; ok && !enabled {
continue
}
enabledVersions.Insert(currVersion)
}
for currVersion, enabled := range flagVersions {
if !enabled {
continue
}
if blacklist.Has(currVersion.Version) {
continue
}
if currVersion.Group != apiGroup {
continue
}
enabledVersions.Insert(currVersion.Version)
}
return enabledVersions.List()
}
// It respects the extended args which are used to enable and disable versions in kube too.
// 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)...)
disabledVersions := allowedVersions.Difference(enabledVersions)
disabledVersions.Insert(config.DisabledAPIGroupVersions[apiGroup]...)
flagVersions := GetKubeAPIServerFlagAPIEnablement(config.APIServerArguments[kubeAPIEnablementFlag])
for currVersion, enabled := range flagVersions {
if enabled {
continue
}
if disabledVersions.Has(currVersion.Version) {
continue
}
if currVersion.Group != apiGroup {
continue
}
disabledVersions.Insert(currVersion.Version)
}
return disabledVersions.List()
}
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)
}