160 lines
6.2 KiB
Go
160 lines
6.2 KiB
Go
package layout
|
|
|
|
// Per-Dockerfile ignore files (`<Dockerfile>.dockerignore`).
|
|
//
|
|
// IntegraCI commits a generated Dockerfile at `.integraci/Dockerfile` with an
|
|
// `.integraci/Dockerfile.dockerignore` beside it. That sidecar is the only thing
|
|
// between the generated `COPY . .` and a `.env` or a private key committed to
|
|
// the repository: whatever the build context holds is readable by every layer,
|
|
// and a secret copied in is a secret published with the image.
|
|
//
|
|
// Two modes (ContextFilters), chosen by WHOSE Dockerfile is built:
|
|
//
|
|
// The generated `.integraci/Dockerfile` (IsGeneratedDockerfile) -- STRICT. The
|
|
// module applies the sidecar ITSELF, before DockerBuild, instead of trusting
|
|
// the engine's Dockerfile frontend to find it, and applies the context's root
|
|
// `.dockerignore` as well (BuildKit ignores the root file whenever a
|
|
// per-Dockerfile one exists, so the sidecar IntegraCI wrote would otherwise
|
|
// silently re-include everything the customer had excluded). Two filters, not
|
|
// one merged list:
|
|
//
|
|
// 1. the root `.dockerignore`, re-includes (`!`) kept, so an allowlist such
|
|
// as `*` then `!src` keeps working;
|
|
// 2. the sidecar's exclusions, its `!` lines DROPPED, applied to what is left.
|
|
//
|
|
// A root `!.env` therefore cannot bring back a `.env` the sidecar excludes,
|
|
// whatever order the engine's matcher gives patterns inside one list.
|
|
//
|
|
// A customer's own Dockerfile -- BuildKit semantics, unchanged: its
|
|
// `<Dockerfile>.dockerignore` REPLACES the root file, and its `!` lines are
|
|
// kept. A customer sidecar written as an allowlist (`*`, `!src`,
|
|
// `!package.json`) is the ordinary BuildKit idiom; dropping its re-includes
|
|
// would empty the build context.
|
|
//
|
|
// Pure functions, no Dagger import, for the same reason as the rest of this
|
|
// package: the rules are unit tested without an engine.
|
|
|
|
import (
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
// SidecarSuffix is appended to a Dockerfile's path to name its ignore file.
|
|
const SidecarSuffix = ".dockerignore"
|
|
|
|
// RootIgnoreFile is the build context's own ignore file.
|
|
const RootIgnoreFile = ".dockerignore"
|
|
|
|
// GeneratedDockerfile is where IntegraCI commits a Dockerfile it generated,
|
|
// relative to the service's directory (containerization.GENERATED_DOCKERFILE_PATH).
|
|
const GeneratedDockerfile = ".integraci/Dockerfile"
|
|
|
|
// IsGeneratedDockerfile reports whether dockerfile (a path inside the build
|
|
// context) is the one IntegraCI generates: `.integraci/Dockerfile` at the
|
|
// context root, or under the service's directory when the whole checkout is
|
|
// the context.
|
|
func IsGeneratedDockerfile(dockerfile string) bool {
|
|
return dockerfile == GeneratedDockerfile || strings.HasSuffix(dockerfile, "/"+GeneratedDockerfile)
|
|
}
|
|
|
|
// SidecarPath is the ignore file that belongs to dockerfile (a path inside the
|
|
// build context). An empty dockerfile is the default `Dockerfile`.
|
|
func SidecarPath(dockerfile string) string {
|
|
if dockerfile == "" {
|
|
dockerfile = DefaultDockerfile
|
|
}
|
|
return dockerfile + SidecarSuffix
|
|
}
|
|
|
|
// IgnorePatterns reads a .dockerignore body into exclude patterns: one per
|
|
// non-empty, non-comment line, cleaned the way Docker's own reader cleans them
|
|
// (surrounding whitespace trimmed, a leading `/` dropped, path.Clean applied),
|
|
// so `/secrets` matches here too. `!` re-include lines are kept only when
|
|
// keepNegations is set; see ContextFilters for why the sidecar drops them.
|
|
func IgnorePatterns(body string, keepNegations bool) []string {
|
|
var out []string
|
|
for _, line := range strings.Split(body, "\n") {
|
|
line = strings.TrimSpace(line)
|
|
if line == "" || strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
negate := strings.HasPrefix(line, "!")
|
|
if negate && !keepNegations {
|
|
continue
|
|
}
|
|
pattern := strings.TrimSpace(strings.TrimPrefix(line, "!"))
|
|
pattern = strings.TrimPrefix(pattern, "/")
|
|
if pattern == "" {
|
|
continue
|
|
}
|
|
pattern = path.Clean(pattern)
|
|
if pattern == "." {
|
|
continue
|
|
}
|
|
if negate {
|
|
pattern = "!" + pattern
|
|
}
|
|
out = append(out, pattern)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// ContextFilters is the ordered list of exclude-pattern lists the module
|
|
// applies to the build context, one Directory.Filter each.
|
|
//
|
|
// generated: the root `.dockerignore` (with its re-includes) and then the
|
|
// sidecar's exclusions (without any). Otherwise BuildKit's rule: the sidecar
|
|
// alone, re-includes kept, the root file not applied. An empty list is
|
|
// omitted. hasRoot is false when the context has no root `.dockerignore`.
|
|
func ContextFilters(rootBody string, hasRoot bool, sidecarBody string, generated bool) [][]string {
|
|
var out [][]string
|
|
if !generated {
|
|
if sidecar := IgnorePatterns(sidecarBody, true); len(sidecar) > 0 {
|
|
out = append(out, sidecar)
|
|
}
|
|
return out
|
|
}
|
|
if hasRoot {
|
|
if root := IgnorePatterns(rootBody, true); len(root) > 0 {
|
|
out = append(out, root)
|
|
}
|
|
}
|
|
if sidecar := IgnorePatterns(sidecarBody, false); len(sidecar) > 0 {
|
|
out = append(out, sidecar)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// GeneratedSidecarPresent refuses a GENERATED Dockerfile built without its
|
|
// sidecar. The platform always commits the pair, and the sidecar is the only
|
|
// thing between the generated `COPY . .` and `.env`, keys and `.git`: building
|
|
// without it would put them in the image behind a green build. Whatever the
|
|
// cause (deleted in the repository, never committed), the build stops and
|
|
// says why; onboarding again restores the pair. A customer's own Dockerfile
|
|
// is not asked this question.
|
|
func GeneratedSidecarPresent(sidecar string, kind Kind) error {
|
|
if kind == Missing {
|
|
return refuse("the generated Dockerfile's ignore file %q is missing; onboard the service again to restore it", sidecar)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// IgnoreFileUsable says whether an ignore file of this kind is read.
|
|
//
|
|
// Missing is "no ignore file" (false, nil). A regular file is read. Anything
|
|
// else is refused rather than skipped: a sidecar replaced by a symlink (to
|
|
// `/dev/null`, or to a file outside the checkout) or by a directory would
|
|
// otherwise switch the exclusions off and put `.env` back in the image behind
|
|
// a green build. The same holds for the root file once a sidecar is in play,
|
|
// because the root rules are applied with it.
|
|
func IgnoreFileUsable(name string, kind Kind) (bool, error) {
|
|
switch kind {
|
|
case Missing:
|
|
return false, nil
|
|
case Regular:
|
|
return true, nil
|
|
default:
|
|
return false, refuse("the ignore file %q is a %s, not a regular file", name, kind)
|
|
}
|
|
}
|