Repository navigation
validate sitenames, validate config during init, fixes #86 - #339
Conversation
rfay
left a comment
There was a problem hiding this comment.
Haven't tested it manually yet, should work out OK, pretty sure the regex is wrong. Regex should get a comment anyway as most people stumble on them.
| var AllowedAppTypes = []string{"drupal7", "drupal8", "wordpress"} | ||
|
|
||
| // Regexp pattern for alphanumeric + dashes | ||
| var re = regexp.MustCompile(`[^a-zA-Z0-9_-]+`) |
There was a problem hiding this comment.
Don't you want '^[a-zA-Z0-9_-]+$' ?
There was a problem hiding this comment.
This doesn't seem to produce the result I'm after: https://play.golang.org/p/X9Q0TrjLLw
There was a problem hiding this comment.
I think I misunderstood the intent - I was giving a validation string, not a replacement string.
rfay
left a comment
There was a problem hiding this comment.
Sorry I didn't take the time to understand this properly, sure you can get Brad to pick it up on Monday. (Trying to cancel my "Request Changes" :)
I jumped the gun on this and didn't actually understand what was being done.
| var AllowedAppTypes = []string{"drupal7", "drupal8", "wordpress"} | ||
|
|
||
| // Regexp pattern to match any non-alphanumeric characters (except dashes) so they can be replaced. | ||
| var re = regexp.MustCompile(`[^a-zA-Z0-9_-]+`) |
There was a problem hiding this comment.
Any reason to not just do a full hostname regex here, since that's our end goal?
It's a lot more wordy, but a full hostname REGEX based on RFC 1123 would be: ^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$ according to https://stackoverflow.com/a/106223
| // Define an application name. | ||
| fmt.Print(namePrompt + ": ") | ||
| c.Name = util.GetInput(c.Name) | ||
| c.Name = re.ReplaceAllString(c.Name, "") |
There was a problem hiding this comment.
We also do this on line 137 - https://github.com/drud/ddev/pull/339/files#diff-1bfb3bdcc7f3b22b48ea905d60583d2dR137
Any reason we are repeating it?
There was a problem hiding this comment.
Requesting changes due to minor feedback issues. I do wonder if this should just be a validation operation instead of trying to strip characters, though.
It may make sense to add a config.Validate() method, which could check the active config for things like this, as well as other potential problems like a missing docroot or unknown application types at start time. Just a thought.
|
I see merit in Brad's comment about validation instead of substitution. Perhaps there is a better middle ground and we can use this as an opportunity to help guide an end-user. If someone tries to use ☃ as a valid UTF-8 character, we simply warn the user "We enforce site names to be alphanumeric with dashes. We detected your input to include non-supported characters. Would you like us to automatically convert them?" Or we can be a little more heavy handed "We automatically converted 'my-☃-site' to 'my--site'". That way we're not roadblocking the user but making them aware of what happened. |
|
Given that config can be manually written I think it does make sense to perform validation on start. I could also see how stripping the sitename could be unexpected to the user, and realistically it's a simple correction to make, and we want them to make it so that the written configuration is valid. I'll move forward on the validation approach as suggested. |
…ig validation to Init
| // validate docroot | ||
| fullPath := filepath.Join(c.AppRoot, c.Docroot) | ||
| if _, err := os.Stat(fullPath); os.IsNotExist(err) { | ||
| return fmt.Errorf("no directory could be found at %s. Please enter a valid docroot in your configuration", fullPath) |
There was a problem hiding this comment.
Absolutely love this. Thanks so much for adding it.
This might fall outside of this PR, but should we also use the app type detection function and issue a warning if a known app type isn't detected/doesn't match configured app type?
Happy to push that one down the road if you would prefer, it's just a check that comes to mind as potentially useful as I look at these others.
There was a problem hiding this comment.
I'm a bit wary of doing this, as it means our detection would need to be more robust than it currently is. The big problem case I think of off-hand is that WordPress allows for a custom core directory, and a site using this structure would fail to detect as a WordPress site.
There was a problem hiding this comment.
My thinking was that it would just be a warning about a potential problem rather than actually prevent the site from starting, but I can accept your answer either way :)
|
@tannerjfco I know you're moving onto the pre/post hook feature #183, but this would probably be a good WIP item to close out. |
beeradb
left a comment
There was a problem hiding this comment.
Code looks good and works as expected.
I do think we should fix tests in master before hitting the merge button, though.
|
So tempted to hit the big green merge button! :) |
The Problem:
#86 OP we currently accept site names allowing any/all characters. This doesn't work out so well for domain names, and attempting to start a site whose sitename contains invalid characters for a domain will result in the site failing to start.
The Fix:
This PR introduces validation of the site name value. Site names will now be validated as hostnames per RFC 1123. If the working directory name does not validate, it will not be suggested to the user. If the user supplies an invalid site name during "ddev config", they will be re-prompted for the site name.
This also introduces a Validate() method for config that runs during App.Init(), which will validate hostname, docroot, and apptype. If any of these values are invalid, an error will be returned to the user instructing them to resolve the invalid configuration.
The Test:
The case in OP should no longer be reproducible:
You can also test config validation on start by manually setting values in config.yaml and running "ddev start:
Automation Overview:
Introduced a test for the new config Validate() method.
Related Issue Link(s):
#86
Release/Deployment notes: