Documentation
¶
Overview ¶
Package pinpoint scans repositories for GitHub Actions workflows and collects the action references used in them to check they are pinned to commit hashes.
Tools embedding pinpoint get the same views its command line renders: ScanStatus describes the pinning state of every external action reference of a repository and BuildSBOM models the repository, its actions and its workflow files as a protobom graph. The lower level pieces are available too: Scanner collects the references, CheckUpdates looks up the versions available for them and Updater pins them in place.
Index ¶
- Constants
- Variables
- func BuildSBOM(ctx context.Context, resolver Resolver, path string, opts ...SBOMOption) (*sbom.Document, error)
- func MarshalStatement(statement *gointoto.Statement) ([]byte, error)
- func SubjectFromRepository(ctx context.Context, path string, resolver SourceResolver) (*gointoto.ResourceDescriptor, error)
- type CheckOption
- type EnvTokenReader
- type GitHubResolver
- func (r *GitHubResolver) LatestRelease(ctx context.Context, repository string) (*Release, error)
- func (r *GitHubResolver) ResolveVersion(ctx context.Context, repository, version string) (*Release, error)
- func (r *GitHubResolver) SourceRepository(ctx context.Context, repository string) (string, error)
- type GitHubResolverOption
- type Kind
- type Plan
- type Predicate
- type PredicateReference
- type PredicateSummary
- type Reference
- type ReferenceStatus
- type Release
- type Report
- type Resolver
- type SBOMOption
- type Scanner
- type ScannerOption
- type Skip
- type SkipReason
- type SourceResolver
- type Status
- type ToolDescriptor
- type Update
- type UpdateStatus
- type Updater
- type UpdaterOption
- type Updates
Constants ¶
const ( // PredicateType is the in-toto predicate type of the pinpoint scan // results. PredicateType = "https://carabiner.dev/pinpoint/v1" )
Variables ¶
var ErrNoReleases = errors.New("repository has no releases or tags")
ErrNoReleases is returned by a resolver when a repository has no releases or tags to pin an action to.
Functions ¶
func BuildSBOM ¶
func BuildSBOM(ctx context.Context, resolver Resolver, path string, opts ...SBOMOption) (*sbom.Document, error)
BuildSBOM scans the GitHub Actions workflows and action definitions found under path and models the external action references of the repository as a protobom document. The graph has the repository commit at the root, each distinct action version hanging from it as a development tool, and the workflow files defining the references as the containers of the actions:
commit --devTool--> action --contained_by--> workflow file
The file level can be turned off with WithWorkflowFiles(false), leaving only the commit and the actions.
The resolver names the version each reference is using: the tag its commit corresponds to for pinned entries, the release its version resolves to for the rest. Lookups are required, a reference that cannot be resolved fails the build so that an emitted SBOM always carries verified versions.
func MarshalStatement ¶
MarshalStatement renders an in-toto statement as indented JSON.
func SubjectFromRepository ¶
func SubjectFromRepository(ctx context.Context, path string, resolver SourceResolver) (*gointoto.ResourceDescriptor, error)
SubjectFromRepository builds the in-toto subject describing the repository at path (or the first one found walking up from it) at the commit it currently has checked out.
Scans commonly run in a fork, which is not the repository the code belongs to. When a resolver is received, the subject names the project the fork was created from and records the fork it was scanned in as an annotation. Pass a nil resolver to name the repository as the local remote has it.
Types ¶
type CheckOption ¶
type CheckOption func(*checkOptions)
CheckOption configures how CheckUpdates looks up versions.
func WithLatestReleases ¶
func WithLatestReleases(check bool) CheckOption
WithLatestReleases controls whether the newest release of each action is looked up. Turning it off leaves only the versions the unpinned entries would be pinned to, at the cost of not knowing what is outdated.
type EnvTokenReader ¶
type EnvTokenReader struct {
VarNames []string
}
EnvTokenReader reads the API token from the first environment variable of a list that has one set.
func (*EnvTokenReader) ReadToken ¶
func (r *EnvTokenReader) ReadToken() (string, error)
ReadToken returns the token found in the environment. Finding none is an error, the client only surfaces it when a token is required.
type GitHubResolver ¶
type GitHubResolver struct {
// contains filtered or unexported fields
}
GitHubResolver resolves action versions using the GitHub API. Responses are cached and the lookups are safe to run concurrently: resolving the same version twice only calls the API once, even when both callers ask at the same time.
func NewGitHubResolver ¶
func NewGitHubResolver(opts ...GitHubResolverOption) (*GitHubResolver, error)
NewGitHubResolver creates a resolver that talks to the GitHub API through the carabiner GitHub client. The token is read from the GITHUB_TOKEN or GH_TOKEN environment variables, unless one is passed with WithToken, which wins over the environment; calls are made anonymously when no token is set anywhere, which works for public repositories but gets rate limited quickly. Callers that hold a client configured some other way can hand it to NewGitHubResolverWithClient.
func NewGitHubResolverWithClient ¶
func NewGitHubResolverWithClient(client apiCaller) *GitHubResolver
NewGitHubResolverWithClient creates a resolver using a preconfigured API client.
func (*GitHubResolver) LatestRelease ¶
LatestRelease returns the newest release of a repository. Repositories that don't publish releases are resolved to their highest semver tag.
func (*GitHubResolver) ResolveVersion ¶
func (r *GitHubResolver) ResolveVersion(ctx context.Context, repository, version string) (*Release, error)
ResolveVersion returns the commit a tag or branch of a repository points to. The release is named after the most precise tag pointing at the same commit, so pinning an entry tracking a major version records the patch release it resolved to.
func (*GitHubResolver) SourceRepository ¶
SourceRepository returns the repository a fork was created from, the root of the fork network when the fork was itself forked. Repositories that are not forks resolve to themselves, with the name the forge has for them now: a repository that was renamed resolves to its current owner/name.
type GitHubResolverOption ¶
type GitHubResolverOption func(*githubResolverOptions)
GitHubResolverOption configures a resolver created by NewGitHubResolver.
func WithToken ¶
func WithToken(token string) GitHubResolverOption
WithToken makes the resolver authenticate its API calls with token instead of reading one from the environment. An empty token keeps the environment lookup, so callers can pass along whatever they were configured with.
type Kind ¶
type Kind string
Kind classifies the thing a reference points to.
const ( // KindAction is an action published in a repository. KindAction Kind = "action" // KindReusableWorkflow is a call to a reusable workflow. KindReusableWorkflow Kind = "reusable_workflow" // KindContainer is an action run from a container image (docker://). KindContainer Kind = "container" // KindLocal is an action or workflow stored in the repository itself. KindLocal Kind = "local" )
type Plan ¶
type Plan struct {
// Updates are the references that can be moved to a new version.
Updates []Update
// Skipped are the references that were left alone.
Skipped []Skip
}
Plan is the set of changes needed to bring a group of references to their latest released version. Commands that only report on updates (instead of applying them) use the same plan.
type Predicate ¶
type Predicate struct {
// Tool identifies the scanner that produced the results. The version is
// carried in the descriptor annotations, the in-toto resource has no
// field for it.
Tool ToolDescriptor `json:"tool"`
// Date is the RFC3339 timestamp of the scan.
Date string `json:"date"`
// UpdatesChecked is true when pinpoint looked up the versions available
// for the references. Policies asserting on outdated actions need to
// check this flag, the counts are zero when no lookup was made.
UpdatesChecked bool `json:"updates_checked"`
// Workflows lists the workflow files that were scanned.
Workflows []string `json:"workflows"`
// Actions lists the action definitions that were scanned. References
// found in a composite action are attributed to the file listed here,
// not to a workflow.
Actions []string `json:"actions"`
// Summary has the reference counts of the scan.
Summary PredicateSummary `json:"summary"`
// References are all the action references found in the scanned files.
References []PredicateReference `json:"references"`
}
Predicate captures the result of scanning a repository for action references. It carries every reference found, pinned or not, so that policies can reason about more than just the pinning status.
type PredicateReference ¶
type PredicateReference struct {
// Workflow is the path of the file defining the reference, relative to
// the repository: a workflow or the action definition of a composite
// action.
Workflow string `json:"workflow"`
// Line is the line in the workflow file defining the reference.
Line int `json:"line"`
// Job is the ID of the job using the action.
Job string `json:"job"`
// Step is the name or ID of the step using the action. It is empty for
// job level references such as reusable workflow calls.
Step string `json:"step"`
// Uses is the raw value of the `uses:` entry.
Uses string `json:"uses"`
// Kind is one of action, reusable_workflow, container or local.
Kind Kind `json:"kind"`
// Owner is the user or organization owning the action, empty for
// container and local references.
Owner string `json:"owner"`
// Repository is the owner/name of the repository hosting the action,
// empty for container and local references.
Repository string `json:"repository"`
// Version is the fragment after the @ sign in the reference: a hash, a
// tag, a branch or an image digest.
Version string `json:"version"`
// Pinned is true when the reference points to an immutable version of
// the action. Local references are always considered pinned as they
// ride along the repository.
Pinned bool `json:"pinned"`
// LatestVersion is the tag of the newest release of the action. It is
// empty when the version was not looked up or could not be resolved.
LatestVersion string `json:"latest_version"`
// LatestCommit is the commit the latest release points at.
LatestCommit string `json:"latest_commit"`
// Outdated is true when the reference is known not to point at the
// commit of the latest release.
Outdated bool `json:"outdated"`
}
PredicateReference is an action reference as captured in an attestation. Every field is always rendered, even when empty, so that policies can read them without having to guard each expression with has().
type PredicateSummary ¶
type PredicateSummary struct {
Workflows int `json:"workflows"`
Actions int `json:"actions"`
References int `json:"references"`
Pinned int `json:"pinned"`
Unpinned int `json:"unpinned"`
// Outdated counts the references that are not using the latest release
// of their action. It is zero when the versions were not looked up.
Outdated int `json:"outdated"`
}
PredicateSummary counts the findings of a scan. It lets policies check the overall result without iterating the reference list.
type Reference ¶
type Reference struct {
// Workflow is the path of the file defining the reference, relative to
// the scan root: a workflow or the action definition of a composite
// action.
Workflow string
// Job is the ID of the job where the reference was found.
Job string
// Step is the name (or ID) of the step using the action. It is empty
// for job-level references, ie calls to reusable workflows.
Step string
// Uses is the raw value of the `uses:` entry.
Uses string
// Comment is the comment trailing the entry, without the hash sign. It
// is where the version a pinned hash corresponds to is kept by
// convention.
Comment string
// Kind classifies what the reference points to.
Kind Kind
// Line is the line of the workflow file where the reference is defined.
Line int
}
Reference captures a `uses:` action reference found in a workflow or in an action definition.
func (*Reference) IsPinned ¶
IsPinned returns true when the reference points to an immutable version of the action: a full length commit hash or, for docker actions, an image digest. Local actions (./path) always return true as they ride along the repository and cannot be pinned.
func (*Reference) Owner ¶
Owner returns the user or organization owning the referenced action or workflow, an empty string when the reference has no repository.
func (*Reference) Repository ¶
Repository returns the owner/name of the repository hosting the referenced action or workflow. It returns an empty string for references that don't live in a repository (local and container actions).
func (*Reference) Version ¶
Version returns the fragment identifying the version of the referenced action: whatever follows the @ sign in the `uses:` value. It returns an empty string when the reference is not versioned.
func (*Reference) VersionInUse ¶
VersionInUse returns the version a reference is using as it is best known: the comment trailing the entry when there is one (the slot where the version of a pinned hash is kept by convention), the version fragment of the entry otherwise.
type ReferenceStatus ¶
type ReferenceStatus struct {
// Reference is the entry as found when scanning.
Reference Reference
// Version is the version the reference is using as it is best known:
// the comment naming a pinned hash when there is one, the version
// fragment of the entry otherwise.
Version string
// Pinned is whether the reference points at an immutable version: a
// commit hash or, for container actions, an image digest.
Pinned bool
// Checked reports whether the versions of the action were looked up.
// Latest and Outdated say nothing when it is false, as for container
// images and references the forge did not answer for.
Checked bool
// Latest is the newest release of the action.
Latest Release
// Outdated is true when the reference does not point at the commit of
// the latest release.
Outdated bool
}
ReferenceStatus describes the pinning state of one external action reference: the version it is using and how it compares to the latest release of the action.
type Release ¶
type Release struct {
// Tag is the name of the release tag, for example v1.3.0.
Tag string
// Commit is the full hash of the commit the tag points to.
Commit string
}
Release captures a released version of an action repository.
type Report ¶
type Report struct {
// Workflows lists the paths of the workflow files that were scanned,
// relative to the scan root, including those with no references.
Workflows []string
// Actions lists the paths of the action definitions that were
// scanned, relative to the scan root, including those with no
// references.
Actions []string
// References are all the action references found in the scanned files.
References []Reference
}
Report holds the action references collected when scanning a repository.
func (*Report) Predicate ¶
Predicate builds the attestation predicate describing the report. The versions available for the references are folded in when they were looked up, a nil updates set marks the predicate as not carrying them.
func (*Report) Statement ¶
func (r *Report) Statement(updates *Updates, subjects ...*gointoto.ResourceDescriptor) (*gointoto.Statement, error)
Statement wraps the report predicate in an in-toto statement attesting to the received subjects. The updates are the versions available for the references, nil when they were not looked up.
type Resolver ¶
type Resolver interface {
// LatestRelease returns the newest release of an owner/name repository.
// It returns ErrNoReleases when the repository has nothing to pin to.
LatestRelease(ctx context.Context, repository string) (*Release, error)
// ResolveVersion returns the commit a version of a repository points to.
// The version is a tag or a branch, an empty version resolves to the tip
// of the default branch. The returned release carries the most precise
// tag naming that commit, falling back to the version as received.
ResolveVersion(ctx context.Context, repository, version string) (*Release, error)
}
Resolver looks up the versions of the repositories hosting actions. It is the piece that talks to the forge, implementations are expected to cache their responses as the same repository is commonly referenced many times.
type SBOMOption ¶
type SBOMOption func(*sbomOptions)
SBOMOption configures how BuildSBOM models the graph.
func WithWorkflowFiles ¶
func WithWorkflowFiles(files bool) SBOMOption
WithWorkflowFiles controls whether the workflow files defining the references are added to the graph as the containers of the actions. Turning them off stops the graph at the action nodes.
type Scanner ¶
type Scanner struct{}
Scanner looks for GitHub Actions workflows and action definitions and extracts the action references used in them.
func NewScanner ¶
func NewScanner(opts ...ScannerOption) *Scanner
NewScanner creates a new workflow scanner.
func (*Scanner) Scan ¶
Scan walks the directory at root looking for the files GitHub Actions reads and returns a report with all the action references found in them.
Files are recognized by where they sit and by what they hold, so that a repository keeping its Actions code outside .github/workflows is covered as well as one that does not. Every YAML file under a forge's workflows directory is a workflow and every action.yml in the tree is an action definition; on top of those, any other YAML file declaring the jobs it runs on an event is read as a workflow and any file saying how an action runs is read as an action definition. That is what catches the composite action a repository publishes from its root, the workflow templates under .github/workflow-templates and the workflows of a monorepo's subprojects.
The .git, vendor, node_modules and testdata directories are never walked.
type Skip ¶
type Skip struct {
// Reference is the entry that was not updated.
Reference Reference
// Reason explains why the reference was skipped.
Reason SkipReason
// Release is the version the reference was resolved to, set for the
// entries skipped because they already point at it.
Release *Release
// Err carries the underlying error for references skipped because the
// resolver could not answer for them.
Err error
}
Skip is a reference that was left untouched, and why.
type SkipReason ¶
type SkipReason string
SkipReason explains why a reference cannot be updated.
const ( // SkipLocal is a reference to an action in the repository itself. SkipLocal SkipReason = "local action, it rides along the repository" // SkipContainer is an action run from a container image. SkipContainer SkipReason = "container action, not hosted in a repository" // SkipNoRepository is a reference we cannot map to a repository. SkipNoRepository SkipReason = "unable to determine the source repository" // SkipPinned is a reference already pinned to a commit hash. SkipPinned SkipReason = "already pinned to a commit hash" // SkipUpToDate is a reference already pinned to the latest release. SkipUpToDate SkipReason = "already pinned to the latest release" // SkipUnresolved is a reference whose version could not be looked up. SkipUnresolved SkipReason = "unable to resolve the latest release" )
type SourceResolver ¶
type SourceResolver interface {
// SourceRepository returns the owner/name of the repository a fork was
// created from, or the repository itself when it is not a fork.
SourceRepository(ctx context.Context, repository string) (string, error)
}
SourceResolver maps a repository to the project it was forked from. It is what lets an attestation name the repository the code comes from instead of the fork it was scanned in.
type Status ¶
type Status struct {
// References are the external action references found.
References []ReferenceStatus
// Checked reports whether the versions of the actions were looked up
// at all. When it is false no reference carries version data.
Checked bool
// Unresolved are the references the forge was asked about and gave no
// answer for, each with the error that stopped the lookup.
Unresolved []Skip
}
Status is the pinning state of the external action references of a repository, one entry per reference in the order they were scanned.
func ScanStatus ¶
ScanStatus scans the GitHub Actions workflows and action definitions found under path and describes the pinning state of every external action reference: those pointing at other repositories or at container images. Local references ride along the repository and are left out.
The resolver looks up the versions of the actions to report the references that are outdated. Pass nil to skip the lookups: the statuses then only know whether the references are pinned.
type ToolDescriptor ¶
type ToolDescriptor struct {
*gointoto.ResourceDescriptor
}
ToolDescriptor is the in-toto resource describing the scanner. It is the official ResourceDescriptor, wrapped only to render it through protojson: the JSON tags of the generated Go type spell some fields in snake case while the in-toto specification writes them in camel case.
func (ToolDescriptor) MarshalJSON ¶
func (t ToolDescriptor) MarshalJSON() ([]byte, error)
MarshalJSON renders the descriptor as in-toto defines it.
func (*ToolDescriptor) UnmarshalJSON ¶
func (t *ToolDescriptor) UnmarshalJSON(data []byte) error
UnmarshalJSON reads a descriptor rendered as in-toto defines it.
type Update ¶
type Update struct {
// Reference is the entry as found when scanning.
Reference Reference
// Release is the version the reference will be pinned to.
Release Release
}
Update is a change to be applied to an action reference to point it at a released version of the action.
type UpdateStatus ¶
type UpdateStatus struct {
// Latest is the newest release of the action.
Latest Release
// Outdated is true when the reference does not point at the commit of
// the latest release.
Outdated bool
// Pin is the version an unpinned reference would be pinned to without
// an upgrade: the release naming the version the entry already uses.
// It is empty for the references that are already pinned.
Pin Release
}
UpdateStatus is what pinpoint knows about the versions available for an action reference.
type Updater ¶
type Updater struct {
// Resolver looks up the versions of the actions.
Resolver Resolver
// Upgrade makes the updater move the references to the latest release
// of each action. When false, references are pinned to the commit their
// current version points at.
Upgrade bool
// Concurrency is how many references are resolved in parallel. Zero
// selects a sensible default.
Concurrency int
}
Updater computes and applies the updates that pin action references to a commit hash.
func NewUpdater ¶
func NewUpdater(opts ...UpdaterOption) (*Updater, error)
NewUpdater creates an updater that resolves versions using the GitHub API. Callers with a resolver of their own build the Updater directly.
func (*Updater) Apply ¶
Apply writes the updates to the files under root and returns the paths of the files that changed. Each entry is rewritten in place, preserving the rest of the line and the file.
func (*Updater) Plan ¶
Plan computes the changes needed to pin a list of references to a commit hash: the commit of the version each reference tracks or, for updaters set to upgrade, the commit of the latest release of the action. References that need no change, and those that cannot be pinned at all, are returned as skips.
type UpdaterOption ¶
type UpdaterOption func(*Updater)
UpdaterOption configures an updater.
func WithConcurrency ¶
func WithConcurrency(workers int) UpdaterOption
WithConcurrency sets how many references the updater resolves in parallel.
func WithUpgrade ¶
func WithUpgrade(upgrade bool) UpdaterOption
WithUpgrade makes the updater pin references to the latest release of the action instead of to the version they already track.
type Updates ¶
type Updates struct {
// contains filtered or unexported fields
}
Updates holds the versions available for the references of a report, indexed by the value of their `uses:` entry: two entries using the same action at the same version share a status.
func CheckUpdates ¶
func CheckUpdates(ctx context.Context, resolver Resolver, refs []Reference, opts ...CheckOption) (*Updates, error)
CheckUpdates looks up the versions available for every reference received: the version each unpinned entry would be pinned to and, unless disabled with WithLatestReleases, the latest release of each action. References that cannot be resolved (local actions, container images, repositories with no releases) are left out of the result and reported as unknown.
func NewUpdates ¶
NewUpdates indexes the versions resolved by a pair of plans. The latest plan is the one of an updater set to upgrade, it carries the newest release of every action. The pin plan is the one of an updater that is not upgrading, it carries the version each unpinned entry would be pinned to. A nil plan simply contributes nothing.
func (*Updates) LatestChecked ¶
LatestChecked reports whether the newest release of the actions was looked up. It is false when only the pin targets were resolved, and nothing can be said about references being outdated.
func (*Updates) Outdated ¶
Outdated returns true when a reference is known not to point at the latest release of its action.
func (*Updates) Status ¶
func (u *Updates) Status(ref *Reference) (UpdateStatus, bool)
Status returns what is known about the versions available for a reference. The second return value is false when pinpoint could not find out.
func (*Updates) Unresolved ¶
Unresolved returns the references pinpoint asked the forge about and got no answer for, each with the error that stopped it. An expired token or a rate limit shows up here.