1- #
1+ #
22# Copyright 2024 ABSA Group Limited
3- #
3+ #
44# Licensed under the Apache License, Version 2.0 (the "License");
55# you may not use this file except in compliance with the License.
66# You may obtain a copy of the License at
7- #
7+ #
88# http://www.apache.org/licenses/LICENSE-2.0
9- #
9+ #
1010# Unless required by applicable law or agreed to in writing, software
1111# distributed under the License is distributed on an "AS IS" BASIS,
1212# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313# See the License for the specific language governing permissions and
1414# limitations under the License.
15- #
15+ #
1616import base64
1717import json
1818import logging
5858 CONFIG = json .load (file )
5959logger .debug ("Loaded main CONFIG" )
6060
61- aws_s3 = boto3 .Session ().resource ("s3" , verify = False )
61+ aws_s3 = boto3 .Session ().resource ('s3' , verify = False )
6262logger .debug ("Initialized AWS S3 Client" )
6363
6464if CONFIG ["access_config" ].startswith ("s3://" ):
65- name_parts = CONFIG ["access_config" ].split ("/" )
65+ name_parts = CONFIG ["access_config" ].split ('/' )
6666 bucket_name = name_parts [2 ]
6767 bucket_object = "/" .join (name_parts [3 :])
6868 ACCESS = json .loads (aws_s3 .Bucket (bucket_name ).Object (bucket_object ).get ()["Body" ].read ().decode ("utf-8" ))
8080writer_kafka .init (logger , CONFIG )
8181writer_postgres .init (logger )
8282
83-
8483def get_api ():
85- return {"statusCode" : 200 , "body" : API }
86-
84+ return {
85+ "statusCode" : 200 ,
86+ "body" : API
87+ }
8788
8889def get_token ():
8990 logger .debug ("Handling GET Token" )
90- return {"statusCode" : 303 , "headers" : {"Location" : TOKEN_PROVIDER_URL }}
91-
92-
91+ return {
92+ "statusCode" : 303 ,
93+ "headers" : {"Location" : TOKEN_PROVIDER_URL }
94+ }
95+
9396def get_topics ():
9497 logger .debug ("Handling GET Topics" )
9598 return {
9699 "statusCode" : 200 ,
97100 "headers" : {"Content-Type" : "application/json" },
98- "body" : json .dumps ([topicName for topicName in TOPICS ]),
101+ "body" : json .dumps ([topicName for topicName in TOPICS ])
99102 }
100-
101-
103+
102104def get_topic_schema (topicName ):
103105 logger .debug (f"Handling GET TopicSchema({ topicName } )" )
104106 if topicName not in TOPICS :
105- return {"statusCode" : 404 }
106-
107- return {"statusCode" : 200 , "headers" : {"Content-Type" : "application/json" }, "body" : json .dumps (TOPICS [topicName ])}
108-
107+ return { "statusCode" : 404 }
108+
109+ return {
110+ "statusCode" : 200 ,
111+ "headers" : {"Content-Type" : "application/json" },
112+ "body" : json .dumps (TOPICS [topicName ])
113+ }
109114
110115def post_topic_message (topicName , topicMessage , tokenEncoded ):
111116 logger .debug (f"Handling POST { topicName } " )
112117 try :
113118 token = jwt .decode (tokenEncoded , TOKEN_PUBLIC_KEY , algorithms = ["RS256" ])
114119 except Exception as e :
115- return {"statusCode" : 401 , "headers" : {"Content-Type" : "text/plain" }, "body" : str (e )}
120+ return {
121+ "statusCode" : 401 ,
122+ "headers" : {"Content-Type" : "text/plain" },
123+ "body" : str (e )
124+ }
116125
117126 if topicName not in TOPICS :
118- return {"statusCode" : 404 }
127+ return { "statusCode" : 404 }
119128
120129 user = token ["sub" ]
121130 if topicName not in ACCESS or user not in ACCESS [topicName ]:
122- return {"statusCode" : 403 }
131+ return { "statusCode" : 403 }
123132
124133 try :
125134 validate (instance = topicMessage , schema = TOPICS [topicName ])
126135 except ValidationError as e :
127- return {"statusCode" : 400 , "headers" : {"Content-Type" : "text/plain" }, "body" : e .message }
128-
136+ return {
137+ "statusCode" : 400 ,
138+ "headers" : {"Content-Type" : "text/plain" },
139+ "body" : e .message
140+ }
141+
129142 success = (
130- writer_kafka .write (topicName , topicMessage )
131- and writer_eventbridge .write (topicName , topicMessage )
132- and writer_postgres .write (topicName , topicMessage )
143+ writer_kafka .write (topicName , topicMessage ) and
144+ writer_eventbridge .write (topicName , topicMessage ) and
145+ writer_postgres .write (topicName , topicMessage )
133146 )
134147 return {"statusCode" : 202 } if success else {"statusCode" : 500 }
135148
136-
137149def extract_token (eventHeaders ):
138150 # Initial implementation used bearer header directly
139151 if "bearer" in eventHeaders :
140152 return eventHeaders ["bearer" ]
141-
153+
142154 if "Authorization" in eventHeaders and eventHeaders ["Authorization" ].startswith ("Bearer " ):
143- return eventHeaders ["Authorization" ][len ("Bearer " ) :]
144-
145- return "" # Will result in 401
146-
155+ return eventHeaders ["Authorization" ][len ("Bearer " ):]
156+
157+ return "" # Will result in 401
147158
148159def lambda_handler (event , context ):
149160 try :
@@ -157,11 +168,7 @@ def lambda_handler(event, context):
157168 if event ["httpMethod" ] == "GET" :
158169 return get_topic_schema (event ["pathParameters" ]["topic_name" ].lower ())
159170 if event ["httpMethod" ] == "POST" :
160- return post_topic_message (
161- event ["pathParameters" ]["topic_name" ].lower (),
162- json .loads (event ["body" ]),
163- extract_token (event ["headers" ]),
164- )
171+ return post_topic_message (event ["pathParameters" ]["topic_name" ].lower (), json .loads (event ["body" ]), extract_token (event ["headers" ]))
165172 if event ["resource" ].lower () == "/terminate" :
166173 sys .exit ("TERMINATING" )
167174 return {"statusCode" : 404 }
0 commit comments