summaryrefslogtreecommitdiffstats
path: root/get-gitlab-keys
diff options
context:
space:
mode:
Diffstat (limited to 'get-gitlab-keys')
-rwxr-xr-xget-gitlab-keys47
1 files changed, 47 insertions, 0 deletions
diff --git a/get-gitlab-keys b/get-gitlab-keys
new file mode 100755
index 0000000..2a84cbc
--- /dev/null
+++ b/get-gitlab-keys
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+import os
+import argparse
+import pathlib
+
+import gitlab
+
+
+def get_config_files():
+ files = []
+ for cfg in ['gitlab.cfg', '~/.gitlab.cfg', '/etc/gitlab.conf']:
+ path = os.path.expanduser(cfg)
+ if os.path.exists(path):
+ files.append(path)
+ return files
+
+
+def write_keys(directory, user):
+ filename = '%s.pub' % user.username.lower()
+ with open(os.path.join(directory, filename), 'w+') as keyfile:
+ for key in user.keys.list():
+ keyfile.write('%s\n' % key.key)
+
+
+def main():
+ parser = argparse.ArgumentParser(
+ description='Get all public ssh keys of all '
+ 'members of a gitlab group.')
+ parser.add_argument('group_id', metavar='GROUP_ID', type=int,
+ help='id of the gitlab group')
+ parser.add_argument('path', metavar='PATH', type=pathlib.Path,
+ help='path to the output directory')
+ args = parser.parse_args()
+
+ if not os.path.exists(args.path):
+ os.makedirs(args.path)
+
+ gl = gitlab.Gitlab.from_config('gitlab', get_config_files())
+
+ group = gl.groups.get(args.group_id)
+ for member in group.members.all(all=True):
+ if member.access_level >= gitlab.const.DEVELOPER_ACCESS:
+ user = gl.users.get(member.id)
+ write_keys(args.path, user)
+
+if __name__ == '__main__':
+ main()