-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a cron job to copy CII badges data
- Loading branch information
1 parent
4502dfb
commit 4795ba2
Showing
11 changed files
with
209 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright 2021 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. | ||
|
||
FROM golang@sha256:3c4de86eec9cbc619cdd72424abd88326ffcf5d813a8338a7743c55e5898734f AS base | ||
WORKDIR /src | ||
ENV CGO_ENABLED=0 | ||
COPY go.* ./ | ||
RUN go mod download | ||
COPY . ./ | ||
|
||
FROM base AS cii | ||
ARG TARGETOS | ||
ARG TARGETARCH | ||
RUN CGO_ENABLED=0 make build-cii-worker | ||
|
||
FROM gcr.io/distroless/base:nonroot@sha256:46d4514c17aca7a68559ee03975983339fc548e6d1014e2d7633f9123f2d3c59 | ||
COPY --from=cii /src/cron/cii/cii-worker cron/cii/cii-worker | ||
ENTRYPOINT ["cron/cii/cii-worker"] |
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,98 @@ | ||
// Copyright 2021 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 ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/ossf/scorecard/v3/cron/config" | ||
"github.com/ossf/scorecard/v3/cron/data" | ||
) | ||
|
||
const ciiBaseURL = "https://bestpractices.coreinfrastructure.org/projects.json" | ||
|
||
type ciiPageResp struct { | ||
RepoURL string `json:"repo_url"` | ||
BadgeLevel string `json:"badge_level"` | ||
} | ||
|
||
func writeToCIIDataBucket(ctx context.Context, pageResp []ciiPageResp, bucketURL string) error { | ||
for _, project := range pageResp { | ||
projectURL := strings.TrimPrefix(project.RepoURL, "https://") | ||
projectURL = strings.TrimPrefix(projectURL, "http://") | ||
fmt.Printf("Writing result for: %s\n", projectURL) | ||
if err := data.WriteToBlobStore(ctx, bucketURL, | ||
fmt.Sprintf("%s/result.json", projectURL), []byte(project.BadgeLevel)); err != nil { | ||
return fmt.Errorf("error during data.WriteToBlobStore: %w", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func getPage(ctx context.Context, pageNum int) ([]ciiPageResp, error) { | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, | ||
fmt.Sprintf("%s?page=%d", ciiBaseURL, pageNum), nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("error during http.NewRequestWithContext: %w", err) | ||
} | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("error during http.DefaultClient.Do: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
respContent, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("error during io.ReadAll: %w", err) | ||
} | ||
|
||
ciiResponse := []ciiPageResp{} | ||
if err := json.Unmarshal(respContent, &ciiResponse); err != nil { | ||
return nil, fmt.Errorf("error during json.Unmarshal: %w", err) | ||
} | ||
return ciiResponse, nil | ||
} | ||
|
||
func main() { | ||
ctx := context.Background() | ||
fmt.Println("Starting...") | ||
|
||
ciiDataBucket, err := config.GetCIIDataBucketURL() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
pageNum := 1 | ||
pageResp, err := getPage(ctx, pageNum) | ||
for err == nil && len(pageResp) > 0 { | ||
if err := writeToCIIDataBucket(ctx, pageResp, ciiDataBucket); err != nil { | ||
panic(err) | ||
} | ||
pageNum++ | ||
pageResp, err = getPage(ctx, pageNum) | ||
} | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println("Job completed") | ||
} |
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,22 @@ | ||
# Copyright 2021 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. | ||
|
||
steps: | ||
- name: 'gcr.io/cloud-builders/docker' | ||
args: ['build', '.', | ||
'--build-arg', 'COMMIT_SHA=$COMMIT_SHA', | ||
'-t', 'gcr.io/openssf/scorecard-cii-worker:$COMMIT_SHA', | ||
'-t', 'gcr.io/openssf/scorecard-cii-worker:latest', | ||
'-f', 'cron/cii/Dockerfile'] | ||
images: ['gcr.io/openssf/scorecard-cii-worker'] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright 2021 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. | ||
|
||
apiVersion: batch/v1beta1 | ||
kind: CronJob | ||
metadata: | ||
name: scorecard-cii-worker | ||
spec: | ||
# At 00:00UTC on 1st of every month. | ||
schedule: "@monthly" | ||
concurrencyPolicy: "Forbid" | ||
jobTemplate: | ||
spec: | ||
template: | ||
spec: | ||
restartPolicy: Never | ||
containers: | ||
- name: cii-worker | ||
image: gcr.io/openssf/scorecard-cii-worker:stable | ||
imagePullPolicy: Always |
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