openwhisk icon indicating copy to clipboard operation
openwhisk copied to clipboard

OPTIONS verb not implemented by OpenWhisk REST API to invoke actions

Open l2fprod opened this issue 10 years ago • 9 comments

Hi,

I've been working on a OpenWhisk action that I want to call directly from the web browser. But I got an issue using jQuery.ajax. JQuery is sending a preflight OPTIONS method to check for cross-domain requests. As OpenWhisk does not emit proper CORS headers today, the AJAX call failed.

Could OpenWhisk REST API implement OPTIONS and send the right CORS headers?

l2fprod avatar Mar 07 '16 16:03 l2fprod

This is not a lot of work to support with spray. There is an option directive already and the implementation of a CORS trait/directive shouldn't be more than 30 lines. With tests, < 100 lines.

rabbah avatar May 19 '16 21:05 rabbah

Suggested solution:

  • we add option calls to the routes() in RestAPIs.scala. With ajax and Cross-domain it is not possible to pass the Authentication-Header (on OPTION-request).
  • Because we do not know if the user is allowed to get information about this action, we only respond with the wildcard for allowed origins and the following headers on OPTION calls:
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Authorization,content-type

The second header is needed to allow Authorization- and content-type header on second call.

  • The OPTION-call is followed by the normal POST call. This request includes the Authorization. We do send Access-Control-Allow-Origin: * in the response again. We also decided not to set a custom URL, because this would only have effect on calls from web browsers.

cbickel avatar May 31 '16 11:05 cbickel

This request includes the Authorization. We do not send any headers in the response, because this request is already allowed because of the header from OPTION response.

This implies, that the user wants to make a call that is "preflighted" by an OPTIONS request, right? That will be true for action invocations, since they make a POST with Content-Type application/json. ~~GET on /actions for example won't be preflighted, hence it won't get the correct Access-Control headers?~~ They will in-fact be preflighted because we need to send Authorization as you mentioned.

markusthoemmes avatar May 31 '16 11:05 markusthoemmes

I updated my comment above. We need to send the Header in POST response again. That was my mistake. We would suggest to send the header only on post requests. Or does anyone think, that we should also support getting and updating entities?

cbickel avatar May 31 '16 12:05 cbickel

I don't see any harm in supporting the whole API and I as a user would expect that i can do all of the API operations from any client.

markusthoemmes avatar May 31 '16 12:05 markusthoemmes

This would be much easier. We commit our current state for review, and if it's OK, we will write our tests.

https://github.com/openwhisk/openwhisk/compare/master...ChrBi:corsHeader?expand=1&w=1

cbickel avatar May 31 '16 12:05 cbickel

Closed by https://github.com/openwhisk/openwhisk/commit/aea64518e44fa4a948617a5d015cd75a7c18bc2d.

rabbah avatar Jun 09 '16 23:06 rabbah

@ChrBi working on missing tests

@csantanapr you know if that is documented? can you make a proposal where to document pls?

jeremiaswerner avatar Jun 13 '16 08:06 jeremiaswerner

@jeremiaswerner OPTIONS is not documented since it's not implemented.

Need to documented in two places

  1. In swagger json file for the API
  2. It would be good to put a note about supporting CORS in reference.md in REST API section

csantanapr avatar Jun 13 '16 20:06 csantanapr