create_users.py 3.8 KB

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