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

CI for tests/releases #49

Merged
merged 8 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: test

on:
push:
branches:
- main
pull_request:

jobs:
analyzer:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Generate org-level access token for test-org
id: org-token
uses: getsentry/action-github-app-token@v1
with:
app_id: ${{ secrets.TEST_GITHUB_APP_ID }}
private_key: ${{ secrets.TEST_GITHUB_APP_PRIVATE_KEY }}

- name: Scan test-org
env:
GH_SECURITY_AUDITOR_TOKEN: ${{ steps.org-token.outputs.token }}
run: |
docker-compose run --rm github-analyzer \
--organization ${{ secrets.TEST_GITHUB_ORG }} \
--userPermissionStats \
--disableServer

- name: "Upload Artifact"
uses: actions/upload-artifact@v3
with:
name: output
path: output
retention-days: 7

asserts:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Generate org-level access token for test-org
id: org-token
uses: getsentry/action-github-app-token@v1
with:
app_id: ${{ secrets.TEST_GITHUB_APP_ID }}
private_key: ${{ secrets.TEST_GITHUB_APP_PRIVATE_KEY }}

- name: Run tests on output data
env:
GH_SECURITY_AUDITOR_TOKEN: ${{ steps.org-token.outputs.token }}
GH_SECURITY_AUDITOR_ORGANIZATION: ${{ secrets.TEST_GITHUB_ORG }}
run: |
docker-compose run --rm tests
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ _testmain.go
tags
wiki
*.envrc*
version.txt

/VERSION.cache
bin/
Expand Down
18 changes: 13 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
# syntax=docker/dockerfile:1
FROM golang:1.19-alpine as build

FROM golang:1.19-alpine
RUN apk add --no-cache git make

WORKDIR /ghanalyzer

ADD . /ghanalyzer
ADD go.* /ghanalyzer/

RUN go mod download
RUN go env -w GO111MODULE=on

RUN mkdir -p bin && go generate && go build -v -o bin/github-analyzer cmd/github-analyzer/main.go
ADD . /ghanalyzer/

ENTRYPOINT [ "/ghanalyzer/bin/github-analyzer" ]
RUN make all

# ----------------------------------------------------------------------------

FROM alpine

COPY --from=build /ghanalyzer/bin/github-analyzer /bin/github-analyzer

ENTRYPOINT [ "/bin/github-analyzer" ]
24 changes: 19 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
BIN=$(notdir $(wildcard cmd/*))
VERSION=$(shell git describe --tags --long)

.PHONY: all
all: ## compile auditor
all: $(addprefix bin/,$(BIN)) ## compile auditor

bin/%: bin generate
go build \
-v \
-ldflags "-X main.version=$(VERSION)" \
-o $@ \
cmd/$*/main.go

bin:
mkdir -p bin
go generate
go build -v -o bin/github-analyzer cmd/github-analyzer/main.go

.PHONY: generate
generate: ## generate go:generate files
go generate ./...

.PHONY: lint
lint: ## lint everything with pre-commit
Expand All @@ -22,11 +36,11 @@ fmt: ## go format
gofmt -w ./$*

.PHONY: vet
vet: ## go vet
vet: generate ## go vet
go vet ./...

.PHONY: test
test: ## run go tests (requires GitHub to be reachable via the network)
test: generate ## run go tests (requires GitHub to be reachable via the network)
go test -v -race -coverprofile coverage.txt ./...

.PHONY: help
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ You can see available options via the `--help` flag.
```sh
docker compose run \
--rm --service-ports \
co-github-analyzer \
github-analyzer \
--organization <your org name> \
--output output \
--token "$GH_SECURITY_AUDITOR_TOKEN"
Expand Down
29 changes: 21 additions & 8 deletions cmd/github-analyzer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"runtime/debug"
"strings"

_ "embed"
Expand All @@ -21,9 +22,22 @@ import (
"github.com/spf13/viper"
)

//go:generate sh version.sh
//go:embed version.txt
var version string
var version = "(devel)"

func getVersion() (response string) {
// inspired from
// https://github.com/mvdan/sh/blob/6ba49e2c622e3f56330f4de6238a390f395db2d8/cmd/shfmt/main.go#L181-L192
if info, ok := debug.ReadBuildInfo(); ok && version == "(devel)" {
mod := &info.Main
if mod.Replace != nil {
mod = mod.Replace
}
if mod.Version != "" {
version = mod.Version
}
}
return version
Comment on lines +28 to +39
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

➜ go install -v github.com/crashappsec/github-analyzer/cmd/github-analyzer@6dcfd30284c0e7593f98b14d303936321cabb7aa
go: downloading github.com/crashappsec/github-analyzer v0.1.4-pre-alpha.0.20221024225143-6dcfd30284c0
github.com/crashappsec/github-analyzer/pkg/config
github.com/crashappsec/github-analyzer/pkg/issue/category
github.com/crashappsec/github-analyzer/pkg/issue/resource
github.com/crashappsec/github-analyzer/pkg/issue/tags
github.com/crashappsec/github-analyzer/pkg/issue/severity
github.com/crashappsec/github-analyzer/pkg/issue
github.com/crashappsec/github-analyzer/pkg/log
github.com/crashappsec/github-analyzer/pkg/github/types
github.com/crashappsec/github-analyzer/pkg/futils
github.com/crashappsec/github-analyzer/pkg/github/utils
github.com/crashappsec/github-analyzer/pkg/scraping
github.com/crashappsec/github-analyzer/pkg/github/repo
github.com/crashappsec/github-analyzer/pkg/github/org
github.com/crashappsec/github-analyzer/pkg/github/auditor
github.com/crashappsec/github-analyzer/pkg/output/html
github.com/crashappsec/github-analyzer/cmd/github-analyzer

➜ github-analyzer --version
v0.1.4-pre-alpha.0.20221024225143-6dcfd30284c0

🎉 (note the commit was not built with version info via Makefile)

}

func main() {
if err := NewRootCommand().Execute(); err != nil {
Expand Down Expand Up @@ -137,7 +151,7 @@ func NewRootCommand() *cobra.Command {
rootCmd := &cobra.Command{
Use: fmt.Sprintf(
"github-analyzer (%s)",
strings.TrimSuffix(version, "\n"),
strings.TrimSuffix(getVersion(), "\n"),
),
Short: "A tool to collect statistics and highlight potential security issues within a GitHub org",
Long: "A tool to collect statistics and highlight potential security issues within a GitHub org",
Expand All @@ -148,7 +162,7 @@ func NewRootCommand() *cobra.Command {
PreRun: func(cmd *cobra.Command, args []string) {
onlyPrintVersion, _ := cmd.Flags().GetBool("version")
if onlyPrintVersion {
fmt.Println(version)
fmt.Println(getVersion())
os.Exit(0)
}
cmd.MarkFlagRequired("organization")
Expand All @@ -175,9 +189,6 @@ func NewRootCommand() *cobra.Command {
rootCmd.Flags().
BoolVarP(&config.ViperEnv.UserPermissionStats, "userPermissionStats", "", false, "enable user permission statistics (might be slow in large orgs due to throttling limits)")

rootCmd.Flags().
BoolVarP(&config.ViperEnv.DisableServer, "disableServer", "", false, "do not spin up an HTTP server, and only emit data in the designated output folder")

rootCmd.Flags().
BoolVarP(&config.ViperEnv.EnableScraping, "enableScraping", "", false, "enable experimental checks that rely on screen scraping")
rootCmd.Flags().
Expand All @@ -189,6 +200,8 @@ func NewRootCommand() *cobra.Command {

rootCmd.Flags().
IntVarP(&config.ViperEnv.Port, "port", "", 3000, "port for local http server used to display HTML with summary of findings (if you are using docker you will need to override the default port appropriately)")
rootCmd.Flags().
BoolVarP(&config.ViperEnv.DisableServer, "disableServer", "", false, "do not spin up an HTTP server, and only emit data in the designated output folder")
return rootCmd
}

Expand Down
2 changes: 0 additions & 2 deletions cmd/github-analyzer/version.sh

This file was deleted.

1 change: 0 additions & 1 deletion cmd/github-analyzer/version.txt

This file was deleted.

24 changes: 17 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
version: "3.8"

services:
co-github-analyzer:
# image allows to cache all deps hence to speed up CI
# CI image: ghcr.io/crashappsec/github-analyzer:latest
build: . # CI
container_name: github-analyzer
working_dir: $PWD
github-analyzer:
build: .
ports:
- 3000:3000
working_dir: $PWD
volumes:
- $PWD:$PWD # this allows to share ./output/
environment:
GH_SECURITY_AUDITOR_TOKEN: ${GH_SECURITY_AUDITOR_TOKEN:-}
GH_SECURITY_AUDITOR_USERNAME: ${GH_SECURITY_AUDITOR_USERNAME:-}
GH_SECURITY_AUDITOR_PASSWORD: ${GH_SECURITY_AUDITOR_PASSWORD:-}
GH_SECURITY_AUDITOR_OTP_SEED: ${GH_SECURITY_AUDITOR_OTP_SEED:-}

tests:
image: golang:1.19
command: make test
init: true
working_dir: $PWD
volumes:
- $PWD:$PWD
- $PWD:$PWD # this allows to share ./output/
environment:
GH_SECURITY_AUDITOR_TOKEN: ${GH_SECURITY_AUDITOR_TOKEN:-}
GH_SECURITY_AUDITOR_USERNAME: ${GH_SECURITY_AUDITOR_USERNAME:-}
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ const (
type ViperEnvVars struct {
CfgFile string `mapstructure:"CFG_FILE"`
EnableScraping bool `mapstructure:"ENABLE_SCRAPING"`
DisableServer bool `mapstructure:"DISABLE_SERVER"`
UserPermissionStats bool `mapstructure:"USER_PERMISSION_STATS"`
Version bool `mapstructure:"VERSION"`
Organization string `mapstructure:"ORGANIZATION"`
OtpSeed string `mapstructure:"OTP_SEED"`
OutputDir string `mapstructure:"OUTPUT_DIR"`
Password string `mapstructure:"PASSWORD"`
Port int `mapstructure:"PORT"`
DisableServer bool `mapstructure:"DISABLE_SERVER"`
ScmURL string `mapstructure:"SCM_URL"`
Token string `mapstructure:"TOKEN"`
Username string `mapstructure:"USERNAME"`
Expand Down
10 changes: 8 additions & 2 deletions pkg/github/auditor/auditor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ func TestSampleOrg(t *testing.T) {
Max: 3 * time.Minute,
Jitter: true,
}
name := "github-security-auditor-test-org"

name := os.Getenv("GH_SECURITY_AUDITOR_ORGANIZATION")
if name == "" {
name = "github-security-auditor-test-org"
}

org, err := org.NewOrganization(ctx, auditor.client, back, name)

assert.Nil(t, err, "Could not create organization")
assert.NotNil(t, org.CoreStats, "Could not fetch core stats")
assert.Equal(t, name, *org.CoreStats.Login)
assert.GreaterOrEqual(t, 1, org.CoreStats.TotalPrivateRepos)
assert.GreaterOrEqual(t, 1, *org.CoreStats.TotalPrivateRepos)
assert.NotNil(
t,
org.CoreStats.TwoFactorRequirementEnabled,
Expand Down