[sysadmin-bin] Catch the case where no SSH key is defined yet for an LDAP account



commit 65541024cc6a2e0ce22ad54bb5c208073f2280eb
Author: Andrea Veri <averi redhat com>
Date:   Fri Nov 10 11:50:15 2017 +0100

    Catch the case where no SSH key is defined yet for an LDAP account

 gitlab/gitlab-operations.py |   44 ++++++++++++++++++++++--------------------
 gnome_ldap_utils.py         |    7 ++++-
 2 files changed, 28 insertions(+), 23 deletions(-)
---
diff --git a/gitlab/gitlab-operations.py b/gitlab/gitlab-operations.py
index 03eadae..26bbb2c 100755
--- a/gitlab/gitlab-operations.py
+++ b/gitlab/gitlab-operations.py
@@ -40,28 +40,30 @@ for person in gnomeusers:
 
 for username, id in ldapusers_dict.iteritems():
     ssh_key = glu.get_attributes_from_ldap(username, 'ipaSshPubKey')
-    user = gl.users.get(id)
-    try:
-        user.keys.create({'title': 'Imported from account.gnome.org', 'key': ssh_key})
 
-        print 'Key for username with id %i has been added' % id
-    except gitlab.exceptions.GitlabCreateError as e:
-        if e.response_code == 400:
-           pass
+    if ssh_key is not None:
+        user = gl.users.get(id)
+        try:
+            user.keys.create({'title': 'Imported from account.gnome.org', 'key': ssh_key})
 
-    try:
-        # An else statement would be ideal here in terms of performances but
-        # not all the users that logged in into Gitlab using the LDAP auth
-        # backend are part of the gnomecvs group while the opposite is always true
-        # as gnomecvs is effectively an LDAP POSIX group.
-        if username in gnomecvs_members:
-            group.members.create({'user_id': id,
-                                  'access_level': gitlab.DEVELOPER_ACCESS})
-
-            print 'Username with id %i has been added to the GNOME group' % id
-    except gitlab.exceptions.GitlabCreateError as e:
-       if e.response_code == 409:
-           pass
+            print 'Key for username with id %i has been added' % id
+        except gitlab.exceptions.GitlabCreateError as e:
+            if e.response_code == 400:
+                pass
+
+        try:
+            # An else statement would be ideal here in terms of performances but
+            # not all the users that logged in into Gitlab using the LDAP auth
+            # backend are part of the gnomecvs group while the opposite is always true
+            # as gnomecvs is effectively an LDAP POSIX group.
+            if username in gnomecvs_members:
+                group.members.create({'user_id': id,
+                                      'access_level': gitlab.DEVELOPER_ACCESS})
+
+                print 'Username with id %i has been added to the GNOME group' % id
+        except gitlab.exceptions.GitlabCreateError as e:
+            if e.response_code == 409:
+                pass
 
 for username, id in gnomeusers_dict.iteritems():
     if username not in gnomecvs_members:
diff --git a/gnome_ldap_utils.py b/gnome_ldap_utils.py
index 1975489..9a50d30 100755
--- a/gnome_ldap_utils.py
+++ b/gnome_ldap_utils.py
@@ -41,7 +41,7 @@ class Gnome_ldap_utils:
     def get_attributes_from_ldap(self, uid, attr, *attrs):
         import ldap.filter
 
-        results = [] 
+        results = []
 
         filter = ldap.filter.filter_format('(uid=%s)', (uid, ))
         if len(attrs) > 0:
@@ -61,7 +61,10 @@ class Gnome_ldap_utils:
         if len(results) > 0:
             return results
         elif len(result) > 0:
-            return result[0][1][attr][0]
+            try:
+                return result[0][1][attr][0]
+            except KeyError:
+                return None
         else:
             return None
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]