Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom resource build prefix #218

Merged
merged 4 commits into from
Aug 12, 2022

Conversation

Swampen
Copy link
Contributor

@Swampen Swampen commented Jul 28, 2022

Background

I discovered a few issues in the validation of the custom_resource_build_prefix parameter.

  1. Validation of the length allowed 11 characters, and not the documented 10
  2. Validation allowed non-valid characters like _, . and ) for Azure resources
    • pkr_123456 is not a valid prefix name for a VM (because of the underscore) as stated by Microsoft's documentation:

    Can't use spaces, control characters, or these characters:
    ~ ! @ # $ % ^ & * ( ) = + _ [ ] { } \ | ; : . ' " , < > / ?
    Windows VMs can't include period or end with hyphen.
    Linux VMs can't end with period or hyphen.

  3. Calling isValidAzureName would not allow the prefix to end with a - witch should be allowed according to the documentation
    • custom_resource_build_prefix + resourcetype + 5 character random alphanumeric string

This PR address the issues I described above as well as adding tests for custom_resource_build_prefix

@Swampen Swampen requested a review from a team as a code owner July 28, 2022 14:14
@hashicorp-cla
Copy link

hashicorp-cla commented Jul 28, 2022

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@lbajolet-hashicorp lbajolet-hashicorp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few comments on that, but overall the code looks good to me, once addressed we can merge that.

Thanks for the contribution @Swampen, much appreciated

@@ -1265,8 +1264,8 @@ func assertManagedImageDataDiskSnapshotName(name, setting string) (bool, error)
}

func assertResourceNamePrefix(name, setting string) (bool, error) {
if !isValidAzureName(reResourceNamePrefix, name) {
return false, fmt.Errorf("The setting %s must only contain characters from a-z, A-Z, 0-9 and _ and the maximum length is 10 characters", setting)
if !reResourceNamePrefix.Match([]byte(name)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively here you could use MatchString, that wouldn't require converting the name to a byte array

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch :) Changed

@@ -65,7 +64,7 @@ var (
reResourceGroupName = regexp.MustCompile(validResourceGroupNameRe)
reSnapshotName = regexp.MustCompile(`^[A-Za-z0-9_]{1,79}$`)
reSnapshotPrefix = regexp.MustCompile(`^[A-Za-z0-9_]{1,59}$`)
reResourceNamePrefix = regexp.MustCompile(validResourceNamePrefix)
reResourceNamePrefix = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9-]{1,9}$`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason why we reject names starting with -? From what I see in the docs it seems to be supported, unless I'm wrong, if so do correct me.
If that's so though, we can change the regex to [A-Za-z0-9-]{1,10}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Come to think of it, if we want a one-character prefix, we can't accept it with your regex since it introduces a minimum of 2 characters, maybe the second part should be {0,9} if we maintain the logic behind validation

Copy link
Contributor Author

@Swampen Swampen Jul 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason why we reject names starting with -? From what I see in the docs it seems to be supported, unless I'm wrong, if so do correct me. If that's so though, we can change the regex to [A-Za-z0-9-]{1,10}

I couldn't find anything in the documentation either, but when I try to create a VM starting with -, I get an error. So it's probably a parent rule that decides that:
image

Come to think of it, if we want a one-character prefix, we can't accept it with your regex since it introduces a minimum of 2 characters, maybe the second part should be {0,9} if we maintain the logic behind validation

Yeah, that's a oversight from my part. The intended regex would be ^[A-Za-z0-9][A-Za-z0-9-]{0,9}$. Is that okay?

Copy link
Contributor

@lbajolet-hashicorp lbajolet-hashicorp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Swampen!

Sorry for letting that one slide, I completely miss your reroll, sorry for that.
The new additions look good to me, we'll merge that since tests are passing.

Thanks for your contribution, much appreciated.

@lbajolet-hashicorp lbajolet-hashicorp merged commit 2b1d0d2 into hashicorp:main Aug 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants