-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
268 lines (212 loc) · 11.2 KB
/
app.py
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
from flask import Flask, request
from azure.storage.blob import BlobServiceClient, BlobClient
import json
from azure.core.exceptions import AzureError
app = Flask(__name__)
# Azure Blob Storage setup
connection_string = "DefaultEndpointsProtocol=https;AccountName=sademobankapi;AccountKey=+BuoJ/UwKfIBf9Rknb2oBO2za6Vd5GmMy07C9AgUaVQl3IdBfAuo49wJrQsrVnyaWRSPUB7s+0SA+AStEjqYZA==;EndpointSuffix=core.windows.net"
container_name = "bankdata"
blob_name = "customer.json"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
# API Endpoints
@app.route('/')
def home():
return 'Welcome to the Bank API App!'
@app.route('/api/customers/<customerId>', methods=['GET'])
def get_customer(customerId):
print(f"Attempting to fetch customer with ID: {customerId}")
try:
# Connect to the Azure Blob storage and retrieve the JSON data
print("Connecting to Azure Blob Storage...")
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
print("Downloading blob data...")
blob_data = blob_client.download_blob().readall()
data = json.loads(blob_data)
print("Blob data downloaded and parsed successfully")
# Find the customer data by customerId
customer_data = next((customer for customer in data if str(customer['id']) == customerId), None)
if customer_data:
print(f"Customer data found for ID {customerId}: {customer_data}")
return customer_data, 200
else:
print(f"Customer data not found for ID: {customerId}")
return {"message": "Customer not found"}, 404
except AzureError as e:
print(f"An error occurred while accessing Azure Blob Storage: {e}")
return {"message": "Failed to access Azure Blob Storage"}, 500
except json.JSONDecodeError as e:
print(f"Error decoding JSON data: {e}")
return {"message": "Invalid JSON data"}, 500
except Exception as e:
print(f"An unexpected error occurred: {e}")
return {"message": "Internal Server Error"}, 500
@app.route('/api/customers', methods=['POST'])
def create_customer():
try:
# Connect to the Azure Blob storage and retrieve the JSON data
print("Connecting to Azure Blob Storage...")
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
print("Downloading existing customer data...")
blob_data = blob_client.download_blob().readall()
customers = json.loads(blob_data)
print("Existing customer data downloaded successfully")
# Fetch new customer data from request
new_customer = request.json
print(f"Received new customer data: {new_customer}")
# Vulnerability: No input validation or sanitization is done
# Add new customer to the list
new_customer_id = max(customer['id'] for customer in customers) + 1 # Generating a new ID
new_customer['id'] = new_customer_id
customers.append(new_customer)
# Update the Blob with the new customer list
print(f"Adding new customer with ID {new_customer_id}...")
blob_client.upload_blob(json.dumps(customers), overwrite=True)
print("New customer added successfully")
return {"message": "Customer created successfully", "id": new_customer_id}, 201
except json.JSONDecodeError as e:
print(f"Error decoding JSON data: {e}")
return {"message": "Invalid JSON data"}, 400
except AzureError as e:
print(f"An error occurred while accessing Azure Blob Storage: {e}")
return {"message": "Failed to access Azure Blob Storage"}, 500
except Exception as e:
print(f"An unexpected error occurred: {e}")
return {"message": "Internal Server Error"}, 500
@app.route('/api/customers/<customerId>', methods=['PUT'])
def update_customer(customerId):
try:
# Connect to the Azure Blob storage and retrieve the JSON data
print("Connecting to Azure Blob Storage...")
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
print("Downloading existing customer data...")
blob_data = blob_client.download_blob().readall()
customers = json.loads(blob_data)
print("Existing customer data downloaded successfully")
# Fetch updated customer data from request
updated_customer_data = request.json
print(f"Received updated data for customer ID {customerId}: {updated_customer_data}")
# Vulnerability: No authentication or authorization check
# Vulnerability: No input validation or sanitization is done
# Update the customer data
customer_index = next((i for i, customer in enumerate(customers) if str(customer['id']) == customerId), None)
if customer_index is not None:
customers[customer_index].update(updated_customer_data)
print(f"Updating customer data for ID {customerId}...")
blob_client.upload_blob(json.dumps(customers), overwrite=True)
print("Customer data updated successfully")
return {"message": "Customer updated successfully"}, 200
else:
print(f"Customer with ID {customerId} not found")
return {"message": "Customer not found"}, 404
except json.JSONDecodeError as e:
print(f"Error decoding JSON data: {e}")
return {"message": "Invalid JSON data"}, 400
except AzureError as e:
print(f"An error occurred while accessing Azure Blob Storage: {e}")
return {"message": "Failed to access Azure Blob Storage"}, 500
except Exception as e:
print(f"An unexpected error occurred: {e}")
return {"message": "Internal Server Error"}, 500
# Add a general catch-all return at the end of the function
return {"message": "An unexpected error occurred"}, 500
@app.route('/api/customers/<customerId>', methods=['DELETE'])
def delete_customer(customerId):
try:
# Connect to the Azure Blob storage and retrieve the JSON data
print("Connecting to Azure Blob Storage...")
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
print("Downloading existing customer data...")
blob_data = blob_client.download_blob().readall()
customers = json.loads(blob_data)
print("Existing customer data downloaded successfully")
# Vulnerability: No authentication or authorization check
# Delete the customer data
customer_index = next((i for i, customer in enumerate(customers) if str(customer['id']) == customerId), None)
if customer_index is not None:
del customers[customer_index]
# Update the Blob with the remaining customers
print(f"Deleting customer data for ID {customerId}...")
blob_client.upload_blob(json.dumps(customers), overwrite=True)
print("Customer data deleted successfully")
return {"message": "Customer deleted successfully"}, 200
else:
print(f"Customer with ID {customerId} not found")
return {"message": "Customer not found"}, 404
except json.JSONDecodeError as e:
print(f"Error decoding JSON data: {e}")
return {"message": "Invalid JSON data"}, 400
except AzureError as e:
print(f"An error occurred while accessing Azure Blob Storage: {e}")
return {"message": "Failed to access Azure Blob Storage"}, 500
except Exception as e:
print(f"An unexpected error occurred: {e}")
return {"message": "Internal Server Error"}, 500
@app.route('/api/customers/<customerId>/ssn', methods=['GET'])
def get_customer_ssn(customerId):
try:
# Connect to the Azure Blob storage and retrieve the JSON data
print("Connecting to Azure Blob Storage...")
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
print("Downloading existing customer data...")
blob_data = blob_client.download_blob().readall()
customers = json.loads(blob_data)
print("Existing customer data downloaded successfully")
# Vulnerability: No authentication or authorization check
# Find the customer by ID and return the SSN
customer = next((customer for customer in customers if str(customer['id']) == customerId), None)
if customer is not None:
ssn = customer.get('ssn')
if ssn:
print(f"Returning SSN for customer ID {customerId}")
return {"ssn": ssn}, 200
else:
print(f"SSN not found for customer ID {customerId}")
return {"message": "SSN not found"}, 404
else:
print(f"Customer with ID {customerId} not found")
return {"message": "Customer not found"}, 404
except json.JSONDecodeError as e:
print(f"Error decoding JSON data: {e}")
return {"message": "Invalid JSON data"}, 400
except AzureError as e:
print(f"An error occurred while accessing Azure Blob Storage: {e}")
return {"message": "Failed to access Azure Blob Storage"}, 500
except Exception as e:
print(f"An unexpected error occurred: {e}")
return {"message": "Internal Server Error"}, 500
@app.route('/api/customers/<customerId>/creditcards', methods=['GET'])
def get_customer_credit_cards(customerId):
try:
# Connect to the Azure Blob storage and retrieve the JSON data
print("Connecting to Azure Blob Storage...")
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
print("Downloading existing customer data...")
blob_data = blob_client.download_blob().readall()
customers = json.loads(blob_data)
print("Existing customer data downloaded successfully")
# Vulnerability: No authentication or authorization check
# Find the customer by ID and return the credit card information
customer = next((customer for customer in customers if str(customer['id']) == customerId), None)
if customer is not None:
credit_cards = customer.get('credit_cards')
if credit_cards:
print(f"Returning credit card information for customer ID {customerId}")
return {"credit_cards": credit_cards}, 200
else:
print(f"Credit card information not found for customer ID {customerId}")
return {"message": "Credit card information not found"}, 404
else:
print(f"Customer with ID {customerId} not found")
return {"message": "Customer not found"}, 404
except json.JSONDecodeError as e:
print(f"Error decoding JSON data: {e}")
return {"message": "Invalid JSON data"}, 400
except AzureError as e:
print(f"An error occurred while accessing Azure Blob Storage: {e}")
return {"message": "Failed to access Azure Blob Storage"}, 500
except Exception as e:
print(f"An unexpected error occurred: {e}")
return {"message": "Internal Server Error"}, 500
if __name__ == '__main__':
app.run(debug=True)