Skip to content
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

Create handler for app repositories backend API. #1397

Merged
merged 3 commits into from
Dec 18, 2019
Merged

Conversation

absoludity
Copy link
Contributor

Description of the change

Fills in the stub implementation of the Create handler for the app repository backend API

Note: this handler is similar in nature to the helm3 work in the respect that it picks up the in-cluster k8s config and uses that with the user token to access the API. But note that it's structured so that:

  • the k8s config, with the blanked BearerToken is read from the system and created once only when the handler is created,
  • an actual working config with the user token is created for each request. This is a copy of the blanked k8s config with the token set.
  • it is tested using the (generated) fake clientset for the resource by injecting a dummy clientsetForConfig function when testing which just returns the fake. This ensures that external use of this handler will always have the real clientsetForConfig function, and only (internal) tests can set a dummy function. In this sense, it may be useful to @SimonAlling as a way to structure the handler code to be testable (warning: client-go testing with fake clientsets is pretty verbose, but once the parts are in place, pretty easy to work with - the setup is the painful part :/)

Applicable issues

Additional information

I've added a --kubeapps-namespace flag which is effectively a feature flag. Without this being set, a response comes back as:

< HTTP/1.1 401 Unauthorized                                                                                                                                                                                                       
< Content-Length: 74                                                                                                                                                                                                              
< Content-Type: text/plain; charset=utf-8                                                                                                                                                                                         < Date: Wed, 18 Dec 2019 04:30:27 GMT                                                                                                                                                                                             < Server: nginx/1.16.1                                                                                                                                                                                                            < X-Content-Type-Options: nosniff                                                                                                                                                                                                 <                                                                                                                                                                                                                                 kubeappsNamespace must be configured to enable app repository handler                                                                                                                                                             EOF                                                                                                                                                                                                                               

FWIW, I need to investigate more on the use of the in-cluster config, as when I did an IRL test with the feature switched on, authing with a cookie for my kubeapps-operator user, I see the following unexpected result:

$ curl -XPOST 'http://localhost:3000/api/v1/apprepositories' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0' -H 'Accept: application/json, text/p
lain, */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'DNT: 1' -H 'Connection: keep-alive' -H 'Referer: http://localhost:3000/' -H 'Cookie: _oauth2_proxy=<redacted>' -vvv --data '{"appRepository": {"name": "test-repo", "url": "http://example.com/test-repo"}}'
...
> Content-Length: 79                                                                                                                                                                                                              
> Content-Type: application/x-www-form-urlencoded                                                                                                                                                                                 
>                                                                                                                                                                                                                                 
* upload completely sent off: 79 out of 79 bytes                                                                                                                                                                                  
< HTTP/1.1 403 Forbidden                                                                                                                                                                                                          
< Content-Encoding: gzip                                                                                                                                                                                                          
< Content-Type: text/plain; charset=utf-8                                                                                                                                                                                         
< Date: Wed, 18 Dec 2019 04:32:37 GMT                                                                                                                                                                                             
< Server: nginx/1.16.1                                                                                                                                                                                                            
< X-Content-Type-Options: nosniff                                                                                                                                                                                                 
< Content-Length: 156                                                                                                                                                                                                             
<                                                                                                                                                                                                                                 
apprepositories.kubeapps.com is forbidden: User "system:serviceaccount:kubeapps:kubeapps-internal-tiller-proxy" cannot create resource "apprepositories" in API group "kubeapps.com" in the namespace "kubeapps"                  

I don't think this should stop the PR from landing (since it's behind the extra flag which is not set in the chart yet), but tomorrow I plan to find out (a) why it assumes the user accessing is the internal-tiller-proxy service account (ie. does the in-cluster config have not only the token data, but the service account info set, which needs to be blanked out), and (b) why the token (or otherwise) is resulting in a 403.

Maybe it'll be obvious during review, but I need to run :)

@absoludity absoludity self-assigned this Dec 18, 2019
Copy link
Contributor

@andresmgot andresmgot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

return &appRepositoriesHandler{
config: *config,
kubeappsNamespace: kubeappsNamespace,
clientsetForConfig: clientsetForConfig,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why this function is within the appRepositoriesHandler struct? As far as I can see it can be called as a normal method (it doesn't depend on the struct properties).

[edit] Got it, you need this because of the tests, to be able to override it. (A comment here explaining it would be nice?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a // See comment in the struct defn above as there's a 5-liner explanation there :)

return
}
// TODO(mnelson): validate both required data and request for index
// https://github.com/kubeapps/kubeapps/issues/1330
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's good that we have a place now to validate this data 👍

@absoludity absoludity merged commit 6d5aeb2 into master Dec 18, 2019
@absoludity absoludity deleted the app-repo-handler-2 branch December 18, 2019 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants