Documentation
¶
Overview ¶
Package ci implements the "bogform ci" commands, which run Bogform from repository pipelines and report the results on pull requests.
Index ¶
- Constants
- Variables
- func DetectPlatformFromRemote(remote string) string
- func Marker(kind, directory, workspace string) string
- func NewPublisher(serverURL, token string, client *http.Client) (func(context.Context, *Report) error, error)
- func NextSteps(platform string, opts ScaffoldOptions) string
- func RenderBogformMD(opts ScaffoldOptions) (string, error)
- func RenderPipeline(opts ScaffoldOptions) ([]byte, error)
- func Scaffold(opts ScaffoldOptions) (string, error)
- func WriteBogformMD(opts ScaffoldOptions) (bool, error)
- type Change
- type Commenter
- type Context
- type Counts
- type EnvFunc
- type Finding
- type Options
- type RenderOptions
- type Report
- type Runner
- type ScaffoldOptions
Constants ¶
const ( CheckS3PublicAccessBlock = "aws-s3-public-access-block" CheckS3PublicACL = "aws-s3-public-acl" CheckS3PublicPolicy = "aws-s3-public-policy" CheckEBSUnencrypted = "aws-ebs-unencrypted" CheckRDSUnencrypted = "aws-rds-unencrypted" CheckEFSUnencrypted = "aws-efs-unencrypted" CheckIAMWildcard = "aws-iam-wildcard" CheckSecurityGroupAdmin = "aws-security-group-open-admin" CheckNSGAdmin = "azure-nsg-open-admin" CheckStoragePublicAccess = "azure-storage-public-access" CheckStorageContainer = "azure-storage-container-public" CheckStorageHTTPSOnly = "azure-storage-https-only" CheckStorageMinTLS = "azure-storage-min-tls" CheckDatabaseFirewallOpen = "azure-database-open-firewall" )
Check IDs. They're stable, so that people can turn individual checks off.
const ( PlatformNone = "none" PlatformGitHub = "github" PlatformGitLab = "gitlab" PlatformAzure = "azure" PlatformBitbucket = "bitbucket" )
Names of the supported pipeline platforms.
const ( ServerURLEnvName = "BOG_SERVER_URL" ServerTokenEnvName = "BOG_SERVER_TOKEN" )
Names of the variables that connect pipelines to a Bogform server.
const ( KindPlan = "plan" KindApply = "apply" KindDrift = "drift" )
Kinds of run.
const ( StatusNoChanges = "no_changes" StatusChanges = "changes" StatusApplied = "applied" StatusDrifted = "drifted" StatusFailed = "failed" )
Results of a run.
const ( ExitOK = 0 ExitFailed = 1 ExitDrifted = 2 )
Exit codes of the "ci" commands.
const ( ApplyManual = "manual" ApplyMerge = "merge" )
When a pipeline applies changes.
const AllowDestroyEnvName = "BOG_CI_ALLOW_DESTROY"
AllowDestroyEnvName is the variable that lets an apply destroy or replace resources.
const AllowDestroyLabel = "allow-destroy"
AllowDestroyLabel is the pull request label that lets an apply destroy or replace resources.
const BogformMDFile = "BOGFORM.md"
BogformMDFile is where "ci init" writes the guide, relative to the repository root.
const ReportFormatVersion = 1
ReportFormatVersion is the version of the report.json format.
const SkipChecksTag = "bogform:skip-checks"
SkipChecksTag is the tag that turns checks off for one resource. Its value is a comma-separated list of check IDs, or "all".
const TokenEnvName = "BOG_CI_TOKEN"
TokenEnvName is the variable holding the token used to post pull request comments. It's removed from the environment of the commands Bogform runs, so that it doesn't reach them by accident. This isn't a security boundary: code running as the same user can read a parent process's memory. So give this token only the permission to comment.
const WorkDir = ".bogform-ci"
WorkDir is the directory, relative to the configuration, where runs keep their plan and report.
Variables ¶
var CheckIDs = []string{ CheckS3PublicAccessBlock, CheckS3PublicACL, CheckS3PublicPolicy, CheckEBSUnencrypted, CheckRDSUnencrypted, CheckEFSUnencrypted, CheckIAMWildcard, CheckSecurityGroupAdmin, CheckNSGAdmin, CheckStoragePublicAccess, CheckStorageContainer, CheckStorageHTTPSOnly, CheckStorageMinTLS, CheckDatabaseFirewallOpen, }
CheckIDs lists every check, for validating -skip-checks.
var ErrNoCommenter = errors.New("this run has no pull request to comment on")
ErrNoCommenter means that the current run can't comment, for example outside a pull request.
Functions ¶
func DetectPlatformFromRemote ¶
DetectPlatformFromRemote guesses the platform from a Git remote URL, or returns an empty string.
func Marker ¶
Marker identifies this directory and workspace's comment on a pull request. A Markdown link reference doesn't render, and unlike an HTML comment that works on every platform.
func NewPublisher ¶
func NewPublisher(serverURL, token string, client *http.Client) (func(context.Context, *Report) error, error)
NewPublisher returns a function that sends reports to a Bogform server. It refuses to send the token over an unencrypted connection, except to this machine.
func NextSteps ¶
func NextSteps(platform string, opts ScaffoldOptions) string
NextSteps explains what to do after generating a pipeline file for a platform.
func RenderBogformMD ¶
func RenderBogformMD(opts ScaffoldOptions) (string, error)
RenderBogformMD returns BOGFORM.md with its last section filled in from the pipeline's options. Details that the options don't include are left blank for the user.
func RenderPipeline ¶
func RenderPipeline(opts ScaffoldOptions) ([]byte, error)
RenderPipeline returns the contents of a pipeline file.
func Scaffold ¶
func Scaffold(opts ScaffoldOptions) (string, error)
Scaffold writes a pipeline file and returns its path.
func WriteBogformMD ¶
func WriteBogformMD(opts ScaffoldOptions) (bool, error)
WriteBogformMD writes BOGFORM.md at the repository root. It returns false, without an error, if the file exists and opts.Force isn't set: the file is usually edited after it's written.
Types ¶
type Change ¶
Change is one resource that a plan would change. It holds no attribute values, so reports are safe to store and display even for resources with sensitive data.
type Commenter ¶
type Commenter interface {
// Upsert updates the comment containing marker, or creates one if there isn't any.
Upsert(ctx context.Context, marker, body string) error
// MaxBody is the longest comment that the platform accepts, in bytes.
MaxBody() int
// Collapsible reports whether the platform renders HTML <details> blocks.
Collapsible() bool
}
Commenter creates or updates a single comment on the current pull request.
type Context ¶
type Context struct {
Platform string `json:"platform"`
Repository string `json:"repository,omitempty"`
Branch string `json:"branch,omitempty"`
Commit string `json:"commit,omitempty"`
RunURL string `json:"run_url,omitempty"`
IsPullRequest bool `json:"is_pull_request"`
PullRequest string `json:"pull_request,omitempty"`
FromFork bool `json:"from_fork,omitempty"`
// Actor is who started the run, and Trigger is the platform's name for why it ran, such as push.
Actor string `json:"actor,omitempty"`
Trigger string `json:"trigger,omitempty"`
// contains filtered or unexported fields
}
Context describes the pipeline run that Bogform is executing in.
type Counts ¶
type Counts struct {
Add int `json:"add"`
Change int `json:"change"`
Destroy int `json:"destroy"`
Replace int `json:"replace"`
Import int `json:"import"`
Read int `json:"read,omitempty"`
}
Counts summarizes the changes in a plan.
func (Counts) Destructive ¶
Destructive is the number of resources that would be destroyed or replaced.
type Finding ¶
type Finding struct {
Check string `json:"check"`
Address string `json:"address"`
Message string `json:"message"`
}
Finding is a check that a planned resource doesn't pass. It names the resource and the problem, but never includes attribute values.
type Options ¶
type Options struct {
Binary string // The bogform executable to run.
Version string
Directory string // The configuration directory, for display.
Workspace string
Vars []string
VarFiles []string
Args []string // More arguments for the plan command.
AllowPullRequestApply bool
NoComment bool
NoOutput bool
// AllowDestroy lets an apply destroy or replace resources. Without it, such a plan is refused
// unless BOG_CI_ALLOW_DESTROY is true, or on GitHub, the merged pull request has the
// allow-destroy label.
AllowDestroy bool
// NoChecks turns the built-in checks off, and SkipChecks turns off the checks it lists.
NoChecks bool
SkipChecks []string
// Cost adds a monthly cost estimate to plans.
Cost bool
Env []string // Environment of this process, as os.Environ returns it.
Stdout io.Writer
Stderr io.Writer
HTTPClient *http.Client
// Publisher, if set, receives the report when a run finishes.
Publisher func(context.Context, *Report) error
Now func() time.Time
}
Options configures a run.
type RenderOptions ¶
type RenderOptions struct {
// MaxBytes limits the size of the result. Zero means no limit.
MaxBytes int
// Collapsible puts long sections inside HTML <details> blocks.
Collapsible bool
// HideOutput leaves out the text of the plan.
HideOutput bool
}
RenderOptions controls how a report is rendered as Markdown.
type Report ¶
type Report struct {
FormatVersion int `json:"format_version"`
Kind string `json:"kind"`
Status string `json:"status"`
BogformVersion string `json:"bogform_version"`
Directory string `json:"directory"`
Workspace string `json:"workspace"`
Run Context `json:"run"`
Counts Counts `json:"counts"`
Changes []Change `json:"changes"`
OutputsChanged []string `json:"outputs_changed,omitempty"`
// Checks are the built-in checks that planned resources don't pass. They're warnings.
Checks []Finding `json:"checks,omitempty"`
// Cost is the monthly cost estimate, when one was asked for.
Cost *cost.Estimate `json:"cost,omitempty"`
// DestroyAllowedBy says what allowed an apply to destroy or replace resources.
DestroyAllowedBy string `json:"destroy_allowed_by,omitempty"`
Error string `json:"error,omitempty"`
StartedAt time.Time `json:"started_at"`
FinishedAt time.Time `json:"finished_at"`
// Output is the text of the plan or the failure. Bogform redacts sensitive values in it, but a
// provider could still print something private, so it's left out of report.json.
Output string `json:"-"`
}
Report is the record of a pipeline run, written to report.json.
func (*Report) Render ¶
func (r *Report) Render(opts RenderOptions) string
Render formats the report as Markdown.
type Runner ¶
Runner carries out "ci" commands by running the bogform executable, exactly as a person would.
type ScaffoldOptions ¶
type ScaffoldOptions struct {
Platform string
Version string // The version of Bogform to pin, or empty to use the latest release.
DefaultBranch string
Directory string
Environment string
// ApplyOn is "manual" to apply only when someone starts the pipeline, or "merge" to apply every
// change merged to the default branch.
ApplyOn string
// GitState sets up the pipeline for the git state backend.
GitState bool
// Cost adds a monthly cost estimate to plans.
Cost bool
Force bool
Root string // The repository root.
// Cloud sign-in. Empty values leave a comment showing where to sign in instead.
AWSPlanRole string // The role that plans assume.
AWSApplyRole string // The role that applies assume.
AWSRegion string
// AzureClientID is the identity for both plans and applies, unless the plan or apply one is set.
AzureClientID string
AzurePlanClientID string
AzureApplyClientID string
AzureTenantID string
AzureSubscriptionID string
AzureServiceConnection string // For Azure Pipelines, which signs in with a service connection.
}
ScaffoldOptions configures a generated pipeline file.