-
Notifications
You must be signed in to change notification settings - Fork 15
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
Issue #179 Refactor and test handleJenkinsUIRequest() #198
Conversation
So that for now is just a variable rename? |
@hferentschik |
1eba87e
to
d60a2b3
Compare
internal/proxy/proxy.go
Outdated
if tj, ok := r.URL.Query()["token_json"]; ok { //If there is token_json in query, process it, find user info and login to Jenkins | ||
if len(tj) < 1 { | ||
p.HandleError(w, errors.New("could not read JWT token from URL"), requestLogEntry) | ||
namespace, noProxy = p.processTokenJSON(responseWriter, request, requestLogEntry, redirectURL) |
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.
Ok, now I get it. It is a start, but I don't think there is a need for returning anything here. Especially look at p.checkCookies.
I guess as a first step this works, but the next is to eliminate the returns. Thale the last if statement for example if needsAuth
. This could be pulled out into a function which then can get called.
Also, we tried to remove most of the used naked returns in the code. In most places we have done it already, but the code in proxy.go
uses still a lot of them. The use of naked returns is really discouraged and should if ever be only used for very small methods. IMO in the current code it just makes it hard to understand. Let's get rid of them.
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.
That said, I think we need to keep some conditional there. Pulling the conditionals into the methods obfuscates the code. Now it looks like, you are always processing the JSON token and you are always proecssing the cookies. There are several phases here which are not obvious anymore.
Leaving the outer if statements here will makes things clearer
internal/proxy/proxy.go
Outdated
func (p *Proxy) processTokenJSON(responseWriter http.ResponseWriter, request *http.Request, requestLogEntry *log.Entry, redirectURL *url.URL) (namespace string, noProxy bool) { | ||
|
||
// Checks if token_json is present in the query | ||
if tokenJSON, ok := request.URL.Query()["token_json"]; ok { |
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.
It might make sense to extract this into something like func isAuthenticatedRequest(request *http.Request) bool
. If you then pull up the if statement again, the method name documents the alogrithm.
func (p *Proxy) handleJenkinsUIRequest(responseWriter http.ResponseWriter, request *http.Request, requestLogEntry *log.Entry) (cacheKey string, namespace string, noProxy bool) {
if isAuthenticatedRequest(request) {
namespace, noProxy = p.processAuthenticatedRequest(responseWriter, request, requestLogEntry, redirectURL)
} else {
return redirectToAuth(...)
}
if isWaitLoopRequest(request) {
cacheKey, namespace, noProxy, needsAuth = p.checkCookies(responseWriter, request, requestLogEntry)
}
}
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.
Not sure which code segment could be kept in isWaitLoopRequest(request)
internal/proxy/proxy.go
Outdated
//redirect determines if we need to redirect to auth service | ||
needsAuth := true | ||
noProxy = true | ||
|
||
//redirectURL is used for auth service as a target of successful auth redirect | ||
redirectURL, err := url.ParseRequestURI(fmt.Sprintf("%s%s", strings.TrimRight(p.redirect, "/"), r.URL.Path)) | ||
// redirectURL is used for auth service as a target of successful auth redirect |
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 don't think we need to do that here. We can do that where it is needed. And most likely it can also happen in a dedicated properly named function
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.
internal/proxy/proxy.go
Outdated
return | ||
} | ||
|
||
//If the user provides OSO token, we can directly proxy | ||
if _, ok := r.Header["Authorization"]; ok { //FIXME Do we need this? | ||
if _, ok := request.Header["Authorization"]; ok { //FIXME Do we need this? |
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.
For the record, this can be removed. Needs to be tested though.
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.
While working on this you might want to verify by running the Proxy locally whether it still works. There might be an issue with cookies in this case. I have not confirmed that and I successfully did a proxying using a local Proxy instance, but there is this issue which might prevent you to test the workflow locally #174. Might be worth following up. Being able to test the workflow locally will help a lot, but remember the end game is to "codify" the flow into some tests. |
Any more changes to this? |
Is this(https://github.com/fabric8-services/fabric8-jenkins-proxy/blob/master/internal/proxy/proxy.go#L315) the function that waits until jenkins is unidled? |
@chmouel If this seems right to you I can move forward with tests. Except for breaking the function this doesnt have much changed. |
@kishansagathiya there is a few concernces about this from @hferentschik what are about? |
@chmouel I think they are all covered
|
I am worried to merge something like this without any tests (i.e: no proxy_test.go) can we have unitests along this commit please? |
@chmouel This is NOT ready for merge yet. It is WIP. We can merge this after tests. The reason for such temporary commits is to make sure that I am on the right path. If I get a review after tests, I will have change implementation and tests both. |
@chmouel Have added some tests. It needs much more, which I keep pushing with this PR. |
look at the way it uses https://github.com/ory/dockertest for the database tests, there is already some example in the in db_store_test here |
b47b83e
to
7bbcacc
Compare
Tested this locally to be working using
It unidled the Jenkins and this is on running it again.
|
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 it's mostly fine, it's quite large so I may miss a few things there and there. The coverage is improving thanks for the tests, but there is still quite a bit not covered there see the coverage html file here https://drive.google.com/a/chmouel.com/uc?id=1cLNs5KLcsSaGqKdaOARADp23p8ubfuED&export=download
internal/proxy/proxy_test.go
Outdated
return "", errors.New("Could not get valid OSO Token") | ||
} | ||
|
||
type mockIdler 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.
It would be nice that the mock class are moved to their own package so this can be reused like we do on idler repo (and usually type/struct are top of the files as convention btw)
So far tests are only for handleJenkinsUIRequest. We can add other tests using a different PR and a different issue. |
@kishansagathiya fair enough, feel free to address the mock package move and i'll be 👍 with this, |
@chmouel on it
…On Thu 5 Apr, 2018, 12:31 PM Chmouel Boudjnah, ***@***.***> wrote:
@kishansagathiya <https://github.com/kishansagathiya> fair enough, feel
free to address the mock package move and i'll be 👍 with this,
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#198 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AJttD0zCMqwTqPqg7SRJlfoLSnMKn2BTks5tlcFhgaJpZM4StVbt>
.
|
- Splitted handleJenkinsUIRequest() to smaller methods - Added error package, removed naked returns, decreased returned parameters - Added tests
7bbcacc
to
9b2d795
Compare
@chmouel
|
Fixes #179