Skip to content

Conversation

@jjakob
Copy link

@jjakob jjakob commented Feb 9, 2021

This changes the logic for finding a matching file to the same one used
by certificates. This allows wildcard host names to find a matching
config file.

The container prints "Template error: open /etc/nginx/htpasswd: no such
file or directory" if /etc/nginx/htpasswd doesn't exist, but this
doesn't impact functionality. A fix would be to make sure that directory
is always created.

Fixes #1309.

@buchdag buchdag added the type/feat PR for a new feature label Dec 8, 2024
@jjakob
Copy link
Author

jjakob commented Jan 10, 2025

I'd like this functionality to be merged, as I still use it. But I'm not satisfied with using the 'closest' function, as it matches anywhere inside the filename string, it isn't anchored to the end, so e.g. a file 'b.c' will match 'a.b.c.d'.

I'd want something like a 'closestSuffix' function, which would anchor the match at the end of the string, so for example, only 'b.c.d' or 'c.d' will match 'a.b.c.d'.
This would allow setting vhost.d and htpasswd files for VIRTUAL_HOST with an arbitrary number of subdomains.

I think the only way to implement this is by adding a 'closestSuffix' function to docker-gen, but I don't have time to set up a build environment for it. Perhaps someone could implement the function there, and I could update this pull request to use it?

I'm envisioning something like:

# same as arrayClosest but anchored to the end of the string
func arrayClosestSuffix(values []string, input string) string {
        best := ""
        for _, v := range values {
                if strings.HasSuffix(input, v) && len(v) > len(best) {
                        best = v
                }
        }
        return best
}

This could be further improved by checking that the first subdomain is not a partial match, e.g. 'bc.def' should not match 'abc.def' (with the above function, it will), but at first, it's not necesarry.

In the meantime I will rebase to main and update the PR.
@buchdag

@jjakob jjakob force-pushed the vhost-htpasswd-closest-match branch from 01dfb5b to 88e792b Compare January 10, 2025 23:34
@jjakob jjakob marked this pull request as draft January 10, 2025 23:35
@jjakob jjakob force-pushed the vhost-htpasswd-closest-match branch 2 times, most recently from 48b6637 to 5fbec43 Compare January 11, 2025 21:04
Add logic to find the closest matching files for vhost.d, vhost.d
_include and htpasswd files.

This allows multiple hosts to share these files, similar to wildcard
certificate files, but not limited to just 1 subdomain depth.

The match is anchored to the end of the string filename, so a largest
suffix match is found.

For example, 'vhost.d/b.c' will match a 'a.b.c' virtual host.
But 'a.b' will not match 'a.b.c'.
This is stricter than the "closest" match function that certificate
files used in the past, which would also match in the 2nd case.

This is a WIP, unfinished sections are marked with TODO.
@jjakob jjakob force-pushed the vhost-htpasswd-closest-match branch from 5fbec43 to 8adc2eb Compare January 13, 2025 18:53
@buchdag
Copy link
Member

buchdag commented Jan 31, 2025

@jjakob I'm not opposed at all to add a function to docker-gen if this can help with a feature here, I'll look into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type/feat PR for a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Check closest host match when looking for vhost.d/config files before checking default

2 participants