Skip to content

Commit

Permalink
OSV scanner github action (#698)
Browse files Browse the repository at this point in the history
Creates docker image for osv scanner to allow it to run as a github
action, and the appropriate action.yml.

The current implementation is not particularly useful, only printing out
the found vulnerabilities to the logs. It should be running under this
PR.

TODO: Decide what the goal of the github action will be.
  • Loading branch information
another-rex authored Sep 16, 2022
1 parent 1c70592 commit a2314c3
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/osv-scanner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2022 Google LLC
#
# 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.

name: osv-scanner

on: [pull_request]

jobs:
osv-scanner:
name: Scan for vulnerabilities
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: osv scan step
id: osv-scanning
uses: ./tools/osv-scanner/
38 changes: 38 additions & 0 deletions tools/osv-scanner/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2022 Google LLC
#
# 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:alpine

RUN mkdir /src
WORKDIR /src

COPY ./go.mod /src/go.mod
COPY ./go.sum /src/go.sum
RUN go mod download

COPY ./ /src/
RUN go build -o osv-scanner ./cmd/osv-scanner/

FROM alpine:latest
RUN apk --no-cache add \
ca-certificates \
git

# Allow git to run on mounted directories
RUN git config --global --add safe.directory '*'

WORKDIR /root/
COPY --from=0 /src/osv-scanner ./

ENTRYPOINT ["/root/osv-scanner"]
13 changes: 13 additions & 0 deletions tools/osv-scanner/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: 'osv-scanner'
description: 'Scans your directory against the OSV database'
inputs:
to-scan:
description: 'Directory to scan'
required: true
default: '/github/workspace'
runs:
using: 'docker'
image: 'Dockerfile'
args:
- '--skip-git'
- ${{ inputs.to-scan }}
12 changes: 9 additions & 3 deletions tools/osv-scanner/cmd/osv-scanner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ import (
)

// scanDir walks through the given directory to try to find any relevant files
func scanDir(query *osv.BatchedQuery, dir string) error {
func scanDir(query *osv.BatchedQuery, dir string, skipGit bool) error {
log.Printf("Scanning dir %s\n", dir)
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Printf("Failed to walk %s: %v", path, err)
return err
}

if info.IsDir() && info.Name() == ".git" {
if !skipGit && info.IsDir() && info.Name() == ".git" {
gitQuery, err := scanGit(filepath.Dir(path))
if err != nil {
log.Printf("scan failed for %s: %v\n", path, err)
Expand Down Expand Up @@ -265,6 +265,11 @@ func main() {
Name: "json",
Usage: "sets output to json (WIP)",
},
&cli.BoolFlag{
Name: "skip-git",
Usage: "skip scanning git repositories",
Value: false,
},
},
ArgsUsage: "[directory1 directory2...]",
Action: func(context *cli.Context) error {
Expand All @@ -291,9 +296,10 @@ func main() {
}
}

skipGit := context.Bool("skip-git")
genericDirs := context.Args().Slice()
for _, dir := range genericDirs {
err := scanDir(&query, dir)
err := scanDir(&query, dir, skipGit)
if err != nil {
return err
}
Expand Down

0 comments on commit a2314c3

Please sign in to comment.