-
Notifications
You must be signed in to change notification settings - Fork 0
/
openshiftcluster_validatedynamic.go
267 lines (219 loc) · 8.52 KB
/
openshiftcluster_validatedynamic.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
package v20191231preview
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"fmt"
"net"
"net/http"
"strings"
"time"
mgmtnetwork "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-07-01/network"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/apparentlymart/go-cidr/cidr"
"github.com/dgrijalva/jwt-go"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/env"
"github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/authorization"
utilpermissions "github.com/Azure/ARO-RP/pkg/util/permissions"
"github.com/Azure/ARO-RP/pkg/util/subnet"
)
type azureClaim struct {
Roles []string `json:"roles,omitempty"`
}
func (*azureClaim) Valid() error {
return fmt.Errorf("unimplemented")
}
type openShiftClusterDynamicValidator struct {
env env.Interface
subnets subnet.Manager
oc *api.OpenShiftCluster
}
// Dynamic validates an OpenShift cluster
func (v *openShiftClusterValidator) Dynamic(ctx context.Context, oc *api.OpenShiftCluster) error {
v.dv.oc = oc
r, err := azure.ParseResourceID(oc.ID)
if err != nil {
return err
}
// TODO: pre-check that the cluster domain doesn't already exist
spAuthorizer, err := v.dv.validateServicePrincipalProfile()
if err != nil {
return err
}
err = v.dv.validateServicePrincipalRole()
if err != nil {
return err
}
spPermissions := authorization.NewPermissionsClient(r.SubscriptionID, spAuthorizer)
err = v.dv.validateVnetPermissions(ctx, spPermissions, api.CloudErrorCodeInvalidServicePrincipalPermissions, "provided service principal")
if err != nil {
return err
}
fpAuthorizer, err := v.dv.env.FPAuthorizer(oc.Properties.ServicePrincipalProfile.TenantID, azure.PublicCloud.ResourceManagerEndpoint)
if err != nil {
return err
}
fpPermissions := authorization.NewPermissionsClient(r.SubscriptionID, fpAuthorizer)
err = v.dv.validateVnetPermissions(ctx, fpPermissions, api.CloudErrorCodeInvalidResourceProviderPermissions, "resource provider")
if err != nil {
return err
}
v.dv.subnets = subnet.NewManager(r.SubscriptionID, spAuthorizer)
return v.dv.validateSubnets(ctx)
}
func (dv *openShiftClusterDynamicValidator) validateServicePrincipalProfile() (autorest.Authorizer, error) {
spp := &dv.oc.Properties.ServicePrincipalProfile
conf := auth.NewClientCredentialsConfig(spp.ClientID, spp.ClientSecret, spp.TenantID)
token, err := conf.ServicePrincipalToken()
if err != nil {
return nil, err
}
wait.PollImmediate(time.Second, 30*time.Second, func() (done bool, err error) {
err = token.EnsureFresh()
return err == nil, nil
})
if err != nil {
return nil, api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidServicePrincipalCredentials, "properties.servicePrincipalProfile", "The provided service principal credentials are invalid.")
}
return autorest.NewBearerAuthorizer(token), nil
}
func (dv *openShiftClusterDynamicValidator) validateServicePrincipalRole() error {
spp := &dv.oc.Properties.ServicePrincipalProfile
conf := auth.NewClientCredentialsConfig(spp.ClientID, spp.ClientSecret, spp.TenantID)
conf.Resource = azure.PublicCloud.GraphEndpoint
token, err := conf.ServicePrincipalToken()
if err != nil {
return err
}
wait.PollImmediate(time.Second, 30*time.Second, func() (done bool, err error) {
err = token.EnsureFresh()
return err == nil, nil
})
if err != nil {
return err
}
p := &jwt.Parser{}
c := &azureClaim{}
_, _, err = p.ParseUnverified(token.OAuthToken(), c)
if err != nil {
return err
}
for _, role := range c.Roles {
if role == "Application.ReadWrite.OwnedBy" {
return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidServicePrincipalCredentials, "properties.servicePrincipalProfile", "The provided service principal must not have the Application.ReadWrite.OwnedBy permission.")
}
}
return nil
}
func (dv *openShiftClusterDynamicValidator) validateVnetPermissions(ctx context.Context, client authorization.PermissionsClient, code, typ string) error {
vnetID, _, err := subnet.Split(dv.oc.Properties.MasterProfile.SubnetID)
if err != nil {
return err
}
r, err := azure.ParseResourceID(vnetID)
if err != nil {
return err
}
permissions, err := client.ListForResource(ctx, r.ResourceGroup, r.Provider, r.ResourceType, "", r.ResourceName)
if detailedErr, ok := err.(autorest.DetailedError); ok &&
detailedErr.StatusCode == http.StatusNotFound {
return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidLinkedVNet, "", "The vnet '%s' could not be found.", vnetID)
}
if err != nil {
return err
}
for _, action := range []string{
"Microsoft.Network/virtualNetworks/subnets/join/action",
"Microsoft.Network/virtualNetworks/subnets/read",
"Microsoft.Network/virtualNetworks/subnets/write",
} {
ok, err := utilpermissions.CanDoAction(permissions, action)
if err != nil {
return err
}
if !ok {
return api.NewCloudError(http.StatusBadRequest, code, "", "The "+typ+" does not have Contributor permission on vnet '%s'.", vnetID)
}
}
return nil
}
func (dv *openShiftClusterDynamicValidator) validateSubnets(ctx context.Context) error {
master, err := dv.validateSubnet(ctx, "properties.masterProfile.subnetId", "master", dv.oc.Properties.MasterProfile.SubnetID)
if err != nil {
return err
}
worker, err := dv.validateSubnet(ctx, `properties.workerProfiles["worker"].subnetId`, "worker", dv.oc.Properties.WorkerProfiles[0].SubnetID)
if err != nil {
return err
}
_, pod, err := net.ParseCIDR(dv.oc.Properties.NetworkProfile.PodCIDR)
if err != nil {
return err
}
_, service, err := net.ParseCIDR(dv.oc.Properties.NetworkProfile.ServiceCIDR)
if err != nil {
return err
}
err = cidr.VerifyNoOverlap([]*net.IPNet{master, worker, pod, service}, &net.IPNet{IP: net.IPv4zero, Mask: net.IPMask(net.IPv4zero)})
if err != nil {
return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidLinkedVNet, "", "The provided CIDRs must not overlap: '%s'.", err)
}
return nil
}
func (dv *openShiftClusterDynamicValidator) validateSubnet(ctx context.Context, path, typ, subnetID string) (*net.IPNet, error) {
s, err := dv.subnets.Get(ctx, subnetID)
if detailedErr, ok := err.(autorest.DetailedError); ok &&
detailedErr.StatusCode == http.StatusNotFound {
return nil, api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidLinkedVNet, path, "The provided "+typ+" VM subnet '%s' could not be found.", subnetID)
}
if err != nil {
return nil, err
}
if strings.EqualFold(dv.oc.Properties.MasterProfile.SubnetID, subnetID) {
if !strings.EqualFold(*s.PrivateLinkServiceNetworkPolicies, "Disabled") {
return nil, api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidLinkedVNet, path, "The provided "+typ+" VM subnet '%s' is invalid: must have privateLinkServiceNetworkPolicies disabled.", subnetID)
}
}
var found bool
for _, se := range *s.ServiceEndpoints {
if strings.EqualFold(*se.Service, "Microsoft.ContainerRegistry") &&
se.ProvisioningState == mgmtnetwork.Succeeded {
found = true
break
}
}
if !found {
return nil, api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidLinkedVNet, path, "The provided "+typ+" VM subnet '%s' is invalid: must have Microsoft.ContainerRegistry serviceEndpoint.", subnetID)
}
if dv.oc.Properties.ProvisioningState == api.ProvisioningStateCreating {
if s.SubnetPropertiesFormat != nil &&
s.SubnetPropertiesFormat.NetworkSecurityGroup != nil {
return nil, api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidLinkedVNet, path, "The provided "+typ+" VM subnet '%s' is invalid: must not have a network security group attached.", subnetID)
}
} else {
nsgID, err := subnet.NetworkSecurityGroupID(dv.oc, *s.ID)
if err != nil {
return nil, err
}
if s.SubnetPropertiesFormat == nil ||
s.SubnetPropertiesFormat.NetworkSecurityGroup == nil ||
!strings.EqualFold(*s.SubnetPropertiesFormat.NetworkSecurityGroup.ID, nsgID) {
return nil, api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidLinkedVNet, path, "The provided "+typ+" VM subnet '%s' is invalid: must have network security group '%s' attached.", subnetID, nsgID)
}
}
_, net, err := net.ParseCIDR(*s.AddressPrefix)
if err != nil {
return nil, err
}
{
ones, _ := net.Mask.Size()
if ones > 27 {
return nil, api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidLinkedVNet, path, "The provided "+typ+" VM subnet '%s' is invalid: must be /27 or larger.", subnetID)
}
}
return net, nil
}