| 1 |
# Copyright (c) 2025, Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
name: onepassword_ssh_key |
| 9 |
author: |
| 10 |
- Mohammed Babelly (@mohammedbabelly20) |
| 11 |
short_description: Fetch SSH keys stored in 1Password |
| 12 |
version_added: "10.3.0" |
| 13 |
description: |
| 14 |
- P(community.general.onepassword_ssh_key#lookup) wraps C(op) command line utility to fetch SSH keys from 1Password. |
| 15 |
notes: |
| 16 |
- By default, it returns the private key value in PKCS#8 format, unless O(ssh_format=true) is passed. |
| 17 |
- The pluging works only for C(SSHKEY) type items. |
| 18 |
options: |
| 19 |
_terms: |
| 20 |
description: Identifier(s) (case-insensitive UUID or name) of item(s) to retrieve. |
| 21 |
required: true |
| 22 |
type: list |
| 23 |
elements: string |
| 24 |
ssh_format: |
| 25 |
description: Output key in SSH format if V(true). Otherwise, outputs in the default format (PKCS#8). |
| 26 |
default: false |
| 27 |
type: bool |
| 28 |
|
| 29 |
extends_documentation_fragment: |
| 30 |
- community.general._onepassword |
| 31 |
- community.general._onepassword.lookup |
| 32 |
""" |
| 33 |
|
| 34 |
EXAMPLES = r""" |
| 35 |
--- |
| 36 |
- name: Retrieve the private SSH key from 1Password |
| 37 |
ansible.builtin.debug: |
| 38 |
msg: "{{ lookup('community.general.onepassword_ssh_key', 'SSH Key', ssh_format=true) }}" |
| 39 |
""" |
| 40 |
|
| 41 |
RETURN = r""" |
| 42 |
_raw: |
| 43 |
description: Private key of SSH keypair. |
| 44 |
type: list |
| 45 |
elements: string |
| 46 |
""" |
| 47 |
import json |
| 48 |
|
| 49 |
from ansible.errors import AnsibleLookupError |
| 50 |
from ansible.plugins.lookup import LookupBase |
| 51 |
|
| 52 |
from ansible_collections.community.general.plugins.lookup.onepassword import ( |
| 53 |
OnePass, |
| 54 |
OnePassCLIv2, |
| 55 |
) |
| 56 |
from ansible_collections.community.general.plugins.plugin_utils._lookup import check_for_wrong_terms |
| 57 |
|
| 58 |
|
| 59 |
class LookupModule(LookupBase): |
| 60 |
def get_ssh_key(self, out, item_id, ssh_format=False): |
| 61 |
data = json.loads(out) |
| 62 |
|
| 63 |
if data.get("category") != "SSH_KEY": |
| 64 |
raise AnsibleLookupError(f"Item {item_id} is not an SSH key") |
| 65 |
|
| 66 |
private_key_field = next( |
| 67 |
( |
| 68 |
field |
| 69 |
for field in data.get("fields", {}) |
| 70 |
if field.get("id") == "private_key" and field.get("type") == "SSHKEY" |
| 71 |
), |
| 72 |
None, |
| 73 |
) |
| 74 |
if not private_key_field: |
| 75 |
raise AnsibleLookupError(f"No private key found for item {item_id}.") |
| 76 |
|
| 77 |
if ssh_format: |
| 78 |
return private_key_field.get("ssh_formats", {}).get("openssh", {}).get("value", "") |
| 79 |
return private_key_field.get("value", "") |
| 80 |
|
| 81 |
def run(self, terms, variables=None, **kwargs): |
| 82 |
self.set_options(var_options=variables, direct=kwargs) |
| 83 |
check_for_wrong_terms(self, direct=kwargs) |
| 84 |
|
| 85 |
ssh_format = self.get_option("ssh_format") |
| 86 |
vault = self.get_option("vault") |
| 87 |
subdomain = self.get_option("subdomain") |
| 88 |
domain = self.get_option("domain", "1password.com") |
| 89 |
username = self.get_option("username") |
| 90 |
secret_key = self.get_option("secret_key") |
| 91 |
master_password = self.get_option("master_password") |
| 92 |
service_account_token = self.get_option("service_account_token") |
| 93 |
account_id = self.get_option("account_id") |
| 94 |
connect_host = self.get_option("connect_host") |
| 95 |
connect_token = self.get_option("connect_token") |
| 96 |
|
| 97 |
op = OnePass( |
| 98 |
subdomain=subdomain, |
| 99 |
domain=domain, |
| 100 |
username=username, |
| 101 |
secret_key=secret_key, |
| 102 |
master_password=master_password, |
| 103 |
service_account_token=service_account_token, |
| 104 |
account_id=account_id, |
| 105 |
connect_host=connect_host, |
| 106 |
connect_token=connect_token, |
| 107 |
cli_class=OnePassCLIv2, |
| 108 |
) |
| 109 |
op.assert_logged_in() |
| 110 |
|
| 111 |
return [self.get_ssh_key(op.get_raw(term, vault), term, ssh_format=ssh_format) for term in terms] |