Skip to content

Commit 13385ee

Browse files
Review & update the Ansible integration (#51)
* feat: use state by default * feedback: reuse expanded state_dir * fix: typo
1 parent d722d4c commit 13385ee

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

plugins/lookup/lookup.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
type: string
4444
state_file_dir:
4545
description: Directory to store state file for authentication.
46+
default: ~/.config/bitwarden-sm-ansible
4647
required: False
4748
type: string
4849
field:
@@ -64,7 +65,7 @@
6465
msg: "{{ lookup('bitwarden.secrets.lookup', 'cdc0a886-6ad6-4136-bfd4-b04f01149173', access_token='<your-access-token>') }}"
6566
- name: Use a state file for authentication
6667
ansible.builtin.debug:
67-
msg: "{{ lookup('bitwarden.secrets.lookup', 'cdc0a886-6ad6-4136-bfd4-b04f01149173', state_file_dir='~/.config/bitwarden-sm') }}"
68+
msg: "{{ lookup('bitwarden.secrets.lookup', 'cdc0a886-6ad6-4136-bfd4-b04f01149173', state_file_dir='~/.config/bitwarden-sm-ansible') }}"
6869
"""
6970

7071
RETURN = """
@@ -160,11 +161,13 @@ def validate_url(url: str, url_type: str) -> None:
160161
raise AnsibleError(INVALID_URL_ERROR.format(url_type, url))
161162

162163

163-
def create_state_dir(state_file_dir: str):
164+
def create_state_dir(state_file_dir: str) -> Path:
164165
try:
166+
state_file_dir = os.path.expanduser(state_file_dir)
165167
display.vv(f"Creating state directory: {state_file_dir}")
166168
state_dir = Path(state_file_dir)
167169
state_dir.mkdir(parents=True, exist_ok=True)
170+
return state_dir
168171
except PermissionError:
169172
raise AnsibleError(
170173
f"You do not have permission to create a directory at {state_file_dir}"
@@ -216,10 +219,10 @@ def _parse_access_token(self):
216219
self._encryption_key = base64.b64decode(encryption_key)
217220
except ValueError:
218221
display.error(
219-
"Invalid access token envryption key. Should be base64-encoded"
222+
"Invalid access token encryption key. Should be base64-encoded"
220223
)
221224
raise AccessTokenInvalidError(
222-
"Invalid access token envryption key. Should be base64-encoded"
225+
"Invalid access token encryption key. Should be base64-encoded"
223226
)
224227

225228
if len(self._encryption_key) != 16:
@@ -376,12 +379,10 @@ def get_secret_data(
376379
)
377380

378381
try:
379-
if not state_file_dir:
380-
client.access_token_login(access_token.str)
381-
else:
382-
create_state_dir(state_file_dir)
383-
state_file = str(Path(state_file_dir, access_token.access_token_id))
384-
client.access_token_login(access_token.str, state_file)
382+
state_dir = create_state_dir(state_file_dir)
383+
state_file = str(state_dir / access_token.access_token_id)
384+
display.vv(f"state_file: {state_file}")
385+
client.access_token_login(access_token.str, state_file)
385386
except AnsibleError as e:
386387
display.error(STATE_FILE_DIR_ERROR.format(e, state_file_dir))
387388
raise AnsibleError(STATE_FILE_DIR_ERROR.format(e, state_file_dir)) from e

0 commit comments

Comments
 (0)