Skip to content

Instantly share code, notes, and snippets.

@recalde
Created February 13, 2024 19:58
Show Gist options
  • Save recalde/131942a2cddd4ffc31f8966703af4e83 to your computer and use it in GitHub Desktop.
Save recalde/131942a2cddd4ffc31f8966703af4e83 to your computer and use it in GitHub Desktop.
import boto3
import requests
import json
import datetime
from dateutil import parser
from requests_ntlm import HttpNtlmAuth
# AWS clients
s3 = boto3.client('s3')
ssm = boto3.client('ssm')
kms = boto3.client('kms')
# S3 and cache configuration
bucket_name = 'your-s3-bucket'
object_key = 'cache/response.json'
cache_ttl_seconds = 3600 # TTL for the cache in seconds, e.g., 1 hour
# Parameter Store names for encrypted credentials
username_parameter_name = '/path/to/username'
password_parameter_name = '/path/to/password'
# URL of the on-premises IIS server
url = 'http://your-on-prem-iis-server/resource'
def get_decrypted_parameter(name):
# Retrieve the encrypted parameter from SSM
encrypted_value = ssm.get_parameter(Name=name, WithDecryption=True)['Parameter']['Value']
return encrypted_value
def cache_response_in_s3(response_data):
response_data['cache_timestamp'] = datetime.datetime.utcnow().isoformat()
s3.put_object(Bucket=bucket_name, Key=object_key, Body=json.dumps(response_data))
def is_cache_valid(last_modified_dt_str, cache_timestamp_str):
current_time = datetime.datetime.utcnow()
last_modified_dt = parser.parse(last_modified_dt_str)
cache_timestamp = parser.parse(cache_timestamp_str)
if last_modified_dt > cache_timestamp or (current_time - cache_timestamp).total_seconds() > cache_ttl_seconds:
return False
return True
def get_cached_response_from_s3():
try:
response = s3.get_object(Bucket=bucket_name, Key=object_key)
cached_data = json.loads(response['Body'].read())
if is_cache_valid(cached_data.get('last_attr_modified_dt'), cached_data.get('cache_timestamp')):
return cached_data
else:
return None
except s3.exceptions.NoSuchKey:
return None
def fetch_and_cache_data(username, password):
# Try to get cached response
cached_response = get_cached_response_from_s3()
if cached_response:
return cached_response
# If no valid cache, make a new request
session = requests.Session()
session.auth = HttpNtlmAuth(username, password)
response = session.get(url)
if response.ok:
response_data = response.json()
# Cache the new response
cache_response_in_s3(response_data)
return response_data
else:
response.raise_for_status()
# Main process
username = get_decrypted_parameter(username_parameter_name)
password = get_decrypted_parameter(password_parameter_name)
response_data = fetch_and_cache_data(username, password)
print(response_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment