Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return location of existing entry if upload tried #226

Merged
merged 1 commit into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions cmd/cli/app/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ import (
)

type uploadCmdOutput struct {
Location string
Index int64
AlreadyExists bool
Location string
Index int64
}

func (u *uploadCmdOutput) String() string {
if u.Location != "" {
return fmt.Sprintf("Created entry at index %d, available at: %v%v\n", u.Index, viper.GetString("rekor_server"), u.Location)
if u.AlreadyExists {
return fmt.Sprintf("Entry already exists; available at: %v%v\n", viper.GetString("rekor_server"), u.Location)
}
return "Entry already exists.\n"
return fmt.Sprintf("Created entry at index %d, available at: %v%v\n", u.Index, viper.GetString("rekor_server"), u.Location)
}

// uploadCmd represents the upload command
Expand Down Expand Up @@ -84,9 +85,12 @@ var uploadCmd = &cobra.Command{

resp, err := rekorClient.Entries.CreateLogEntry(params)
if err != nil {
switch err.(type) {
switch e := err.(type) {
case *entries.CreateLogEntryConflict:
return &uploadCmdOutput{Location: ""}, nil
return &uploadCmdOutput{
Location: e.Location.String(),
AlreadyExists: true,
}, nil
default:
return nil, err
}
Expand Down
4 changes: 4 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ responses:
description: The request conflicts with the current state of the transparency log
schema:
$ref: "#/definitions/Error"
headers:
Location:
type: string
format: uri
NotFound:
description: The content requested could not be found
InternalServerError:
Expand Down
12 changes: 9 additions & 3 deletions pkg/api/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"errors"
"fmt"
"net/http"
"net/url"

"github.com/google/trillian"
"github.com/spf13/viper"
Expand Down Expand Up @@ -102,7 +103,8 @@ func CreateLogEntryHandler(params entries.CreateLogEntryParams) middleware.Respo
switch insertionStatus.Code {
case int32(code.Code_OK):
case int32(code.Code_ALREADY_EXISTS), int32(code.Code_FAILED_PRECONDITION):
return handleRekorAPIError(params, http.StatusConflict, fmt.Errorf("grpc error: %v", insertionStatus.String()), entryAlreadyExists)
existingUUID := hex.EncodeToString(rfc6962.DefaultHasher.HashLeaf(leaf))
return handleRekorAPIError(params, http.StatusConflict, fmt.Errorf("grpc error: %v", insertionStatus.String()), fmt.Sprintf(entryAlreadyExists, existingUUID), "entryURL", getEntryURL(*httpReq.URL, existingUUID))
default:
return handleRekorAPIError(params, http.StatusInternalServerError, fmt.Errorf("grpc error: %v", insertionStatus.String()), trillianUnexpectedResult)
}
Expand Down Expand Up @@ -131,13 +133,17 @@ func CreateLogEntryHandler(params entries.CreateLogEntryParams) middleware.Respo
}()
}

locationURL := httpReq.URL
return entries.NewCreateLogEntryCreated().WithPayload(logEntry).WithLocation(getEntryURL(*httpReq.URL, uuid)).WithETag(uuid)
}

func getEntryURL(locationURL url.URL, uuid string) strfmt.URI {
// remove API key from output
query := locationURL.Query()
query.Del("apiKey")
locationURL.RawQuery = query.Encode()
locationURL.Path = fmt.Sprintf("%v/%v", locationURL.Path, uuid)
return entries.NewCreateLogEntryCreated().WithPayload(logEntry).WithLocation(strfmt.URI(locationURL.String())).WithETag(uuid)
return strfmt.URI(locationURL.String())

}

func GetLogEntryByUUIDHandler(params entries.GetLogEntryByUUIDParams) middleware.Responder {
Expand Down
17 changes: 15 additions & 2 deletions pkg/api/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"regexp"

"github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/strfmt"
"github.com/mitchellh/mapstructure"
"github.com/sigstore/rekor/pkg/generated/models"
"github.com/sigstore/rekor/pkg/generated/restapi/operations/entries"
Expand All @@ -33,7 +34,7 @@ const (
trillianCommunicationError = "Unexpected error communicating with transparency log"
trillianUnexpectedResult = "Unexpected result from transparency log"
failedToGenerateCanonicalEntry = "Error generating canonicalized entry"
entryAlreadyExists = "An equivalent entry already exists in the transparency log"
entryAlreadyExists = "An equivalent entry already exists in the transparency log with UUID %v"
firstSizeLessThanLastSize = "firstSize(%d) must be less than lastSize(%d)"
malformedUUID = "UUID must be a 64-character hexadecimal string"
malformedHash = "Hash must be a 64-character hexadecimal string created from SHA256 algorithm"
Expand Down Expand Up @@ -98,7 +99,19 @@ func handleRekorAPIError(params interface{}, code int, err error, message string
case http.StatusBadRequest:
return entries.NewCreateLogEntryBadRequest().WithPayload(errorMsg(message, code))
case http.StatusConflict:
return entries.NewCreateLogEntryConflict().WithPayload(errorMsg(message, code))
resp := entries.NewCreateLogEntryConflict().WithPayload(errorMsg(message, code))
locationFound := false
for _, field := range fields {
if locationFound {
existingURL := field.(strfmt.URI)
resp.SetLocation(existingURL)
break
} else if field.(string) == "entryURL" {
dlorenc marked this conversation as resolved.
Show resolved Hide resolved
locationFound = true
continue
}
}
return resp
default:
return entries.NewCreateLogEntryDefault(code).WithPayload(errorMsg(message, code))
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/generated/client/entries/create_log_entry_responses.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions pkg/generated/restapi/embedded_spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.