Skip to content

Commit

Permalink
Add tenant name header allowing other headers (CASMINST-6514)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brad Klein authored and bklei committed Jul 7, 2023
1 parent ca43f34 commit a10e08d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
5 changes: 5 additions & 0 deletions cray/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def _make_envvar(var):
DANGER_TAG = "cli_danger"
FROM_FILE_TAG = "cli_from_file"
CONVERSION_FLAG = "cray_converted"
HEADER_ORIGIN = "header"
HEADERS_ORIGIN = "headers"

# Config constants
DEFAULT_CONFIG = 'default'
Expand All @@ -52,3 +54,6 @@ def _make_envvar(var):
CONFIG_DIR_NAME = 'configurations'
LOG_DIR_NAME = 'logs'
AUTH_DIR_NAME = 'tokens'

# Rest constants
TENANT_HEADER_NAME_KEY = "Cray-Tenant-Name"
7 changes: 4 additions & 3 deletions cray/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@
from cray.constants import CONVERSION_FLAG
from cray.constants import DANGER_TAG
from cray.constants import FROM_FILE_TAG
from cray.constants import HEADER_ORIGIN
from cray.constants import HEADERS_ORIGIN
from cray.constants import HIDDEN_TAG
from cray.constants import IGNORE_TAG
from cray.constants import TAG_SPLIT
from cray.nesteddict import NestedDict

PATH_ORIGIN = 'path'
QUERY_ORIGIN = 'query'
HEADER_ORIGIN = 'header'
PARAM_ORIGIN = 'params'
FILE_ORIGIN = 'filepath'

Expand Down Expand Up @@ -169,15 +170,15 @@ def _parse_data(data, base=None, **kwargs):
if opts[QUERY_ORIGIN]:
args['params'] = opts[QUERY_ORIGIN]
if opts[HEADER_ORIGIN]:
args['headers'] = opts[HEADER_ORIGIN]
args[HEADERS_ORIGIN] = opts[HEADER_ORIGIN]
if opts[FILE_ORIGIN]:
fields = {}
for k, v in opts[FILE_ORIGIN].items():
# This explicitly returns an open file object, can't use 'with' here
# pylint: disable=consider-using-with
fields[k] = (os.path.basename(v), open(v, 'rb'))
args['data'] = MultipartEncoder(fields=fields)
args.setdefault('headers', {})['Content-Type'] = args[
args.setdefault(HEADERS_ORIGIN, {})['Content-Type'] = args[
'data'].content_type
return (method, route, args)

Expand Down
10 changes: 7 additions & 3 deletions cray/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@
from six.moves import urllib
from urllib3.exceptions import InsecureRequestWarning

from cray.constants import HEADERS_ORIGIN
from cray.constants import TENANT_HEADER_NAME_KEY
from cray.echo import echo
from cray.echo import LOG_DEBUG
from cray.echo import LOG_RAW
from cray.errors import BadResponseError
from cray.errors import InsecureError
from cray.errors import UnauthorizedError
from cray.utils import get_hostname
from cray.utils import get_headers
from cray.utils import get_tenant


def make_url(route, url=None, default_scheme='https', ctx=None):
Expand Down Expand Up @@ -91,13 +93,15 @@ def request(method, route, callback=None, **kwargs):

try:
url = make_url(route)
headers = get_headers(ctx=ctx)
tenant = get_tenant(ctx=ctx)
if tenant:
opts.setdefault(HEADERS_ORIGIN, {})[TENANT_HEADER_NAME_KEY] = tenant
echo(f'REQUEST: {method} to {url}', ctx=ctx, level=LOG_DEBUG)
echo(f'OPTIONS: {opts}', ctx=ctx, level=LOG_RAW)
# TODO: Find solution for this.
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=InsecureRequestWarning)
response = requester.request(method, url, **opts, headers=headers)
response = requester.request(method, url, **opts)
if not response.ok:
_log_request_error(response.text, ctx)
raise BadResponseError(response, ctx=ctx)
Expand Down
11 changes: 1 addition & 10 deletions cray/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,9 @@ def get_tenant(ctx=None):
""" Get the tenant requests are scoped to (None if not scoped to a tenant) """
ctx = ctx or click.get_current_context()
config = ctx.obj['config']
tenant = config.get('core.tenant', None)
tenant = config.get('core.tenant', "")
return tenant

def get_headers(ctx=None):
""" Get the headers which may or may not contain a tenant """
headers={}
ctx = ctx or click.get_current_context()
tenant = get_tenant(ctx=ctx)
if tenant:
headers={"Cray-Tenant-Name":tenant}
return headers


def hostname_to_name(hostname=None, ctx=None):
""" Convert hostname to name value for saving as filename"""
Expand Down

0 comments on commit a10e08d

Please sign in to comment.