def create_new_user_content(): user_content = APIBridge.create_engagement() APIGitLab.git_clone_push(user_content) APIBridge.frontend_login( user_content['email'], Constants.Default.Password.TEXT) vfName = user_content['vfName'] uuid = user_content['uuid'] inviteEmail = Helper.rand_invite_email() newObj = [vfName, uuid, inviteEmail] return newObj, user_content
def set_ssh(user_content, sshKey=None): # Set SSH key to user. r1 = None if sshKey is None: logger.debug("About to generate an ssh key for the user: %s" % user_content['email']) sshKey = Helper.generate_sshpub_key() postURL = settings.ICE_EM_URL + '/v1/engmgr/users/ssh' logger.debug("Post user URL: " + postURL) headers = dict() # Create header for post request. headers['Content-type'] = 'application/json' headers['Authorization'] = user_content['session_token'] post_data = dict() # Create JSON data for post request. post_data['ssh_key'] = sshKey try: r1 = requests.post( postURL, json=post_data, headers=headers, verify=False) Helper.internal_assert(r1.status_code, 200) logger.debug( "SSH Key was added successfully") if not APIBridge.is_gitlab_ready(user_content): raise return sshKey except BaseException: if r1 is None: logger.error("Failed to add public SSH key.") else: logger.error( "POST request failed to add SSH key to user, " + "see response >>> %s %s" % (r1.status_code, r1.reason)) raise
class API: Bridge = APIBridge() Checklist = APIChecklist() GitLab = APIGitLab() Jenkins = APIJenkins() Rados = APIRados User = APIUser() VirtualFunction = APIVirtualFunction()
def signup_invited_user( company, invited_email, invite_token, invite_url, user_content, is_contact_user="******", activate=False, wait_for_gitlab=True): r1 = None postURL = settings.ICE_EM_URL + '/v1/engmgr/signup' logger.debug("Post signup URL: " + postURL) if is_contact_user == "true": fullName = re.sub("http.*full_name=", "", invite_url) fullName = re.sub("&.*", "", fullName) logger.debug( "Invited contact full name is (according to url): " + fullName) else: fullName = Helper.rand_string('randomString') post_data = dict() # Create JSON data for post request. post_data['company'] = company post_data['email'] = invited_email post_data['full_name'] = fullName post_data['invitation'] = invite_token post_data['is_contact_user'] = is_contact_user post_data['password'] = "******" post_data['phone_number'] = "+1201" + \ Helper.rand_string("randomNumber", 6) post_data['regular_email_updates'] = "False" post_data['terms'] = "True" try: requests.get(invite_url, verify=False) r1 = requests.post( postURL, json=post_data, verify=False) Helper.internal_assert(r1.status_code, 200) logger.debug("Invited user signed-up successfully!") user_data = r1.json() if activate: APIUser.activate_user( user_data['uuid'], user_data["user"]["activation_token"]) if wait_for_gitlab: if not APIBridge.is_gitlab_ready(user_content): raise return post_data except BaseException: if r1 is None: logger.error("Failed to sign up the invited team member.") else: logger.error( "POST request failed to sign up the invited " + "team member, see response >>> %s %s \n %s" % (r1.status_code, r1.reason, str( r1.content, 'utf-8'))) raise
def update_account(user_content): r1 = None token = APIUser.login_user(user_content['email']) user_content['session_token'] = 'token ' + token sshKey = Helper.generate_sshpub_key() putURL = settings.ICE_EM_URL + '/v1/engmgr/users/account' logger.debug("Put user URL: " + putURL) headers = dict() # Create header for put request. headers['Content-type'] = 'application/json' headers['Authorization'] = user_content['session_token'] # headers['Authorization'] = user_content['activation_token'] put_data = dict() # Create JSON data for put request. user_content['vendor'] = user_content['company']['name'] if user_content['vendor'] == "AT&T": put_data['company'] = "AT&T" else: put_data['company'] = user_content['vendor'] put_data['email'] = user_content['email'] put_data['full_name'] = user_content['full_name'] put_data['password'] = "" put_data['phone_number'] = "+1201" + \ Helper.rand_string("randomNumber", 6) put_data['ssh_key'] = sshKey try: r1 = requests.put( putURL, json=put_data, headers=headers, verify=False) Helper.internal_assert(r1.status_code, 200) logger.debug( "SSH Key was added successfully to user " + user_content['full_name']) if not APIBridge.is_gitlab_ready(user_content): raise return sshKey except BaseException: if r1 is None: logger.error("Failed to add public SSH key to user.") else: logger.error( "PUT request failed to add SSH key to user, see " + "response >>> %s %s \n %s" % (r1.status_code, r1.reason, str( r1.content, 'utf-8'))) raise
def update_account_injec_script(user_content): r1 = None putURL = settings.ICE_EM_URL + '/v1/engmgr/users/account' logger.debug("Put user URL: " + putURL) headers = dict() # Create header for put request. headers['Content-type'] = 'application/json' headers['Authorization'] = user_content['session_token'] put_data = dict() # Create JSON data for put request. if user_content['vendor'] == "AT&T": put_data['company'] = "AT&T" else: put_data['company'] = user_content['vendor'] put_data['email'] = user_content['email'] script = "<script>;</script>" put_data['full_name'] = script put_data['password'] = "" put_data['phone_number'] = "+1201" + \ Helper.rand_string("randomNumber", 6) try: r1 = requests.put( putURL, json=put_data, headers=headers, verify=False) Helper.internal_assert(r1.status_code, 200) msg = "Testing for Cross site scripting successfully : " + \ user_content['full_name'] + \ "stattus Code = " + str(r1.status_code) logger.debug(msg) if not APIBridge.is_gitlab_ready(user_content): raise return True except BaseException: if r1 is None: logger.error("Failed to add public SSH key to user.") else: logger.error( "PUT request failed to add SSH key to user, " + "see response >>> %s %s \n %s" % (r1.status_code, r1.reason, str( r1.content, 'utf-8'))) raise
def create_new_user_content_login_with_api(): user_content = APIBridge.create_engagement() APIGitLab.git_clone_push(user_content) token = "token " + APIBridge.login_user(user_content['el_email']) user_content['session_token'] = token return user_content
def git_clone_push(user_content): if settings.DATABASE_TYPE != 'local': logger.debug( "About to push files into project's repository on the " + "local folder(not over origin).") try: user_content['session_token'] = "token " + \ APIBridge.login_user(Constants.Users.Admin.EMAIL) used_email_for_actions = Constants.Users.Admin.EMAIL repo_dir = Constants.Paths.LocalGitFolder.PATH + \ user_content['vfName'] if not os.path.exists(repo_dir): os.makedirs(repo_dir) logger.debug("Created the following folder: %s" % repo_dir) # Create pair of keys for user. user_pub_key = Helper.get_or_create_rsa_key_for_admin() DBVirtualFunction.add_admin_to_eng_team( user_content['engagement_uuid']) # Set SSH Key for the user. APIBridge.set_ssh(user_content, user_pub_key) git_user = APIGitLab.get_git_user(used_email_for_actions) counter = 0 git_user_pub_key = None while user_pub_key != git_user_pub_key and counter < \ Constants.GitLabConstants.RETRIES_NUMBER: try: git_user_pub_key = APIGitLab.get_git_user_ssh_key( git_user['id']) except Exception as e: pass counter += 1 time.sleep(session.wait_until_time_pause) # Check that the SSH key was added to user on APIGitLab. if user_pub_key != git_user_pub_key: raise Exception( "The SSH Key received does not equal to the" " one provided! The key from" "APIGitLab:\n %s ==<>== %s" % (git_user_pub_key, user_pub_key)) gitRepoURL = "git@gitlab:%s/%s.git" % ( user_content['engagement_manual_id'], user_content['vfName']) logger.debug("Clone repo from: " + gitRepoURL) APIGitLab.is_gitlab_ready(user_content) cmd = 'cd ' + repo_dir + \ '; git config --global user.email \"' + Constants.\ Users.Admin.EMAIL + \ '\"; git config --global user.name \"' + \ Constants.Users.Admin.FULLNAME + '\";' # Commit all changes. p = subprocess_popen = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE) while subprocess_popen is None: logger.debug( "waiting to subprocess command to complete...") APIGitLab.display_output(p) # Clone project from APIGitLab. repo = git.Repo.clone_from(gitRepoURL, repo_dir) logger.debug("Successfully cloned repo to " + repo_dir) # Create three files (file0, file1, file2) and add them to git # index. for i in range(3): fileName = repo_dir + '/file' + str(i) with open(fileName, 'w') as content_file: os.chmod(fileName, 0o600) content_file.write("Test file " + fileName) repo.index.add([fileName]) logger.debug(fileName + " was created and added to commit list.") cmd = 'cd ' + repo_dir + \ '; git commit -a -m \"Create and add 3 files to git.\"' # Commit all changes. p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE) APIGitLab.display_output(p) logger.debug("All files added to commit list.") cmd = 'cd ' + repo_dir + '; git push' # Push commit to APIGitLab. p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE) APIGitLab.display_output(p) logger.debug("All files were pushed to APIGitLab.") except Exception as e: logger.error( "_-_-_-_-_- Unexpected error in git_clone_push: " + str(e)) raise Exception(e)