Skip to content

Adding PAT based auth for JIRA #542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion workflows/jira/versions/0.0.1/images/jira-manager/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ RUN apk add --no-cache libffi libffi-dev
RUN /usr/local/bin/python -m pip install --upgrade pip
RUN pip3 install --no-cache-dir jira

WORKDIR /jira
COPY lib/. /jira/

ENTRYPOINT ["python", "/jira_issue_manager.py"]
ENTRYPOINT ["python", "jira_issue_manager.py"]
CMD [""]
50 changes: 50 additions & 0 deletions workflows/jira/versions/0.0.1/images/jira-manager/lib/jira_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from jira import JIRA
from step_utility import StepUtility
import sys

class JiraAuth:
def __init__(self, environment: 'Environment'):
self.environment = environment
self.auth_methods = {
"basic_auth": self.basic_auth_func,
"token_auth": self.token_auth_func,
"oauth": self.oauth_func,
"jwt": self.jwt_auth_func
}

def do_auth(self) -> JIRA:
if self.environment.auth_method not in self.auth_methods:
StepUtility.printFail("Exiting Step - Invalid AUTH method - Options: basic_auth/token_auth/oauth/jwt")
sys.exit(1)

res = self.auth_methods.get(self.environment.auth_method)()
return JIRA(self.environment.jira_base_url, **res)

def basic_auth_func(self) -> dict:
if not self.environment.jira_username or not self.environment.jira_api_key:
StepUtility.printFail("Exiting Step - basic auth needs username and API key")
sys.exit(1)
return {
'basic_auth': (self.environment.jira_username, self.environment.jira_api_key)
}

def token_auth_func(self) -> dict:
if not self.environment.jira_pat_token:
StepUtility.printFail("Exiting Step - token auth needs PAT token")
sys.exit(1)
return {
'token_auth': self.environment.jira_pat_token
}

def oauth_func(self):
StepUtility.printFail("Exiting Step - OAuth not implemented")
sys.exit(1)

def jwt_auth_func(self):
StepUtility.printFail("Exiting Step - JWT Auth not implemented")
sys.exit(1)





Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
from jira import JIRA
from step_utility import StepUtility
from requests.auth import HTTPBasicAuth
from jira_auth import JiraAuth

class Environment:
def __init__(self, jira_base_url, jira_username, jira_api_key, action,
def __init__(self, jira_base_url, auth_method, jira_username, jira_api_key, jira_pat_token, action,
issue, issue_project, issue_summary, issue_description, issue_type, issue_components, issue_customfields,
existing_comment_id, comment_body, status, jql_query, jql_query_max_results,
verbose):
self.jira_base_url = jira_base_url
self.auth_method = auth_method
self.jira_username = jira_username
self.jira_api_key = jira_api_key
self.jira_pat_token = jira_pat_token
self.action = action
self.issue = issue
self.issue_project = issue_project
Expand All @@ -43,8 +46,10 @@ def environment_setup():
# Grab all of the environment variables
env = os.environ
jira_base_url = StepUtility.getEnvironmentVariable('JIRA_BASE_URL', env)
jira_auth_method = StepUtility.getEnvironmentVariable('JIRA_AUTH_METHOD', env)
jira_username = StepUtility.getEnvironmentVariable('JIRA_USERNAME', env)
jira_api_key = StepUtility.getEnvironmentVariable('JIRA_API_KEY', env)
jira_pat_token = StepUtility.getEnvironmentVariable('JIRA_PAT_TOKEN', env)
action = StepUtility.getEnvironmentVariable('ACTION', env)

# Logic here to use the regex to grab the jira issue key and assign it to issue
Expand Down Expand Up @@ -98,8 +103,10 @@ def environment_setup():

current_environment = Environment(
jira_base_url,
jira_auth_method,
jira_username,
jira_api_key,
jira_pat_token,
action,
issue,
issue_project,
Expand All @@ -116,14 +123,10 @@ def environment_setup():
verbose)
return current_environment

def authentication(current_environment) -> JIRA:
auth = JiraAuth(current_environment)
return auth.do_auth()

def authentication(current_environment):
# Basic authentication with an API Token
jira = JIRA(
current_environment.jira_base_url,
basic_auth=(current_environment.jira_username, current_environment.jira_api_key)
)
return jira


def step_action(action, authenticated_jira, current_environment):
Expand Down
Loading