|
43 | 43 | type: string
|
44 | 44 | state_file_dir:
|
45 | 45 | description: Directory to store state file for authentication.
|
| 46 | + default: ~/.config/bitwarden-sm-ansible |
46 | 47 | required: False
|
47 | 48 | type: string
|
48 | 49 | field:
|
|
64 | 65 | msg: "{{ lookup('bitwarden.secrets.lookup', 'cdc0a886-6ad6-4136-bfd4-b04f01149173', access_token='<your-access-token>') }}"
|
65 | 66 | - name: Use a state file for authentication
|
66 | 67 | 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') }}" |
68 | 69 | """
|
69 | 70 |
|
70 | 71 | RETURN = """
|
@@ -160,11 +161,13 @@ def validate_url(url: str, url_type: str) -> None:
|
160 | 161 | raise AnsibleError(INVALID_URL_ERROR.format(url_type, url))
|
161 | 162 |
|
162 | 163 |
|
163 |
| -def create_state_dir(state_file_dir: str): |
| 164 | +def create_state_dir(state_file_dir: str) -> Path: |
164 | 165 | try:
|
| 166 | + state_file_dir = os.path.expanduser(state_file_dir) |
165 | 167 | display.vv(f"Creating state directory: {state_file_dir}")
|
166 | 168 | state_dir = Path(state_file_dir)
|
167 | 169 | state_dir.mkdir(parents=True, exist_ok=True)
|
| 170 | + return state_dir |
168 | 171 | except PermissionError:
|
169 | 172 | raise AnsibleError(
|
170 | 173 | f"You do not have permission to create a directory at {state_file_dir}"
|
@@ -216,10 +219,10 @@ def _parse_access_token(self):
|
216 | 219 | self._encryption_key = base64.b64decode(encryption_key)
|
217 | 220 | except ValueError:
|
218 | 221 | display.error(
|
219 |
| - "Invalid access token envryption key. Should be base64-encoded" |
| 222 | + "Invalid access token encryption key. Should be base64-encoded" |
220 | 223 | )
|
221 | 224 | raise AccessTokenInvalidError(
|
222 |
| - "Invalid access token envryption key. Should be base64-encoded" |
| 225 | + "Invalid access token encryption key. Should be base64-encoded" |
223 | 226 | )
|
224 | 227 |
|
225 | 228 | if len(self._encryption_key) != 16:
|
@@ -376,12 +379,10 @@ def get_secret_data(
|
376 | 379 | )
|
377 | 380 |
|
378 | 381 | 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) |
385 | 386 | except AnsibleError as e:
|
386 | 387 | display.error(STATE_FILE_DIR_ERROR.format(e, state_file_dir))
|
387 | 388 | raise AnsibleError(STATE_FILE_DIR_ERROR.format(e, state_file_dir)) from e
|
|
0 commit comments