| 1 |
# Copyright (c) 2018, Scott Buchanan <[email protected]> |
| 2 |
# Copyright (c) 2016, Andrew Zenk <[email protected]> (lastpass.py used as starting point) |
| 3 |
# Copyright (c) 2018, Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
name: onepassword |
| 11 |
author: |
| 12 |
- Scott Buchanan (@scottsb) |
| 13 |
- Andrew Zenk (@azenk) |
| 14 |
- Sam Doran (@samdoran) |
| 15 |
short_description: Fetch field values from 1Password |
| 16 |
description: |
| 17 |
- P(community.general.onepassword#lookup) wraps the C(op) command line utility to fetch specific field values from 1Password. |
| 18 |
options: |
| 19 |
_terms: |
| 20 |
description: |
| 21 |
- Identifier(s) (case-insensitive UUID or name or secret reference) of item(s) to retrieve. |
| 22 |
- Secret references start with V(op://) and are supported since community.general 13.0.0. |
| 23 |
required: true |
| 24 |
type: list |
| 25 |
elements: string |
| 26 |
account_id: |
| 27 |
version_added: 7.5.0 |
| 28 |
domain: |
| 29 |
version_added: 3.2.0 |
| 30 |
field: |
| 31 |
description: |
| 32 |
- Field to return from each matching item (case-insensitive). |
| 33 |
- Ignored when using a secret reference, as the field is included in the secret reference. |
| 34 |
default: 'password' |
| 35 |
type: str |
| 36 |
service_account_token: |
| 37 |
version_added: 7.1.0 |
| 38 |
extends_documentation_fragment: |
| 39 |
- community.general._onepassword |
| 40 |
- community.general._onepassword.lookup |
| 41 |
""" |
| 42 |
|
| 43 |
EXAMPLES = r""" |
| 44 |
# These examples only work when already signed in to 1Password |
| 45 |
- name: Retrieve password for KITT when already signed in to 1Password |
| 46 |
ansible.builtin.debug: |
| 47 |
var: lookup('community.general.onepassword', 'KITT') |
| 48 |
|
| 49 |
- name: Retrieve password for Wintermute when already signed in to 1Password |
| 50 |
ansible.builtin.debug: |
| 51 |
var: lookup('community.general.onepassword', 'Tessier-Ashpool', section='Wintermute') |
| 52 |
|
| 53 |
- name: Retrieve username for HAL when already signed in to 1Password |
| 54 |
ansible.builtin.debug: |
| 55 |
var: lookup('community.general.onepassword', 'HAL 9000', field='username', vault='Discovery') |
| 56 |
|
| 57 |
- name: Retrieve password for HAL when not signed in to 1Password |
| 58 |
ansible.builtin.debug: |
| 59 |
var: lookup('community.general.onepassword', 'HAL 9000', subdomain='Discovery', master_password=vault_master_password) |
| 60 |
|
| 61 |
- name: Retrieve password for HAL when never signed in to 1Password |
| 62 |
ansible.builtin.debug: |
| 63 |
var: >- |
| 64 |
lookup('community.general.onepassword', 'HAL 9000', subdomain='Discovery', master_password=vault_master_password, |
| 65 |
username='[email protected]', secret_key=vault_secret_key) |
| 66 |
|
| 67 |
- name: Retrieve password from specific account |
| 68 |
ansible.builtin.debug: |
| 69 |
var: lookup('community.general.onepassword', 'HAL 9000', account_id='abc123') |
| 70 |
""" |
| 71 |
|
| 72 |
RETURN = r""" |
| 73 |
_raw: |
| 74 |
description: Field data requested. |
| 75 |
type: list |
| 76 |
elements: str |
| 77 |
""" |
| 78 |
|
| 79 |
import abc |
| 80 |
import json |
| 81 |
import os |
| 82 |
import subprocess |
| 83 |
|
| 84 |
from ansible.errors import AnsibleLookupError, AnsibleOptionsError |
| 85 |
from ansible.module_utils.common.process import get_bin_path |
| 86 |
from ansible.module_utils.common.text.converters import to_bytes, to_text |
| 87 |
from ansible.plugins.lookup import LookupBase |
| 88 |
|
| 89 |
from ansible_collections.community.general.plugins.module_utils._onepassword import OnePasswordConfig |
| 90 |
from ansible_collections.community.general.plugins.plugin_utils._lookup import check_for_wrong_terms |
| 91 |
|
| 92 |
|
| 93 |
def _lower_if_possible(value): |
| 94 |
"""Return the lower case version value, otherwise return the value""" |
| 95 |
try: |
| 96 |
return value.lower() |
| 97 |
except AttributeError: |
| 98 |
return value |
| 99 |
|
| 100 |
|
| 101 |
class OnePassCLIBase(metaclass=abc.ABCMeta): |
| 102 |
bin = "op" |
| 103 |
|
| 104 |
def __init__( |
| 105 |
self, |
| 106 |
subdomain=None, |
| 107 |
domain="1password.com", |
| 108 |
username=None, |
| 109 |
secret_key=None, |
| 110 |
master_password=None, |
| 111 |
service_account_token=None, |
| 112 |
account_id=None, |
| 113 |
connect_host=None, |
| 114 |
connect_token=None, |
| 115 |
): |
| 116 |
self.subdomain = subdomain |
| 117 |
self.domain = domain |
| 118 |
self.username = username |
| 119 |
self.master_password = master_password |
| 120 |
self.secret_key = secret_key |
| 121 |
self.service_account_token = service_account_token |
| 122 |
self.account_id = account_id |
| 123 |
self.connect_host = connect_host |
| 124 |
self.connect_token = connect_token |
| 125 |
|
| 126 |
self._path = None |
| 127 |
self._version = None |
| 128 |
|
| 129 |
def _check_required_params(self, required_params): |
| 130 |
non_empty_attrs = {param: getattr(self, param) for param in required_params if getattr(self, param, None)} |
| 131 |
missing = set(required_params).difference(non_empty_attrs) |
| 132 |
if missing: |
| 133 |
prefix = "Unable to sign in to 1Password. Missing required parameter" |
| 134 |
plural = "" |
| 135 |
suffix = f": {', '.join(missing)}." |
| 136 |
if len(missing) > 1: |
| 137 |
plural = "s" |
| 138 |
|
| 139 |
msg = f"{prefix}{plural}{suffix}" |
| 140 |
raise AnsibleLookupError(msg) |
| 141 |
|
| 142 |
@abc.abstractmethod |
| 143 |
def _parse_field(self, data_json, field_name, section_title): |
| 144 |
"""Main method for parsing data returned from the op command line tool""" |
| 145 |
|
| 146 |
def _run(self, args, expected_rc=0, command_input=None, ignore_errors=False, environment_update=None): |
| 147 |
command = [self.path] + args |
| 148 |
call_kwargs = { |
| 149 |
"stdout": subprocess.PIPE, |
| 150 |
"stderr": subprocess.PIPE, |
| 151 |
"stdin": subprocess.PIPE, |
| 152 |
} |
| 153 |
|
| 154 |
if environment_update: |
| 155 |
env = os.environ.copy() |
| 156 |
env.update(environment_update) |
| 157 |
call_kwargs["env"] = env |
| 158 |
|
| 159 |
p = subprocess.Popen(command, **call_kwargs) |
| 160 |
out, err = p.communicate(input=command_input) |
| 161 |
rc = p.wait() |
| 162 |
|
| 163 |
if not ignore_errors and rc != expected_rc: |
| 164 |
raise AnsibleLookupError(str(err)) |
| 165 |
|
| 166 |
return rc, out, err |
| 167 |
|
| 168 |
@abc.abstractmethod |
| 169 |
def assert_logged_in(self): |
| 170 |
"""Check whether a login session exists""" |
| 171 |
|
| 172 |
@abc.abstractmethod |
| 173 |
def full_signin(self): |
| 174 |
"""Performa full login""" |
| 175 |
|
| 176 |
@abc.abstractmethod |
| 177 |
def get_raw(self, item_id, vault=None, token=None): |
| 178 |
"""Gets the specified item from the vault""" |
| 179 |
|
| 180 |
@abc.abstractmethod |
| 181 |
def signin(self): |
| 182 |
"""Sign in using the master password""" |
| 183 |
|
| 184 |
@property |
| 185 |
def path(self): |
| 186 |
if self._path is None: |
| 187 |
self._path = get_bin_path(self.bin) |
| 188 |
|
| 189 |
return self._path |
| 190 |
|
| 191 |
@property |
| 192 |
def version(self): |
| 193 |
if self._version is None: |
| 194 |
self._version = self.get_current_version() |
| 195 |
|
| 196 |
return self._version |
| 197 |
|
| 198 |
@classmethod |
| 199 |
def get_current_version(cls): |
| 200 |
"""Standalone method to get the op CLI version. Useful when determining which class to load |
| 201 |
based on the current version.""" |
| 202 |
try: |
| 203 |
bin_path = get_bin_path(cls.bin) |
| 204 |
except ValueError as e: |
| 205 |
raise AnsibleLookupError(f"Unable to locate '{cls.bin}' command line tool") from e |
| 206 |
|
| 207 |
try: |
| 208 |
b_out = subprocess.check_output([bin_path, "--version"], stderr=subprocess.PIPE) |
| 209 |
except subprocess.CalledProcessError as cpe: |
| 210 |
raise AnsibleLookupError(f"Unable to get the op version: {cpe}") from cpe |
| 211 |
|
| 212 |
return to_text(b_out).strip() |
| 213 |
|
| 214 |
|
| 215 |
class OnePassCLIv2(OnePassCLIBase): |
| 216 |
""" |
| 217 |
CLIv2 Syntax Reference: https://developer.1password.com/docs/cli/upgrade#step-2-update-your-scripts |
| 218 |
""" |
| 219 |
|
| 220 |
supports_version = "2" |
| 221 |
|
| 222 |
def _parse_field(self, data_json, field_name, section_title=None): |
| 223 |
""" |
| 224 |
Schema reference: https://developer.1password.com/docs/cli/item-template-json |
| 225 |
|
| 226 |
Example Data: |
| 227 |
|
| 228 |
# Password item |
| 229 |
{ |
| 230 |
"id": "ywvdbojsguzgrgnokmcxtydgdv", |
| 231 |
"title": "Authy Backup", |
| 232 |
"version": 1, |
| 233 |
"vault": { |
| 234 |
"id": "bcqxysvcnejjrwzoqrwzcqjqxc", |
| 235 |
"name": "Personal" |
| 236 |
}, |
| 237 |
"category": "PASSWORD", |
| 238 |
"last_edited_by": "7FUPZ8ZNE02KSHMAIMKHIVUE17", |
| 239 |
"created_at": "2015-01-18T13:13:38Z", |
| 240 |
"updated_at": "2016-02-20T16:23:54Z", |
| 241 |
"additional_information": "Jan 18, 2015, 08:13:38", |
| 242 |
"fields": [ |
| 243 |
{ |
| 244 |
"id": "password", |
| 245 |
"type": "CONCEALED", |
| 246 |
"purpose": "PASSWORD", |
| 247 |
"label": "password", |
| 248 |
"value": "OctoberPoppyNuttyDraperySabbath", |
| 249 |
"reference": "op://Personal/Authy Backup/password", |
| 250 |
"password_details": { |
| 251 |
"strength": "FANTASTIC" |
| 252 |
} |
| 253 |
}, |
| 254 |
{ |
| 255 |
"id": "notesPlain", |
| 256 |
"type": "STRING", |
| 257 |
"purpose": "NOTES", |
| 258 |
"label": "notesPlain", |
| 259 |
"value": "Backup password to restore Authy", |
| 260 |
"reference": "op://Personal/Authy Backup/notesPlain" |
| 261 |
} |
| 262 |
] |
| 263 |
} |
| 264 |
|
| 265 |
# Login item |
| 266 |
{ |
| 267 |
"id": "awk4s2u44fhnrgppszcsvc663i", |
| 268 |
"title": "Dummy Login", |
| 269 |
"version": 2, |
| 270 |
"vault": { |
| 271 |
"id": "stpebbaccrq72xulgouxsk4p7y", |
| 272 |
"name": "Personal" |
| 273 |
}, |
| 274 |
"category": "LOGIN", |
| 275 |
"last_edited_by": "LSGPJERUYBH7BFPHMZ2KKGL6AU", |
| 276 |
"created_at": "2018-04-25T21:55:19Z", |
| 277 |
"updated_at": "2018-04-25T21:56:06Z", |
| 278 |
"additional_information": "agent.smith", |
| 279 |
"urls": [ |
| 280 |
{ |
| 281 |
"primary": true, |
| 282 |
"href": "https://acme.com" |
| 283 |
} |
| 284 |
], |
| 285 |
"sections": [ |
| 286 |
{ |
| 287 |
"id": "linked items", |
| 288 |
"label": "Related Items" |
| 289 |
} |
| 290 |
], |
| 291 |
"fields": [ |
| 292 |
{ |
| 293 |
"id": "username", |
| 294 |
"type": "STRING", |
| 295 |
"purpose": "USERNAME", |
| 296 |
"label": "username", |
| 297 |
"value": "agent.smith", |
| 298 |
"reference": "op://Personal/Dummy Login/username" |
| 299 |
}, |
| 300 |
{ |
| 301 |
"id": "password", |
| 302 |
"type": "CONCEALED", |
| 303 |
"purpose": "PASSWORD", |
| 304 |
"label": "password", |
| 305 |
"value": "Q7vFwTJcqwxKmTU]Dzx7NW*wrNPXmj", |
| 306 |
"entropy": 159.6083697084228, |
| 307 |
"reference": "op://Personal/Dummy Login/password", |
| 308 |
"password_details": { |
| 309 |
"entropy": 159, |
| 310 |
"generated": true, |
| 311 |
"strength": "FANTASTIC" |
| 312 |
} |
| 313 |
}, |
| 314 |
{ |
| 315 |
"id": "notesPlain", |
| 316 |
"type": "STRING", |
| 317 |
"purpose": "NOTES", |
| 318 |
"label": "notesPlain", |
| 319 |
"reference": "op://Personal/Dummy Login/notesPlain" |
| 320 |
} |
| 321 |
] |
| 322 |
} |
| 323 |
""" |
| 324 |
data = json.loads(data_json) |
| 325 |
field_name = _lower_if_possible(field_name) |
| 326 |
for field in data.get("fields", []): |
| 327 |
if section_title is None: |
| 328 |
# If the field name exists in the section, return that value |
| 329 |
if field.get(field_name): |
| 330 |
return field.get(field_name) |
| 331 |
|
| 332 |
# If the field name doesn't exist in the section, match on the value of "label" |
| 333 |
# then "id" and return "value" |
| 334 |
if field.get("label", "").lower() == field_name: |
| 335 |
return field.get("value", "") |
| 336 |
|
| 337 |
if field.get("id", "").lower() == field_name: |
| 338 |
return field.get("value", "") |
| 339 |
|
| 340 |
# Look at the section data and get an identifier. The value of 'id' is either a unique ID |
| 341 |
# or a human-readable string. If a 'label' field exists, prefer that since |
| 342 |
# it is the value visible in the 1Password UI when both 'id' and 'label' exist. |
| 343 |
section = field.get("section", {}) |
| 344 |
section_title = _lower_if_possible(section_title) |
| 345 |
|
| 346 |
current_section_title = section.get("label", section.get("id", "")).lower() |
| 347 |
if section_title == current_section_title: |
| 348 |
# In the correct section. Check "label" then "id" for the desired field_name |
| 349 |
if field.get("label", "").lower() == field_name: |
| 350 |
return field.get("value", "") |
| 351 |
|
| 352 |
if field.get("id", "").lower() == field_name: |
| 353 |
return field.get("value", "") |
| 354 |
|
| 355 |
return "" |
| 356 |
|
| 357 |
def assert_logged_in(self): |
| 358 |
if self.connect_host and self.connect_token: |
| 359 |
return True |
| 360 |
|
| 361 |
if self.service_account_token: |
| 362 |
args = ["whoami"] |
| 363 |
environment_update = {"OP_SERVICE_ACCOUNT_TOKEN": self.service_account_token} |
| 364 |
rc, out, err = self._run(args, environment_update=environment_update) |
| 365 |
|
| 366 |
return not bool(rc) |
| 367 |
|
| 368 |
args = ["account", "list"] |
| 369 |
if self.subdomain: |
| 370 |
account = f"{self.subdomain}.{self.domain}" |
| 371 |
args.extend(["--account", account]) |
| 372 |
|
| 373 |
rc, out, err = self._run(args) |
| 374 |
|
| 375 |
if out: |
| 376 |
# Running 'op account get' if there are no accounts configured on the system drops into |
| 377 |
# an interactive prompt. Only run 'op account get' after first listing accounts to see |
| 378 |
# if there are any previously configured accounts. |
| 379 |
args = ["account", "get"] |
| 380 |
if self.account_id: |
| 381 |
args.extend(["--account", self.account_id]) |
| 382 |
elif self.subdomain: |
| 383 |
account = f"{self.subdomain}.{self.domain}" |
| 384 |
args.extend(["--account", account]) |
| 385 |
|
| 386 |
rc, out, err = self._run(args, ignore_errors=True) |
| 387 |
|
| 388 |
return not bool(rc) |
| 389 |
|
| 390 |
return False |
| 391 |
|
| 392 |
def full_signin(self): |
| 393 |
required_params = [ |
| 394 |
"subdomain", |
| 395 |
"username", |
| 396 |
"secret_key", |
| 397 |
"master_password", |
| 398 |
] |
| 399 |
self._check_required_params(required_params) |
| 400 |
|
| 401 |
args = [ |
| 402 |
"account", |
| 403 |
"add", |
| 404 |
"--raw", |
| 405 |
"--address", |
| 406 |
f"{self.subdomain}.{self.domain}", |
| 407 |
"--email", |
| 408 |
to_bytes(self.username), |
| 409 |
"--signin", |
| 410 |
] |
| 411 |
|
| 412 |
environment_update = {"OP_SECRET_KEY": self.secret_key} |
| 413 |
return self._run(args, command_input=to_bytes(self.master_password), environment_update=environment_update) |
| 414 |
|
| 415 |
def _add_parameters_and_run(self, args, vault=None, token=None): |
| 416 |
if self.account_id: |
| 417 |
args.extend(["--account", self.account_id]) |
| 418 |
|
| 419 |
if vault is not None: |
| 420 |
args += [f"--vault={vault}"] |
| 421 |
|
| 422 |
if self.connect_host and self.connect_token: |
| 423 |
if vault is None: |
| 424 |
raise AnsibleLookupError("'vault' is required with 1Password Connect") |
| 425 |
environment_update = { |
| 426 |
"OP_CONNECT_HOST": self.connect_host, |
| 427 |
"OP_CONNECT_TOKEN": self.connect_token, |
| 428 |
} |
| 429 |
return self._run(args, environment_update=environment_update) |
| 430 |
|
| 431 |
if self.service_account_token: |
| 432 |
if vault is None: |
| 433 |
raise AnsibleLookupError("'vault' is required with 'service_account_token'") |
| 434 |
environment_update = {"OP_SERVICE_ACCOUNT_TOKEN": self.service_account_token} |
| 435 |
return self._run(args, environment_update=environment_update) |
| 436 |
|
| 437 |
if token is not None: |
| 438 |
args += [to_bytes("--session=") + token] |
| 439 |
|
| 440 |
return self._run(args) |
| 441 |
|
| 442 |
def get_raw(self, item_id, vault=None, token=None): |
| 443 |
args = ["item", "get", item_id, "--format", "json"] |
| 444 |
return self._add_parameters_and_run(args, vault=vault, token=token) |
| 445 |
|
| 446 |
def get_secret_reference(self, reference, token=None): |
| 447 |
args = ["read", reference] |
| 448 |
return self._add_parameters_and_run(args, token=token) |
| 449 |
|
| 450 |
def signin(self): |
| 451 |
self._check_required_params(["master_password"]) |
| 452 |
|
| 453 |
args = ["signin", "--raw"] |
| 454 |
if self.subdomain: |
| 455 |
args.extend(["--account", self.subdomain]) |
| 456 |
|
| 457 |
return self._run(args, command_input=to_bytes(self.master_password)) |
| 458 |
|
| 459 |
|
| 460 |
class OnePass: |
| 461 |
def __init__( |
| 462 |
self, |
| 463 |
subdomain=None, |
| 464 |
domain="1password.com", |
| 465 |
username=None, |
| 466 |
secret_key=None, |
| 467 |
master_password=None, |
| 468 |
service_account_token=None, |
| 469 |
account_id=None, |
| 470 |
connect_host=None, |
| 471 |
connect_token=None, |
| 472 |
cli_class=None, |
| 473 |
): |
| 474 |
self.subdomain = subdomain |
| 475 |
self.domain = domain |
| 476 |
self.username = username |
| 477 |
self.secret_key = secret_key |
| 478 |
self.master_password = master_password |
| 479 |
self.service_account_token = service_account_token |
| 480 |
self.account_id = account_id |
| 481 |
self.connect_host = connect_host |
| 482 |
self.connect_token = connect_token |
| 483 |
|
| 484 |
self.logged_in = False |
| 485 |
self.token = None |
| 486 |
|
| 487 |
self._config = OnePasswordConfig() |
| 488 |
self._cli = self._get_cli_class(cli_class) |
| 489 |
|
| 490 |
if (self.connect_host or self.connect_token) and None in (self.connect_host, self.connect_token): |
| 491 |
raise AnsibleOptionsError("connect_host and connect_token are required together") |
| 492 |
|
| 493 |
def _get_cli_class(self, cli_class=None): |
| 494 |
if cli_class is not None: |
| 495 |
return cli_class( |
| 496 |
self.subdomain, |
| 497 |
self.domain, |
| 498 |
self.username, |
| 499 |
self.secret_key, |
| 500 |
self.master_password, |
| 501 |
self.service_account_token, |
| 502 |
self.account_id, |
| 503 |
self.connect_host, |
| 504 |
self.connect_token, |
| 505 |
) |
| 506 |
|
| 507 |
version = OnePassCLIBase.get_current_version() |
| 508 |
for cls in OnePassCLIBase.__subclasses__(): |
| 509 |
if cls.supports_version == version.split(".")[0]: |
| 510 |
try: |
| 511 |
return cls( |
| 512 |
self.subdomain, |
| 513 |
self.domain, |
| 514 |
self.username, |
| 515 |
self.secret_key, |
| 516 |
self.master_password, |
| 517 |
self.service_account_token, |
| 518 |
self.account_id, |
| 519 |
self.connect_host, |
| 520 |
self.connect_token, |
| 521 |
) |
| 522 |
except TypeError as e: |
| 523 |
raise AnsibleLookupError(e) from e |
| 524 |
|
| 525 |
raise AnsibleLookupError(f"op version {version} is unsupported") |
| 526 |
|
| 527 |
def set_token(self): |
| 528 |
if self._config.config_file_path and os.path.isfile(self._config.config_file_path): |
| 529 |
# If the config file exists, assume an initial sign in has taken place and try basic sign in |
| 530 |
try: |
| 531 |
rc, out, err = self._cli.signin() |
| 532 |
except AnsibleLookupError as exc: |
| 533 |
test_strings = ( |
| 534 |
"missing required parameters", |
| 535 |
"unauthorized", |
| 536 |
) |
| 537 |
if any(string in exc.message.lower() for string in test_strings): |
| 538 |
# A required parameter is missing, or a bad master password was supplied |
| 539 |
# so don't bother attempting a full signin |
| 540 |
raise |
| 541 |
|
| 542 |
rc, out, err = self._cli.full_signin() |
| 543 |
|
| 544 |
self.token = out.strip() |
| 545 |
|
| 546 |
else: |
| 547 |
# Attempt a full signin since there appears to be no existing signin |
| 548 |
rc, out, err = self._cli.full_signin() |
| 549 |
self.token = out.strip() |
| 550 |
|
| 551 |
def assert_logged_in(self): |
| 552 |
logged_in = self._cli.assert_logged_in() |
| 553 |
if logged_in: |
| 554 |
self.logged_in = logged_in |
| 555 |
pass |
| 556 |
else: |
| 557 |
self.set_token() |
| 558 |
|
| 559 |
def get_raw(self, item_id, vault=None): |
| 560 |
rc, out, err = self._cli.get_raw(item_id, vault, self.token) |
| 561 |
return out |
| 562 |
|
| 563 |
def get_field(self, item_id, field, section=None, vault=None): |
| 564 |
output = self.get_raw(item_id, vault) |
| 565 |
if output: |
| 566 |
return self._cli._parse_field(output, field, section) |
| 567 |
|
| 568 |
return "" |
| 569 |
|
| 570 |
def get_secret_reference(self, reference): |
| 571 |
path = reference[5:] |
| 572 |
if not path: |
| 573 |
raise AnsibleLookupError("Secret references must have a path") |
| 574 |
# Split into parts, to check length in a second |
| 575 |
path_parts = [part for part in path.split("/") if part] |
| 576 |
|
| 577 |
# Must be 3 parts (vault,item,field) or 4 (vault,item,section,field) |
| 578 |
if len(path_parts) not in (3, 4): |
| 579 |
raise AnsibleLookupError("Not a valid secret reference") |
| 580 |
|
| 581 |
rc, out, err = self._cli.get_secret_reference(reference, self.token) |
| 582 |
return out.strip() |
| 583 |
|
| 584 |
|
| 585 |
class LookupModule(LookupBase): |
| 586 |
def run(self, terms, variables=None, **kwargs): |
| 587 |
self.set_options(var_options=variables, direct=kwargs) |
| 588 |
check_for_wrong_terms(self, direct=kwargs) |
| 589 |
|
| 590 |
field = self.get_option("field") |
| 591 |
section = self.get_option("section") |
| 592 |
vault = self.get_option("vault") |
| 593 |
subdomain = self.get_option("subdomain") |
| 594 |
domain = self.get_option("domain") |
| 595 |
username = self.get_option("username") |
| 596 |
secret_key = self.get_option("secret_key") |
| 597 |
master_password = self.get_option("master_password") |
| 598 |
service_account_token = self.get_option("service_account_token") |
| 599 |
account_id = self.get_option("account_id") |
| 600 |
connect_host = self.get_option("connect_host") |
| 601 |
connect_token = self.get_option("connect_token") |
| 602 |
|
| 603 |
op = OnePass( |
| 604 |
subdomain=subdomain, |
| 605 |
domain=domain, |
| 606 |
username=username, |
| 607 |
secret_key=secret_key, |
| 608 |
master_password=master_password, |
| 609 |
service_account_token=service_account_token, |
| 610 |
account_id=account_id, |
| 611 |
connect_host=connect_host, |
| 612 |
connect_token=connect_token, |
| 613 |
) |
| 614 |
op.assert_logged_in() |
| 615 |
|
| 616 |
values = [] |
| 617 |
for term in terms: |
| 618 |
if term.startswith("op://"): |
| 619 |
values.append(op.get_secret_reference(term)) |
| 620 |
else: |
| 621 |
values.append(op.get_field(term, field, section, vault)) |
| 622 |
|
| 623 |
return values |