Guest User

Untitled

a guest
Oct 12th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import sys
  3. import optparse
  4. import ldap
  5. from suds.client import Client
  6.  
  7. from vidyo_disabler_config import LDAP_HOST, LDAP_USER, LDAP_PASS, VIDYO_API, \
  8. VIDYO_USER, VIDYO_PASS, VIDYO_EXCEPTIONS
  9.  
  10. def get_all_ldap_users(ldap_conn, verbose):
  11. # For vidyo, we basically care about all human users in LDAP, regardless of
  12. # which org they are in, since I think community members could have vidyo
  13. # accounts, as well as moco and mofo staff. We only need their e-mail
  14. # address. Doing one query and gathering *all* 3000+ users into a giant list
  15. # is far less expensive than checking each of the 1400+ users from vidyo
  16. # individually
  17. all_users = ldap_conn.search_s(
  18. 'dc=mozilla',
  19. ldap.SCOPE_SUBTREE,
  20. '(&(objectClass=inetOrgPerson)(!(employeeType=DISABLED))\
  21. (|(o:dn:=org)(o:dn:=com)(o:dn:=net)))',
  22. attrlist=['mail'])
  23. ldap_users = []
  24. for user in all_users:
  25. ldap_users.append(user[1]['mail'][0])
  26.  
  27. if verbose:
  28. print "LDAP users:"
  29. print ldap_users
  30. return ldap_users
  31.  
  32.  
  33. def get_vidyo_users(vidyo_client, verbose):
  34. # The getMembers method of the API returns both the total number of users
  35. # as well as up to 200 users at a time. Here's my attempt at a simple
  36. # pagination fix in order to get all the users.
  37. # The getMembers method of the vidyo portal API takes the following
  38. # parameters:
  39. #(Filter){
  40. # start = None
  41. # limit = None
  42. # sortBy = None
  43. # dir =
  44. # (sortDir){
  45. # value = None
  46. # }
  47. # query = None
  48. # }
  49. # We don't care about most of them, but first we just need to get the total
  50. # number of users, so we set filter.limit to 1 to just get the minimum
  51. # amount of data. Every call to getMembers returns the total number of
  52. # users
  53.  
  54. filter_param = vidyo_client.factory.create('Filter')
  55. filter_param.limit = 1
  56.  
  57. total_accounts = vidyo_client.service.getMembers(filter_param).total
  58.  
  59. # Now that we know the total number of users in vidyo, we can form a simple
  60. # while loop to grab 200 at a time, which is the max limit that the API will
  61. # return at once. So start at 0 position, then increment the position by 200
  62. # at the end of each iteration, while subtracting 200, so we loop until
  63. # there none left.
  64. max_results = 200
  65. filter_param.limit = max_results
  66. filter_param.start = 0
  67. filter_param.sortBy = 'name'
  68. member_dict = {}
  69. while total_accounts > 0:
  70. resp = vidyo_client.service.getMembers(filter_param)
  71. for member in resp.member:
  72. # It seems that human LDAP users have name and emailAddress set to
  73. # the same thing. Non-human accounts, like conference rooms and such
  74. # don't seem to have that similarity, and we probably don't care
  75. # about those anyway, so to make things easy, let's only look at
  76. # users where the "name" attribute is identical to emailAddress
  77. # attribute.
  78. if member.name == member.emailAddress:
  79. member_dict[member.name] = member.memberID
  80. total_accounts -= max_results
  81. filter_param.start += max_results
  82.  
  83. # We care about the member.name (username/email) for comparing to LDAP, but
  84. # we need the memberID in order to delete a user, so we return a dict with
  85. # both
  86. if verbose:
  87. print "vidyo users:"
  88. print member_dict
  89. return member_dict
  90.  
  91. def delete_vidyo_member(vidyo_client, member, member_id, commit):
  92. print "deleting %s" % member
  93. if commit:
  94. # fix me. For testing and initial review, don't actually delete yet
  95. print "for real"
  96. #vidyo_client.service.deleteMember(member_id)
  97.  
  98. def main(prog_args=None):
  99.  
  100. if prog_args is None:
  101. prog_args = sys.argv
  102. # command line options. For Cron usage, we probably want only --commit.
  103. # For debug purposes, --verbose is more helpful
  104. parser = optparse.OptionParser()
  105. parser.usage = "Script to generate LDAP groups from search filters"
  106. parser.add_option('-v', '--verbose',
  107. action='store_true',
  108. default=False,
  109. dest='verbose',
  110. help='verbose output')
  111. parser.add_option('--commit',
  112. action='store_true',
  113. default=False,
  114. dest='commit',
  115. help='run script in commit mode')
  116.  
  117. options, args = parser.parse_args(sys.argv[1:])
  118.  
  119. commit = options.commit
  120. verbose = options.verbose
  121.  
  122. # Using suds, initialize a SOAP client for the vidyo portal. Apparently it
  123. # only works with username and password.
  124. vidyo_client = Client(VIDYO_API, username=VIDYO_USER, password=VIDYO_PASS)
  125.  
  126. # Main LDAP connection. This is used to get users from LDAP
  127. ldap_conn = ldap.initialize('ldap://%s' % LDAP_HOST)
  128. ldap_conn.start_tls_s()
  129. ldap_conn.simple_bind_s(LDAP_USER, LDAP_PASS)
  130.  
  131. # We pretty much just need a list active users from vidyo and a list of
  132. # active users from LDAP in order to compare
  133. all_ldap_users = get_all_ldap_users(ldap_conn, verbose)
  134. all_vidyo_users = get_vidyo_users(vidyo_client, verbose)
  135.  
  136. # For vidyo users, we get a dict back, because we want to display the e-mail
  137. # address of the user, but the deleteMember method needs the memberID.
  138. # Iterate through the email/memberid pairs checking each user against active
  139. # LDAP users, and if not found, and not in the exceptions list, delete.
  140. for member, member_id in all_vidyo_users.items():
  141. if member not in all_ldap_users:
  142. if member not in VIDYO_EXCEPTIONS:
  143. delete_vidyo_member(vidyo_client, member, member_id, commit)
  144.  
  145.  
  146. if __name__ == "__main__":
  147. main()
Add Comment
Please, Sign In to add comment