URL: https://github.com/freeipa/freeipa/pull/1430
Author: pilou-
 Title: #1430: Avoid to use non existent attributes
Action: opened

PR body:
"""
Closes: https://pagure.io/freeipa/issue/7345

2nd commit: about `path` used by `ServerInfo._read` and `ServerInfo._write`, I 
am not sure what value could be used here (with this patch both methods do 
nothing). Should the domain be used there (for example: 
`~/.cache/ipa/servers/ipa.test`) or network location extracted from 
`ipalib.request.context.request_url` ?

Related: 3f6411a49c49da7013341ff8feae3a63e75e0fbf.
"""

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/1430/head:pr1430
git checkout pr1430
From 7a3cc3b696ae4e1b4853a067e5f781658b660097 Mon Sep 17 00:00:00 2001
From: Pierre-Louis Bonicoli <pierre-louis.bonic...@libregerbil.fr>
Date: Wed, 3 Jan 2018 10:34:16 +0100
Subject: [PATCH 1/2] api.env.server doesn't have a default value anymore

Fix AttributeError when both xmlrpc_uri and jsonrpc_uri are unset.

Default value was removed by 3f6411a49c49da7013341ff8feae3a63e75e0fbf.

Using FreeIPA Ansible dynamic inventory:
$ ./freeipa.py --list
Traceback (most recent call last):
  File "./ansible/contrib/inventory/freeipa.py", line 95, in <module>
    api = initialize()
  File "./ansible/contrib/inventory/freeipa.py", line 18, in initialize
    api.finalize()
  File "local/lib/python2.7/site-packages/ipalib/plugable.py", line 738, in finalize
    self.__do_if_not_done('load_plugins')
  File "local/lib/python2.7/site-packages/ipalib/plugable.py", line 425, in __do_if_not_done
    getattr(self, name)()
  File "local/lib/python2.7/site-packages/ipalib/plugable.py", line 618, in load_plugins
    for package in self.packages:
  File "local/lib/python2.7/site-packages/ipalib/__init__.py", line 949, in packages
    ipaclient.remote_plugins.get_package(self),
  File "local/lib/python2.7/site-packages/ipaclient/remote_plugins/__init__.py", line 120, in get_package
    server_info = ServerInfo(api)
  File "local/lib/python2.7/site-packages/ipaclient/remote_plugins/__init__.py", line 26, in __init__
    hostname = DNSName(api.env.server).ToASCII()
AttributeError: 'Env' object has no attribute 'server

https://pagure.io/freeipa/issue/7345
---
 ipaclient/remote_plugins/__init__.py | 28 +++++++++++++++++++---------
 ipaclient/remote_plugins/schema.py   |  2 +-
 2 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/ipaclient/remote_plugins/__init__.py b/ipaclient/remote_plugins/__init__.py
index 0dff001762..c70b1259ea 100644
--- a/ipaclient/remote_plugins/__init__.py
+++ b/ipaclient/remote_plugins/__init__.py
@@ -23,8 +23,6 @@ class ServerInfo(collections.MutableMapping):
     _DIR = os.path.join(USER_CACHE_PATH, 'ipa', 'servers')
 
     def __init__(self, api):
-        hostname = DNSName(api.env.server).ToASCII()
-        self._path = os.path.join(self._DIR, hostname)
         self._force_check = api.env.force_schema_check
         self._dict = {}
 
@@ -36,11 +34,17 @@ def __init__(self, api):
         except locale.Error:
             self._language = 'en_us'
 
-        self._read()
+        self._read(api)
+
+    def _read(self, api):
+        if 'server' not in api.env:
+            return
+
+        hostname = DNSName(api.env.server).ToASCII()
+        _path = os.path.join(self._DIR, hostname)
 
-    def _read(self):
         try:
-            with open(self._path, 'r') as sc:
+            with open(_path, 'r') as sc:
                 self._dict = json.load(sc)
         except Exception as e:
             if (isinstance(e, EnvironmentError) and
@@ -52,14 +56,20 @@ def _read(self):
                 # warn that the file is unreadable, probably corrupted
                 logger.warning('Failed to read server info: %s', e)
 
-    def _write(self):
+    def _write(self, api):
+        if 'server' not in api.env:
+            return
+
+        hostname = DNSName(api.env.server).ToASCII()
+        _path = os.path.join(self._DIR, hostname)
+
         try:
             try:
                 os.makedirs(self._DIR)
             except EnvironmentError as e:
                 if e.errno != errno.EEXIST:
                     raise
-            with open(self._path, 'w') as sc:
+            with open(_path, 'w') as sc:
                 json.dump(self._dict, sc)
         except EnvironmentError as e:
             logger.warning('Failed to write server info: %s', e)
@@ -79,12 +89,12 @@ def __iter__(self):
     def __len__(self):
         return len(self._dict)
 
-    def update_validity(self, ttl=None):
+    def update_validity(self, client, ttl=None):
         if ttl is None:
             ttl = 3600
         self['expiration'] = time.time() + ttl
         self['language'] = self._language
-        self._write()
+        self._write(client)
 
     def is_valid(self):
         if self._force_check:
diff --git a/ipaclient/remote_plugins/schema.py b/ipaclient/remote_plugins/schema.py
index 863d8f1992..a05985c236 100644
--- a/ipaclient/remote_plugins/schema.py
+++ b/ipaclient/remote_plugins/schema.py
@@ -560,7 +560,7 @@ def get_package(server_info, client):
             ttl = schema.ttl
 
         server_info['fingerprint'] = fingerprint
-        server_info.update_validity(ttl)
+        server_info.update_validity(client, ttl=ttl)
 
     if fingerprint is None:
         raise NotAvailable()

From d5e5c652375b67ea63dafcbe1922440d0e0266a2 Mon Sep 17 00:00:00 2001
From: Pierre-Louis Bonicoli <pierre-louis.bonic...@libregerbil.fr>
Date: Wed, 3 Jan 2018 10:44:03 +0100
Subject: [PATCH 2/2] rpc_uri don't have a default value anymore

Fix KeyError when both xmlrpc_uri and jsonrpc_uri are unset.
Default values for xmlrpc_uri and jsonrpc_uri were removed by
3f6411a49c49da7013341ff8feae3a63e75e0fbf.

Not sure which path should be used when the configuration relies on DNS
discovery: using 4.5.0, when xmlrpc_uri/jsonrpc_uri aren't specified, used
path is ~/.cache/ipa/servers/localhost:8888.

https://pagure.io/freeipa/issue/7345
---
 ipalib/rpc.py | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/ipalib/rpc.py b/ipalib/rpc.py
index d868518aff..7c4d4060cd 100644
--- a/ipalib/rpc.py
+++ b/ipalib/rpc.py
@@ -866,8 +866,12 @@ def get_url_list(self, rpc_uri):
         Create a list of urls consisting of the available IPA servers.
         """
         # the configured URL defines what we use for the discovered servers
-        (_scheme, _netloc, path, _params, _query, _fragment
-            ) = urllib.parse.urlparse(rpc_uri)
+        if rpc_uri:
+            (_scheme, _netloc, path, _params, _query, _fragment
+                ) = urllib.parse.urlparse(rpc_uri)
+        else:
+            path = '/ipa/json'
+
         servers = []
         name = '_ldap._tcp.%s.' % self.env.domain
 
@@ -889,7 +893,7 @@ def get_url_list(self, rpc_uri):
             # it is the first one
             servers.remove(cfg_server)
             servers.insert(0, cfg_server)
-        else:
+        elif cfg_server:
             servers.insert(0, cfg_server)
 
         return servers
@@ -1008,7 +1012,10 @@ def create_connection(self, ccache=None, verbose=None, fallback=None,
             ca_certfile = self.api.env.tls_ca_cert
         context.ca_certfile = ca_certfile
 
-        rpc_uri = self.env[self.env_rpc_uri_key]
+        if self.env_rpc_uri_key in self.env:
+            rpc_uri = self.env[self.env_rpc_uri_key]
+        else:
+            rpc_uri = None
         try:
             principal = get_principal(ccache_name=ccache)
             stored_principal = getattr(context, 'principal', None)
@@ -1020,7 +1027,7 @@ def create_connection(self, ccache=None, verbose=None, fallback=None,
             setattr(context, 'principal', principal)
             # We have a session cookie, try using the session URI to see if it
             # is still valid
-            if not delegate:
+            if not delegate and rpc_uri:
                 rpc_uri = self.apply_session_cookie(rpc_uri)
         except (errors.CCacheError, ValueError):
             # No session key, do full Kerberos auth
_______________________________________________
FreeIPA-devel mailing list -- freeipa-devel@lists.fedorahosted.org
To unsubscribe send an email to freeipa-devel-le...@lists.fedorahosted.org

Reply via email to