dn-scribe commited on
Commit
24f5129
·
1 Parent(s): cbcecd2

fix: attestation templating

Browse files
Files changed (1) hide show
  1. api/webhooks.py +64 -40
api/webhooks.py CHANGED
@@ -122,48 +122,72 @@ def render_template_string(template, context=None, attestation=None, webhook=Non
122
  return attestation.get("product_version", "")
123
  if varname == "attestation_details":
124
  return attestation
125
- # Attestation-derived (load only if needed)
126
- if varname in ("raw_attestation", "attestation_json", "attestation_predicate", "attestation_predicate_b64"):
127
- if "raw" not in attestation_loaded:
128
- attestation_loaded["raw"] = None
129
- if storage_path or storage_url:
130
- backend = get_storage_backend()
131
- try:
132
- if storage_path:
133
- attestation_loaded["raw"] = backend.download_file(storage_path)
134
- elif storage_url:
135
- import requests
136
- resp = requests.get(storage_url)
137
- if resp.ok:
138
- attestation_loaded["raw"] = resp.content
139
- except Exception:
140
- attestation_loaded["raw"] = None
141
- raw = attestation_loaded["raw"]
142
- if raw is None:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  return ""
144
- if varname == "raw_attestation":
145
- return raw
146
- if varname == "attestation_json":
147
- try:
148
- return json.loads(raw)
149
- except Exception:
150
- return ""
151
  if varname == "attestation_predicate":
152
- try:
153
- j = json.loads(raw)
154
- return j.get("predicate", "")
155
- except Exception:
156
- return ""
157
- if varname == "attestation_predicate_b64":
158
- try:
159
- import base64
160
- j = json.loads(raw)
161
- pred = j.get("predicate", "")
162
- if isinstance(pred, (dict, list)):
163
- pred = json.dumps(pred)
164
- return base64.b64encode(pred.encode()).decode()
165
- except Exception:
166
- return ""
167
  # Context fields
168
  if varname.startswith("labels."):
169
  label_name = varname.split(".", 1)[1]
 
122
  return attestation.get("product_version", "")
123
  if varname == "attestation_details":
124
  return attestation
125
+
126
+ # Only support attestation_raw, attestation_predicate, attestation_predicate_base64
127
+ if varname in ("attestation_raw", "attestation_predicate", "attestation_predicate_base64"):
128
+ from models import Attestation as AttestationModel
129
+ import importlib
130
+ web_routes = importlib.import_module("web.routes")
131
+ get_attestation_file_content = getattr(web_routes, "get_attestation_file_content")
132
+
133
+ # Try to get Attestation object from DB using id or storage_path
134
+ att_obj = None
135
+ att_id = attestation.get("id")
136
+ storage_path_val = attestation.get("storage_path")
137
+ if att_id is not None:
138
+ att_obj = AttestationModel.query.filter_by(id=att_id).first()
139
+ elif storage_path_val:
140
+ att_obj = AttestationModel.query.filter_by(storage_path=storage_path_val).first()
141
+ if not att_obj:
142
+ return ""
143
+
144
+ # --- Get raw file content as in get_attestation_file_content, before decoding ---
145
+ config = None
146
+ storage = None
147
+ try:
148
+ from flask import current_app
149
+ config = current_app.config.get('STORAGE_CONFIG')
150
+ storage = get_storage_backend()
151
+ except Exception:
152
+ return ""
153
+ raw_content = None
154
+ try:
155
+ if config.STORAGE_TYPE == 'FILE_MOUNT':
156
+ import os
157
+ file_path = os.path.join(config.FILE_STORAGE_PATH, att_obj.storage_path)
158
+ if not os.path.exists(file_path):
159
+ return ""
160
+ with open(file_path, "rb") as f:
161
+ raw_content = f.read()
162
+ else:
163
+ import urllib.request
164
+ url = storage.generate_download_url(att_obj.storage_path)
165
+ if not url:
166
+ return ""
167
+ response = urllib.request.urlopen(url)
168
+ raw_content = response.read()
169
+ except Exception:
170
+ return ""
171
+
172
+ # Now handle the variable
173
+ if varname == "attestation_raw":
174
+ return raw_content
175
+
176
+ # For predicate and predicate_base64, use the decoded predicate as before
177
+ predicate, content_type, is_json, error = get_attestation_file_content(att_obj)
178
+ if error or predicate is None:
179
  return ""
 
 
 
 
 
 
 
180
  if varname == "attestation_predicate":
181
+ return predicate
182
+ if varname == "attestation_predicate_base64":
183
+ import base64
184
+ if isinstance(predicate, (dict, list)):
185
+ pred_str = json.dumps(predicate)
186
+ else:
187
+ pred_str = str(predicate)
188
+ return base64.b64encode(pred_str.encode()).decode()
189
+ return ""
190
+
 
 
 
 
 
191
  # Context fields
192
  if varname.startswith("labels."):
193
  label_name = varname.split(".", 1)[1]