|
| 1 | +// Copyright 2022 Security Scorecard Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package main implements the PubSub controller. |
| 16 | +package main |
| 17 | + |
| 18 | +import ( |
| 19 | + "bytes" |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/ossf/scorecard/v4/cron/config" |
| 25 | + "github.com/ossf/scorecard/v4/cron/data" |
| 26 | +) |
| 27 | + |
| 28 | +// getPrefix returns the prefix used when reading input files from a bucket. |
| 29 | +// If "prefix" is set, the value is used irrespective of the value of "prefix-file". |
| 30 | +// Otherwise, the contents of "prefix-file" (if set) are used. |
| 31 | +func getPrefix(ctx context.Context, bucket string) (string, error) { |
| 32 | + prefix, err := config.GetInputBucketPrefix() |
| 33 | + if err != nil { |
| 34 | + return "", fmt.Errorf("config.GetInputBucketPrefix: %w", err) |
| 35 | + } |
| 36 | + if prefix != "" { |
| 37 | + // prioritize prefix if set |
| 38 | + return prefix, nil |
| 39 | + } |
| 40 | + |
| 41 | + prefixFile, err := config.GetInputBucketPrefixFile() |
| 42 | + if err != nil { |
| 43 | + return "", fmt.Errorf("config.GetInputBucketPrefixFile: %w", err) |
| 44 | + } |
| 45 | + if prefixFile == "" { |
| 46 | + // cant read a file which doesnt exist, but the value is optional so no error |
| 47 | + return "", nil |
| 48 | + } |
| 49 | + |
| 50 | + b, err := data.GetBlobContent(ctx, bucket, prefixFile) |
| 51 | + if err != nil { |
| 52 | + return "", fmt.Errorf("fetching contents of prefix-file: %w", err) |
| 53 | + } |
| 54 | + s := string(b) |
| 55 | + return strings.TrimSpace(s), nil |
| 56 | +} |
| 57 | + |
| 58 | +func bucketFiles(ctx context.Context) (data.Iterator, error) { |
| 59 | + var iters []data.Iterator |
| 60 | + |
| 61 | + bucket, err := config.GetInputBucketURL() |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("config.GetInputBucketURL: %w", err) |
| 64 | + } |
| 65 | + prefix, err := getPrefix(ctx, bucket) |
| 66 | + if err != nil { |
| 67 | + return nil, fmt.Errorf("getPrefix: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + files, err := data.GetBlobKeysWithPrefix(ctx, bucket, prefix) |
| 71 | + if err != nil { |
| 72 | + return nil, fmt.Errorf("data.GetBlobKeysWithPrefix: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + for _, f := range files { |
| 76 | + b, err := data.GetBlobContent(ctx, bucket, f) |
| 77 | + if err != nil { |
| 78 | + return nil, fmt.Errorf("data.GetBlobContent: %w", err) |
| 79 | + } |
| 80 | + r := bytes.NewReader(b) |
| 81 | + i, err := data.MakeIteratorFrom(r) |
| 82 | + if err != nil { |
| 83 | + return nil, fmt.Errorf("data.MakeIteratorFrom: %w", err) |
| 84 | + } |
| 85 | + iters = append(iters, i) |
| 86 | + } |
| 87 | + iter, err := data.MakeNestedIterator(iters) |
| 88 | + if err != nil { |
| 89 | + return nil, fmt.Errorf("data.MakeNestedIterator: %w", err) |
| 90 | + } |
| 91 | + return iter, nil |
| 92 | +} |
0 commit comments