NixOS configuration for HPC cluster https://docs.hpc.informatik.hs-fulda.de/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.1 KiB

  1. import click
  2. import ssl
  3. from ldap3 import Server, Connection, Tls
  4. @click.group()
  5. @click.option('--server', default='edir1.rz.hs-fulda.de', help='LDAP server URL')
  6. @click.option('--username', prompt=True, default='cn=fdhpc,ou=AI,o=FH-Fulda', help='LDAP bind username')
  7. @click.option('--password', prompt=True, hide_input=True, help='LDAP bind password')
  8. @click.pass_context
  9. def cli(ctx, server, username, password):
  10. tls = Tls(validate=ssl.CERT_REQUIRED,
  11. version=ssl.PROTOCOL_TLSv1_2,
  12. ciphers="AES256-GCM-SHA384")
  13. server = Server(server, tls=tls, get_info="ALL", use_ssl=True)
  14. ctx.obj = Connection(server,
  15. username,
  16. password,
  17. auto_bind=True)
  18. @cli.command()
  19. @click.pass_context
  20. def list(ctx):
  21. ctx.obj.search('o=FH-Fulda', '''(&
  22. (cn=fd*)
  23. (objectClass=inetOrgPerson)
  24. (groupMembership=cn=ORG-AI-HPC,ou=AI,o=FH-Fulda)
  25. (!
  26. (|
  27. (description=*funktion*)
  28. (loginDisabled=true)
  29. (sn=fd*)
  30. )
  31. )
  32. )''',
  33. attributes = ['cn', 'member', 'sn', 'givenName'])
  34. for e in ctx.obj.entries:
  35. click.echo(f'{click.style(e.cn, fg="blue", bold=True)}: {e.sn}, {e.givenName}')
  36. def find(ctx, name):
  37. from ldap3.utils.conv import escape_filter_chars
  38. ctx.obj.search('o=FH-Fulda', f'''(&
  39. (cn={escape_filter_chars(name)})
  40. (objectClass=inetOrgPerson)
  41. (!
  42. (|
  43. (description=*funktion*)
  44. (loginDisabled=true)
  45. (sn=fd*)
  46. )
  47. )
  48. )''')
  49. if not ctx.obj.entries:
  50. ctx.fail(f'No user found: {name}')
  51. return ctx.obj.entries[0].entry_dn
  52. @cli.command()
  53. @click.argument('name', nargs=-1, required=True)
  54. @click.pass_context
  55. def add(ctx, name):
  56. members = [find(ctx, name) for name in name]
  57. ctx.obj.extend.novell.add_members_to_groups(members, 'cn=ORG-AI-HPC,ou=AI,o=FH-Fulda')
  58. @cli.command()
  59. @click.pass_context
  60. @click.argument('name', nargs=-1, required=True)
  61. def remove(ctx, name):
  62. members = [find(ctx, name) for name in name]
  63. ctx.obj.extend.novell.remove_members_from_groups(members, 'cn=ORG-AI-HPC,ou=AI,o=FH-Fulda')
  64. if __name__ == '__main__':
  65. cli()