| 1 |
package rekor |
| 2 |
|
| 3 |
// The following code is the essence of the relevant code paths from github.com/sigstore/rekor/pkg/generated/client/..., |
| 4 |
// heavily modified since. |
| 5 |
|
| 6 |
// Copyright 2021 The Sigstore Authors. |
| 7 |
// |
| 8 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
// you may not use this file except in compliance with the License. |
| 10 |
// You may obtain a copy of the License at |
| 11 |
// |
| 12 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
// |
| 14 |
// Unless required by applicable law or agreed to in writing, software |
| 15 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
// See the License for the specific language governing permissions and |
| 18 |
// limitations under the License. |
| 19 |
// |
| 20 |
|
| 21 |
import ( |
| 22 |
"context" |
| 23 |
"fmt" |
| 24 |
"net/http" |
| 25 |
"net/url" |
| 26 |
) |
| 27 |
|
| 28 |
// createLogEntryConflictError describes a response with status code 409: |
| 29 |
// The request conflicts with the current state of the transparency log. |
| 30 |
// This typically happens when trying to upload an existing entry again. |
| 31 |
type createLogEntryConflictError struct { |
| 32 |
location string |
| 33 |
err string |
| 34 |
} |
| 35 |
|
| 36 |
func (o *createLogEntryConflictError) Error() string { |
| 37 |
return o.err |
| 38 |
} |
| 39 |
|
| 40 |
// createLogEntry creates an entry in the transparency log |
| 41 |
// |
| 42 |
// Creates an entry in the transparency log for a detached signature, public key, and content. Items can be included in the request or fetched by the server when URLs are specified. |
| 43 |
func (r *rekorClient) createLogEntry(ctx context.Context, proposedEntry rekorProposedEntry) (rekorLogEntry, error) { |
| 44 |
res, err := r.makeRequest(ctx, http.MethodPost, "/api/v1/log/entries", proposedEntry) |
| 45 |
if err != nil { |
| 46 |
return nil, err |
| 47 |
} |
| 48 |
defer res.Body.Close() |
| 49 |
|
| 50 |
switch res.StatusCode { |
| 51 |
case http.StatusCreated: |
| 52 |
result := rekorLogEntry{} |
| 53 |
if err := decodeHTTPResponseBodyAsJSON(res, &result); err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
return result, nil |
| 57 |
|
| 58 |
case http.StatusBadRequest: |
| 59 |
result := rekorError{} |
| 60 |
if err := decodeHTTPResponseBodyAsJSON(res, &result); err != nil { |
| 61 |
return nil, err |
| 62 |
} |
| 63 |
return nil, fmt.Errorf("Rekor /api/v1/log/entries failed: bad request (%d), %+v", res.StatusCode, result) |
| 64 |
|
| 65 |
case http.StatusConflict: |
| 66 |
result := rekorError{} |
| 67 |
if err := decodeHTTPResponseBodyAsJSON(res, &result); err != nil { |
| 68 |
return nil, err |
| 69 |
} |
| 70 |
return nil, &createLogEntryConflictError{ |
| 71 |
location: res.Header.Get("Location"), |
| 72 |
err: fmt.Sprintf("Rekor /api/v1/log/entries failed with a conflict (%d), %+v", res.StatusCode, result), |
| 73 |
} |
| 74 |
|
| 75 |
default: |
| 76 |
result := rekorError{} |
| 77 |
if err := decodeHTTPResponseBodyAsJSON(res, &result); err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
return nil, fmt.Errorf("Rekor /api/v1/log/entries failed with unexpected status %d: %+v", res.StatusCode, result) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// getLogEntryByUUID gets log entry and information required to generate an inclusion proof for the entry in the transparency log |
| 85 |
// |
| 86 |
// Returns the entry, root hash, tree size, and a list of hashes that can be used to calculate proof of an entry being included in the transparency log |
| 87 |
func (r *rekorClient) getLogEntryByUUID(ctx context.Context, entryUUID string) (rekorLogEntry, error) { |
| 88 |
res, err := r.makeRequest(ctx, http.MethodGet, "/api/v1/log/entries/"+url.PathEscape(entryUUID), nil) |
| 89 |
if err != nil { |
| 90 |
return nil, err |
| 91 |
} |
| 92 |
defer res.Body.Close() |
| 93 |
|
| 94 |
switch res.StatusCode { |
| 95 |
case http.StatusOK: |
| 96 |
result := rekorLogEntry{} |
| 97 |
if err := decodeHTTPResponseBodyAsJSON(res, &result); err != nil { |
| 98 |
return nil, err |
| 99 |
} |
| 100 |
return result, nil |
| 101 |
|
| 102 |
case http.StatusNotFound: // We don’t care to define a separate error type; we don’t need it ourselves. |
| 103 |
return nil, fmt.Errorf("Rekor /api/v1/log/entries/{entryUUID}: entry not found (%d)", res.StatusCode) |
| 104 |
|
| 105 |
default: |
| 106 |
result := rekorError{} |
| 107 |
if err := decodeHTTPResponseBodyAsJSON(res, &result); err != nil { |
| 108 |
return nil, err |
| 109 |
} |
| 110 |
return nil, fmt.Errorf("Rekor /api/v1/log/entries/{entryUUID} failed with unexpected status %d: %+v", res.StatusCode, result) |
| 111 |
} |
| 112 |
} |