def command(self): if not self.options.noinput: password = None for _n in range(3): print "Insert the new password, the spaces will be stripped" password_1 = getpass("password [1]: ").strip() password_2 = getpass("password [2]: ").strip() if password_1 and password_2 and password_1 == password_2: password = password_1 break else: print "Both passwords doesn't match or any of them is empty\n" if not password: print "You can't set the password, please retry later" sys.exit(1) else: password = password_generator() print "The generated password is \n{0}\n".format(password) try: self.pyramid.userdb.create_user( self.options.username, password, self.options.email, {'is_superuser': self.options.is_superuser} ) except UserAlreadyExists: print "The user already exists" else: print "\nThe user was created"
def command(self): if not self.options.noinput: password = None for _n in range(3): print "Insert the new password, the spaces will be stripped" password_1 = getpass("password [1]: ").strip() password_2 = getpass("password [2]: ").strip() if password_1 and password_2 and password_1 == password_2: password = password_1 break else: print "Both passwords doesn't match or any of them is empty\n" if not password: print "You can't set the password, please retry later" sys.exit(1) else: password = password_generator() print "The generated password is \n{0}\n".format(password) try: self.pyramid.userdb.change_password(self.options.username, password) except UserDoesNotExist: print "\nThe user does not exist" else: print "\nThe password was changed"
def command(self): if not self.options.noinput: password = None for _n in range(3): print("Insert the new password, the spaces will be stripped") password_1 = getpass("password [1]: ").strip() password_2 = getpass("password [2]: ").strip() if password_1 and password_2 and password_1 == password_2: password = password_1 break else: print("Both passwords doesn't match or any of them is " "empty\n") if not password: print("You can't set the password, please retry later") sys.exit(1) else: password = password_generator() print("The generated password is \n{0}\n".format(password)) try: self.pyramid.userdb.create_user( self.options.username, password, self.options.email, {'is_superuser': self.options.is_superuser}) except UserAlreadyExists: print("The user already exists") else: print("\nThe user was created")
def create_password(self, msg_input, msg_noinput): if not self.options.noinput: password = None for _n in range(3): print msg_input password_1 = getpass("password [1]: ").strip() password_2 = getpass("password [2]: ").strip() if password_1 and password_2 and password_1 == password_2: password = password_1 break else: print "Both passwords doesn't match or any of them is empty\n" if not password: print "You can't set the password, please retry later" sys.exit(1) else: password = password_generator() print msg_noinput.format(password) return password
def create_password(self, msg_input, msg_noinput): if not self.options.noinput: password = None for n in range(3): print msg_input password_1 = getpass("password [1]: ").strip() password_2 = getpass("password [2]: ").strip() if password_1 and password_2 and password_1 == password_2: password = password_1 break else: print "Both passwords doesn't match or any of them is empty\n" if not password: print "You can't set the password, please retry later" sys.exit(1) else: password = password_generator() print msg_noinput.format(password) return password
def command(self): api = _get_chef_api(self.settings.get('chef.url'), toChefUsername(self.options.chef_username), self.options.chef_pem, False, self.settings.get('chef.version')) print '============ CHECKING ADMINISTRATOR USERS =============' # Check if all the GECOS CC administrators # are properly created in Chef 12 admin_users = self.pyramid.userdb.list_users() for admin_user in admin_users: print 'Checking admin user: %s'%(admin_user['username']) # The email must be unique users_with_email = self.pyramid.userdb.list_users({'email': admin_user['email']}) if users_with_email.count() > 1: print "ERROR: more than one user with this email: %s"%(admin_user['email']) # Get the Chef user chef_user = None try: chef_user = api['/users/%s' % toChefUsername(admin_user['username'])] except ChefServerNotFoundError: pass if chef_user is None: # No chef user found print "WARNING: No Chef user found. We will try to create it!" chef_password = password_generator() try: create_chef_admin_user(api, self.settings, toChefUsername(admin_user['username']), chef_password, admin_user['email']) except ChefServerError, e: print "ERROR: User not created in chef, error was: %s" % e print "(Check /opt/opscode/embedded/service/opscode-erchef/log/requests.log* for more info)" sys.exit(1) chef_user = api['/users/%s' % toChefUsername(admin_user['username'])] # Check the email of the chef user if chef_user['email'] != admin_user['email']: print "WARNING: The chef user email and the GECOS CC user email doesn't match!" print "Try to change the chef user email!" chef_user['email'] = admin_user['email'] api.api_request('PUT', '/users/%s'%(toChefUsername(admin_user['username'])), data=chef_user) # Check if the administrator belongs to the "admins" group in the "default" organization admins_group = None try: admins_group = api['/organizations/default/groups/admins'] except ChefServerNotFoundError: pass if not toChefUsername(admin_user['username']) in admins_group['users']: print "WARNING: GECOS administrator is not a chef administrator for the default organization. We will try to change this!" # Check if exists an association request for this user assoc_requests = None try: assoc_requests = api['/organizations/default/association_requests'] except ChefServerNotFoundError: pass association_id = None for req in assoc_requests: if req["username"] == toChefUsername(admin_user['username']): association_id = req["id"] if association_id is None: # Set an association request for the user in that organization try: data = {"user": toChefUsername(admin_user['username'])} response = api.api_request('POST', '/organizations/default/association_requests', data=data) association_id = response["uri"].split("/")[-1] except ChefServerError: # Association already exists? pass if association_id is not None: # Accept the association request api.api_request('PUT', '/users/%s/association_requests/%s'%(toChefUsername(admin_user['username']), association_id), data={ "response": 'accept' }) # Add the user to the group admins_group['users'].append(toChefUsername(admin_user['username'])) api.api_request('PUT', '/organizations/default/groups/admins', data={ "groupname": admins_group["groupname"], "actors": { "users": admins_group['users'], "groups": admins_group["groups"] } })
def command(self): api = _get_chef_api(self.settings.get('chef.url'), toChefUsername(self.options.chef_username), self.options.chef_pem, False, self.settings.get('chef.version')) print '============ CHECKING ADMINISTRATOR USERS =============' # Check if all the GECOS CC administrators # are properly created in Chef 12 admin_users = self.pyramid.userdb.list_users() for admin_user in admin_users: print 'Checking admin user: %s' % (admin_user['username']) # The email must be unique users_with_email = self.pyramid.userdb.list_users( {'email': admin_user['email']}) if users_with_email.count() > 1: print "ERROR: more than one user with this email: %s" % ( admin_user['email']) # Get the Chef user chef_user = None try: chef_user = api['/users/%s' % toChefUsername(admin_user['username'])] except ChefServerNotFoundError: pass if chef_user is None: # No chef user found print "WARNING: No Chef user found. We will try to create it!" chef_password = password_generator() try: create_chef_admin_user( api, self.settings, toChefUsername(admin_user['username']), chef_password, admin_user['email']) except ChefServerError, e: print "ERROR: User not created in chef, error was: %s" % e print "(Check /opt/opscode/embedded/service/opscode-erchef/log/requests.log* for more info)" sys.exit(1) chef_user = api['/users/%s' % toChefUsername(admin_user['username'])] # Check the email of the chef user if chef_user['email'] != admin_user['email']: print "WARNING: The chef user email and the GECOS CC user email doesn't match!" print "Try to change the chef user email!" chef_user['email'] = admin_user['email'] api.api_request('PUT', '/users/%s' % (toChefUsername(admin_user['username'])), data=chef_user) # Check if the administrator belongs to the "admins" group in the "default" organization admins_group = None try: admins_group = api['/organizations/default/groups/admins'] except ChefServerNotFoundError: pass if not toChefUsername( admin_user['username']) in admins_group['users']: print "WARNING: GECOS administrator is not a chef administrator for the default organization. We will try to change this!" # Check if exists an association request for this user assoc_requests = None try: assoc_requests = api[ '/organizations/default/association_requests'] except ChefServerNotFoundError: pass association_id = None for req in assoc_requests: if req["username"] == toChefUsername( admin_user['username']): association_id = req["id"] if association_id is None: # Set an association request for the user in that organization try: data = {"user": toChefUsername(admin_user['username'])} response = api.api_request( 'POST', '/organizations/default/association_requests', data=data) association_id = response["uri"].split("/")[-1] except ChefServerError: # Association already exists? pass if association_id is not None: # Accept the association request api.api_request( 'PUT', '/users/%s/association_requests/%s' % (toChefUsername( admin_user['username']), association_id), data={"response": 'accept'}) # Add the user to the group admins_group['users'].append( toChefUsername(admin_user['username'])) api.api_request('PUT', '/organizations/default/groups/admins', data={ "groupname": admins_group["groupname"], "actors": { "users": admins_group['users'], "groups": admins_group["groups"] } })