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

add support for intersection & union in search operations #968

Merged
merged 9 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/rekor-cli/app/pflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
uuidFlag FlagType = "uuid"
shaFlag FlagType = "sha"
emailFlag FlagType = "email"
operatorFlag FlagType = "operator"
logIndexFlag FlagType = "logIndex"
pkiFormatFlag FlagType = "pkiFormat"
typeFlag FlagType = "type"
Expand Down Expand Up @@ -67,6 +68,10 @@ func initializePFlagMap() {
// this validates a valid sha256 checksum which is optionally prefixed with 'sha256:'
return valueFactory(shaFlag, validateSHAValue, "")
},
operatorFlag: func() pflag.Value {
// this validates a valid operator name
return valueFactory(shaFlag, validateString("oneof=and or"), "")
},
emailFlag: func() pflag.Value {
// this validates an email address
return valueFactory(emailFlag, validateString("required,email"), "")
Expand Down
4 changes: 4 additions & 0 deletions cmd/rekor-cli/app/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func addSearchPFlags(cmd *cobra.Command) error {
cmd.Flags().Var(NewFlagValue(shaFlag, ""), "sha", "the SHA256 or SHA1 sum of the artifact")

cmd.Flags().Var(NewFlagValue(emailFlag, ""), "email", "email associated with the public key's subject")

cmd.Flags().Var(NewFlagValue(operatorFlag, ""), "operator", "operator to use for the search")
asraa marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

Expand Down Expand Up @@ -142,6 +144,8 @@ var searchCmd = &cobra.Command{
params.Query.Hash = "sha256:" + hashVal
}

params.Query.Operator = viper.GetString("operator")

publicKeyStr := viper.GetString("public-key")
if publicKeyStr != "" {
params.Query.PublicKey = &models.SearchIndexPublicKey{}
Expand Down
3 changes: 3 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@ definitions:
hash:
type: string
pattern: '^(sha256:)?[0-9a-fA-F]{64}$|^(sha1:)?[0-9a-fA-F]{40}$'
operator:
type: string
enum: ['and','or']

SearchLogQuery:
type: object
Expand Down
46 changes: 41 additions & 5 deletions pkg/api/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,23 @@ import (

func SearchIndexHandler(params index.SearchIndexParams) middleware.Responder {
httpReqCtx := params.HTTPRequest.Context()
var result Container

// default behaviour mimics "or"
if params.Query.Operator == "and" {
result = make(Uniq)
} else {
result = &Arr{}
}

var result []string
if params.Query.Hash != "" {
// This must be a valid sha256 hash
sha := util.PrefixSHA(params.Query.Hash)
var resultUUIDs []string
if err := redisClient.Do(httpReqCtx, radix.Cmd(&resultUUIDs, "LRANGE", strings.ToLower(sha), "0", "-1")); err != nil {
return handleRekorAPIError(params, http.StatusInternalServerError, err, redisUnexpectedResult)
}
result = append(result, resultUUIDs...)
result.Add(resultUUIDs)
}
if params.Query.PublicKey != nil {
af, err := pki.NewArtifactFactory(pki.Format(swag.StringValue(params.Query.PublicKey.Format)))
Expand All @@ -70,17 +77,17 @@ func SearchIndexHandler(params index.SearchIndexParams) middleware.Responder {
if err := redisClient.Do(httpReqCtx, radix.Cmd(&resultUUIDs, "LRANGE", strings.ToLower(hex.EncodeToString(keyHash[:])), "0", "-1")); err != nil {
return handleRekorAPIError(params, http.StatusInternalServerError, err, redisUnexpectedResult)
}
result = append(result, resultUUIDs...)
result.Add(resultUUIDs)
}
if params.Query.Email != "" {
var resultUUIDs []string
if err := redisClient.Do(httpReqCtx, radix.Cmd(&resultUUIDs, "LRANGE", strings.ToLower(params.Query.Email.String()), "0", "-1")); err != nil {
return handleRekorAPIError(params, http.StatusInternalServerError, err, redisUnexpectedResult)
}
result = append(result, resultUUIDs...)
result.Add(resultUUIDs)
}

return index.NewSearchIndexOK().WithPayload(result)
return index.NewSearchIndexOK().WithPayload(result.Result())
}

func SearchIndexNotImplementedHandler(params index.SearchIndexParams) middleware.Responder {
Expand All @@ -100,3 +107,32 @@ func addToIndex(ctx context.Context, key, value string) error {
func storeAttestation(ctx context.Context, uuid string, attestation []byte) error {
return storageClient.StoreAttestation(ctx, uuid, attestation)
}

type Container interface {
Add([]string)
Result() []string
}

type Uniq map[string]struct{}

func (u Uniq) Add(elements []string) {
dsa0x marked this conversation as resolved.
Show resolved Hide resolved
for _, v := range elements {
u[v] = struct{}{}
}
}
func (u Uniq) Result() []string {
var result []string
for k := range u {
result = append(result, k)
}
return result
}

type Arr []string

func (a *Arr) Add(elements []string) {
*a = append(*a, elements...)
}
func (a Arr) Result() []string {
return a
}
50 changes: 50 additions & 0 deletions pkg/generated/models/search_index.go

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

14 changes: 14 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.

17 changes: 17 additions & 0 deletions tests/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,23 @@ func TestX509(t *testing.T) {
out = runCli(t, "search", "--email", "test@rekor.dev")
outputContains(t, out, uuid)

artifactBytes, err := ioutil.ReadFile(artifactPath)
if err != nil {
t.Error(err)
}
sha := sha256.Sum256(artifactBytes)
dataSHA := hex.EncodeToString(sha[:])

out = runCli(t, "search", "--email", "test@rekor.dev", "--operator", "and","--sha", dataSHA)
if strings.Count(out, uuid) != 1 {
t.Errorf("expected to find one match for %v, found %v", uuid, strings.Count(out, uuid))
}

out = runCli(t, "search", "--email", "test@rekor.dev", "--operator", "or","--sha", dataSHA)
if strings.Count(out, uuid) != 2 {
t.Errorf("expected to find two matches for %v, found %v", uuid, strings.Count(out, uuid))
}

}

func TestUploadNoAPIKeyInOutput(t *testing.T) {
Expand Down