-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
77 lines (67 loc) · 2.63 KB
/
app.py
File metadata and controls
77 lines (67 loc) · 2.63 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
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from config import Config
from utils.sts import assume_role
from services.aws_service import list_all_resources, get_resource_details
from models.resource import db, Resource
app = Flask(__name__)
app.config.from_object(Config)
db.init_app(app)
with app.app_context():
db.create_all()
@app.route('/assume_role', methods=['POST'])
def assume_role_endpoint():
data = request.get_json()
account_id = data.get('account_id')
role_name = data.get('role_name')
external_id = data.get('external_id')
try:
creds = assume_role(account_id, role_name, external_id)
return jsonify({"message": "Role assumed successfully", "credentials": creds}), 200
except Exception as e:
return jsonify({"error": str(e)}), 400
@app.route('/list_resources', methods=['POST'])
def list_resources_endpoint():
data = request.get_json()
account_id = data.get('account_id')
role_name = data.get('role_name')
external_id = data.get('external_id')
try:
creds = assume_role(account_id, role_name, external_id)
resources = list_all_resources(creds)
for resource in resources:
existing_resource = Resource.query.filter_by(resource_id=resource['ResourceType']).first()
if existing_resource:
existing_resource.details = resource
else:
new_resource = Resource(
resource_id=resource['ResourceType'],
resource_type=resource['ResourceType'],
region=resource['Region'],
details=resource
)
db.session.add(new_resource)
db.session.commit()
return jsonify({"resources": resources}), 200
except Exception as e:
return jsonify({"error": str(e)}), 400
@app.route('/resource_details', methods=['POST'])
def resource_details_endpoint():
data = request.get_json()
account_id = data.get('account_id')
role_name = data.get('role_name')
external_id = data.get('external_id')
region = data.get('region')
resource_id = data.get('resource_id')
resource_type = data.get('resource_type')
try:
creds = assume_role(account_id, role_name, external_id)
details = get_resource_details(creds, region, resource_id, resource_type)
if details:
return jsonify({"resource_details": details}), 200
else:
return jsonify({"error": "Resource not found"}), 404
except Exception as e:
return jsonify({"error": str(e)}), 400
if __name__ == '__main__':
app.run(debug=True)