"""Jira utility functions.""" from jira import JIRA from jira import JIRAError from lambdacommon.common_config import logger import config def attach_to_jira_issue(jira_issue, audit_log_file_path, user): """ Attach audit log to Jira issue. Args: jira_issue (str): name of Jira issue to use, e.g. DEVEX-123 audit_log_file_path (str): local path to audit log file user (str): name of user who broke glass Returns: str: Timestamp that audit log was attached """ try: jira = JIRA(config.JIRA_BASE_URL, basic_auth=( config.JIRA_EMAIL_ADDRESS, config.JIRA_API_TOKEN)) attachment_response = jira.add_attachment( issue=jira_issue, attachment=audit_log_file_path) comment = f'Attached AWS break-glass tool audit log for {user}' jira.add_comment(jira_issue, comment) return attachment_response.created except JIRAError as error: # Jira throws an exception when the issue does not exist if error.status_code == 404: logger.exception(f'Issue not found: {error.text}') else: logger.exception( f'Error attaching log to Jira issue: {error.text}') raise def verify_jira_issue(jira_issue): """ Look up Jira issue and verify it exists. Args: jira_issue (str): Name of Jira issue to look up, e.g. DEVEX-123 Returns: JIRA: Jira issue object """ try: jira = JIRA(config.JIRA_BASE_URL, basic_auth=( config.JIRA_EMAIL_ADDRESS, config.JIRA_API_TOKEN)) # Look up jira issue issue = jira.issue(jira_issue) return issue.permalink() except JIRAError as error: # Jira throws an exception when the issue does not exist if error.status_code == 404: logger.exception(f'Issue not found: {error.text}') else: logger.exception(f'Error looking up issue: {error.text}') raise