-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow running Atlantis behind a path-based proxy #357
Conversation
Resolves #213 Allows users to run Atlantis behind a shared reverse proxy, which is a fairly common use case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jml I've added a bunch of comments to the changes I made to your code. Thought this would be faster than commenting on each one but still lets you know why I wanted certain changes made.
@@ -74,7 +74,7 @@ const redTermEnd = "\033[39m" | |||
var stringFlags = []stringFlag{ | |||
{ | |||
name: AtlantisURLFlag, | |||
description: "URL that Atlantis can be reached at. Defaults to http://$(hostname):$port where $port is from --" + PortFlag + ".", | |||
description: "URL that Atlantis can be reached at. Defaults to http://$(hostname):$port where $port is from --" + PortFlag + ". Supports a base path ex. https://example.com/basepath.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a .
at end to match rest of help output.
@@ -16,6 +16,7 @@ import ( | |||
// LocksController handles all requests relating to Atlantis locks. | |||
type LocksController struct { | |||
AtlantisVersion string | |||
AtlantisURL *url.URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing around the reference because that's what's returned from url.Parse
and would rather trust that they return a reference for a reason.
@@ -57,8 +58,12 @@ func (l *LocksController) GetLock(w http.ResponseWriter, r *http.Request) { | |||
LockedBy: lock.Pull.Author, | |||
Workspace: lock.Workspace, | |||
AtlantisVersion: l.AtlantisVersion, | |||
CleanedBasePath: l.AtlantisURL.Path, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed this from AtlantisURL and am only passing the path around because think it's a best practice to only use paths in html so that it doesn't necessarily have to be served from the original domain.
queryParam := "queryparam" | ||
routeName := "routename" | ||
atlantisURL := "https://example.com" | ||
cases := []struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved all your tests into a single case. No real reason other than it was more concise. The way you had it was fine too.
@@ -229,9 +231,14 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) { | |||
projectLocker := &events.DefaultProjectLocker{ | |||
Locker: lockingClient, | |||
} | |||
parsedURL, err := ParseAtlantisURL(userConfig.AtlantisURL) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved the call to url.Parse
inside this function so that all the validation happened in the same place
// and we can use it in our templates. | ||
// It removes any trailing slashes from the path so we can concatenate it | ||
// with other paths without checking. | ||
func ParseAtlantisURL(u string) (*url.URL, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kept this in server.go
because it's small and I didn't feel it warranted its own file. Just personal preference here.
if err != nil { | ||
return nil, err | ||
} | ||
if !(parsed.Scheme == "http" || parsed.Scheme == "https") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The call to .IsAbs()
returns true if the scheme isn't empty so we didn't need that since we're already checking the scheme here.
@@ -116,6 +122,80 @@ func TestHealthz(t *testing.T) { | |||
}`, string(body)) | |||
} | |||
|
|||
func TestParseAtlantisURL(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, here is where I consolidated your tests I mean
@@ -82,8 +83,9 @@ var indexTemplate = template.Must(template.New("index.html.tmpl").Parse(` | |||
<section> | |||
<p class="title-heading small"><strong>Locks</strong></p> | |||
{{ if .Locks }} | |||
{{ $basePath := .CleanedBasePath }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you were missing this from your PR which caused the template to not render if there were any locks. You can't reference .CleanedBasePath
from inside the range
because .
is redefined to be the element in the range. Thus you need to define a variable reference to it before entering the range.
Codecov Report
@@ Coverage Diff @@
## master #357 +/- ##
==========================================
+ Coverage 70.67% 70.72% +0.05%
==========================================
Files 61 61
Lines 3655 3676 +21
==========================================
+ Hits 2583 2600 +17
- Misses 893 895 +2
- Partials 179 181 +2
Continue to review full report at Codecov.
|
Thanks! |
- Refactoring work from #314 - Use just the path in templates, not the fully qualified URL since that is a best practice. - Add logging to errors when rendering templates. - Silence help output on cli errors since the help output is now so large that you have to scroll up to see the errors.
Allows users to run Atlantis behind a path-based proxy (ex. https://proxy.com/basepath). This change will cause the Atlantis UI to add
/basepath
to all its URLs.To use, run
atlantis server --atlantis-url https://proxy.com/basepath
.Notes