-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
277 lines (244 loc) · 8.16 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
269
270
271
272
273
274
275
276
277
"""
Main entry point for this example app
"""
import json
import logging
import os
import subprocess
import click
import jwt
import requests
from dotenv import load_dotenv
from flask import Flask, jsonify, render_template, request
from yarl import URL
load_dotenv()
app = Flask(__name__)
KEY_DIR = "keys"
PRIVATE_KEY_PATH = os.path.join(KEY_DIR, "embedded-example-private-key.pem")
PUBLIC_KEY_PATH = os.path.join(KEY_DIR, "embedded-example-public-key.pem")
app.config.from_mapping(
{
"API_TOKEN": os.environ.get("API_TOKEN"),
"API_SECRET": os.environ.get("API_SECRET"),
"DASHBOARD_ID": os.environ.get("DASHBOARD_ID"),
"SUPERSET_DOMAIN": os.environ.get("SUPERSET_DOMAIN"),
"PRESET_TEAM": os.environ.get("PRESET_TEAM"),
"WORKSPACE_SLUG": os.environ.get("WORKSPACE_SLUG"),
"PRESET_BASE_URL": URL("https://api.app.preset.io/"),
"KEY_ID": os.environ.get("KEY_ID"),
},
)
# Configure logging
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s:%(levelname)s:%(message)s",
handlers=[logging.StreamHandler()],
)
# CLI command to generate public/private PEM keys
@app.cli.command("generate-keys")
@click.option(
"--overwrite",
is_flag=True,
help="Overwrite existing keys if they exist.",
)
def generate_keys(overwrite=False):
"""Generate RSA private and public keys using OpenSSL."""
# Check if OpenSSL is installed
try:
result = subprocess.run(
["openssl", "version"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
)
print(f"OpenSSL version: {result.stdout.decode().strip()}")
except FileNotFoundError as exc:
raise RuntimeError(
"OpenSSL is not installed or not available in PATH.",
) from exc
except subprocess.CalledProcessError as exc:
raise RuntimeError(
f"Failed to execute OpenSSL: {exc.stderr.decode().strip()}",
) from exc
# Ensure the output directory exists
if not os.path.exists(KEY_DIR):
os.makedirs(KEY_DIR)
# Check if the files already exist
if (
os.path.exists(PRIVATE_KEY_PATH) or os.path.exists(PUBLIC_KEY_PATH)
) and not overwrite:
raise Exception( # pylint: disable=broad-exception-raised
"Key files already exist. Use --overwrite to overwrite the existing files.",
)
print("Overwriting existing PEM keys.")
# Generate the private key
try:
subprocess.run(
[
"openssl",
"genpkey",
"-algorithm",
"RSA",
"-out",
PRIVATE_KEY_PATH,
"-pkeyopt",
"rsa_keygen_bits:2048",
],
check=True,
)
print(f"Private key generated at: {PRIVATE_KEY_PATH}")
except subprocess.CalledProcessError as exc:
raise RuntimeError(f"Failed to generate private key: {exc}") from exc
# Generate the public key
try:
subprocess.run(
[
"openssl",
"rsa",
"-pubout",
"-in",
PRIVATE_KEY_PATH,
"-out",
PUBLIC_KEY_PATH,
],
check=True,
)
print(f"Public key generated at: {PUBLIC_KEY_PATH}")
except subprocess.CalledProcessError as exc:
raise RuntimeError(f"Failed to generate private key: {exc}") from exc
@app.route("/")
def main_page():
"""
Default route to load index.html (loads the Embedded SDK).
"""
auth_type = request.args.get("auth_type", "api")
if auth_type == "pem":
if not os.path.exists(PRIVATE_KEY_PATH) or not os.path.exists(PUBLIC_KEY_PATH):
raise FileNotFoundError("PEM key files not found.")
if app.config["KEY_ID"] is None:
raise KeyError("Key ID not defined in environment variables.")
return render_template(
"index.html",
dashboardId=app.config["DASHBOARD_ID"],
supersetDomain=app.config["SUPERSET_DOMAIN"],
authType=auth_type,
)
# Default to API key auth
return render_template(
"index.html",
dashboardId=app.config["DASHBOARD_ID"],
supersetDomain=app.config["SUPERSET_DOMAIN"],
authType="api",
)
@app.route("/guest-token", methods=["GET"])
def guest_token_generator():
"""
Route used by frontend to retrieve a Guest Token.
"""
try:
jwt_token = authenticate_with_preset()
guest_token = jsonify(fetch_guest_token(jwt_token))
return guest_token, 200
except requests.exceptions.HTTPError as error:
return jsonify({"error": str(error)}), 500
def authenticate_with_preset():
"""
Authenticate with the Preset API to generate a JWT token.
"""
url = app.config["PRESET_BASE_URL"] / "v1/auth/"
payload = {"name": app.config["API_TOKEN"], "secret": app.config["API_SECRET"]}
headers = {"Content-Type": "application/json", "Accept": "application/json"}
try:
response = requests.post(
url,
headers=headers,
json=payload,
timeout=7,
)
response.raise_for_status()
return response.json()["payload"]["access_token"]
except requests.exceptions.HTTPError as http_error:
error_msg = http_error.response.text
logging.error(
"\nERROR: Unable to generate a JWT token.\nError details: %s",
error_msg,
)
raise requests.exceptions.HTTPError(
"Unable to generate a JWT token. "
"Please make sure your API key is enabled.",
)
def fetch_guest_token(jwt_key):
"""
Fetch and return a Guest Token for the embedded dashboard.
"""
url = (
app.config["PRESET_BASE_URL"]
/ "v1/teams"
/ app.config["PRESET_TEAM"]
/ "workspaces"
/ app.config["WORKSPACE_SLUG"]
/ "guest-token/"
)
payload = {
"user": {"username": "test_user", "first_name": "test", "last_name": "user"},
"resources": [{"type": "dashboard", "id": app.config["DASHBOARD_ID"]}],
"rls": [
# Apply an RLS to a specific dataset
# { "dataset": dataset_id, "clause": "column = 'filter'" },
# Apply an RLS to all datasets
# { "clause": "column = 'filter'" },
],
}
headers = {
"Authorization": f"Bearer {jwt_key}",
"Accept": "application/json",
"Content-Type": "application/json",
}
try:
response = requests.post(
url,
headers=headers,
json=payload,
timeout=7,
)
response.raise_for_status()
return response.json()["payload"]["token"]
except requests.exceptions.HTTPError as http_error:
error_msg = http_error.response.text
logging.error(
"\nERROR: Unable to fetch a Guest Token.\nError details: %s",
error_msg,
)
raise requests.exceptions.HTTPError(
"Unable to generate a Guest token. "
"Please make sure the API key has admin access and the payload is correct.",
)
@app.route("/pem-key", methods=["GET"])
def get_guest_token_using_pem_key():
"""
Encode and return a Guest Token for the embedded dashboard.
"""
with open(PRIVATE_KEY_PATH, "r", encoding="utf-8") as file:
private_key = file.read()
# Payload to encode
payload = {
"user": {"username": "test_user", "first_name": "test", "last_name": "user"},
"resources": [{"type": "dashboard", "id": app.config["DASHBOARD_ID"]}],
"rls_rules": [
# Apply an RLS to a specific dataset
# { "dataset": dataset_id, "clause": "column = 'filter'" },
# Apply an RLS to all datasets
# { "clause": "column = 'filter'" },
],
"type": "guest",
"aud": app.config["WORKSPACE_SLUG"],
}
encoded_jwt = jwt.encode(
payload,
private_key,
algorithm="RS256",
headers={"kid": app.config["KEY_ID"]},
)
return json.dumps(encoded_jwt)
if __name__ == "__main__":
app.run(host="0.0.0.0")