Files
impala/shell/cookie_util.py
Michael Smith 100693d5ad IMPALA-12093: impala-shell to preserve all cookies
Updates impala-shell to preserve all cookies by default, defined as
setting 'http_cookie_names=*'. Prior behavior of restricting cookies to
a user-specified list is preserved when 'http_cookie_names' is given any
value besides '*'. Setting 'http_cookie_names=' prevents any cookies
from being preserved.

Adds verbose output that prints all cookies that are preserved by the
HTTP client.

Existing cookie tests with LDAP still work. Adds a test where Impala
returns an extra cookie, and test verifies that verbose mode prints all
expected cookies.

Change-Id: Ic81f790288460b086ab218e6701e8115a996dfa7
Reviewed-on: http://gerrit.cloudera.org:8080/19827
Reviewed-by: Impala Public Jenkins <impala-public-jenkins@cloudera.com>
Tested-by: Michael Smith <michael.smith@cloudera.com>
2024-06-27 22:36:57 +00:00

94 lines
2.6 KiB
Python

#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
import datetime
import os.path
import sys
from six.moves import http_cookies
def cookie_matches_path(c, path):
if 'path' not in c or not c['path']:
return True
cookie_path = c['path'].strip()
if not cookie_path.startswith('/'):
cookie_path = '/' + cookie_path
cookie_path = os.path.normpath(cookie_path)
if cookie_path == '/':
return True
if not path.startswith('/'):
path = '/' + path
path = os.path.normpath(path)
return path == cookie_path or path.startswith(cookie_path + '/')
def get_cookie_expiry(c):
if 'max-age' in c and c['max-age']:
try:
max_age_sec = int(c['max-age'])
return datetime.datetime.now() + datetime.timedelta(seconds=max_age_sec)
except Exception:
pass
# TODO: implement support for 'expires' cookie attribute as well.
return None
def get_cookies(resp_headers):
if 'Set-Cookie' not in resp_headers:
return None
cookies = http_cookies.SimpleCookie()
try:
if sys.version_info.major == 2:
cookies.load(resp_headers['Set-Cookie'])
else:
cookie_headers = resp_headers.get_all('Set-Cookie')
for header in cookie_headers:
cookies.load(header)
return cookies
except Exception:
return None
def get_all_cookies(path, resp_headers):
cookies = get_cookies(resp_headers)
if not cookies:
return None
matching_cookies = []
for c in cookies.values():
if c and cookie_matches_path(c, path):
matching_cookies.append(c)
return matching_cookies
def get_all_matching_cookies(cookie_names, path, resp_headers):
cookies = get_cookies(resp_headers)
if not cookies:
return None
matching_cookies = []
for cn in cookie_names:
if cn in cookies:
c = cookies[cn]
if c and cookie_matches_path(c, path):
matching_cookies.append(c)
return matching_cookies