-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
307 lines (249 loc) · 11.5 KB
/
app.py
File metadata and controls
307 lines (249 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python3
"""
Azure Storage Blob Upload Test with Pod Identity
This application tests uploading a file to Azure Storage using Kubernetes pod identity.
"""
import os
import sys
import logging
import asyncio
from datetime import datetime
from azure.identity import DefaultAzureCredential, AzureAuthorityHosts
from azure.storage.blob.aio import BlobServiceClient
from azure.core.exceptions import AzureError, ClientAuthenticationError
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class StorageUploadTester:
"""
Test class for uploading files to Azure Storage using pod identity.
Implements retry logic, proper error handling, and managed identity authentication.
"""
def __init__(self, storage_account_name: str, container_name: str, cloud_environment: str = "AzurePublic"):
"""
Initialize the storage upload tester.
Args:
storage_account_name: Name of the Azure Storage account
container_name: Name of the blob container
cloud_environment: Azure cloud environment (AzurePublic or AzureGovernment)
"""
self.storage_account_name = storage_account_name
self.container_name = container_name
self.cloud_environment = cloud_environment
if cloud_environment == "AzureGovernment":
self.storage_url = f"https://{storage_account_name}.blob.core.usgovcloudapi.net"
authority = AzureAuthorityHosts.AZURE_GOVERNMENT
else:
self.storage_url = f"https://{storage_account_name}.blob.core.windows.net"
authority = AzureAuthorityHosts.AZURE_PUBLIC_CLOUD
# Use DefaultAzureCredential for pod identity
# This will automatically use the managed identity assigned to the pod
self.credential = DefaultAzureCredential(authority=authority)
# Initialize blob service client
self.blob_service_client = BlobServiceClient(
account_url=self.storage_url,
credential=self.credential
)
async def create_test_file(self, filename: str = "test.txt") -> str:
"""
Create a test file with sample content.
Args:
filename: Name of the test file to create
Returns:
Path to the created test file
"""
try:
test_content = f"""Test file created at: {datetime.now().isoformat()}
This is a test file for Azure Storage upload using pod identity.
Storage Account: {self.storage_account_name}
Container: {self.container_name}
Pod Identity Authentication Test
"""
with open(filename, 'w') as f:
f.write(test_content)
logger.info(f"Created test file: {filename}")
return filename
except Exception as e:
logger.error(f"Failed to create test file: {e}")
raise
async def test_authentication(self) -> bool:
"""
Test authentication with Azure Storage using pod identity.
Returns:
True if authentication is successful, False otherwise
"""
try:
logger.info("Testing authentication with Azure Storage...")
# Test authentication by listing containers
async with self.blob_service_client:
containers = []
async for container in self.blob_service_client.list_containers():
containers.append(container.name)
logger.info(f"Successfully authenticated. Found {len(containers)} containers")
if self.container_name in containers:
logger.info(f"Target container '{self.container_name}' exists")
else:
logger.warning(f"Target container '{self.container_name}' not found in: {containers}")
return True
except ClientAuthenticationError as e:
logger.error(f"Authentication failed: {e}")
return False
except Exception as e:
logger.error(f"Unexpected error during authentication test: {e}")
return False
async def upload_file_with_retry(self, local_file_path: str, blob_name: str, max_retries: int = 3) -> bool:
"""
Upload file to Azure Storage with retry logic and exponential backoff.
Args:
local_file_path: Path to the local file to upload
blob_name: Name of the blob in storage
max_retries: Maximum number of retry attempts
Returns:
True if upload is successful, False otherwise
"""
for attempt in range(max_retries):
try:
logger.info(f"Upload attempt {attempt + 1}/{max_retries} for {blob_name}")
# Get blob client
blob_client = self.blob_service_client.get_blob_client(
container=self.container_name,
blob=blob_name
)
# Upload file with overwrite enabled
with open(local_file_path, 'rb') as data:
await blob_client.upload_blob(
data,
overwrite=True,
content_type='text/plain'
)
logger.info(f"Successfully uploaded {blob_name} to container {self.container_name}")
# Verify upload by checking blob properties
blob_properties = await blob_client.get_blob_properties()
logger.info(f"Blob size: {blob_properties.size} bytes")
logger.info(f"Last modified: {blob_properties.last_modified}")
return True
except AzureError as e:
logger.error(f"Azure error on attempt {attempt + 1}: {e}")
if attempt < max_retries - 1:
# Exponential backoff: wait 2^attempt seconds
wait_time = 2 ** attempt
logger.info(f"Retrying in {wait_time} seconds...")
await asyncio.sleep(wait_time)
else:
logger.error(f"Upload failed after {max_retries} attempts")
return False
except Exception as e:
logger.error(f"Unexpected error on attempt {attempt + 1}: {e}")
return False
return False
async def download_and_verify(self, blob_name: str, download_path: str) -> bool:
"""
Download the uploaded blob and verify its content.
Args:
blob_name: Name of the blob to download
download_path: Path where to save the downloaded file
Returns:
True if download and verification are successful, False otherwise
"""
try:
logger.info(f"Downloading blob {blob_name} for verification...")
blob_client = self.blob_service_client.get_blob_client(
container=self.container_name,
blob=blob_name
)
# Download blob content
with open(download_path, 'wb') as download_file:
download_stream = await blob_client.download_blob()
download_file.write(await download_stream.readall())
logger.info(f"Downloaded blob to {download_path}")
# Verify file exists and has content
if os.path.exists(download_path) and os.path.getsize(download_path) > 0:
logger.info("Download verification successful")
return True
else:
logger.error("Downloaded file is empty or missing")
return False
except Exception as e:
logger.error(f"Download verification failed: {e}")
return False
async def cleanup_test_files(self, *file_paths):
"""
Clean up local test files.
Args:
*file_paths: Variable number of file paths to clean up
"""
for file_path in file_paths:
try:
if os.path.exists(file_path):
os.remove(file_path)
logger.info(f"Cleaned up file: {file_path}")
except Exception as e:
logger.warning(f"Failed to clean up {file_path}: {e}")
async def run_upload_test(self) -> bool:
"""
Run the complete upload test workflow.
Returns:
True if all tests pass, False otherwise
"""
test_filename = "test.txt"
download_filename = "downloaded_test.txt"
blob_name = f"test-uploads/{test_filename}"
try:
# Step 1: Test authentication
logger.info("=== Starting Azure Storage Upload Test ===")
if not await self.test_authentication():
logger.error("Authentication test failed")
return False
# Step 2: Create test file
await self.create_test_file(test_filename)
# Step 3: Upload file
logger.info(f"Uploading {test_filename} to blob {blob_name}...")
upload_success = await self.upload_file_with_retry(test_filename, blob_name)
if not upload_success:
logger.error("Upload test failed")
return False
# Step 4: Download and verify
verify_success = await self.download_and_verify(blob_name, download_filename)
if not verify_success:
logger.error("Download verification failed")
return False
# Step 5: Cleanup
await self.cleanup_test_files(test_filename, download_filename)
logger.info("=== Upload test completed successfully! ===")
return True
except Exception as e:
logger.error(f"Upload test failed with unexpected error: {e}")
return False
finally:
# Ensure cleanup happens even if test fails
await self.cleanup_test_files(test_filename, download_filename)
await self.blob_service_client.close()
async def main():
"""
Main function to run the storage upload test.
"""
# Get configuration from environment variables
storage_account_name = os.getenv('AZURE_STORAGE_ACCOUNT_NAME')
container_name = os.getenv('AZURE_STORAGE_CONTAINER_NAME', 'upload-test')
cloud_environment = os.getenv('AZURE_CLOUD_ENVIRONMENT', 'AzureGovernment')
if not storage_account_name:
logger.error("AZURE_STORAGE_ACCOUNT_NAME environment variable is required")
sys.exit(1)
logger.info(f"Configuration:")
logger.info(f" Storage Account: {storage_account_name}")
logger.info(f" Container: {container_name}")
logger.info(f" Cloud Environment: {cloud_environment}")
# Run the upload test
tester = StorageUploadTester(storage_account_name, container_name, cloud_environment)
success = await tester.run_upload_test()
if success:
logger.info("All tests passed!")
sys.exit(0)
else:
logger.error("Tests failed!")
sys.exit(1)
if __name__ == "__main__":
asyncio.run(main())