Skip to content

Commit

Permalink
Incorporate additional deletions from review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCoding committed Nov 18, 2024
1 parent d777b67 commit c2d05bb
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 57 deletions.
3 changes: 2 additions & 1 deletion awx_collection/plugins/module_utils/awxkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def __init__(self, argument_spec, **kwargs):
def authenticate(self):
try:
if self.oauth_token:
self.connection.login(None, None, token=self.oauth_token)
# MERGE: fix conflicts with removal of OAuth2 token from collection branch
self.connection.login(None, None)
self.authenticated = True
elif self.username:
self.connection.login(username=self.username, password=self.password)
Expand Down
13 changes: 1 addition & 12 deletions awxkit/awxkit/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ class ConnectionException(exc.Common):
pass


class Token_Auth(requests.auth.AuthBase):
def __init__(self, token):
self.token = token

def __call__(self, request):
request.headers['Authorization'] = 'Bearer {0.token}'.format(self)
return request


def log_elapsed(r, *args, **kwargs): # requests hook to display API elapsed time
log.debug('"{0.request.method} {0.url}" elapsed: {0.elapsed}'.format(r))

Expand All @@ -47,7 +38,7 @@ def get_session_requirements(self, next=config.api_base_path):
self.get(config.api_base_path) # this causes a cookie w/ the CSRF token to be set
return dict(next=next)

def login(self, username=None, password=None, token=None, **kwargs):
def login(self, username=None, password=None, **kwargs):
if username and password:
_next = kwargs.get('next')
if _next:
Expand All @@ -62,8 +53,6 @@ def login(self, username=None, password=None, token=None, **kwargs):
self.uses_session_cookie = True
else:
self.session.auth = (username, password)
elif token:
self.session.auth = Token_Auth(token)
else:
self.session.auth = None

Expand Down
21 changes: 1 addition & 20 deletions awxkit/awxkit/api/pages/base.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import logging
import typing

from awxkit.api.pages import Page, get_registered_page
from awxkit.api.pages import Page
from awxkit.config import config
from awxkit.api.resources import resources
import awxkit.exceptions as exc


log = logging.getLogger(__name__)


class AuthUrls(typing.TypedDict):
access_token: str
personal_token: str


class Base(Page):
def silent_delete(self):
"""Delete the object. If it's already deleted, ignore the error"""
Expand Down Expand Up @@ -132,18 +125,6 @@ def object_roles(self):
for obj_role in Roles(self.connection, endpoint=url).get().json.results:
yield Role(self.connection, endpoint=obj_role.url).get()

def get_authtoken(self, username='', password=''):
default_cred = config.credentials.default
payload = dict(username=username or default_cred.username, password=password or default_cred.password)
auth_url = resources.authtoken
return get_registered_page(auth_url)(self.connection, endpoint=auth_url).post(payload).token

def load_authtoken(self, username='', password=''):
self.connection.login(token=self.get_authtoken(username, password))
return self

load_default_authtoken = load_authtoken

def load_session(self, username='', password=''):
default_cred = config.credentials.default
self.connection.login(
Expand Down
13 changes: 1 addition & 12 deletions awxkit/awxkit/awx/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def check_related(resource):
@contextmanager
def as_user(v, username, password=None):
"""Context manager to allow running tests as an alternative login user."""
access_token = False
if not isinstance(v, api.client.Connection):
connection = v.connection
else:
Expand All @@ -83,11 +82,6 @@ def as_user(v, username, password=None):
password = username.password
username = username.username

if isinstance(username, api.OAuth2AccessToken):
access_token = username.token
username = None
password = None

try:
if config.use_sessions:
session_id = None
Expand All @@ -101,19 +95,14 @@ def as_user(v, username, password=None):
break
if session_id:
del connection.session.cookies[connection.session_cookie_name]
if access_token:
kwargs = dict(token=access_token)
else:
kwargs = connection.get_session_requirements()
kwargs = connection.get_session_requirements()
else:
previous_auth = connection.session.auth
kwargs = dict()
connection.login(username, password, **kwargs)
yield
finally:
if config.use_sessions:
if access_token:
connection.session.auth = None
del connection.session.cookies[connection.session_cookie_name]
if session_id:
connection.session.cookies.set(connection.session_cookie_name, session_id, domain=domain)
Expand Down
9 changes: 3 additions & 6 deletions awxkit/awxkit/scripts/basic_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os

from awxkit import api, config, utils, exceptions, WSClient # noqa
from awxkit.awx.utils import check_related, delete_all, get_all, uses_sessions # noqa
from awxkit.awx.utils import check_related, delete_all, get_all # noqa
from awxkit.awx.utils import as_user as _as_user

if str(os.getenv('AWXKIT_DEBUG', 'false')).lower() in ['true', '1']:
Expand Down Expand Up @@ -59,11 +59,8 @@ def main():

global root
root = api.Api()
if uses_sessions(root.connection):
config.use_sessions = True
root.load_session().get()
else:
root.load_authtoken().get()
config.use_sessions = True
root.load_session().get()

if 'v2' in root.available_versions:
global v2
Expand Down
8 changes: 2 additions & 6 deletions awxkit/awxkit/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class WSClient(object):
'control': ['limit_reached']}
e.x:
```
ws = WSClient(token, port=8013, secure=False).connect()
ws = WSClient(port=8013, secure=False, session_id='xyz', csrftoken='abc').connect()
ws.job_details()
... # launch job
job_messages = [msg for msg in ws]
Expand All @@ -52,7 +52,6 @@ class WSClient(object):

def __init__(
self,
token=None,
hostname='',
port=443,
secure=True,
Expand Down Expand Up @@ -80,15 +79,12 @@ def __init__(
self.suffix = ws_suffix
self._use_ssl = secure
self.hostname = hostname
self.token = token
self.session_id = session_id
self.csrftoken = csrftoken
self._recv_queue = Queue()
self._ws_closed = False
self._ws_connected_flag = threading.Event()
if self.token is not None:
auth_cookie = 'token="{0.token}";'.format(self)
elif self.session_id is not None:
if self.session_id is not None:
auth_cookie = '{1}="{0.session_id}"'.format(self, session_cookie_name)
if self.csrftoken:
auth_cookie += ';csrftoken={0.csrftoken}'.format(self)
Expand Down

0 comments on commit c2d05bb

Please sign in to comment.