26
26
27
27
import rekor_types
28
28
import requests
29
+ from sigstore_protobuf_specs .dev .sigstore .rekor import v1 as rekor_v1
29
30
30
31
from sigstore ._internal import USER_AGENT
31
32
from sigstore ._internal .rekor_tiles .dev .sigstore .rekor import v2
@@ -148,25 +149,12 @@ def get(
148
149
return LogEntry ._from_response (resp .json ())
149
150
150
151
def post (
151
- self ,
152
- proposed_entry : rekor_types .Hashedrekord
153
- | rekor_types .Dsse
154
- | v2 .CreateEntryRequest ,
152
+ self , proposed_entry : rekor_types .Hashedrekord | rekor_types .Dsse
155
153
) -> LogEntry :
156
154
"""
157
155
Submit a new entry for inclusion in the Rekor log.
158
156
"""
159
- # There may be a bug in betterproto, where the V_0_0_2 is changed to V002.
160
- if isinstance (proposed_entry , v2 .CreateEntryRequest ):
161
- payload = proposed_entry .to_dict ()
162
- if "hashedRekordRequestV002" in payload :
163
- payload ["hashedRekordRequestV0_0_2" ] = payload .pop (
164
- "hashedRekordRequestV002"
165
- )
166
- if "dsseRequestV002" in payload :
167
- payload ["dsseRequestV0_0_2" ] = payload .pop ("dsseRequestV002" )
168
- else :
169
- payload = proposed_entry .model_dump (mode = "json" , by_alias = True )
157
+ payload = proposed_entry .model_dump (mode = "json" , by_alias = True )
170
158
_logger .debug (f"proposed: { json .dumps (payload )} " )
171
159
resp : requests .Response = self .session .post (self .url , json = payload )
172
160
try :
@@ -176,10 +164,7 @@ def post(
176
164
177
165
integrated_entry = resp .json ()
178
166
_logger .debug (f"integrated: { integrated_entry } " )
179
- if isinstance (proposed_entry , v2 .CreateEntryRequest ):
180
- return LogEntry ._from_dict_rekor (integrated_entry )
181
- else :
182
- return LogEntry ._from_response (integrated_entry )
167
+ return LogEntry ._from_response (integrated_entry )
183
168
184
169
@property
185
170
def retrieve (self ) -> RekorEntriesRetrieve :
@@ -236,14 +221,11 @@ def post(
236
221
class RekorClient :
237
222
"""The internal Rekor client"""
238
223
239
- def __init__ (
240
- self , url : str , major_api_version : int = REKOR_V1_API_MAJOR_VERSION
241
- ) -> None :
224
+ def __init__ (self , url : str ) -> None :
242
225
"""
243
226
Create a new `RekorClient` from the given URL.
244
227
"""
245
- self .url = f"{ url } /api/v{ major_api_version } "
246
- self .major_api_version = major_api_version
228
+ self .url = f"{ url } /api/v1"
247
229
self .session = requests .Session ()
248
230
self .session .headers .update (
249
231
{
@@ -281,3 +263,71 @@ def log(self) -> RekorLog:
281
263
Returns a `RekorLog` adapter for making requests to a Rekor log.
282
264
"""
283
265
return RekorLog (f"{ self .url } /log" , session = self .session )
266
+
267
+
268
+ class RekorV2Client :
269
+ """The internal Rekor client for the v2 API"""
270
+
271
+ # TODO: implement get_tile, get_entry_bundle, get_checkpoint.
272
+
273
+ def __init__ (self , base_url : str ) -> None :
274
+ """
275
+ Create a new `RekorV2Client` from the given URL.
276
+ """
277
+ self .url = f"{ base_url } /api/v2"
278
+ self .session = requests .Session ()
279
+ self .session .headers .update (
280
+ {
281
+ "Content-Type" : "application/json" ,
282
+ "Accept" : "application/json" ,
283
+ "User-Agent" : USER_AGENT ,
284
+ }
285
+ )
286
+
287
+ def __del__ (self ) -> None :
288
+ """
289
+ Terminates the underlying network session.
290
+ """
291
+ self .session .close ()
292
+
293
+ def create_entry (
294
+ self , request : v2 .CreateEntryRequest
295
+ ) -> rekor_v1 .TransparencyLogEntry :
296
+ """
297
+ Submit a new entry for inclusion in the Rekor log.
298
+ """
299
+ # There may be a bug in betterproto, where the V_0_0_2 is changed to V002.
300
+ payload = request .to_dict ()
301
+ if "hashedRekordRequestV002" in payload :
302
+ payload ["hashedRekordRequestV0_0_2" ] = payload .pop (
303
+ "hashedRekordRequestV002"
304
+ )
305
+ if "dsseRequestV002" in payload :
306
+ payload ["dsseRequestV0_0_2" ] = payload .pop ("dsseRequestV002" )
307
+ _logger .debug (f"request: { json .dumps (payload )} " )
308
+ resp = self .session .post (f"{ self .url } /log/entries" , json = payload )
309
+
310
+ try :
311
+ resp .raise_for_status ()
312
+ except requests .HTTPError as http_error :
313
+ raise RekorClientError (http_error )
314
+
315
+ integrated_entry = resp .json ()
316
+ _logger .debug (f"integrated: { integrated_entry } " )
317
+ return LogEntry ._from_dict_rekor (integrated_entry )
318
+
319
+ @classmethod
320
+ def production (cls ) -> RekorClient :
321
+ """
322
+ Returns a `RekorClient` populated with the default Rekor production instance.
323
+ """
324
+ return cls (
325
+ DEFAULT_REKOR_URL ,
326
+ )
327
+
328
+ @classmethod
329
+ def staging (cls ) -> RekorClient :
330
+ """
331
+ Returns a `RekorClient` populated with the default Rekor staging instance.
332
+ """
333
+ return cls (STAGING_REKOR_URL )
0 commit comments