Skip to content

Commit 0313a69

Browse files
committed
Updated AWS dedicated host id validation pattern
1 parent 33dd26d commit 0313a69

File tree

2 files changed

+119
-10
lines changed

2 files changed

+119
-10
lines changed

pkg/webhooks/machine_webhook.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var (
5353
// AWS Variables / Defaults
5454

5555
// awsDedicatedHostNamePattern is used to validate the id of a dedicated host
56-
awsDedicatedHostNamePattern = regexp.MustCompile(`^h-[0-9a-f]{17}$`)
56+
awsDedicatedHostNamePattern = regexp.MustCompile(`^h-([0-9a-f]{8}|[0-9a-f]{17})$`)
5757

5858
// Azure Defaults
5959
defaultAzureVnet = func(clusterID string) string {
@@ -950,7 +950,7 @@ func processAWSPlacementTenancy(placement machinev1beta1.Placement) field.ErrorL
950950
case machinev1beta1.HostAffinityAnyAvailable:
951951
// DedicatedHost is optional. If it is set, make sure it follows conventions
952952
if placement.Host.DedicatedHost != nil && !awsDedicatedHostNamePattern.MatchString(placement.Host.DedicatedHost.ID) {
953-
errs = append(errs, field.Invalid(field.NewPath("spec.placement.host.dedicatedHost.id"), placement.Host.DedicatedHost.ID, "id must start with 'h-' followed by 17 lowercase hexadecimal characters (0-9 and a-f)"))
953+
errs = append(errs, field.Invalid(field.NewPath("spec.placement.host.dedicatedHost.id"), placement.Host.DedicatedHost.ID, "id must start with 'h-' followed by 8 or 17 lowercase hexadecimal characters (0-9 and a-f)"))
954954
}
955955
case machinev1beta1.HostAffinityDedicatedHost:
956956
// We need to make sure DedicatedHost is set with an ID
@@ -959,9 +959,9 @@ func processAWSPlacementTenancy(placement machinev1beta1.Placement) field.ErrorL
959959
} else {
960960
// If not set, return required error. If it does not match pattern, return pattern failure message.
961961
if placement.Host.DedicatedHost.ID == "" {
962-
errs = append(errs, field.Required(field.NewPath("spec.placement.host.dedicatedHost.id"), "id is required and must start with 'h-' followed by 17 lowercase hexadecimal characters (0-9 and a-f)"))
962+
errs = append(errs, field.Required(field.NewPath("spec.placement.host.dedicatedHost.id"), "id is required and must start with 'h-' followed by 8 or 17 lowercase hexadecimal characters (0-9 and a-f)"))
963963
} else if !awsDedicatedHostNamePattern.MatchString(placement.Host.DedicatedHost.ID) {
964-
errs = append(errs, field.Invalid(field.NewPath("spec.placement.host.dedicatedHost.id"), placement.Host.DedicatedHost.ID, "id must start with 'h-' followed by 17 lowercase hexadecimal characters (0-9 and a-f)"))
964+
errs = append(errs, field.Invalid(field.NewPath("spec.placement.host.dedicatedHost.id"), placement.Host.DedicatedHost.ID, "id must start with 'h-' followed by 8 or 17 lowercase hexadecimal characters (0-9 and a-f)"))
965965
}
966966
}
967967
default:

pkg/webhooks/machine_webhook_test.go

Lines changed: 115 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ func TestMachineCreation(t *testing.T) {
376376
},
377377
},
378378
},
379-
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: spec.placement.host.dedicatedHost.id: Required value: id is required and must start with 'h-' followed by 17 lowercase hexadecimal characters (0-9 and a-f)",
379+
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: spec.placement.host.dedicatedHost.id: Required value: id is required and must start with 'h-' followed by 8 or 17 lowercase hexadecimal characters (0-9 and a-f)",
380380
},
381381
{
382382
name: "configure host placement with AnyAvailable affinity and empty ID",
@@ -399,7 +399,7 @@ func TestMachineCreation(t *testing.T) {
399399
},
400400
},
401401
},
402-
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: spec.placement.host.dedicatedHost.id: Required value: id is required and must start with 'h-' followed by 17 lowercase hexadecimal characters (0-9 and a-f)",
402+
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: spec.placement.host.dedicatedHost.id: Required value: id is required and must start with 'h-' followed by 8 or 17 lowercase hexadecimal characters (0-9 and a-f)",
403403
},
404404
{
405405
name: "configure host placement with AnyAvailable affinity and invalid ID",
@@ -422,7 +422,7 @@ func TestMachineCreation(t *testing.T) {
422422
},
423423
},
424424
},
425-
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: spec.placement.host.dedicatedHost.id: Invalid value: \"invalid\": id must start with 'h-' followed by 17 lowercase hexadecimal characters (0-9 and a-f)",
425+
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: spec.placement.host.dedicatedHost.id: Invalid value: \"invalid\": id must start with 'h-' followed by 8 or 17 lowercase hexadecimal characters (0-9 and a-f)",
426426
},
427427
{
428428
name: "configure host placement with invalid affinity",
@@ -467,6 +467,52 @@ func TestMachineCreation(t *testing.T) {
467467
},
468468
expectedError: "",
469469
},
470+
{
471+
name: "configure host placement with DedicatedHost affinity and valid 8-character ID",
472+
platformType: osconfigv1.AWSPlatformType,
473+
clusterID: "aws-cluster",
474+
providerSpecValue: &kruntime.RawExtension{
475+
Object: &machinev1beta1.AWSMachineProviderConfig{
476+
AMI: machinev1beta1.AWSResourceReference{
477+
ID: ptr.To[string]("ami"),
478+
},
479+
InstanceType: "test",
480+
Placement: machinev1beta1.Placement{
481+
Tenancy: machinev1beta1.HostTenancy,
482+
Host: &machinev1beta1.HostPlacement{
483+
Affinity: ptr.To(machinev1beta1.HostAffinityDedicatedHost),
484+
DedicatedHost: &machinev1beta1.DedicatedHost{
485+
ID: "h-12345678",
486+
},
487+
},
488+
},
489+
},
490+
},
491+
expectedError: "",
492+
},
493+
{
494+
name: "configure host placement with AnyAvailable affinity and valid 8-character ID",
495+
platformType: osconfigv1.AWSPlatformType,
496+
clusterID: "aws-cluster",
497+
providerSpecValue: &kruntime.RawExtension{
498+
Object: &machinev1beta1.AWSMachineProviderConfig{
499+
AMI: machinev1beta1.AWSResourceReference{
500+
ID: ptr.To[string]("ami"),
501+
},
502+
InstanceType: "test",
503+
Placement: machinev1beta1.Placement{
504+
Tenancy: machinev1beta1.HostTenancy,
505+
Host: &machinev1beta1.HostPlacement{
506+
Affinity: ptr.To(machinev1beta1.HostAffinityAnyAvailable),
507+
DedicatedHost: &machinev1beta1.DedicatedHost{
508+
ID: "h-12345678",
509+
},
510+
},
511+
},
512+
},
513+
},
514+
expectedError: "",
515+
},
470516
{
471517
name: "configure host placement with DedicatedHost affinity and empty ID",
472518
platformType: osconfigv1.AWSPlatformType,
@@ -486,7 +532,7 @@ func TestMachineCreation(t *testing.T) {
486532
},
487533
},
488534
},
489-
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: spec.placement.host.dedicatedHost.id: Required value: id is required and must start with 'h-' followed by 17 lowercase hexadecimal characters (0-9 and a-f)",
535+
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: spec.placement.host.dedicatedHost.id: Required value: id is required and must start with 'h-' followed by 8 or 17 lowercase hexadecimal characters (0-9 and a-f)",
490536
},
491537
{
492538
name: "configure host placement with DedicatedHost affinity and ID not set",
@@ -505,7 +551,7 @@ func TestMachineCreation(t *testing.T) {
505551
},
506552
},
507553
},
508-
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: spec.placement.host.dedicatedHost.id: Required value: id is required and must start with 'h-' followed by 17 lowercase hexadecimal characters (0-9 and a-f)",
554+
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: spec.placement.host.dedicatedHost.id: Required value: id is required and must start with 'h-' followed by 8 or 17 lowercase hexadecimal characters (0-9 and a-f)",
509555
},
510556
{
511557
name: "configure host placement with DedicatedHost affinity and invalid ID",
@@ -526,7 +572,70 @@ func TestMachineCreation(t *testing.T) {
526572
},
527573
},
528574
},
529-
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: spec.placement.host.dedicatedHost.id: Invalid value: \"invalid\": id must start with 'h-' followed by 17 lowercase hexadecimal characters (0-9 and a-f)",
575+
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: spec.placement.host.dedicatedHost.id: Invalid value: \"invalid\": id must start with 'h-' followed by 8 or 17 lowercase hexadecimal characters (0-9 and a-f)",
576+
},
577+
{
578+
name: "configure host placement with DedicatedHost affinity and 9-character ID (invalid length)",
579+
platformType: osconfigv1.AWSPlatformType,
580+
clusterID: "aws-cluster",
581+
providerSpecValue: &kruntime.RawExtension{
582+
Object: &machinev1beta1.AWSMachineProviderConfig{
583+
AMI: machinev1beta1.AWSResourceReference{ID: ptr.To[string]("ami")},
584+
InstanceType: "test",
585+
Placement: machinev1beta1.Placement{
586+
Tenancy: machinev1beta1.HostTenancy,
587+
Host: &machinev1beta1.HostPlacement{
588+
Affinity: ptr.To(machinev1beta1.HostAffinityDedicatedHost),
589+
DedicatedHost: &machinev1beta1.DedicatedHost{
590+
ID: "h-123456789",
591+
},
592+
},
593+
},
594+
},
595+
},
596+
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: spec.placement.host.dedicatedHost.id: Invalid value: \"h-123456789\": id must start with 'h-' followed by 8 or 17 lowercase hexadecimal characters (0-9 and a-f)",
597+
},
598+
{
599+
name: "configure host placement with DedicatedHost affinity and 16-character ID (invalid length)",
600+
platformType: osconfigv1.AWSPlatformType,
601+
clusterID: "aws-cluster",
602+
providerSpecValue: &kruntime.RawExtension{
603+
Object: &machinev1beta1.AWSMachineProviderConfig{
604+
AMI: machinev1beta1.AWSResourceReference{ID: ptr.To[string]("ami")},
605+
InstanceType: "test",
606+
Placement: machinev1beta1.Placement{
607+
Tenancy: machinev1beta1.HostTenancy,
608+
Host: &machinev1beta1.HostPlacement{
609+
Affinity: ptr.To(machinev1beta1.HostAffinityDedicatedHost),
610+
DedicatedHost: &machinev1beta1.DedicatedHost{
611+
ID: "h-1234567890abcdef",
612+
},
613+
},
614+
},
615+
},
616+
},
617+
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: spec.placement.host.dedicatedHost.id: Invalid value: \"h-1234567890abcdef\": id must start with 'h-' followed by 8 or 17 lowercase hexadecimal characters (0-9 and a-f)",
618+
},
619+
{
620+
name: "configure host placement with DedicatedHost affinity and 8-character ID with uppercase (invalid)",
621+
platformType: osconfigv1.AWSPlatformType,
622+
clusterID: "aws-cluster",
623+
providerSpecValue: &kruntime.RawExtension{
624+
Object: &machinev1beta1.AWSMachineProviderConfig{
625+
AMI: machinev1beta1.AWSResourceReference{ID: ptr.To[string]("ami")},
626+
InstanceType: "test",
627+
Placement: machinev1beta1.Placement{
628+
Tenancy: machinev1beta1.HostTenancy,
629+
Host: &machinev1beta1.HostPlacement{
630+
Affinity: ptr.To(machinev1beta1.HostAffinityDedicatedHost),
631+
DedicatedHost: &machinev1beta1.DedicatedHost{
632+
ID: "h-1234ABCD",
633+
},
634+
},
635+
},
636+
},
637+
},
638+
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: spec.placement.host.dedicatedHost.id: Invalid value: \"h-1234ABCD\": id must start with 'h-' followed by 8 or 17 lowercase hexadecimal characters (0-9 and a-f)",
530639
},
531640
{
532641
name: "configure dedicated tenancy with host placement",

0 commit comments

Comments
 (0)