Skip to content
Draft
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,13 @@ by a private CA, on top of the system ones. It is checked on every run, and
never read for an archive. `--insecure-skip-tls-verify` accepts any
certificate instead, for a test cluster only, and prints a warning.
`--plain-http` reaches a registry that serves plain HTTP, and prints a warning
too. It excludes the two certificate flags.
too. It applies to the registry of the source only, or to the mirror when
`--mirror` is given, and excludes the two certificate flags. Without it, the registry is reached over HTTPS only, even
on a private or loopback IP address. The agent takes a list instead, `--plain-http-registry`,
see [agent/README.md](agent/README.md).
`--mirror=<host[:port]>` pulls the source from that registry first, with the
source's registry in `?ns=`, and falls back on the registry the source names.
The host has no cluster DNS: give the mirror as an IP.

The import refuses to replace a directory without a sentinel. The command runs
as root, so a wrong name or cache path that lands on a directory the store did
Expand Down
62 changes: 52 additions & 10 deletions agent/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,56 @@ Pulling and extraction use
unusable one stops the agent at startup rather than failing every pull.
`--insecure-skip-tls-verify` turns verification off for test clusters. It
excludes `--ca-file`, so neither setting wins in silence.
- On its own, the library uses plain HTTP only for a loopback or private IP
address, or a `localhost` host name.
`--plain-http` marks every registry insecure (`name.Insecure`). The client
then tries HTTPS first, and also HTTP when HTTPS fails or takes more than
300ms: the first answer wins, on every pull. So the flag also weakens a
registry that serves HTTPS: a certificate error no longer fails the pull,
and anyone on the path can answer the HTTP attempt (port 80 when the
reference names no port). It is for a registry without TLS, and excludes
both certificate flags rather than mixing the two modes.
- On its own, the library also tries plain HTTP for a loopback or private IP
address, or a `localhost` host name, and nothing turns that off. The agent
hands it a transport that refuses any plain HTTP request to a registry that
is not listed, before the request leaves, so only the HTTPS attempt is left
to answer and a bad certificate fails the pull again. Without it, a registry
on a private address, the usual case on premises, would be reached over
plain HTTP whatever the list says. `imagecachectl import` goes through the
same transport: without `--plain-http` it refuses plain HTTP to such an
address too.
`--plain-http-registry` lists registries, as `host[:port]`, to mark
insecure (`name.Insecure`). For those, the client tries HTTPS first, and
also HTTP when HTTPS fails or takes more than 300ms: the first answer wins,
on every pull. So marking a registry also weakens it if it serves HTTPS: a
certificate error no longer fails the pull, and anyone on the path can
answer the HTTP attempt (port 80 when the reference names no port). That
is why the setting is a list and not a switch: a single global flag would
weaken every HTTPS registry the resources name to serve one plain HTTP
registry. Docker's `insecure-registries` and containerd's `hosts.toml`
scope it the same way.
An entry matches the registry of a parsed reference
(`Context().RegistryStr()`) exactly, after the same normalization, so
`docker.io` matches `index.docker.io`. There is no matching by resolved
address: a registry listed by IP and referenced by name, or listed without
its port, does not match. The list goes with `--ca-file`, which covers the
registries that serve TLS, and excludes `--insecure-skip-tls-verify`, meant
for test clusters only. `imagecachectl import` keeps a `--plain-http`
switch: it pulls one source per run, so the switch already covers that
source's registry alone.
- The library has no mirror support. `--mirror <host[:port]>` adds one global
mirror, tried before the registry each source names, as containerd does
with `hosts.toml`. The source keeps its real name (`ghcr.io/scality/...`)
and the puller rewrites it to `<mirror>/<repository>:<tag>` (or
`@<digest>`). Each request to the mirror carries `ns=<registry>`, the query
parameter containerd sends, which static-oci-registry prepends to the
repository. Docker Hub goes as `ns=docker.io`. The rewritten reference is
a registry like any other: `--ca-file` covers it, and it is reached over
plain HTTP only if `--plain-http-registry` lists it. Rewriting the reference
rather than the host of each request lets the library pick the scheme and
name the mirror in its errors.
The choice is made when the manifest and the configuration are resolved.
If the mirror fails there, the registry is tried, and when both fail the
error carries both causes. The layers then stream from whichever answered:
a mirror lost during the stream fails the pull, and the next pass resolves
again. The sentinel records the source as the resource gives it, never the
mirror reference. A source that already names the mirror is pulled once,
with no `ns`. One mirror per registry is out of scope.
`imagecachectl import --mirror` does the same. Its `--plain-http` then goes
to the mirror, not to the source's registry: on a node, the mirror is the
local registry, and the source names a real one that keeps HTTPS. It runs
on the host with no cluster DNS, so the mirror is given as a node IP.

The cache images are regular container images (they must remain importable
and mountable with `ctr` by provisioning tooling), so an artifact-oriented
Expand Down Expand Up @@ -452,7 +493,8 @@ The agent never writes ImageCache resources (no status, no finalizers).
nobody listens on leaves the node `pending`. Two registries run in the
cluster: a pull over plain HTTP lands on the selected node only, and a
registry signed by a private CA is refused until the shipped CA patch is
applied. An archive rewritten on the node with other content of the same
applied. With `--mirror`, a source the registry does not hold is pulled
from the mirror, then from the registry once the mirror is stopped. An archive rewritten on the node with other content of the same
size is extracted again. `imagecachectl` seeds two directories on the
worker: the agent adopts the one holding the image its resource names, with
no extraction, and replaces the other.
28 changes: 25 additions & 3 deletions agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,31 @@ On a test cluster set up by hand, `--insecure-skip-tls-verify` accepts any
registry certificate instead. The agent logs a warning at startup. It excludes
`--ca-file`, and no manifest here sets it: never use it on a real node.

A registry that serves plain HTTP needs `--plain-http`. The agent logs a
warning at startup. It excludes `--ca-file` and `--insecure-skip-tls-verify`,
and no manifest here sets it.
A registry that serves plain HTTP needs `--plain-http-registry=<host[:port]>`,
repeated for each one. Only the listed registries may be reached over plain
HTTP: every other one is reached over HTTPS only, and still fails on a bad
certificate. That includes a registry on a private or loopback IP address or
a `localhost` name, which the pull library would otherwise also try over plain
HTTP: list it if it serves plain HTTP. Write each entry as the `ImageCache` sources name the registry:

- by the same host, an IP if they use an IP and a name if they use a name;
- with the port they carry, and with no port if they carry none;
- with no scheme and no path: `10.0.0.1:5000`, not `http://10.0.0.1:5000`.

The match is exact, so an entry written another way does not apply and the
pull fails on `HTTP response to HTTPS client`. The agent logs the list with a
warning at startup. It goes with `--ca-file`, which covers the registries that
serve TLS, but not with `--insecure-skip-tls-verify`. No manifest here sets
it.

`--mirror=<host[:port]>` names a registry every source is pulled from first, as
a containerd mirror is. The `ImageCache` sources keep the real image name,
such as `ghcr.io/scality/x:1.0`, and the agent asks the mirror for
`scality/x:1.0` with `?ns=ghcr.io`. When the mirror fails, the agent pulls
from the registry the source names, and when both fail the error gives both
reasons. The mirror takes `--ca-file` like any registry, and needs to be
listed with `--plain-http-registry` if it serves plain HTTP. See "Image
pulling" in [DESIGN.md](DESIGN.md).

Leader election is deliberately absent. Every agent converges the node it runs
on, so there is nothing to elect a leader for.
Expand Down
45 changes: 37 additions & 8 deletions agent/cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"flag"
"os"
"path/filepath"
"strings"
"time"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
Expand Down Expand Up @@ -59,6 +60,18 @@ func init() {
// +kubebuilder:scaffold:scheme
}

// stringList is a flag that can be repeated, each occurrence adding a value.
type stringList []string

// String implements flag.Value.
func (l *stringList) String() string { return strings.Join(*l, ",") }

// Set implements flag.Value.
func (l *stringList) Set(v string) error {
*l = append(*l, v)
return nil
}

// nolint:gocyclo
func main() {
var metricsAddr string
Expand All @@ -70,7 +83,9 @@ func main() {
var resyncPeriod time.Duration
var cachePath string
var caFile string
var skipVerify, plainHTTP bool
var skipVerify bool
var plainHTTPRegistries stringList
var mirror string
var tlsOpts []func(*tls.Config)
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
Expand Down Expand Up @@ -100,9 +115,14 @@ func main() {
"Read once at startup: restart the agent after rotating it.")
flag.BoolVar(&skipVerify, "insecure-skip-tls-verify", false,
"Accept any registry certificate. For a test cluster set up by hand only.")
flag.BoolVar(&plainHTTP, "plain-http", false,
"Allow plain HTTP to every registry. For a registry without TLS. "+
"Excludes --ca-file and --insecure-skip-tls-verify.")
flag.Var(&plainHTTPRegistries, "plain-http-registry",
"A registry, as host[:port], the agent may reach over plain HTTP. Repeat it for several. "+
"It must be written as the sources name it: by IP or by name, with the port they carry. "+
"Any other registry is reached over HTTPS only. Excludes --insecure-skip-tls-verify.")
flag.StringVar(&mirror, "mirror", "",
"A registry, as host[:port], every source is pulled from first. The registry the source "+
"names is tried next, when the mirror fails. Reached over plain HTTP only if it is "+
"listed with --plain-http-registry.")
opts := zap.Options{
Development: true,
}
Expand Down Expand Up @@ -210,16 +230,25 @@ func main() {

// Checked before anything starts: an unusable CA would otherwise surface
// as a failed pull on every node and every resource.
remote, err := puller.NewRemote(puller.TLS{CAFile: caFile, SkipVerify: skipVerify, PlainHTTP: plainHTTP})
remote, err := puller.NewRemote(puller.TLS{
CAFile: caFile, SkipVerify: skipVerify, PlainHTTPRegistries: plainHTTPRegistries, Mirror: mirror,
})
if err != nil {
setupLog.Error(err, "unable to set up registry TLS")
setupLog.Error(err, "unable to set up the registry access")
os.Exit(1)
}
if skipVerify {
setupLog.Info("WARNING: registry certificates are not verified (--insecure-skip-tls-verify)")
}
if plainHTTP {
setupLog.Info("WARNING: registries may be reached over plain HTTP (--plain-http)")
if len(plainHTTPRegistries) > 0 {
// Logged in full: an entry that does not match a source exactly
// shows up as "HTTP response to HTTPS client" on the pull.
setupLog.Info("WARNING: these registries may be reached over plain HTTP (--plain-http-registry)",
"registries", []string(plainHTTPRegistries))
}

if mirror != "" {
setupLog.Info("sources are pulled from the mirror first", "mirror", mirror)
}

fw, err := controller.NewFSWatcher(nodeName)
Expand Down
36 changes: 27 additions & 9 deletions agent/internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"strings"

imgname "github.com/google/go-containerregistry/pkg/name"
"github.com/scality/go-errors"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -51,10 +52,10 @@ const importExample = ` imagecachectl import --name worker-1-0-0 registry.examp
// Run executes the command line and returns the process exit code: 0 on
// success, ExitInterrupted when the context was cancelled, 1 otherwise.
func Run(ctx context.Context, args []string, out, errOut io.Writer) int {
var name, cachePath, caFile string
var name, cachePath, caFile, mirror string
var skipVerify, plainHTTP bool
importCmd := &cobra.Command{
Use: "import --name <resource> [--cache-path <dir>] [--ca-file <file> | --insecure-skip-tls-verify | --plain-http] <source>",
Use: "import --name <resource> [--cache-path <dir>] [--mirror <host[:port]>] [--ca-file <file> | --insecure-skip-tls-verify | --plain-http] <source>",
Short: "Fill the image cache from a registry or a docker archive",
Long: importLong,
Example: importExample,
Expand All @@ -80,7 +81,7 @@ func Run(ctx context.Context, args []string, out, errOut io.Writer) int {
return errors.New("--insecure-skip-tls-verify and --plain-http exclude each other")
}
err := do(cmd.Context(), cachePath, name, args[0],
puller.TLS{CAFile: caFile, SkipVerify: skipVerify, PlainHTTP: plainHTTP}, out, errOut)
puller.TLS{CAFile: caFile, SkipVerify: skipVerify, Mirror: mirror}, plainHTTP, out, errOut)
// An interrupted run is not a failure to diagnose: the extraction
// publishes by rename, so nothing half written is left behind.
if errors.Is(err, context.Canceled) {
Expand All @@ -97,7 +98,9 @@ func Run(ctx context.Context, args []string, out, errOut io.Writer) int {
importCmd.Flags().BoolVar(&skipVerify, "insecure-skip-tls-verify", false,
"accept any registry certificate, for a test cluster set up by hand only")
importCmd.Flags().BoolVar(&plainHTTP, "plain-http", false,
"allow plain HTTP to the registry, for a registry without TLS")
"allow plain HTTP to the registry, for a registry without TLS: to the mirror when --mirror is given")
importCmd.Flags().StringVar(&mirror, "mirror", "",
"registry, as `host[:port]`, to pull the source from first, before the registry it names")
_ = importCmd.MarkFlagRequired("name")

root := &cobra.Command{
Expand Down Expand Up @@ -145,21 +148,22 @@ func validate(name, cachePath string) error {
return nil
}

func do(ctx context.Context, cachePath, name, source string, tls puller.TLS, out, errOut io.Writer) error {
func do(ctx context.Context, cachePath, name, source string, tls puller.TLS, plainHTTP bool,
out, errOut io.Writer) error {
store := cache.Store{}

// Before the cache state, so that a CA that cannot be used is reported
// on every run, including one that would have found the resource
// complete, rather than only on the run that finally needs the registry.
src, err := pullerFor(source, tls)
src, err := pullerFor(source, tls, plainHTTP)
if err != nil {
return err
}
if _, remote := src.(puller.Remote); remote {
if tls.SkipVerify {
printf(errOut, "imagecachectl: warning: the registry certificate is not verified\n")
}
if tls.PlainHTTP {
if plainHTTP {
printf(errOut, "imagecachectl: warning: the registry may be reached over plain HTTP\n")
}
}
Expand Down Expand Up @@ -216,14 +220,28 @@ func do(ctx context.Context, cachePath, name, source string, tls puller.TLS, out
// file rather than coming back with a complaint about a reference.
//
// The TLS and plain HTTP settings only matter to a registry, so an archive
// never reads them.
func pullerFor(source string, tls puller.TLS) (puller.Puller, error) {
// never reads them. The command pulls one source per run, so plain HTTP is
// allowed to one registry: the puller takes a list, and it is the only entry.
// With a mirror, that registry is the mirror: on a node, the mirror is the
// local registry without TLS, and the registry the source names is a real
// one that keeps HTTPS.
func pullerFor(source string, tls puller.TLS, plainHTTP bool) (puller.Puller, error) {
switch {
case strings.HasPrefix(source, string(os.PathSeparator)),
strings.HasPrefix(source, "."),
strings.HasSuffix(source, ".tar"):
return puller.Tarball{}, nil
default:
// A reference that does not parse is reported by the pull, which
// names it as one.
switch ref, err := imgname.ParseReference(source); {
case !plainHTTP:
// Every registry stays HTTPS only.
case tls.Mirror != "":
tls.PlainHTTPRegistries = []string{tls.Mirror}
case err == nil:
tls.PlainHTTPRegistries = []string{ref.Context().RegistryStr()}
}
return puller.NewRemote(tls)
}
}
Loading
Loading