-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prefix URLs in templates with AtlantisURL
Resolves #213 Allows users to run Atlantis behind a shared reverse proxy, which is a fairly common use case.
- Loading branch information
Showing
9 changed files
with
132 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
// NormalizeBaseURL ensures the given URL is a valid base URL for Atlantis. | ||
// | ||
// URLs that are fundamentally invalid (e.g. "hi") will return an error. | ||
// Otherwise, the returned URL will have no trailing slashes and be guaranteed | ||
// to be suitable for use as a base URL. | ||
func NormalizeBaseURL(u *url.URL) (*url.URL, error) { | ||
if !u.IsAbs() { | ||
return nil, fmt.Errorf("Base URLs must be absolute.") | ||
} | ||
if !(u.Scheme == "http" || u.Scheme == "https") { | ||
return nil, fmt.Errorf("Base URLs must be HTTP or HTTPS.") | ||
} | ||
out := *u | ||
out.Path = strings.TrimRight(out.Path, "/") | ||
return &out, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package server_test | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/runatlantis/atlantis/server" | ||
. "github.com/runatlantis/atlantis/testing" | ||
) | ||
|
||
func TestNormalizeBaseURL_Valid(t *testing.T) { | ||
t.Log("When given a valid base URL, NormalizeBaseURL returns such URLs unchanged.") | ||
examples := []string{ | ||
"https://example.com", | ||
"https://example.com/some/path", | ||
"http://example.com:8080", | ||
} | ||
for _, example := range examples { | ||
url, err := url.Parse(example) | ||
Ok(t, err) | ||
normalized, err := server.NormalizeBaseURL(url) | ||
Ok(t, err) | ||
Equals(t, url, normalized) | ||
} | ||
} | ||
|
||
func TestNormalizeBaseURL_Relative(t *testing.T) { | ||
t.Log("We do not allow relative URLs as base URLs.") | ||
_, err := server.NormalizeBaseURL(&url.URL{Path: "hi"}) | ||
Assert(t, err != nil, "should be an error") | ||
Equals(t, "Base URLs must be absolute.", err.Error()) | ||
} | ||
|
||
func TestNormalizeBaseURL_NonHTTP(t *testing.T) { | ||
t.Log("Base URLs must be http or https.") | ||
_, err := server.NormalizeBaseURL(&url.URL{Scheme: "ftp", Host: "example", Path: "hi"}) | ||
Assert(t, err != nil, "should be an error") | ||
Equals(t, "Base URLs must be HTTP or HTTPS.", err.Error()) | ||
} | ||
|
||
func TestNormalizeBaseURL_TrailingSlashes(t *testing.T) { | ||
t.Log("We strip off any trailing slashes from the base URL.") | ||
examples := []struct { | ||
input string | ||
output string | ||
}{ | ||
{"https://example.com/", "https://example.com"}, | ||
{"https://example.com/some/path/", "https://example.com/some/path"}, | ||
{"http://example.com:8080/", "http://example.com:8080"}, | ||
{"https://example.com//", "https://example.com"}, | ||
{"https://example.com/path///", "https://example.com/path"}, | ||
} | ||
for _, example := range examples { | ||
inputURL, err := url.Parse(example.input) | ||
Ok(t, err) | ||
outputURL, err := url.Parse(example.output) | ||
Ok(t, err) | ||
normalized, err := server.NormalizeBaseURL(inputURL) | ||
Ok(t, err) | ||
Equals(t, outputURL, normalized) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters