Skip to content

Commit db93854

Browse files
authored
Merge pull request #621 from alexeldeib/ace/hook
🐛 fix webhooks
2 parents 4b04993 + 9faa72d commit db93854

11 files changed

+32
-18
lines changed

api/v1alpha3/azurecluster_validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func validateNetworkSpec(networkSpec NetworkSpec, fldPath *field.Path) field.Err
7474

7575
// validateResourceGroup validates a ResourceGroup
7676
func validateResourceGroup(resourceGroup string, fldPath *field.Path) *field.Error {
77-
if success, _ := regexp.Match(resourceGroupRegex, []byte(resourceGroup)); !success {
77+
if success, _ := regexp.MatchString(resourceGroupRegex, resourceGroup); !success {
7878
return field.Invalid(fldPath, resourceGroup,
7979
fmt.Sprintf("resourceGroup doesn't match regex %s", resourceGroupRegex))
8080
}

api/v1alpha3/azurecluster_webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (c *AzureCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
3232
Complete()
3333
}
3434

35-
// +kubebuilder:webhook:verbs=create;update;delete,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha3-azurecluster,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=azurecluster,versions=v1alpha3,name=validation.azurecluster.infrastructure.cluster.x-k8s.io,sideEffects=None
35+
// +kubebuilder:webhook:verbs=create;update;delete,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha3-azurecluster,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=azureclusters,versions=v1alpha3,name=validation.azurecluster.infrastructure.cluster.x-k8s.io,sideEffects=None
3636

3737
var _ webhook.Validator = &AzureCluster{}
3838

api/v1alpha3/azuremachine_default.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package v1alpha3
1919
import (
2020
"crypto/rand"
2121
"crypto/rsa"
22+
"encoding/base64"
2223

2324
"github.com/pkg/errors"
2425
"golang.org/x/crypto/ssh"
@@ -37,7 +38,7 @@ func (m *AzureMachine) SetDefaultSSHPublicKey() error {
3738
if perr != nil {
3839
return errors.Wrap(perr, "Failed to generate public key")
3940
}
40-
m.Spec.SSHPublicKey = string(ssh.MarshalAuthorizedKey(publicRsaKey))
41+
m.Spec.SSHPublicKey = base64.StdEncoding.EncodeToString(ssh.MarshalAuthorizedKey(publicRsaKey))
4142
}
4243

4344
return nil

api/v1alpha3/azuremachine_validation.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package v1alpha3
1818

1919
import (
20+
"encoding/base64"
21+
2022
"golang.org/x/crypto/ssh"
2123
"k8s.io/apimachinery/pkg/util/validation/field"
2224
)
@@ -25,7 +27,13 @@ import (
2527
func ValidateSSHKey(sshKey string, fldPath *field.Path) field.ErrorList {
2628
allErrs := field.ErrorList{}
2729

28-
if _, _, _, _, err := ssh.ParseAuthorizedKey([]byte(sshKey)); err != nil {
30+
decoded, err := base64.StdEncoding.DecodeString(sshKey)
31+
if err != nil {
32+
allErrs = append(allErrs, field.Required(fldPath, "the SSH public key is not properly base64 encoded"))
33+
return allErrs
34+
}
35+
36+
if _, _, _, _, err := ssh.ParseAuthorizedKey(decoded); err != nil {
2937
allErrs = append(allErrs, field.Required(fldPath, "the SSH public key is not valid"))
3038
return allErrs
3139
}

api/v1alpha3/azuremachine_validation_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package v1alpha3
1919
import (
2020
"crypto/rand"
2121
"crypto/rsa"
22+
"encoding/base64"
2223
"testing"
2324

2425
. "github.com/onsi/gomega"
@@ -61,5 +62,5 @@ func TestAzureMachine_ValidateSSHKey(t *testing.T) {
6162
func generateSSHPublicKey() string {
6263
privateKey, _ := rsa.GenerateKey(rand.Reader, 2048)
6364
publicRsaKey, _ := ssh.NewPublicKey(&privateKey.PublicKey)
64-
return string(ssh.MarshalAuthorizedKey(publicRsaKey))
65+
return base64.StdEncoding.EncodeToString(ssh.MarshalAuthorizedKey(publicRsaKey))
6566
}

api/v1alpha3/azuremachine_webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (m *AzureMachine) SetupWebhookWithManager(mgr ctrl.Manager) error {
3535
Complete()
3636
}
3737

38-
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha3-azuremachine,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=azuremachine,versions=v1alpha3,name=validation.azuremachine.infrastructure.cluster.x-k8s.io,sideEffects=None
38+
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha3-azuremachine,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=azuremachines,versions=v1alpha3,name=validation.azuremachine.infrastructure.cluster.x-k8s.io,sideEffects=None
3939

4040
var _ webhook.Validator = &AzureMachine{}
4141

config/webhook/manifests.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ webhooks:
5353
- UPDATE
5454
- DELETE
5555
resources:
56-
- azurecluster
56+
- azureclusters
5757
sideEffects: None
5858
- clientConfig:
5959
caBundle: Cg==
@@ -73,7 +73,7 @@ webhooks:
7373
- CREATE
7474
- UPDATE
7575
resources:
76-
- azuremachine
76+
- azuremachines
7777
sideEffects: None
7878
- clientConfig:
7979
caBundle: Cg==

config/webhook/webhookcainjection_patch.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
# This patch add annotation to admission webhook config and
33
# the variables $(CERTIFICATE_NAMESPACE) and $(CERTIFICATE_NAME) will be substituted by kustomize.
44
# uncomment the following lines to enable mutating and validating webhook
5-
#apiVersion: admissionregistration.k8s.io/v1beta1
6-
#kind: MutatingWebhookConfiguration
7-
#metadata:
8-
# name: mutating-webhook-configuration
9-
# annotations:
10-
# cert-manager.k8s.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME)
5+
apiVersion: admissionregistration.k8s.io/v1beta1
6+
kind: MutatingWebhookConfiguration
7+
metadata:
8+
name: mutating-webhook-configuration
9+
annotations:
10+
cert-manager.k8s.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME)
1111
---
1212
apiVersion: admissionregistration.k8s.io/v1beta1
1313
kind: ValidatingWebhookConfiguration

test/e2e/azure_suite_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ var _ = BeforeSuite(func() {
128128
framework.InstallComponents(ctx, mgmt, capi, cabpk, kcp, infra)
129129
framework.WaitForPodsReadyInNamespace(ctx, mgmt, "capi-system")
130130
framework.WaitForPodsReadyInNamespace(ctx, mgmt, "capz-system")
131+
framework.WaitForPodsReadyInNamespace(ctx, mgmt, "capi-webhook-system")
131132

132133
// go func() {
133134
// defer GinkgoRecover()
@@ -143,6 +144,7 @@ var _ = AfterSuite(func() {
143144
// DO NOT stream "capi-controller-manager" logs as it prints out azure.json
144145
Expect(writeLogs(mgmt, "capi-kubeadm-bootstrap-system", "capi-kubeadm-bootstrap-controller-manager", logPath)).To(Succeed())
145146
Expect(writeLogs(mgmt, "capz-system", "capz-controller-manager", logPath)).To(Succeed())
147+
Expect(writeLogs(mgmt, "capi-webhook-system", "capz-controller-manager", logPath)).To(Succeed())
146148
By("Tearing down management cluster")
147149
mgmt.Teardown(ctx)
148150
})

test/e2e/azure_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ var _ = Describe("CAPZ e2e tests", func() {
5151
nodeGen = &NodeGenerator{}
5252
clusterGen.VariablesInit()
5353
machineDeploymentGen = &MachineDeploymentGenerator{}
54-
cluster, infraCluster = clusterGen.GenerateCluster(namespace)
5554
})
5655

5756
AfterEach(func() {
@@ -61,6 +60,7 @@ var _ = Describe("CAPZ e2e tests", func() {
6160

6261
Context("Create single controlplane cluster", func() {
6362
It("Should create a single node cluster", func() {
63+
cluster, infraCluster = clusterGen.GenerateCluster(creds.SubscriptionID, namespace)
6464
controlplane := nodeGen.GenerateKubeadmControlplane(creds, cluster.GetName(), 1)
6565
machineTemplate := nodeGen.GenerateMachineTemplate(creds, cluster.GetName())
6666
input = &ControlPlaneClusterInput{
@@ -77,6 +77,7 @@ var _ = Describe("CAPZ e2e tests", func() {
7777

7878
Context("Create multiple controlplane cluster with machine deployments", func() {
7979
It("Should create a 3 node cluster", func() {
80+
cluster, infraCluster = clusterGen.GenerateCluster(creds.SubscriptionID, namespace)
8081
controlplane := nodeGen.GenerateKubeadmControlplane(creds, cluster.GetName(), 3)
8182
machineTemplate := nodeGen.GenerateMachineTemplate(creds, cluster.GetName())
8283
machineDeployment := machineDeploymentGen.Generate(creds, cluster.GetNamespace(), cluster.GetName(), 1)

0 commit comments

Comments
 (0)