create_users.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/python
  2. #
  3. # Copyright (c) 2017-2019 Joe Clarke <jclarke@cisco.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. # SUCH DAMAGE.
  26. from pyad import *
  27. import sys
  28. import re
  29. import sparker
  30. import CLEUCreds
  31. import time
  32. from cleu.config import Config as C
  33. DEFAULT_GROUP = 'CL NOC Users'
  34. if __name__ == '__main__':
  35. spark = sparker.Sparker(token=CLEUCreds.SPARK_TOKEN)
  36. members = spark.get_members(C.WEBEX_TEAM)
  37. #pyad.set_defaults(ldap_server=AD_DC, username=AD_USERNAME, password=AD_PASSWORD, ssl=True)
  38. ou = adcontainer.ADContainer.from_dn(C.AD_DN_BASE)
  39. if members is not None:
  40. for member in members:
  41. m = re.search(r'([^@]+)@cisco.com$', member['personEmail'])
  42. if m:
  43. names = member['personDisplayName'].split(' ')
  44. fullname = names[0] + ' ' + names[-1]
  45. try:
  46. ad_user = aduser.ADUser.from_dn('cn={}, {}'.format(
  47. fullname, AD_DN_BASE))
  48. if ad_user is not None:
  49. sys.stderr.write(
  50. 'Not creating {} ({}) as they already exist.\n'.format(m.group(1), fullname))
  51. continue
  52. except Exception:
  53. pass
  54. try:
  55. new_user = aduser.ADUser.create(
  56. fullname, ou, password=CLEUCreds.DEFAULT_USER_PASSWORD)
  57. except Exception as e:
  58. sys.stderr.write(
  59. "Failed to create user {}: {}\n".format(m.group(1), e))
  60. continue
  61. new_user.update_attribute('mail', member['personEmail'])
  62. try:
  63. new_user.update_attribute('sAMAccountName', m.group(1))
  64. new_user.update_attribute(
  65. 'userPrincipalName', '{}@{}'.format(m.group(1), C.AD_DOMAIN))
  66. except Exception:
  67. try:
  68. new_user.delete()
  69. sys.stderr.write(
  70. 'Error adding user {} (maybe duplicate?)\n'.format(m.group(1)))
  71. continue
  72. except:
  73. pass
  74. try:
  75. new_user.force_pwd_change_on_login()
  76. except Exception as e:
  77. sys.stderr.write(
  78. 'Error setting password policy for user {}: {}'.format(m.group(1), e))
  79. def_group = adgroup.ADGroup.from_cn(DEFAULT_GROUP)
  80. def_group.add_members([new_user])
  81. print('Added user {}'.format(m.group(1)))
  82. time.sleep(1)
  83. else:
  84. sys.stderr.write(
  85. 'Unable to get members from Webex Teams.\nMake sure the bot is part of the Webex team.\n')