generated from WebexSamples/Webex-Samples-Template
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlambda_function.py
More file actions
155 lines (130 loc) · 5.39 KB
/
lambda_function.py
File metadata and controls
155 lines (130 loc) · 5.39 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
#!/usr/bin/env python3
"""
AWS Lambda Function for Webex BYODS Token Extension
This Lambda function automatically extends Webex BYODS (Bring Your Own Data Source)
tokens on a scheduled basis to prevent expiration.
Environment Variables:
DATA_SOURCE_ID: The ID of the data source to extend
SECRET_NAME: Name of the AWS Secrets Manager secret containing credentials
TOKEN_LIFETIME_MINUTES: Token lifetime in minutes (optional, defaults to 1440)
Returns:
dict: Lambda response with statusCode, body, and execution details
"""
import json
import os
import logging
from datetime import datetime
from token_manager import TokenManager
# Configure logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
"""
AWS Lambda handler function for extending BYODS tokens.
Args:
event: Lambda event object (not used, supports EventBridge scheduled events)
context: Lambda context object
Returns:
dict: Response with statusCode and body containing operation result
"""
logger.info("Starting BYODS token extension Lambda function")
logger.info(f"Event: {json.dumps(event)}")
# Get configuration from environment variables
data_source_id = os.environ.get('DATA_SOURCE_ID')
secret_name = os.environ.get('SECRET_NAME', 'webex-byods-credentials')
token_lifetime_minutes = int(os.environ.get('TOKEN_LIFETIME_MINUTES', '1440'))
# Validate required environment variables
if not data_source_id:
error_msg = "DATA_SOURCE_ID environment variable is required"
logger.error(error_msg)
return {
'statusCode': 400,
'body': json.dumps({
'success': False,
'error': error_msg,
'timestamp': datetime.now().isoformat()
})
}
logger.info(f"Data Source ID: {data_source_id}")
logger.info(f"Secret Name: {secret_name}")
logger.info(f"Token Lifetime: {token_lifetime_minutes} minutes")
try:
# Initialize TokenManager with AWS Secrets Manager support
logger.info("Initializing TokenManager")
token_manager = TokenManager(
config_path='token-config.json', # Fallback for local testing
secret_name=secret_name
)
# Extend the data source token
logger.info(f"Extending token for data source: {data_source_id}")
result = token_manager.extend_data_source_token(
data_source_id,
token_lifetime_minutes
)
if result['success']:
logger.info("Data source token extended successfully")
logger.info(f"New nonce: {result.get('nonce_updated')}")
logger.info(f"Token expiry: {result.get('token_expiry')}")
logger.info(f"Token lifetime: {result.get('token_lifetime_minutes')} minutes")
return {
'statusCode': 200,
'body': json.dumps({
'success': True,
'message': 'Data source token extended successfully',
'data_source_id': data_source_id,
'nonce_updated': result.get('nonce_updated'),
'token_expiry': result.get('token_expiry'),
'token_lifetime_minutes': result.get('token_lifetime_minutes'),
'timestamp': datetime.now().isoformat()
})
}
else:
error_msg = result.get('error', 'Unknown error')
status_code = result.get('status_code', 500)
logger.error(f"Failed to extend data source token: {error_msg}")
return {
'statusCode': status_code,
'body': json.dumps({
'success': False,
'error': error_msg,
'data_source_id': data_source_id,
'timestamp': datetime.now().isoformat()
})
}
except Exception as e:
error_msg = f"Unexpected error: {str(e)}"
logger.error(error_msg, exc_info=True)
return {
'statusCode': 500,
'body': json.dumps({
'success': False,
'error': error_msg,
'timestamp': datetime.now().isoformat()
})
}
# For local testing
if __name__ == "__main__":
# Mock Lambda event and context for local testing
class Context:
def __init__(self):
self.function_name = "webex-byods-token-extender"
self.memory_limit_in_mb = 256
self.invoked_function_arn = "arn:aws:lambda:us-east-1:123456789012:function:webex-byods-token-extender"
self.aws_request_id = "local-test-request-id"
# Set environment variables for local testing
if not os.environ.get('DATA_SOURCE_ID'):
print("Please set DATA_SOURCE_ID environment variable for local testing")
print("Example: export DATA_SOURCE_ID=85895e47-3096-4c47-aae8-f5a52f7b7870")
exit(1)
test_event = {
"source": "aws.events",
"detail-type": "Scheduled Event",
"time": datetime.now().isoformat()
}
test_context = Context()
print("Running local test...")
print("-" * 60)
response = lambda_handler(test_event, test_context)
print("-" * 60)
print("Response:")
print(json.dumps(response, indent=2))