-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathidentity.go
155 lines (130 loc) · 3.84 KB
/
identity.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
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package identity
import (
"context"
"errors"
"fmt"
"strings"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"
infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
)
const (
UsernameKey = "username"
PasswordKey = "password"
)
type Credentials struct {
Username string
Password string
}
func GetCredentials(ctx context.Context, c client.Client, cluster *infrav1.VSphereCluster, controllerNamespace string) (*Credentials, error) {
if err := validateInputs(c, cluster); err != nil {
return nil, err
}
ref := cluster.Spec.IdentityRef
secret := &apiv1.Secret{}
var secretKey client.ObjectKey
switch ref.Kind {
case infrav1.SecretKind:
secretKey = client.ObjectKey{
Namespace: cluster.Namespace,
Name: ref.Name,
}
case infrav1.VSphereClusterIdentityKind:
identity := &infrav1.VSphereClusterIdentity{}
key := client.ObjectKey{
Name: ref.Name,
}
if err := c.Get(ctx, key, identity); err != nil {
return nil, err
}
if !identity.Status.Ready {
return nil, errors.New("identity isn't ready to be used yet")
}
if identity.Spec.AllowedNamespaces == nil {
return nil, errors.New("allowedNamespaces set to nil, no namespaces are allowed to use this identity")
}
selector, err := metav1.LabelSelectorAsSelector(&identity.Spec.AllowedNamespaces.Selector)
if err != nil {
return nil, errors.New("failed to build selector")
}
ns := &apiv1.Namespace{}
nsKey := client.ObjectKey{
Name: cluster.Namespace,
}
if err := c.Get(ctx, nsKey, ns); err != nil {
return nil, err
}
if !selector.Matches(labels.Set(ns.GetLabels())) {
return nil, fmt.Errorf("namespace %s is not allowed to use specifified identity", cluster.Namespace)
}
secretKey = client.ObjectKey{
Name: identity.Spec.SecretName,
Namespace: controllerNamespace,
}
default:
return nil, fmt.Errorf("unknown type %s used for Identity", ref.Kind)
}
if err := c.Get(ctx, secretKey, secret); err != nil {
return nil, err
}
credentials := &Credentials{
Username: getData(secret, UsernameKey),
Password: getData(secret, PasswordKey),
}
return credentials, nil
}
func validateInputs(c client.Client, cluster *infrav1.VSphereCluster) error {
if c == nil {
return errors.New("kubernetes client is required")
}
if cluster == nil {
return errors.New("vsphere cluster is required")
}
ref := cluster.Spec.IdentityRef
if ref == nil {
return errors.New("IdentityRef is required")
}
return nil
}
func IsSecretIdentity(cluster *infrav1.VSphereCluster) bool {
if cluster == nil || cluster.Spec.IdentityRef == nil {
return false
}
return cluster.Spec.IdentityRef.Kind == infrav1.SecretKind
}
func IsOwnedByIdentityOrCluster(ownerReferences []metav1.OwnerReference) bool {
if len(ownerReferences) > 0 {
for _, ownerReference := range ownerReferences {
if !strings.Contains(ownerReference.APIVersion, infrav1.GroupName+"/") {
continue
}
if ownerReference.Kind == "VSphereCluster" || ownerReference.Kind == "VSphereClusterIdentity" {
return true
}
}
}
return false
}
func getData(secret *apiv1.Secret, key string) string {
if secret.Data == nil {
return ""
}
if val, ok := secret.Data[key]; ok {
return string(val)
}
return ""
}