File size: 8,837 Bytes
9abace2 | 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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | """
AWS Operations
Part of SOVEREIGN PYTHON LLM ENGINE
AWS cloud operations using boto3.
"""
from typing import Any
from pathlib import Path
from dataclasses import dataclass
from ...core.evidence import WORMLedger
@dataclass
class S3Object:
"""S3 object metadata"""
key: str
size: int
last_modified: str
etag: str
class AWSOperations:
"""
AWS cloud operations.
Supports:
- S3 (storage)
- Lambda (functions)
- Secrets Manager
- SSM Parameter Store
"""
def __init__(
self,
region: str = "us-east-1",
worm_ledger: WORMLedger | None = None
):
"""
Initialize AWS operations.
Args:
region: AWS region
worm_ledger: Optional WORM ledger
"""
self.region = region
self.worm_ledger = worm_ledger
try:
import boto3
except ImportError:
raise ImportError("boto3 is required. Install: pip install boto3")
self.boto3 = boto3
# S3 Operations
async def s3_upload(
self,
bucket: str,
key: str,
file_path: Path
) -> str:
"""
Upload file to S3.
Args:
bucket: S3 bucket name
key: Object key
file_path: Local file path
Returns:
S3 URL
"""
import asyncio
def _sync_upload():
s3 = self.boto3.client("s3", region_name=self.region)
s3.upload_file(str(file_path), bucket, key)
return f"s3://{bucket}/{key}"
url = await asyncio.to_thread(_sync_upload)
# Log to WORM
if self.worm_ledger:
await self.worm_ledger.append({
"event": "s3_upload",
"bucket": bucket,
"key": key,
"size": file_path.stat().st_size
})
return url
async def s3_download(
self,
bucket: str,
key: str,
file_path: Path
) -> None:
"""
Download file from S3.
Args:
bucket: S3 bucket name
key: Object key
file_path: Local file path
"""
import asyncio
def _sync_download():
s3 = self.boto3.client("s3", region_name=self.region)
s3.download_file(bucket, key, str(file_path))
await asyncio.to_thread(_sync_download)
async def s3_list(
self,
bucket: str,
prefix: str = ""
) -> list[S3Object]:
"""
List objects in S3 bucket.
Args:
bucket: S3 bucket name
prefix: Optional prefix filter
Returns:
List of S3Object
"""
import asyncio
def _sync_list():
s3 = self.boto3.client("s3", region_name=self.region)
response = s3.list_objects_v2(Bucket=bucket, Prefix=prefix)
objects = []
for obj in response.get("Contents", []):
objects.append(S3Object(
key=obj["Key"],
size=obj["Size"],
last_modified=obj["LastModified"].isoformat(),
etag=obj["ETag"]
))
return objects
return await asyncio.to_thread(_sync_list)
async def s3_delete(
self,
bucket: str,
key: str
) -> None:
"""
Delete object from S3.
Args:
bucket: S3 bucket name
key: Object key
"""
import asyncio
def _sync_delete():
s3 = self.boto3.client("s3", region_name=self.region)
s3.delete_object(Bucket=bucket, Key=key)
await asyncio.to_thread(_sync_delete)
# Log to WORM
if self.worm_ledger:
await self.worm_ledger.append({
"event": "s3_delete",
"bucket": bucket,
"key": key
})
# Secrets Manager Operations
async def get_secret(
self,
secret_id: str
) -> dict[str, Any]:
"""
Get secret from Secrets Manager.
Args:
secret_id: Secret ID or ARN
Returns:
Secret value (as dict if JSON)
"""
import asyncio
import json
def _sync_get_secret():
sm = self.boto3.client("secretsmanager", region_name=self.region)
response = sm.get_secret_value(SecretId=secret_id)
secret_string = response["SecretString"]
# Try to parse as JSON
try:
return json.loads(secret_string)
except json.JSONDecodeError:
return {"value": secret_string}
return await asyncio.to_thread(_sync_get_secret)
async def put_secret(
self,
secret_id: str,
secret_value: dict[str, Any] | str
) -> None:
"""
Put secret to Secrets Manager.
Args:
secret_id: Secret ID
secret_value: Secret value (dict or string)
"""
import asyncio
import json
def _sync_put_secret():
sm = self.boto3.client("secretsmanager", region_name=self.region)
# Convert dict to JSON string
if isinstance(secret_value, dict):
secret_string = json.dumps(secret_value)
else:
secret_string = secret_value
try:
sm.update_secret(SecretId=secret_id, SecretString=secret_string)
except sm.exceptions.ResourceNotFoundException:
sm.create_secret(Name=secret_id, SecretString=secret_string)
await asyncio.to_thread(_sync_put_secret)
# Log to WORM
if self.worm_ledger:
await self.worm_ledger.append({
"event": "secrets_manager_put",
"secret_id": secret_id
})
# Lambda Operations
async def invoke_lambda(
self,
function_name: str,
payload: dict[str, Any]
) -> dict[str, Any]:
"""
Invoke Lambda function.
Args:
function_name: Function name or ARN
payload: Function payload
Returns:
Function response
"""
import asyncio
import json
def _sync_invoke():
lambda_client = self.boto3.client("lambda", region_name=self.region)
response = lambda_client.invoke(
FunctionName=function_name,
InvocationType="RequestResponse",
Payload=json.dumps(payload)
)
result_payload = json.loads(response["Payload"].read())
return result_payload
result = await asyncio.to_thread(_sync_invoke)
# Log to WORM
if self.worm_ledger:
await self.worm_ledger.append({
"event": "lambda_invoke",
"function": function_name
})
return result
# Tool registration helpers
async def s3_upload_tool(
bucket: str,
key: str,
file_path: str,
region: str = "us-east-1"
) -> dict:
"""Upload to S3 tool"""
aws = AWSOperations(region=region)
url = await aws.s3_upload(bucket, key, Path(file_path))
return {
"url": url,
"bucket": bucket,
"key": key
}
async def s3_list_tool(
bucket: str,
prefix: str = "",
region: str = "us-east-1"
) -> dict:
"""List S3 objects tool"""
aws = AWSOperations(region=region)
objects = await aws.s3_list(bucket, prefix)
return {
"objects": [
{
"key": obj.key,
"size": obj.size,
"last_modified": obj.last_modified
}
for obj in objects
],
"count": len(objects)
}
async def get_secret_tool(
secret_id: str,
region: str = "us-east-1"
) -> dict:
"""Get secret tool"""
aws = AWSOperations(region=region)
secret = await aws.get_secret(secret_id)
return {
"secret_id": secret_id,
"value": secret
}
async def invoke_lambda_tool(
function_name: str,
payload: dict,
region: str = "us-east-1"
) -> dict:
"""Invoke Lambda tool"""
aws = AWSOperations(region=region)
result = await aws.invoke_lambda(function_name, payload)
return {
"function": function_name,
"result": result
}
|