|
| 1 | +import base64 |
| 2 | + |
| 3 | +import boto3 |
| 4 | +import requests |
| 5 | +from localstack.utils.strings import short_uid |
| 6 | + |
| 7 | + |
| 8 | +# Vault connection details |
| 9 | +VAULT_ADDR = "http://vault.localhost.localstack.cloud:4566" |
| 10 | +VAULT_TOKEN = "root" |
| 11 | +LOCALSTACK_ENDPOINT = "http://localhost:4566" |
| 12 | + |
| 13 | + |
| 14 | +def vault_request(method, path, data=None, token=VAULT_TOKEN): |
| 15 | + """Make a request to Vault API.""" |
| 16 | + url = f"{VAULT_ADDR}/v1/{path}" |
| 17 | + headers = {"X-Vault-Token": token} |
| 18 | + if data: |
| 19 | + headers["Content-Type"] = "application/json" |
| 20 | + return requests.request(method, url, headers=headers, json=data) |
| 21 | + return requests.request(method, url, headers=headers) |
| 22 | + |
| 23 | + |
| 24 | +def test_vault_health(): |
| 25 | + """Test that Vault is running and healthy.""" |
| 26 | + response = requests.get(f"{VAULT_ADDR}/v1/sys/health") |
| 27 | + assert response.status_code == 200 |
| 28 | + |
| 29 | + data = response.json() |
| 30 | + assert data["initialized"] is True |
| 31 | + assert data["sealed"] is False |
| 32 | + |
| 33 | + |
| 34 | +def test_vault_auth_with_token(): |
| 35 | + """Test authentication with root token.""" |
| 36 | + response = vault_request("GET", "auth/token/lookup-self") |
| 37 | + assert response.status_code == 200 |
| 38 | + |
| 39 | + data = response.json() |
| 40 | + assert "data" in data |
| 41 | + assert data["data"]["id"] == VAULT_TOKEN |
| 42 | + |
| 43 | + |
| 44 | +def test_kv_secrets_engine(): |
| 45 | + """Test KV v2 secrets engine operations.""" |
| 46 | + secret_path = f"myapp/config-{short_uid()}" |
| 47 | + |
| 48 | + # Write a secret |
| 49 | + secret_data = { |
| 50 | + "data": { |
| 51 | + "api_key": "test-api-key-123", |
| 52 | + "db_password": "supersecret", |
| 53 | + } |
| 54 | + } |
| 55 | + response = vault_request("POST", f"secret/data/{secret_path}", secret_data) |
| 56 | + assert response.status_code == 200 |
| 57 | + |
| 58 | + # Read the secret back |
| 59 | + response = vault_request("GET", f"secret/data/{secret_path}") |
| 60 | + assert response.status_code == 200 |
| 61 | + |
| 62 | + data = response.json() |
| 63 | + assert data["data"]["data"]["api_key"] == "test-api-key-123" |
| 64 | + assert data["data"]["data"]["db_password"] == "supersecret" |
| 65 | + |
| 66 | + # Delete the secret |
| 67 | + response = vault_request("DELETE", f"secret/data/{secret_path}") |
| 68 | + assert response.status_code == 204 |
| 69 | + |
| 70 | + |
| 71 | +def test_kv_list_secrets(): |
| 72 | + """Test listing secrets in KV engine.""" |
| 73 | + # Create a few secrets |
| 74 | + for i in range(3): |
| 75 | + secret_data = {"data": {"value": f"secret-{i}"}} |
| 76 | + vault_request("POST", f"secret/data/list-test/item-{i}", secret_data) |
| 77 | + |
| 78 | + # List secrets |
| 79 | + response = vault_request("LIST", "secret/metadata/list-test") |
| 80 | + assert response.status_code == 200 |
| 81 | + |
| 82 | + data = response.json() |
| 83 | + keys = data["data"]["keys"] |
| 84 | + assert len(keys) == 3 |
| 85 | + assert "item-0" in keys |
| 86 | + assert "item-1" in keys |
| 87 | + assert "item-2" in keys |
| 88 | + |
| 89 | + # Cleanup |
| 90 | + for i in range(3): |
| 91 | + vault_request("DELETE", f"secret/metadata/list-test/item-{i}") |
| 92 | + |
| 93 | + |
| 94 | +def test_transit_engine(): |
| 95 | + """Test Transit secrets engine for encryption.""" |
| 96 | + key_name = f"test-key-{short_uid()}" |
| 97 | + |
| 98 | + # Create an encryption key |
| 99 | + response = vault_request("POST", f"transit/keys/{key_name}") |
| 100 | + assert response.status_code in (200, 204) # Vault may return either |
| 101 | + |
| 102 | + # Encrypt some data |
| 103 | + plaintext = "Hello, Vault!" |
| 104 | + plaintext_b64 = base64.b64encode(plaintext.encode()).decode() |
| 105 | + |
| 106 | + response = vault_request( |
| 107 | + "POST", |
| 108 | + f"transit/encrypt/{key_name}", |
| 109 | + {"plaintext": plaintext_b64}, |
| 110 | + ) |
| 111 | + assert response.status_code == 200 |
| 112 | + ciphertext = response.json()["data"]["ciphertext"] |
| 113 | + assert ciphertext.startswith("vault:v1:") |
| 114 | + |
| 115 | + # Decrypt the data |
| 116 | + response = vault_request( |
| 117 | + "POST", |
| 118 | + f"transit/decrypt/{key_name}", |
| 119 | + {"ciphertext": ciphertext}, |
| 120 | + ) |
| 121 | + assert response.status_code == 200 |
| 122 | + decrypted_b64 = response.json()["data"]["plaintext"] |
| 123 | + decrypted = base64.b64decode(decrypted_b64).decode() |
| 124 | + assert decrypted == plaintext |
| 125 | + |
| 126 | + # Delete the key |
| 127 | + vault_request("POST", f"transit/keys/{key_name}/config", {"deletion_allowed": True}) |
| 128 | + vault_request("DELETE", f"transit/keys/{key_name}") |
| 129 | + |
| 130 | + |
| 131 | +def test_aws_auth_method_enabled(): |
| 132 | + """Test that AWS auth method is enabled and configured.""" |
| 133 | + response = vault_request("GET", "sys/auth") |
| 134 | + assert response.status_code == 200 |
| 135 | + |
| 136 | + data = response.json() |
| 137 | + assert "aws/" in data["data"] |
| 138 | + assert data["data"]["aws/"]["type"] == "aws" |
| 139 | + |
| 140 | + |
| 141 | +def test_default_lambda_role_exists(): |
| 142 | + """Test that the default Lambda IAM auth role exists or can be created.""" |
| 143 | + response = vault_request("GET", "auth/aws/role/default-lambda-role") |
| 144 | + |
| 145 | + # If role doesn't exist (e.g., after Terraform testing), create it |
| 146 | + if response.status_code == 404: |
| 147 | + role_config = { |
| 148 | + "auth_type": "iam", |
| 149 | + "bound_iam_principal_arn": ["arn:aws:iam::000000000000:role/*"], |
| 150 | + "token_policies": ["default-lambda-policy"], |
| 151 | + "resolve_aws_unique_ids": False, |
| 152 | + } |
| 153 | + create_response = vault_request( |
| 154 | + "POST", "auth/aws/role/default-lambda-role", role_config |
| 155 | + ) |
| 156 | + assert create_response.status_code == 204 |
| 157 | + response = vault_request("GET", "auth/aws/role/default-lambda-role") |
| 158 | + |
| 159 | + assert response.status_code == 200 |
| 160 | + data = response.json() |
| 161 | + assert data["data"]["auth_type"] == "iam" |
| 162 | + assert data["data"]["resolve_aws_unique_ids"] is False |
| 163 | + |
| 164 | + |
| 165 | +def test_default_lambda_policy_exists(): |
| 166 | + """Test that the default Lambda policy exists with correct permissions.""" |
| 167 | + response = vault_request("GET", "sys/policies/acl/default-lambda-policy") |
| 168 | + assert response.status_code == 200 |
| 169 | + |
| 170 | + data = response.json() |
| 171 | + policy = data["data"]["policy"] |
| 172 | + assert "secret/*" in policy |
| 173 | + assert "transit/*" in policy |
| 174 | + |
| 175 | + |
| 176 | +def test_mixed_vault_and_aws_traffic(): |
| 177 | + """ |
| 178 | + Test that Vault HTTP traffic and AWS API traffic work together. |
| 179 | +
|
| 180 | + This verifies that the Vault extension properly proxies Vault requests |
| 181 | + while not interfering with regular AWS API requests. |
| 182 | + """ |
| 183 | + # Test Vault API |
| 184 | + response = vault_request("GET", "sys/health") |
| 185 | + assert response.status_code == 200 |
| 186 | + assert response.json()["sealed"] is False |
| 187 | + |
| 188 | + # Test AWS S3 API |
| 189 | + s3_client = boto3.client( |
| 190 | + "s3", |
| 191 | + endpoint_url=LOCALSTACK_ENDPOINT, |
| 192 | + aws_access_key_id="test", |
| 193 | + aws_secret_access_key="test", |
| 194 | + region_name="us-east-1", |
| 195 | + ) |
| 196 | + |
| 197 | + bucket_name = f"vault-test-bucket-{short_uid()}" |
| 198 | + s3_client.create_bucket(Bucket=bucket_name) |
| 199 | + |
| 200 | + buckets = s3_client.list_buckets() |
| 201 | + bucket_names = [b["Name"] for b in buckets["Buckets"]] |
| 202 | + assert bucket_name in bucket_names |
| 203 | + |
| 204 | + # Cleanup |
| 205 | + s3_client.delete_bucket(Bucket=bucket_name) |
| 206 | + |
| 207 | + # Test AWS STS API (used by Vault for IAM auth) |
| 208 | + sts_client = boto3.client( |
| 209 | + "sts", |
| 210 | + endpoint_url=LOCALSTACK_ENDPOINT, |
| 211 | + aws_access_key_id="test", |
| 212 | + aws_secret_access_key="test", |
| 213 | + region_name="us-east-1", |
| 214 | + ) |
| 215 | + |
| 216 | + identity = sts_client.get_caller_identity() |
| 217 | + assert "Account" in identity |
| 218 | + assert "Arn" in identity |
| 219 | + |
| 220 | + # Verify Vault still works after AWS calls |
| 221 | + response = vault_request("GET", "auth/token/lookup-self") |
| 222 | + assert response.status_code == 200 |
| 223 | + |
| 224 | + |
| 225 | +def test_create_custom_policy_and_role(): |
| 226 | + """Test creating custom Vault policies and IAM auth roles.""" |
| 227 | + policy_name = f"custom-policy-{short_uid()}" |
| 228 | + role_name = f"custom-role-{short_uid()}" |
| 229 | + |
| 230 | + # Create a custom policy |
| 231 | + policy_hcl = """ |
| 232 | + path "secret/data/custom/*" { |
| 233 | + capabilities = ["read"] |
| 234 | + } |
| 235 | + """ |
| 236 | + response = vault_request( |
| 237 | + "PUT", f"sys/policies/acl/{policy_name}", {"policy": policy_hcl} |
| 238 | + ) |
| 239 | + assert response.status_code == 204 |
| 240 | + |
| 241 | + # Create a custom IAM auth role |
| 242 | + role_config = { |
| 243 | + "auth_type": "iam", |
| 244 | + "bound_iam_principal_arn": [ |
| 245 | + "arn:aws:iam::000000000000:role/custom-lambda-role" |
| 246 | + ], |
| 247 | + "token_policies": [policy_name], |
| 248 | + "resolve_aws_unique_ids": False, |
| 249 | + } |
| 250 | + response = vault_request("POST", f"auth/aws/role/{role_name}", role_config) |
| 251 | + assert response.status_code == 204 |
| 252 | + |
| 253 | + # Verify the role was created |
| 254 | + response = vault_request("GET", f"auth/aws/role/{role_name}") |
| 255 | + assert response.status_code == 200 |
| 256 | + assert policy_name in response.json()["data"]["token_policies"] |
| 257 | + |
| 258 | + # Cleanup |
| 259 | + vault_request("DELETE", f"auth/aws/role/{role_name}") |
| 260 | + vault_request("DELETE", f"sys/policies/acl/{policy_name}") |
0 commit comments