|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright (C) Grimoirelab Contributors |
| 4 | +# |
| 5 | +# This program is free software; you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation; either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +# |
| 18 | +# Author: |
| 19 | +# Alberto Ferrer Sánchez (alberefe@gmail.com) |
| 20 | +# |
| 21 | + |
| 22 | +import unittest |
| 23 | +from unittest.mock import patch |
| 24 | +import hvac.exceptions |
| 25 | + |
| 26 | +from grimoirelab_toolkit.credential_manager.hc_manager import HashicorpManager |
| 27 | +from grimoirelab_toolkit.credential_manager.exceptions import ( |
| 28 | + CredentialNotFoundError, |
| 29 | + HashicorpVaultError, |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +class TestHashicorpManager(unittest.TestCase): |
| 34 | + """Tests for HashicorpManager class.""" |
| 35 | + |
| 36 | + def setUp(self): |
| 37 | + """Set up common test fixtures.""" |
| 38 | + self.vault_url = "http://vault-url" |
| 39 | + self.token = "test-token" |
| 40 | + self.certificate = "test-certificate" |
| 41 | + |
| 42 | + self.mock_secret_response = { |
| 43 | + "auth": None, |
| 44 | + "data": { |
| 45 | + "data": { |
| 46 | + "password": "test_pass", |
| 47 | + "username": "test_user", |
| 48 | + "api_key": "test_key", |
| 49 | + }, |
| 50 | + "metadata": { |
| 51 | + "created_time": "2024-11-23T12:20:59.985132927Z", |
| 52 | + "custom_metadata": None, |
| 53 | + "deletion_time": "", |
| 54 | + "destroyed": False, |
| 55 | + "version": 1, |
| 56 | + }, |
| 57 | + }, |
| 58 | + "lease_duration": 0, |
| 59 | + "lease_id": "", |
| 60 | + "mount_type": "kv", |
| 61 | + "renewable": False, |
| 62 | + "request_id": "d09e2bb5-00ee-576b-6078-5d291d35ccc3", |
| 63 | + "warnings": None, |
| 64 | + "wrap_info": None, |
| 65 | + } |
| 66 | + |
| 67 | + @patch("hvac.Client") |
| 68 | + def test_initialization_success(self, mock_hvac_client): |
| 69 | + """Test successful initialization with valid credentials.""" |
| 70 | + mock_instance = mock_hvac_client.return_value |
| 71 | + |
| 72 | + manager = HashicorpManager(self.vault_url, self.token, self.certificate) |
| 73 | + |
| 74 | + self.assertIsNotNone(manager.client) |
| 75 | + mock_hvac_client.assert_called_once_with( |
| 76 | + url=self.vault_url, token=self.token, verify=self.certificate |
| 77 | + ) |
| 78 | + |
| 79 | + @patch("hvac.Client") |
| 80 | + def test_initialization_failure(self, mock_hvac_client): |
| 81 | + """Test initialization fails when connection to Vault fails.""" |
| 82 | + mock_hvac_client.side_effect = hvac.exceptions.VaultError("Connection failed") |
| 83 | + |
| 84 | + with self.assertRaises(hvac.exceptions.VaultError) as context: |
| 85 | + HashicorpManager(self.vault_url, self.token, self.certificate) |
| 86 | + |
| 87 | + self.assertIn("Connection failed", str(context.exception)) |
| 88 | + |
| 89 | + @patch("hvac.Client") |
| 90 | + def test_get_secret_success(self, mock_hvac_client): |
| 91 | + """Test successful secret retrieval.""" |
| 92 | + mock_instance = mock_hvac_client.return_value |
| 93 | + mock_instance.secrets.kv.read_secret.return_value = self.mock_secret_response |
| 94 | + |
| 95 | + manager = HashicorpManager(self.vault_url, self.token, self.certificate) |
| 96 | + result = manager.get_secret("test_service") |
| 97 | + |
| 98 | + # Verify it returns the full secret object |
| 99 | + self.assertIsInstance(result, dict) |
| 100 | + self.assertEqual(result, self.mock_secret_response) |
| 101 | + self.assertEqual(result["data"]["data"]["api_key"], "test_key") |
| 102 | + mock_instance.secrets.kv.read_secret.assert_called_once_with( |
| 103 | + path="test_service" |
| 104 | + ) |
| 105 | + |
| 106 | + @patch("hvac.Client") |
| 107 | + def test_get_secret_not_found(self, mock_hvac_client): |
| 108 | + """Test get_secret raises error when secret path not found.""" |
| 109 | + mock_instance = mock_hvac_client.return_value |
| 110 | + mock_instance.secrets.kv.read_secret.side_effect = hvac.exceptions.InvalidPath() |
| 111 | + |
| 112 | + manager = HashicorpManager(self.vault_url, self.token, self.certificate) |
| 113 | + |
| 114 | + with self.assertRaises(CredentialNotFoundError) as context: |
| 115 | + manager.get_secret("nonexistent_service") |
| 116 | + |
| 117 | + self.assertIn("nonexistent_service", str(context.exception)) |
| 118 | + self.assertIn("not found", str(context.exception)) |
| 119 | + |
| 120 | + @patch("hvac.Client") |
| 121 | + def test_get_secret_permission_denied(self, mock_hvac_client): |
| 122 | + """Test get_secret raises error when access is forbidden.""" |
| 123 | + mock_instance = mock_hvac_client.return_value |
| 124 | + mock_instance.secrets.kv.read_secret.side_effect = hvac.exceptions.Forbidden() |
| 125 | + |
| 126 | + manager = HashicorpManager(self.vault_url, self.token, self.certificate) |
| 127 | + |
| 128 | + with self.assertRaises(HashicorpVaultError) as context: |
| 129 | + manager.get_secret("test_service") |
| 130 | + |
| 131 | + self.assertIn("Vault operation failed", str(context.exception)) |
| 132 | + |
| 133 | + @patch("hvac.Client") |
| 134 | + def test_vault_connection_error(self, mock_hvac_client): |
| 135 | + """Test get_secret raises error when Vault is down or sealed.""" |
| 136 | + mock_instance = mock_hvac_client.return_value |
| 137 | + mock_instance.secrets.kv.read_secret.side_effect = hvac.exceptions.VaultDown( |
| 138 | + "Vault is sealed" |
| 139 | + ) |
| 140 | + |
| 141 | + manager = HashicorpManager(self.vault_url, self.token, self.certificate) |
| 142 | + |
| 143 | + with self.assertRaises(HashicorpVaultError) as context: |
| 144 | + manager.get_secret("test_service") |
| 145 | + |
| 146 | + self.assertIn("Vault operation failed", str(context.exception)) |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + unittest.main(warnings="ignore") |
0 commit comments