diff options
author | Vladimir Shushlin <v.shushlin@gmail.com> | 2019-03-28 15:22:54 +0300 |
---|---|---|
committer | Vladimir Shushlin <v.shushlin@gmail.com> | 2019-03-28 16:10:24 +0300 |
commit | 95d4077f3bd840a7e56e6b85e0bbe0b00ed57b44 (patch) | |
tree | 14ef84276b9ae0298b3e2710db9bc2f18adecdad | |
parent | bb2af7ea515177b56c88c57ed03c2a76b4f74876 (diff) |
Use artifacts server as host for acme challengestc-le-redir
-rw-r--r-- | internal/domain/domain.go | 26 | ||||
-rw-r--r-- | internal/domain/domain_test.go | 2 |
2 files changed, 24 insertions, 4 deletions
diff --git a/internal/domain/domain.go b/internal/domain/domain.go index 3592be14..b245da20 100644 --- a/internal/domain/domain.go +++ b/internal/domain/domain.go @@ -8,6 +8,7 @@ import ( "mime" "net" "net/http" + "net/url" "os" "path/filepath" "strconv" @@ -129,6 +130,26 @@ func (d *D) isAcmeChallenge(path string) bool { return strings.HasPrefix(path, "/.well-known/acme-challenge/") } +// This should be moved to additional config param +func (d *D) gitlabServer() string { + url, err := url.Parse(d.appConfig.ArtifactsServer) + if err != nil { + return "" + } + host, _, _ := net.SplitHostPort(url.Host) + return host +} + +func (d *D) redirectForAcmeChallenge(w http.ResponseWriter, r *http.Request) bool { + log.Debug("Get request for acme-challenge, redirecting to gitlab instance") + + host := getHost(r) + redirectPath := "//" + d.gitlabServer() + "/-/acme-challenge/" + host + "/" + filepath.Base(r.URL.Path) + http.Redirect(w, r, redirectPath, 302) + return true + +} + func setContentType(w http.ResponseWriter, fullPath string) { ext := filepath.Ext(fullPath) ctype := mime.TypeByExtension(ext) @@ -481,10 +502,7 @@ func (d *D) serveFileFromConfig(w http.ResponseWriter, r *http.Request) bool { } if err != nil && d.isAcmeChallenge(r.URL.Path) { - log.Debug("Get request for acme-challenge, redirecting to gitlab instance") - redirectPath := "//gitlab.com/-/acme-challenge/" + r.Host + "/" + filepath.Base(r.URL.Path) - http.Redirect(w, r, redirectPath, 302) - return true + d.redirectForAcmeChallenge(w, r) } return false diff --git a/internal/domain/domain_test.go b/internal/domain/domain_test.go index a6e3fa70..0269e3e5 100644 --- a/internal/domain/domain_test.go +++ b/internal/domain/domain_test.go @@ -14,6 +14,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitlab-pages/internal/config" "gitlab.com/gitlab-org/gitlab-pages/internal/fixture" ) @@ -491,6 +492,7 @@ func TestAcmeChallengeRedirect(t *testing.T) { config: &domainConfig{ Domain: "test.example.com", }, + appConfig: &config.Config{ArtifactsServer: "example.com"}, } testHTTP404(t, serveFileOrNotFound(testGroup), "GET", "http://group.test.io/project2/.well-known/acme-challenge/0123456789abcdef", nil, "The page you're looking for could not be found") |