Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/lookup/test_bitwarden_secrets_manager.py

raw
1 # Copyright (c) 2023, jantari (https://github.com/jantari)
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 import json
8 import unittest
9 from unittest.mock import patch
10
11 from ansible.errors import AnsibleLookupError
12 from ansible.plugins.loader import lookup_loader
13
14 from ansible_collections.community.general.plugins.lookup.bitwarden_secrets_manager import BitwardenSecretsManager
15
16 MOCK_SECRETS = [
17 {
18 "object": "secret",
19 "id": "ababc4a8-c242-4e54-bceb-77d17cdf2e07",
20 "organizationId": "3c33066c-a0bf-4e70-9a3c-24cda6aaddd5",
21 "projectId": "81869439-bfe5-442f-8b4e-b172e68b0ab2",
22 "key": "TEST_SECRET",
23 "value": "1234supersecret5678",
24 "note": "A test secret to use when developing the ansible bitwarden_secrets_manager lookup plugin",
25 "creationDate": "2023-04-23T13:13:37.7507017Z",
26 "revisionDate": "2023-04-23T13:13:37.7507017Z",
27 },
28 {
29 "object": "secret",
30 "id": "d4b7c8fa-fc95-40d7-a13c-6e186ee69d53",
31 "organizationId": "3c33066c-a0bf-4e70-9a3c-24cda6aaddd5",
32 "projectId": "81869439-bfe5-442f-8b4e-b172e68b0ab2",
33 "key": "TEST_SECRET_2",
34 "value": "abcd_such_secret_very_important_efgh",
35 "note": "notes go here",
36 "creationDate": "2023-04-23T13:26:44.0392906Z",
37 "revisionDate": "2023-04-23T13:26:44.0392906Z",
38 },
39 ]
40
41
42 class MockBitwardenSecretsManager(BitwardenSecretsManager):
43 def _run(self, args, stdin=None):
44 # mock the --version call
45 if args[0] == "--version":
46 return "bws 1.0.0", "", 0
47
48 # secret_id is the last argument passed to the bws CLI
49 secret_id = args[-1]
50 rc = 1
51 out = ""
52 err = ""
53 found_secrets = list(filter(lambda record: record["id"] == secret_id, MOCK_SECRETS))
54
55 if len(found_secrets) == 0:
56 err = "simulated bws CLI error: 404 no secret with such id"
57 elif len(found_secrets) == 1:
58 rc = 0
59 # The real bws CLI will only ever return one secret / json object for the "get secret <secret-id>" command
60 out = json.dumps(found_secrets[0])
61 else:
62 # This should never happen unless there's an error in the test MOCK_SECRETS.
63 # The real Bitwarden Secrets Manager assigns each secret a unique ID.
64 raise ValueError(f"More than 1 secret found with id: '{secret_id}'. Impossible!")
65
66 return out, err, rc
67
68
69 class TestLookupModule(unittest.TestCase):
70 def setUp(self):
71 self.lookup = lookup_loader.get("community.general.bitwarden_secrets_manager")
72
73 @patch(
74 "ansible_collections.community.general.plugins.lookup.bitwarden_secrets_manager._bitwarden_secrets_manager",
75 new=MockBitwardenSecretsManager(),
76 )
77 def test_bitwarden_secrets_manager(self):
78 # Getting a secret by its id should return the full secret info
79 self.assertEqual(
80 [MOCK_SECRETS[0]], self.lookup.run(["ababc4a8-c242-4e54-bceb-77d17cdf2e07"], bws_access_token="123")
81 )
82
83 @patch(
84 "ansible_collections.community.general.plugins.lookup.bitwarden_secrets_manager._bitwarden_secrets_manager",
85 new=MockBitwardenSecretsManager(),
86 )
87 def test_bitwarden_secrets_manager_no_match(self):
88 # Getting a nonexistent secret id throws exception
89 with self.assertRaises(AnsibleLookupError):
90 self.lookup.run(["nonexistant_id"], bws_access_token="123")