create_users.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/python
  2. #
  3. # Copyright (c) 2017-2018 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. AD_DN_BASE = 'cn=Users, dc=ad, dc=ciscolive, dc=network'
  33. DEFAULT_GROUP = 'NOC Users'
  34. AD_DOMAIN = 'ad.ciscolive.network'
  35. SPARK_TEAM = 'CL19 NOC Team'
  36. if __name__ == '__main__':
  37. spark = sparker.Sparker(token=CLEUCreds.SPARK_TOKEN)
  38. members = spark.get_members(SPARK_TEAM)
  39. #pyad.set_defaults(ldap_server=AD_DC, username=AD_USERNAME, password=AD_PASSWORD, ssl=True)
  40. ou = adcontainer.ADContainer.from_dn(AD_DN_BASE)
  41. if members is not None:
  42. for member in members['items']:
  43. m = re.search(r'([^@]+)@cisco.com$', member['personEmail'])
  44. if m:
  45. names = member['personDisplayName'].split(' ')
  46. fullname = names[0] + ' ' + names[-1]
  47. try:
  48. ad_user = aduser.ADUser.from_dn('cn={}, {}'.format(
  49. fullname, AD_DN_BASE))
  50. if ad_user is not None:
  51. sys.stderr.write(
  52. 'Not creating {} as they already exist.\n'.format(m.group(1)))
  53. continue
  54. except Exception:
  55. pass
  56. try:
  57. new_user = aduser.ADUser.create(
  58. fullname, ou, password=CLEUCreds.DEFAULT_USER_PASSWORD)
  59. except Exception as e:
  60. sys.stderr.write(
  61. "Failed to create user {}: {}\n".format(m.group(1), e))
  62. continue
  63. new_user.update_attribute('mail', member['personEmail'])
  64. try:
  65. new_user.update_attribute('sAMAccountName', m.group(1))
  66. new_user.update_attribute(
  67. 'userPrincipalName', '{}@{}'.format(m.group(1), AD_DOMAIN))
  68. except Exception:
  69. try:
  70. new_user.delete()
  71. sys.stderr.write(
  72. 'Error adding user {} (maybe duplicate?)\n'.format(m.group(1)))
  73. continue
  74. except:
  75. pass
  76. try:
  77. new_user.force_pwd_change_on_login()
  78. except Exception as e:
  79. sys.stderr.write('Error setting password policy for user {}: {}'.format(m.group(1), e))
  80. def_group = adgroup.ADGroup.from_cn(DEFAULT_GROUP)
  81. def_group.add_members([new_user])
  82. print('Added user {}'.format(m.group(1)))
  83. time.sleep(1)
  84. else:
  85. sys.stderr.write(
  86. 'Unable to get members from Spark.\nMake sure the bot is part of the Spark team.\n')