Skip to content

Commit

Permalink
fix: handle wildcard hostnames when name resolution not available, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rfay committed Mar 27, 2024
1 parent 18e53dc commit 1daf784
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
4 changes: 1 addition & 3 deletions cmd/ddev/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ func init() {

// Populate custom/script commands so they're visible.
// We really don't want ~/.ddev or .ddev/homeadditions or .ddev/.globalcommands to have root ownership, breaks things.
if os.Geteuid() == 0 {
util.Warning("Not populating custom commands or hostadditions because running with root privileges")
} else {
if os.Geteuid() != 0 {
err := ddevapp.PopulateExamplesCommandsHomeadditions("")
if err != nil {
util.Warning("populateExamplesAndCommands() failed: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/ddevapp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ func (app *DdevApp) ValidateConfig() error {
// If they have provided "*.<hostname>" then ignore the *. part.
hn = strings.TrimPrefix(hn, "*.")
if hn == nodeps.DdevDefaultTLD {
return fmt.Errorf("wildcarding the full hostname or using 'ddev.site' as FQDN for the project %s is not allowed because other projects would not work in that case", app.Name)
return fmt.Errorf("wildcarding the full hostname\nor using 'ddev.site' as FQDN for the project %s is not allowed\nbecause other projects would not work in that case", app.Name)
}
if !hostRegex.MatchString(hn) {
return fmt.Errorf("the %s project has an invalid hostname: '%s', see https://en.wikipedia.org/wiki/Hostname#Syntax for valid hostname requirements", app.Name, hn).(invalidHostname)
Expand Down
19 changes: 11 additions & 8 deletions pkg/ddevapp/hostname_mgt.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (app *DdevApp) AddHostsEntriesIfNeeded() error {

// If we're able to resolve the hostname via DNS or otherwise we
// don't have to worry about this. This will allow resolution
// of *.ddev.site for example
// of <whatever>.ddev.site for example
if app.UseDNSWhenPossible && globalconfig.IsInternetActive() {
// If they have provided "*.<name>" then look up the suffix
checkName := strings.TrimPrefix(name, "*.")
Expand All @@ -110,13 +110,16 @@ func (app *DdevApp) AddHostsEntriesIfNeeded() error {
util.Warning("Unable to open hosts file: %v", err)
continue
}
util.Warning("The hostname %s is not currently resolvable, trying to add it to the hosts file", name)

out, err := escalateToAddHostEntry(name, dockerIP)
if err != nil {
return err
if strings.Contains(name, `*`) {
util.Warning("DDEV cannot add unresolvable wildcard hostnames like `%s` to your hosts file", name)
} else {
util.Warning("The hostname %s is not currently resolvable, trying to add it to the hosts file", name)
out, err := escalateToAddHostEntry(name, dockerIP)
if err != nil {
return err
}
util.Success(out)
}
util.Success(out)
}

return nil
Expand Down Expand Up @@ -248,7 +251,7 @@ func runCommandWithSudo(args []string) (out string, err error) {
c = []string{"sudo.exe"}
}
c = append(c, args...)
output.UserOut.Printf("DDEV needs to run with administrative privileges.\nYou may be required to enter your password for sudo or allow escalation. DDEV is about to issue the command:\n %s\n", strings.Join(c, ` `))
output.UserOut.Printf("DDEV needs to run with administrative privileges.\nThis is normally to add unresolvable hostnames to the hosts file.\nYou may be required to enter your password for sudo or allow escalation.\nDDEV is about to issue the command:\n %s\n", strings.Join(c, ` `))

out, err = exec.RunHostCommand(c[0], c[1:]...)
return out, err
Expand Down

0 comments on commit 1daf784

Please sign in to comment.