Skip to content

Commit

Permalink
minor improvements to auth plugins (#17041)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErniGH authored Sep 23, 2024
2 parents d0eb5f2 + 141cd0e commit 28739c7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
11 changes: 7 additions & 4 deletions conan/cli/commands/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,13 @@ def remote_login(conan_api, parser, subparser, *args):
for r in remotes:
previous_info = conan_api.remotes.user_info(r)

user, password = creds.auth(r, args.username, args.password)
if args.username is not None and args.username != user:
raise ConanException(f"User '{args.username}' doesn't match user '{user}' in "
f"credentials.json or environment variables")
if args.username is not None and args.password is not None:
user, password = args.username, args.password
else:
user, password = creds.auth(r, args.username)
if args.username is not None and args.username != user:
raise ConanException(f"User '{args.username}' doesn't match user '{user}' in "
f"credentials.json or environment variables")

conan_api.remotes.user_login(r, user, password)
info = conan_api.remotes.user_info(r)
Expand Down
12 changes: 6 additions & 6 deletions conans/client/rest/conan_requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class _SourceURLCredentials:
"""
def __init__(self, cache_folder):
self._urls = {}
self.auth_source_plugin = None
self._auth_source_plugin = None
if not cache_folder:
return
auth_source_plugin_path = HomePaths(cache_folder).auth_source_plugin_path
self.auth_source_plugin = _load_auth_source_plugin(auth_source_plugin_path)
self._auth_source_plugin = _load_auth_source_plugin(auth_source_plugin_path)
creds_path = os.path.join(cache_folder, "source_credentials.json")
if not os.path.exists(creds_path):
return
Expand Down Expand Up @@ -68,9 +68,9 @@ def _get_auth(credentials):

def add_auth(self, url, kwargs):
# First, try to use "auth_source_plugin"
if self.auth_source_plugin:
if self._auth_source_plugin:
try:
c = self.auth_source_plugin(url)
c = self._auth_source_plugin(url)
except Exception as e:
msg = f"Error while processing 'auth_source_remote.py' plugin"
msg = scoped_traceback(msg, e, scope="/extensions/plugins")
Expand Down Expand Up @@ -206,8 +206,8 @@ def _call_method(self, method, url, **kwargs):
os.environ.clear()
os.environ.update(old_env)


def _load_auth_source_plugin(auth_source_plugin_path):
if os.path.exists(auth_source_plugin_path):
mod, _ = load_python_file(auth_source_plugin_path)
if hasattr(mod, "auth_source_plugin"):
return mod.auth_source_plugin
return getattr(mod, "auth_source_plugin", None)
18 changes: 8 additions & 10 deletions conans/client/rest/remote_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
from conans.errors import ConanException, scoped_traceback
from conans.util.files import load


class RemoteCredentials:
def __init__(self, cache_folder, global_conf):
self._global_conf = global_conf
self._urls = {}
self.auth_remote_plugin = _load_auth_remote_plugin(HomePaths(cache_folder).auth_remote_plugin_path)
auth_plugin_path = HomePaths(cache_folder).auth_remote_plugin_path
self._auth_remote_plugin = _load_auth_remote_plugin(auth_plugin_path)
creds_path = os.path.join(cache_folder, "credentials.json")
if not os.path.exists(creds_path):
return
Expand All @@ -31,15 +33,11 @@ def __init__(self, cache_folder, global_conf):
except Exception as e:
raise ConanException(f"Error loading 'credentials.json' {creds_path}: {repr(e)}")

def auth(self, remote, user=None, password=None):
if user is not None and password is not None:
return user, password

def auth(self, remote, user=None):
# First get the auth_remote_plugin

if self.auth_remote_plugin is not None:
if self._auth_remote_plugin is not None:
try:
plugin_user, plugin_password = self.auth_remote_plugin(remote, user=user)
plugin_user, plugin_password = self._auth_remote_plugin(remote, user=user)
except Exception as e:
msg = f"Error while processing 'auth_remote.py' plugin"
msg = scoped_traceback(msg, e, scope="/extensions/plugins")
Expand Down Expand Up @@ -82,8 +80,8 @@ def _get_env(remote, user):
ConanOutput().info("Got password '******' from environment")
return user, passwd


def _load_auth_remote_plugin(auth_remote_plugin_path):
if os.path.exists(auth_remote_plugin_path):
mod, _ = load_python_file(auth_remote_plugin_path)
if hasattr(mod, "auth_remote_plugin"):
return mod.auth_remote_plugin
return getattr(mod, "auth_remote_plugin", None)

0 comments on commit 28739c7

Please sign in to comment.