Kaixhin / FGLab

Future Gadget Laboratory
https://kaixhin.github.io/FGLab/
MIT License
223 stars 34 forks source link

"add project to machine" button does not work cross-domain #26

Open dcferreira opened 7 years ago

dcferreira commented 7 years ago

Hi,

Issue #15 implements a button to add a project to a FGMachine, but the way it is implemented only works when both FGLab and FGMachine have the same domain name.

This is the way the HTTP request is currently made:

$.ajax({
    url: address + "/projects",
    type: "PUT",
    contentType: "application/json",
    data: JSON.stringify({project_id: id})
})

However, according to the documentation for contentType in http://api.jquery.com/jquery.ajax/,

Note: For cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plain will trigger the browser to send a preflight OPTIONS request to the server.

This means that currently if address is different from the address you're using to access FGLab, an HTTP OPTIONS request will be sent (which as far as I understood FGMachine doesn't know how to deal with), and no PUT request is ever sent.

I ran into this problem by running FGLab and FGMachine in docker containers, and accessing FGLab's UI at http://localhost:5080. I temporarily fixed this by changing the contentType to text/plain, but this is not a good solution.

Kaixhin commented 7 years ago

This should be addressed (I just tested by using localhost for FGLab and 127.0.0.1 for a FGMachine, and both OPTIONS and PUT went through). The Docker builds were quite old though, so I've rebuilt them on the Docker Hub just now. Try this out and let me know if it works now.

dcferreira commented 7 years ago

I pulled the new images and tried again, with the same results (I think I had also built them locally when I opened the issue).

A more detailed description of what I did:

$ docker run -d --name mongodb mongo
$ docker run -d --name fglab --link mongodb:mongo -p 5080:5080 kaixhin/fglab
$ docker run -d --name fgmachine -h $(hostname) -v /var/run/docker.sock:/var/run/docker.sock -v `pwd`/examples/Bayesian-Optimisation:/data -e FGLAB_URL=http://172.17.0.1:5080 -e FGMACHINE_URL=http://172.17.0.1:5081 -p 5081:5081 kaixhin/fgmachine

Then I went to http://localhost:5080 and added the Bayesian-Optimisation JSON file to create the project. Finally, I clicked the "add project" button, which creates an alert window with

localhost:5080 says:
undefined

Here's the full FGMachine log:

Server listening on port 5081
Registered with FGLab successfully
Projects registered with FGLab successfully
::ffff:172.17.0.1 - - [16/Oct/2017:08:57:40 +0000] "OPTIONS /projects HTTP/1.1" 204 0

Let me know if there's more I can do to help debug :)

Kaixhin commented 7 years ago

Hmmm first check in the machine (via the dashboard) to see if the project got added anyway, and if not, try the button again and see (please attach the FGMachine log again, that's quite useful). I vaguely remember this happening to me as well, but it actually worked fine, or the second time worked.

dcferreira commented 7 years ago

Checked http://localhost:5080/machines/, and the project list is empty. Went back to the homepage and tried adding again, same result as before :/

FGMachine log (note it's still the same session as in the previous reply):

Server listening on port 5081
Registered with FGLab successfully
Projects registered with FGLab successfully
::ffff:172.17.0.1 - - [16/Oct/2017:08:57:40 +0000] "OPTIONS /projects HTTP/1.1" 204 0
::ffff:172.17.0.1 - - [16/Oct/2017:09:17:05 +0000] "OPTIONS /projects HTTP/1.1" 204 0

Checked again the machine webpage after this, and project list is still empty.

Kaixhin commented 7 years ago

There might be a conflict with a browser/extension you are using. If you have several extensions activated, please try a version of your default browser without any extensions, and also other browsers (preferably a new one that you install now).

Otherwise, perhaps the networking setup with Docker containers is causing the issue, but it all looks fine to me...

Unfortunately I won't have time to look into this further until the weekend, so fingers crossed it is a browser issue.

dcferreira commented 7 years ago

Was using Chrome, tried with Chrome in private mode, with extensions disabled, and also tried with a clean Firefox. All with the same results, unfortunately :/

Am using Linux with kernel 4.4.0-96-generic, if it makes any difference. Both FGLab and FGMachine are using the bridge docker network.

Here are both logs: FGMachine:

Server listening on port 5081
Registered with FGLab successfully
Projects registered with FGLab successfully
::ffff:172.17.0.1 - - [16/Oct/2017:08:57:40 +0000] "OPTIONS /projects HTTP/1.1" 204 0
::ffff:172.17.0.1 - - [16/Oct/2017:09:17:05 +0000] "OPTIONS /projects HTTP/1.1" 204 0
::ffff:172.17.0.1 - - [16/Oct/2017:09:33:39 +0000] "OPTIONS /projects HTTP/1.1" 204 0
::ffff:172.17.0.1 - - [16/Oct/2017:09:34:12 +0000] "OPTIONS /projects HTTP/1.1" 204 0

FGLab:

Server listening on port 5080
::ffff:172.17.0.1 - - [16/Oct/2017:08:57:37 +0000] "POST /api/v1/machines HTTP/1.1" 201 381
::ffff:172.17.0.1 - - [16/Oct/2017:08:57:37 +0000] "POST /api/v1/machines/59e47481ef28030001fec352/projects HTTP/1.1" 200 15
::ffff:172.17.0.1 - - [16/Oct/2017:08:57:38 +0000] "GET / HTTP/1.1" 200 -
::ffff:172.17.0.1 - - [16/Oct/2017:09:16:39 +0000] "GET /machines/59e47481ef28030001fec352 HTTP/1.1" 200 -
::ffff:172.17.0.1 - - [16/Oct/2017:09:16:58 +0000] "GET /machines/59e47481ef28030001fec352 HTTP/1.1" 304 -
::ffff:172.17.0.1 - - [16/Oct/2017:09:17:00 +0000] "GET / HTTP/1.1" 304 -
::ffff:172.17.0.1 - - [16/Oct/2017:09:20:02 +0000] "GET /machines/59e47481ef28030001fec352 HTTP/1.1" 304 -
::ffff:172.17.0.1 - - [16/Oct/2017:09:26:12 +0000] "GET /experiments/59de423a0e8b4500014f9ac5 HTTP/1.1" 500 60
::ffff:172.17.0.1 - - [16/Oct/2017:09:30:43 +0000] "GET / HTTP/1.1" 304 -
::ffff:172.17.0.1 - - [16/Oct/2017:09:33:26 +0000] "GET / HTTP/1.1" 200 -
::ffff:172.17.0.1 - - [16/Oct/2017:09:33:31 +0000] "GET / HTTP/1.1" 304 -
::ffff:172.17.0.1 - - [16/Oct/2017:09:33:36 +0000] "GET /machines/59e47481ef28030001fec352 HTTP/1.1" 200 -
::ffff:172.17.0.1 - - [16/Oct/2017:09:33:38 +0000] "GET / HTTP/1.1" 304 -
::ffff:172.17.0.1 - - [16/Oct/2017:09:33:42 +0000] "GET /machines/59e47481ef28030001fec352 HTTP/1.1" 304 -
::ffff:172.17.0.1 - - [16/Oct/2017:09:33:44 +0000] "GET / HTTP/1.1" 304 -
::ffff:172.17.0.1 - - [16/Oct/2017:09:34:08 +0000] "GET / HTTP/1.1" 200 -
::ffff:172.17.0.1 - - [16/Oct/2017:09:34:19 +0000] "GET /machines/59e47481ef28030001fec352 HTTP/1.1" 200 -
::ffff:172.17.0.1 - - [16/Oct/2017:09:34:21 +0000] "GET / HTTP/1.1" 304 -

No rush on my side, so far working only on one machine, so using the correct domain is an easy workaround :) But do let me know if I can get you more information.

Kaixhin commented 7 years ago

OK I think there's a solution 😄 I replicated your setup with Docker (thanks for the detailed reproduction steps). Accessing FGLab at http://localhost:5080/, I could now repeat your problem. But by accessing it at http://<IP>:5080, CORS worked fine. Without looking into it further, I suspect it is something to do with localhost and containers.

Rule of thumb, if you're accessing the FGLab dashboard at the address provided in the FGLAB_URL flag, you should be fine. Let me know if this works for you too.