diff --git a/CHANGES.md b/CHANGES.md index 6bc9313..16786e2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,10 @@ # Change Log +## 2.3.0 + +- Add new `api_token` auth method. This authentication method is different than a `pat` authentication request. (PR #54) +- Added `pat` and `cookie` auth methods to the sensors. + ## 2.2.0 - Adjust jql in sensor to better support large JIRA projects diff --git a/README.md b/README.md index b862c78..bfa8060 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,11 @@ Include the following settings when using the `PAT` auth_method: * ``token`` - PAT token +Include the following settings when using the `api_token` auth_method: + +* ``token`` - api token +* ``username`` - Username + If using the `oauth` auth_method, take a look at the OAuth section below for further setup instructions. You can also use dynamic values from the datastore. See the diff --git a/actions/lib/base.py b/actions/lib/base.py index 062bbc9..262ef6c 100644 --- a/actions/lib/base.py +++ b/actions/lib/base.py @@ -1,4 +1,5 @@ from jira import JIRA +import base64 # from st2common.runners.base_action import Action __all__ = [ @@ -50,9 +51,15 @@ def _get_client(self): basic_creds = (config['username'], config['password']) client = JIRA(options=options, auth=basic_creds) + elif auth_method == 'api_token': + headers = JIRA.DEFAULT_OPTIONS["headers"].copy() + b64_header = base64.b64encode(f"{config['username']}:{config['token']}".encode()) + headers["Authorization"] = f"Basic {b64_header.decode()}" + client = JIRA(server=config['url'], options={"headers": headers}) + else: msg = ('You must set auth_method to either "oauth", ', - '"basic", or "cookie" in your Jira pack config file.') + '"basic", "pat", "api_token", or "cookie" in your Jira pack config file.') raise Exception(msg) return client diff --git a/config.schema.yaml b/config.schema.yaml index 1494b65..d78be64 100644 --- a/config.schema.yaml +++ b/config.schema.yaml @@ -19,8 +19,9 @@ - basic - pat - cookie + - api_token username: - description: "Username if using the basic or cookie auth_method" + description: "Username if using the basic, api_token, or cookie auth_method" type: "string" secret: false required: false @@ -30,7 +31,7 @@ secret: true required: false token: - description: "PAT token" + description: "PAT or API token" type: "string" secret: true required: false diff --git a/pack.yaml b/pack.yaml index 8ee7255..03d7479 100644 --- a/pack.yaml +++ b/pack.yaml @@ -6,7 +6,7 @@ keywords: - issues - ticket management - project management -version: 2.2.0 +version: 2.3.0 python_versions: - "3" author : StackStorm, Inc. diff --git a/sensors/jira_sensor.py b/sensors/jira_sensor.py index 120074a..bd39740 100644 --- a/sensors/jira_sensor.py +++ b/sensors/jira_sensor.py @@ -1,6 +1,6 @@ # See ./requirements.txt for requirements. import os - +import base64 from jira.client import JIRA from st2reactor.sensor.base import PollingSensor @@ -61,9 +61,25 @@ def setup(self): self._jira_client = JIRA(options={'server': self._jira_url}, basic_auth=basic_creds) + elif auth_method == 'pat': + headers = JIRA.DEFAULT_OPTIONS["headers"].copy() + headers["Authorization"] = f"Bearer {self._config['token']}" + self._jira_client = JIRA(server=self._jira_url, options={"headers": headers}) + + elif auth_method == 'cookie': + basic_creds = (self._config['username'], self._config['password']) + self._jira_client = JIRA(server=self._jira_url, auth=basic_creds) + + elif auth_method == 'api_token': + headers = JIRA.DEFAULT_OPTIONS["headers"].copy() + b64_header = base64.b64encode(f"{self._config['username']}:{self._config['token']}" + .encode()) + headers["Authorization"] = f"Basic {b64_header.decode()}" + self._jira_client = JIRA(server=self._jira_url, options={"headers": headers}) + else: - msg = ('You must set auth_method to either "oauth"', - 'or "basic" your jira.yaml config file.') + msg = ('You must set auth_method to either "oauth", ', + '"basic", "pat", "api_token", or "cookie" in your Jira pack config file.') raise Exception(msg) if self._projects_available is None: diff --git a/sensors/jira_sensor_for_apiv2.py b/sensors/jira_sensor_for_apiv2.py index 4b488e4..83ccd57 100644 --- a/sensors/jira_sensor_for_apiv2.py +++ b/sensors/jira_sensor_for_apiv2.py @@ -1,5 +1,6 @@ # See ./requirements.txt for requirements. import os +import base64 from jira.client import JIRA @@ -61,9 +62,25 @@ def setup(self): self._jira_client = JIRA(options={'server': self._jira_url}, basic_auth=basic_creds) + elif auth_method == 'pat': + headers = JIRA.DEFAULT_OPTIONS["headers"].copy() + headers["Authorization"] = f"Bearer {self._config['token']}" + self._jira_client = JIRA(server=self._jira_url, options={"headers": headers}) + + elif auth_method == 'cookie': + basic_creds = (self._config['username'], self._config['password']) + self._jira_client = JIRA(server=self._jira_url, auth=basic_creds) + + elif auth_method == 'api_token': + headers = JIRA.DEFAULT_OPTIONS["headers"].copy() + b64_header = base64.b64encode(f"{self._config['username']}:{self._config['token']}" + .encode()) + headers["Authorization"] = f"Basic {b64_header.decode()}" + self._jira_client = JIRA(server=self._jira_url, options={"headers": headers}) + else: - msg = ('You must set auth_method to either "oauth"', - 'or "basic" your jira.yaml config file.') + msg = ('You must set auth_method to either "oauth", ', + '"basic", "pat", "api_token", or "cookie" in your Jira pack config file.') raise Exception(msg) if self._projects_available is None: