Merge "Modernize CORS config and setup"

This commit is contained in:
Zuul 2019-06-10 17:12:41 +00:00 committed by Gerrit Code Review
commit c919b1f568
5 changed files with 17 additions and 16 deletions

View File

@ -15,6 +15,7 @@
from __future__ import absolute_import
from oslo_log import log as logging
from oslo_middleware import cors
from placement.conf import api
from placement.conf import base
@ -33,3 +34,6 @@ def register_opts(conf):
paths.register_opts(conf)
placement.register_opts(conf)
logging.register_options(conf)
# The CORS middleware does not present a register_opts method, instead
# it shares a list of available opts.
conf.register_opts(cors.CORS_OPTS, 'cors')

View File

@ -15,7 +15,6 @@ import os
from microversion_parse import middleware as mp_middleware
import oslo_middleware
from oslo_middleware import cors
from placement import auth
from placement.db.sqlalchemy import migration
@ -50,10 +49,8 @@ def deploy(conf):
auth_middleware = auth.filter_factory(
{}, oslo_config_config=conf)
# Pass in our CORS config, if any, manually as that's a)
# explicit, b) makes testing more straightfoward, c) let's
# us control the use of cors by the presence of its config.
conf.register_opts(cors.CORS_OPTS, 'cors')
# Conditionally add CORS middleware based on setting 'allowed_origin'
# in config.
if conf.cors.allowed_origin:
cors_middleware = oslo_middleware.CORS.factory(
{}, **conf.cors)

View File

@ -18,7 +18,6 @@ import os_resource_classes as orc
from oslo_config import cfg
from oslo_config import fixture as config_fixture
from oslo_log.fixture import logging_error
from oslo_middleware import cors
from oslo_policy import opts as policy_opts
from oslo_utils.fixture import uuidsentinel as uuids
from oslo_utils import uuidutils
@ -80,11 +79,6 @@ class APIFixture(fixture.GabbiFixture):
self.context = context.RequestContext()
# Register CORS opts, but do not set config. This has the
# effect of exercising the "don't use cors" path in
# deploy.py. Without setting some config the group will not
# be present.
self.conf_fixture.register_opts(cors.CORS_OPTS, 'cors')
# Set default policy opts, otherwise the deploy module can
# NoSuchOptError.
policy_opts.set_defaults(self.conf_fixture.conf)
@ -394,12 +388,13 @@ class CORSFixture(APIFixture):
def start_fixture(self):
super(CORSFixture, self).start_fixture()
# NOTE(cdent): If we remove this override, then the cors
# group ends up not existing in the conf, so when deploy.py
# wants to load the CORS middleware, it will not.
# Turn on the CORS middleware by setting 'allowed_origin'.
self.conf_fixture.config(
group='cors',
allowed_origin='http://valid.example.com')
self.conf_fixture.config(
group='cors',
allow_headers=['openstack-api-version'])
class GranularFixture(APIFixture):

View File

@ -15,9 +15,12 @@ tests:
request_headers:
origin: http://valid.example.com
access-control-request-method: GET
access-control-request-headers: openstack-api-version
status: 200
response_headers:
access-control-allow-origin: http://valid.example.com
# Confirm allow-headers configuration.
access-control-allow-headers: openstack-api-version
- name: invalid options request
OPTIONS: /

View File

@ -95,11 +95,13 @@ def _set_middleware_defaults():
'X-Roles',
'X-Service-Catalog',
'X-User-Id',
'X-Tenant-Id'],
'X-Tenant-Id',
'OpenStack-API-Version'],
expose_headers=['X-Auth-Token',
'X-Openstack-Request-Id',
'X-Subject-Token',
'X-Service-Token'],
'X-Service-Token',
'OpenStack-API-Version'],
allow_methods=['GET',
'PUT',
'POST',