Skip to content

Commit

Permalink
plugins: googledomains: Use configured authentication source.
Browse files Browse the repository at this point in the history
After merging #23 the actual source for credentials could be
something else than the .netrc file. To cope with this, the library
function get_netrc_auth should be used despite that it has a bad
nam).

Re-factor the bundled get_netrc_auth to use the library function
instead of just the .netrc file.
  • Loading branch information
Alec Leamas committed Apr 1, 2022
1 parent 18a39eb commit 66ba183
Showing 1 changed file with 13 additions and 25 deletions.
38 changes: 13 additions & 25 deletions plugins/googledomains.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import os.path
import urllib.parse
import urllib.request
from ddupdate.ddplugin import ServiceError, ServicePlugin, \
get_response
from ddupdate.ddplugin import ServiceError, ServicePlugin
from ddupdate.ddplugin import AuthError, get_response, get_netrc_auth


# See https://github.com/leamas/ddupdate/pull/56
Expand All @@ -20,7 +20,7 @@ def http_basic_auth_setup(url, *, providerhost=None, targethost=None):
"""
Configure urllib to provide basic authentication.
See get_netrc_auth for how providerhost and targethost
See get_auth for how providerhost and targethost
are resolved to credentials stored in netrc.
Parameters:
Expand All @@ -33,18 +33,16 @@ def http_basic_auth_setup(url, *, providerhost=None, targethost=None):
"""
if not providerhost:
providerhost = urllib.parse.urlparse(url).hostname
user, password = get_netrc_auth(providerhost, targethost)
user, password = get_auth(providerhost, targethost)
pwmgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
pwmgr.add_password(None, url, user, password)
auth_handler = urllib.request.HTTPBasicAuthHandler(pwmgr)
opener = urllib.request.build_opener(auth_handler)
urllib.request.install_opener(opener)

def get_netrc_auth(providerhost, targethost=None):
def get_auth(providerhost, targethost=None):
"""
Retrieve data from ~/-netrc or /etc/netrc.
Will look for matching identifiers in the netrc file.
Retrieve credentials from configured source.
If a targethost is passed, the first machine name we look for
is targethost.providerhost.ddupdate, falling back to providerhost.
Expand All @@ -57,28 +55,18 @@ def get_netrc_auth(providerhost, targethost=None):
Returns:
- A (user, password) tuple. User might be None.
Raises:
- ServiceError if .netrc or password is not found.
See:
- netrc(5)
- AuthError password is not found.
"""
if os.path.exists(os.path.expanduser('~/.netrc')):
path = os.path.expanduser('~/.netrc')
elif os.path.exists('/etc/netrc'):
path = '/etc/netrc'
else:
raise ServiceError("Cannot locate the netrc file (see manpage).")
netrcdata = netrc.netrc(path)
if targethost is not None:
machine1 = "%s.%s.ddupdate" % (targethost, providerhost)
auth = netrcdata.authenticators(machine1) or netrcdata.authenticators(providerhost)
try:
credentials = get_netrc_auth(machine1)
except AuthError:
credentials = get_netrc_auth(providerhost)
else:
auth = netrcdata.authenticators(providerhost)
if not auth:
raise ServiceError("No .netrc data found for " + providerhost)
if not auth[2]:
raise ServiceError("No password found for " + providerhost)
return auth[0], auth[2]
credentials = get_netrc_auth(providerhost)
return credentials


class GoogleDomainsPlugin(ServicePlugin):
Expand Down

0 comments on commit 66ba183

Please sign in to comment.