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

add Dagger module #154

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions dagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "poutine",
"sdk": "go",
"source": "dagger",
"engineVersion": "v0.11.6"
}
4 changes: 4 additions & 0 deletions dagger/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/dagger.gen.go linguist-generated
/internal/dagger/** linguist-generated
/internal/querybuilder/** linguist-generated
/internal/telemetry/** linguist-generated
4 changes: 4 additions & 0 deletions dagger/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/dagger.gen.go
/internal/dagger
/internal/querybuilder
/internal/telemetry
3 changes: 3 additions & 0 deletions dagger/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module dagger/poutine

go 1.22.3
164 changes: 164 additions & 0 deletions dagger/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Supply Chain Vulnerability Scanner for Build Pipelines by BoostSecurity.io
package main

import (
"context"
)

const image = "ghcr.io/boostsecurityio/poutine"
const currentVersion = "0.13.0@sha256:ea94b53ea45393b696570c625d755e82cdd1fdaffc1c0a9f1ad46cca368d2e7d"

// Poutine analysis options
type Poutine struct {
Config string
ConfigSrc *Directory
Format string
Scm string
ScmBaseUrl string
Threads string
Version string
Quiet bool
}

// Poutine analysis options
func New(ctx context.Context,
// Path to the configuration file
//+optional
config string,
// Directory containing additional configuration files
// +optional
configSrc *Directory,
// Output format (pretty, json, sarif)
//+optional
format string,
// SCM platform
//+optional
scm string,
// Base URI of the self-hosted SCM platform
//+optional
scmBaseUrl string,
// The number of threads to use for analysis
// +optional
threads string,
// Version of poutine to use
//+optional
version string,

) *Poutine {
return &Poutine{
Config: config,
ConfigSrc: configSrc,
Format: format,
Scm: scm,
ScmBaseUrl: scmBaseUrl,
Threads: threads,
Version: version,
}
}

func (m *Poutine) Container() *Container {
version := m.Version
if version == "" {
version = currentVersion
}

return dag.Container().
From(image + ":" + version).
WithoutEntrypoint().
WithExec([]string{"git", "config", "--global", "--add", "safe.directory", "/src"}).
With(func(c *Container) *Container {
if m.ConfigSrc != nil {
return c.
WithMountedDirectory("/config", m.ConfigSrc).
WithWorkdir("/config")
} else {
return c.WithWorkdir("/src")
}
})
}

// Analyze a Git repository in a directory
func (m *Poutine) AnalyzeLocal(ctx context.Context, src *Directory) (string, error) {
args := []string{"poutine", "analyze_local", "/src"}
args = append(args, m.poutineArgs()...)

return m.Container().
WithMountedDirectory("/src", src).
WithExec(args).
Stdout(ctx)
}

// Analyze a remote repository
func (m *Poutine) AnalyzeRepo(ctx context.Context,
// Repository to analyze in the format owner/repo
repo string,
// SCM access token
token *Secret,
// Git ref to analyze
// +optional
ref string,
) (string, error) {
args := []string{"poutine", "analyze_repo", repo}
args = append(args, m.poutineArgs()...)

if ref != "" {
args = append(args, "--ref", ref)
}

return m.Container().
WithSecretVariable("GH_TOKEN", token).
WithExec(args).
Stdout(ctx)
}

// Analyze an organization's repositories
func (m *Poutine) AnalyzeOrg(ctx context.Context,
// Organization name
org string,
// SCM access token
token *Secret,
// Ignore forked repositories
//+optional
ignoreForks bool,
) (string, error) {
args := []string{"poutine", "analyze_org", org}
args = append(args, m.poutineArgs()...)

if ignoreForks {
args = append(args, "--ignore-forks")
}

return m.Container().
WithSecretVariable("GH_TOKEN", token).
WithExec(args).
Stdout(ctx)
}

func (m *Poutine) poutineArgs() []string {
args := []string{}
if m.Format != "" {
args = append(args, "--format", m.Format)
}

if m.Config != "" {
args = append(args, "--config", m.Config)
}

if m.Scm != "" {
args = append(args, "--scm", m.Scm)
}

if m.ScmBaseUrl != "" {
args = append(args, "--scm-base-url", m.ScmBaseUrl)
}

if m.Threads != "" {
args = append(args, "--threads", m.Threads)
}

if m.Quiet {
args = append(args, "--quiet")
}

return args
}