Documentation ¶
Overview ¶
Package docked provides types and functionality for analyzing and linting Dockerfiles.
docked uses the Docker buildkit parser to retrieve the AST of an input Dockerfile. It also provides a simple API for defining and registering rules for processing of the AST. All in-built rules are built upon this API. See those defined under the validations package.
Configuration ¶
An external YAML configuration is supported by docked.Config. The configuration allows for ignoring in-built rules, overriding priority of in-built rules, as well as defining custom rules based on the validations.SimpleRegexRule structure.
Analysis ¶
Invoking docked.Docked#Analysis will use the list of in-built validation rules, and return a docked.AnalysisResult. The result should be walked programmatically to generate a report. Please see reports under the reporting package for examples. The HTML and JSON reporters under the reporter package provide implementations for use in the accompanying cli tool for use in CI/CD pipelines.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AnalysisResult ¶
type AnalysisResult struct { Evaluated []validations.Validation `json:"evaluated"` NotEvaluated []validations.Validation `json:"not_evaluated"` }
AnalysisResult holds final validations, separated in those which have been Evaluated and those which have not (NotEvaluated). A validations.Validation holds references to the rule and the result of validation to simplify reporting.
func (AnalysisResult) GoString ¶ added in v0.2.0
func (a AnalysisResult) GoString() string
GoString returns a string representation for formatter patterns %#v
type Config ¶
type Config struct { // Ignore this collection of rule ids Ignore []string `yaml:"ignore"` // RuleOverrides allows users to override the ConfigRuleOverride.Priority of a specific rule by ConfigRuleOverride.ID RuleOverrides *RuleOverrides `yaml:"rule_overrides,omitempty"` CustomRules []validations.SimpleRegexRule `yaml:"custom_rules,omitempty"` SkipDefaultRules bool `yaml:"skip_default_rules,omitempty"` // IncludeRules allows setting an approved list of rules to include when SkipDefaultRules is true IncludeRules []string `yaml:"include_rules,omitempty"` }
Config represents the YAML config structure exposed to users
type ConfigRuleOverride ¶
type ConfigRuleOverride struct { // The rule id to override ID string `yaml:"id"` // The overridden priority Priority *model.Priority `yaml:"priority,omitempty"` }
ConfigRuleOverride defines the id-priority override mapping used in a config file
type ConfiguredRules ¶
ConfiguredRules partitions results into active and inactive lists
type Docked ¶
type Docked struct { // Configuration for analysis Config Config // Suppress the underlying warnings presented by buildkit's parser. Use this if you want to pipe text summary to file. SuppressBuildKitWarnings bool // contains filtered or unexported fields }
Docked is the main type for initializing Dockerfile linting/analysis
func (*Docked) Analyze ¶
func (d *Docked) Analyze(location string) (AnalysisResult, error)
Analyze a dockerfile residing at location.
All known rules which are applicable to the Dockerfile contents are evaluated, allowing configuration-based ignores and manipulation of priority/severity of rules.
Returns the AnalysisResult or error.
Example ¶
ExampleDocked_Analyze provides an example of programmatically invoking Docked.Analyze with default rules
c := Config{} if err := c.Load("./testdata/config/example.yml"); err != nil { panic(err) } d := Docked{ Config: c, SuppressBuildKitWarnings: true, } result, err := d.Analyze("./testdata/minimal.dockerfile") if err != nil { panic("Failed to analyze dockerfile") } // programmatically consume array of evaluated and/or not-evaluated rules printEvaluated(result.Evaluated)
Output: D5:no-debian-frontend - Success D5:secret-aws-access-key - Success D5:secret-aws-secret-access-key - Success DC:avoid-sudo - Success DC:consider-multistage - Success DC:curl-without-fail - Success DC:gpg-without-batch - Success DC:layered-ownership-change - Success
Example (WithCustomRules) ¶
ExampleDocked_Analyze_withCustomRules provides an example of programmatically invoking Docked.Analyze with custom rules
c := Config{} // The config file will define a rule named adding-full-directory if err := c.Load("./testdata/config/example_custom.yml"); err != nil { panic(err) } d := Docked{ Config: c, SuppressBuildKitWarnings: true, } result, err := d.Analyze("./testdata/minimal_custom.dockerfile") if err != nil { panic("Failed to analyze dockerfile") } // programmatically consume array of evaluated and/or not-evaluated rules printEvaluated(result.Evaluated)
Output: D0:adding-full-directory - Failure * [ 7] ADD . /go/src/app D5:no-debian-frontend - Success D5:secret-aws-access-key - Success D5:secret-aws-secret-access-key - Success DC:avoid-sudo - Success DC:consider-multistage - Success DC:curl-without-fail - Success DC:gpg-without-batch - Success DC:layered-ownership-change - Success
func (*Docked) AnalyzeWithRuleList ¶
func (d *Docked) AnalyzeWithRuleList(location string, configuredRules ConfiguredRules) (AnalysisResult, error)
AnalyzeWithRuleList is just like Analyze, but accepts an additional parameter of ConfiguredRules
This allows programmatic evaluation of rules without the ignore/priority overrides done as a default within Analyze.
Returns the AnalysisResult or error.
Example ¶
ExampleDocked_AnalyzeWithRuleList provides an example of programmatically invoking Docked.AnalyzeWithRuleList with user-defined rules. See also reporter.TextReporter and reporter.HTMLReporter for in-built output formatters.
d := Docked{} // user can extend default rule set or define their own activeRules := rules.RuleList{} myRule := validations.SimpleRegexRule{ Name: "no-distroless", Pattern: `\bgcr\.io/distroless\b`, Priority: model.CriticalPriority, Command: commands.From, } activeRules.AddRule(myRule) result, err := d.AnalyzeWithRuleList("./testdata/minimal.dockerfile", ConfiguredRules{Active: activeRules}) if err != nil { panic("Failed to analyze dockerfile") } // programmatically consume array of evaluated and/or not-evaluated rules printEvaluated(result.Evaluated)
Output: D7:no-distroless - Success D7:no-distroless - Failure * [13] FROM gcr.io/distroless/base-debian10
type RuleOverrides ¶
type RuleOverrides []ConfigRuleOverride
RuleOverrides is a slice of ConfigRuleOverride. This type allows for simpler definitions and YAML parsing.
func (*RuleOverrides) UnmarshalYAML ¶
func (r *RuleOverrides) UnmarshalYAML(value *yaml.Node) error
UnmarshalYAML implements the interface necessary to have greater control over deserializing RuleOverrides