-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🌱 cron: support reading prefix from file for controller input files (…
…7/n) (#2445) * add prefix marker file to config Signed-off-by: Spencer Schrock <sschrock@google.com> * Read the new config values, if they exist. Signed-off-by: Spencer Schrock <sschrock@google.com> * Add function to fetch prefix file config value. Signed-off-by: Spencer Schrock <sschrock@google.com> * Read prefix file if prefix not set. Signed-off-by: Spencer Schrock <sschrock@google.com> * Add tests to verify how List works with various prefixes Signed-off-by: Spencer Schrock <sschrock@google.com> * Add tests for getPrefix Signed-off-by: Spencer Schrock <sschrock@google.com> * Remove panics from iterator helper functions Signed-off-by: Spencer Schrock <sschrock@google.com> Signed-off-by: Spencer Schrock <sschrock@google.com>
- Loading branch information
1 parent
9c07d11
commit 9e947c1
Showing
9 changed files
with
305 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2022 Security Scorecard Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package main implements the PubSub controller. | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/ossf/scorecard/v4/cron/config" | ||
"github.com/ossf/scorecard/v4/cron/data" | ||
) | ||
|
||
// getPrefix returns the prefix used when reading input files from a bucket. | ||
// If "prefix" is set, the value is used irrespective of the value of "prefix-file". | ||
// Otherwise, the contents of "prefix-file" (if set) are used. | ||
func getPrefix(ctx context.Context, bucket string) (string, error) { | ||
prefix, err := config.GetInputBucketPrefix() | ||
if err != nil { | ||
return "", fmt.Errorf("config.GetInputBucketPrefix: %w", err) | ||
} | ||
if prefix != "" { | ||
// prioritize prefix if set | ||
return prefix, nil | ||
} | ||
|
||
prefixFile, err := config.GetInputBucketPrefixFile() | ||
if err != nil { | ||
return "", fmt.Errorf("config.GetInputBucketPrefixFile: %w", err) | ||
} | ||
if prefixFile == "" { | ||
// cant read a file which doesnt exist, but the value is optional so no error | ||
return "", nil | ||
} | ||
|
||
b, err := data.GetBlobContent(ctx, bucket, prefixFile) | ||
if err != nil { | ||
return "", fmt.Errorf("fetching contents of prefix-file: %w", err) | ||
} | ||
s := string(b) | ||
return strings.TrimSpace(s), nil | ||
} | ||
|
||
func bucketFiles(ctx context.Context) (data.Iterator, error) { | ||
var iters []data.Iterator | ||
|
||
bucket, err := config.GetInputBucketURL() | ||
if err != nil { | ||
return nil, fmt.Errorf("config.GetInputBucketURL: %w", err) | ||
} | ||
prefix, err := getPrefix(ctx, bucket) | ||
if err != nil { | ||
return nil, fmt.Errorf("getPrefix: %w", err) | ||
} | ||
|
||
files, err := data.GetBlobKeysWithPrefix(ctx, bucket, prefix) | ||
if err != nil { | ||
return nil, fmt.Errorf("data.GetBlobKeysWithPrefix: %w", err) | ||
} | ||
|
||
for _, f := range files { | ||
b, err := data.GetBlobContent(ctx, bucket, f) | ||
if err != nil { | ||
return nil, fmt.Errorf("data.GetBlobContent: %w", err) | ||
} | ||
r := bytes.NewReader(b) | ||
i, err := data.MakeIteratorFrom(r) | ||
if err != nil { | ||
return nil, fmt.Errorf("data.MakeIteratorFrom: %w", err) | ||
} | ||
iters = append(iters, i) | ||
} | ||
iter, err := data.MakeNestedIterator(iters) | ||
if err != nil { | ||
return nil, fmt.Errorf("data.MakeNestedIterator: %w", err) | ||
} | ||
return iter, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2022 Security Scorecard Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
//nolint:tparallel,paralleltest // since t.Setenv is used | ||
func TestGetPrefix(t *testing.T) { | ||
//nolint:govet | ||
testcases := []struct { | ||
name string | ||
url string | ||
prefix string | ||
prefixFile string | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "no prefix", | ||
url: "testdata/getPrefix", | ||
prefix: "", | ||
prefixFile: "", | ||
want: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "prefix set", | ||
url: "testdata/getPrefix", | ||
prefix: "foo", | ||
prefixFile: "", | ||
want: "foo", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "prefix file set", | ||
url: "testdata/getPrefix", | ||
prefix: "", | ||
prefixFile: "marker", | ||
want: "bar", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "prefix and prefix file set", | ||
url: "testdata/getPrefix", | ||
prefix: "foo", | ||
prefixFile: "marker", | ||
want: "foo", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "non existent prefix file", | ||
url: "testdata/getPrefix", | ||
prefix: "", | ||
prefixFile: "baz", | ||
want: "", | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, testcase := range testcases { | ||
testcase := testcase | ||
t.Run(testcase.name, func(t *testing.T) { | ||
t.Setenv("INPUT_BUCKET_URL", testcase.url) | ||
t.Setenv("INPUT_BUCKET_PREFIX", testcase.prefix) | ||
t.Setenv("INPUT_BUCKET_PREFIX_FILE", testcase.prefixFile) | ||
testdataPath, err := filepath.Abs(testcase.url) | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
got, err := getPrefix(context.Background(), "file:///"+testdataPath) | ||
if (err != nil) != testcase.wantErr { | ||
t.Errorf("unexpected error produced: %v", err) | ||
} | ||
if got != testcase.want { | ||
t.Errorf("test failed: expected - %s, got = %s", testcase.want, got) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.