Closed
Description
I am writing a client-side AngularJS app that uses XMLHttpRequest trying to talk to the apiserver. The apiserver was brought up with the following flags, especially with cors-allowed-origins
hoping to allow CORS.
--cors-allowed-origins=.*
--insecure-bind-address=127.0.0.1
--insecure-port=8080
--bind-address=0.0.0.0
--secure-port=443
and I configured apiserver to support basic auth with https.
However, the pre-flight OPTIONS request failed with 401. Below is my rationale:
- For CORS to work, the client will always first send a pre-flight OPTIONS request to the api endpoint
- When the apiserver runs in the secure mode (with authn), for each request the master will use "m.Handler" to process the request; whereas an "m.InsecureHandler" will be used if running in insecure mode
- The m.InsecureHandler will handle CORS correctly without performing any auth.
- In comparison, the m.Handler will add authn functionality before CORS, as shown here, where the handlers.NewRequestAuthenticator will add auth before the InsecureHandler that handles CORS. In short, in the secure mode, we first authenticate all requests (including OPTIONS) before detecting if it is an OPTIONS request
- Meanwhile (unfortunately), as the OPTIONS request cannot carry any auth headers (by w3c standards), the OPTIONS requests will always fail the authentication.
Is this expected behavior or did I miss anything? If it is expected, then what is the recommended way to allow CORS with authn on? I am thinking of using a proxy in front of the apiserver