Skip to content

Commit

Permalink
feat: add support for using grant token
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufhm committed Jul 19, 2024
1 parent 6a45c83 commit 61e3f61
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
13 changes: 10 additions & 3 deletions api/plugins/action/token.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
from ..module_utils import token as LagoonToken
from ansible.plugins.action import ActionBase

Expand All @@ -15,6 +16,8 @@ def run(self, tmp=None, task_vars=None):
lagoon_ssh_private_key = task_vars.get('lagoon_ssh_private_key')
lagoon_ssh_private_key_file = task_vars.get('lagoon_ssh_private_key_file')

grant = self._task.args.get("grant", False)

if lagoon_ssh_private_key:
self._display.vvvv("writing private key to file")
if not lagoon_ssh_private_key_file:
Expand All @@ -26,15 +29,19 @@ def run(self, tmp=None, task_vars=None):
result['error'] = e
return result

self._display.vvvv(
f"lagoon_ssh_private_key_file: {lagoon_ssh_private_key_file}")
rc, result['token'], result['error'] = LagoonToken.fetch_token(
self._display.vvvv(f"lagoon_ssh_private_key_file: {lagoon_ssh_private_key_file}")
rc, grant_token, result['error'] = LagoonToken.fetch_token(
self._templar.template(task_vars.get('lagoon_ssh_host')),
self._templar.template(task_vars.get('lagoon_ssh_port')),
self._task.args.get('ssh_options', ""),
lagoon_ssh_private_key_file
)
if rc > 0:
result['failed'] = True
elif grant:
grant_token['expiry_time'] = time.time() + grant_token['expires_in']
result['token'] = grant_token
else:
result['token'] = grant_token['access_token']

return result
7 changes: 5 additions & 2 deletions api/plugins/module_utils/token.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import subprocess

import json
from typing import List, Union

def write_ssh_key(key_content: str, key_path: str):
Expand All @@ -25,12 +26,14 @@ def fetch_token(ssh_host, ssh_port, ssh_options: Union[str, List[str]], key_path

if key_path:
ssh_command.extend(['-i', key_path])
ssh_command.extend([f"lagoon@{ssh_host}", 'token'])
ssh_command.extend([f"lagoon@{ssh_host}", 'grant'])

try:
ssh_res = subprocess.run(ssh_command, capture_output=True, check=True)
except subprocess.CalledProcessError as e:
print(e.stderr)
print(e.stdout)
raise
return ssh_res.returncode, ssh_res.stdout.strip(), ssh_res.stderr

grant_token = json.loads(ssh_res.stdout.strip())
return ssh_res.returncode, grant_token, ssh_res.stderr
6 changes: 6 additions & 0 deletions api/plugins/modules/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
DOCUMENTATION = r'''
module: token
short_description: Fetches a Lagoon token using ssh
options:
grant:
description:
- Return the full grant token object, with expiry & refresh token.
type: bool
default: false
'''

EXAMPLES = r'''
Expand Down

0 comments on commit 61e3f61

Please sign in to comment.