Skip to content
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

VIRA-293: allow access token auth #77

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ one of the options `password` or `password_cmd` will be set depending on a
- `password` - Enter Jira server password in plain text. This is not
recommended for security reasons, but we're not going to tell you how to live
your life.
- `access_token_cmd` - Run a CLI command to retrieve the Jira server personal access token e.g. [sops -d /home/username/access_token.sops](https://github.com/mozilla/sops) or a password manager ala `pass` or `lpass`.
- `access_token` - Use a personal access token, not recommended for security reasons. Seriously, just sops encrypt a file with a gpg/aws-kms/gcp-kms/* key or use CLI's offered by _1Password_ or _LastPass_.
- `skip_cert_verify` - This option can be set in order to connect to a sever
that is using self-signed TLS certificates.

Expand Down
44 changes: 30 additions & 14 deletions python/Vira/vira_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ def connect(self, server):
self.versions = set()
self.users_type = ''

auth_kwargs = {}
option_kwargs = {}
try:
# Specify whether the server's TLS certificate needs to be verified
if self.vira_servers[server].get('skip_cert_verify'):
Expand All @@ -186,12 +188,21 @@ def connect(self, server):
cert_verify = True

# Get auth for current server
username = self.vira_servers[server].get('username')
password_cmd = self.vira_servers[server].get('password_cmd')
if password_cmd:
password = run_command(password_cmd)['stdout'].strip().split('\n')[0]
if not self.vira_servers[server].get('access_token_cmd') and not self.vira_servers[server].get('access_token'):
username = self.vira_servers[server].get('username')
password_cmd = self.vira_servers[server].get('password_cmd')
if password_cmd:
password = run_command(password_cmd)['stdout'].strip().split('\n')[0]
else:
password = self.vira_servers[server]['password']
auth_kwargs['basic_auth'] == (username, password)
else:
password = self.vira_servers[server]['password']
token_cmd = self.vira_servers[server].get('access_token_cmd')
if token_cmd:
access_token = run_command(token_cmd)['stdout'].strip().split('\n')[0]
else:
access_token = self.vira_servers[server]['access_token']
option_kwargs={"headers": {"Authorization": "Bearer %s" % access_token}}
except:
self.msg_server_fail()
raise
Expand All @@ -202,16 +213,21 @@ def connect(self, server):
server = 'https://' + server
vim.command('let g:vira_serv = "' + server + '"')

option_kwargs.update({
'server': server,
'verify': cert_verify,
})

# Authorize
self.jira = JIRA(
options={
'server': server,
'verify': cert_verify,
},
basic_auth=(username, password),
timeout=2,
async_=True,
max_retries=2)
kwargs={
"timeout": 2,
"async_": True,
"max_retries": 2,
"options": option_kwargs,
}
kwargs.update(auth_kwargs)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of hate the way way I construct the final kwargs array, but I didn't want to import some deep-dict-merge module for this. So right now it's a bit ratty and probably fails cyclomatic complexity scores with the janky if/else loop earlier.

I don't think it's quite yet shit enough to refactor :P I do think that dictionary.update sucks by default and doesn't do what one expects (sensible recursive merge ala jsonpatch).


self.jira = JIRA(**kwargs)

# Initial list updates
self.users = self.get_users()
Expand Down