feat(catalogue,helm,ci): add private OCI auth, offline catalogue mirror, CI image/SBOM, metrics config, and OpenShift overlay (#2153)
This commit is contained in:
23 files changed
+1095
-12
No files matched your search
@@ -0,0 +1,145 @@
|
||||
package archie
|
||||
|
||||
import (
|
||||
"context"
|
||||
json "encoding/json/v2"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/modules/connectors"
|
||||
)
|
||||
|
||||
// RunCatalogueCLI dispatches `archie catalogue <keygen|mirror>`.
|
||||
func RunCatalogueCLI(ctx context.Context, args []string, stdout io.Writer) error {
|
||||
if stdout == nil {
|
||||
stdout = os.Stdout
|
||||
}
|
||||
if len(args) == 0 {
|
||||
return errors.New("usage: archie catalogue <keygen|mirror> [flags]")
|
||||
}
|
||||
switch args[0] {
|
||||
case "keygen":
|
||||
return runCatalogueKeygen(args[1:], stdout)
|
||||
case "mirror":
|
||||
return runCatalogueMirror(ctx, args[1:], stdout)
|
||||
default:
|
||||
return fmt.Errorf("unknown catalogue subcommand %q", args[0])
|
||||
}
|
||||
}
|
||||
|
||||
func runCatalogueKeygen(args []string, stdout io.Writer) error {
|
||||
fs := flag.NewFlagSet("catalogue keygen", flag.ContinueOnError)
|
||||
asJSON := fs.Bool("json", false, "emit output as JSON")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return err
|
||||
}
|
||||
priv, pub, err := connectors.GenerateCatalogueKeypair()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if *asJSON {
|
||||
out, err := json.Marshal(struct {
|
||||
PrivateKey string `json:"private_key"`
|
||||
PublicKey string `json:"public_key"`
|
||||
}{PrivateKey: priv, PublicKey: pub})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = fmt.Fprintln(stdout, string(out))
|
||||
return err
|
||||
}
|
||||
_, err = fmt.Fprintf(stdout, "private_key: %s\npublic_key: %s\n", priv, pub)
|
||||
return err
|
||||
}
|
||||
|
||||
func runCatalogueMirror(ctx context.Context, args []string, stdout io.Writer) error {
|
||||
defaultLoc, defaultKey := connectors.DefaultCatalogueSource()
|
||||
fs := flag.NewFlagSet("catalogue mirror", flag.ContinueOnError)
|
||||
from := fs.String("from", defaultLoc, "upstream OCI repository, HTTPS URL, or local path")
|
||||
pubKey := fs.String("public-key", defaultKey, "base64 Ed25519 public key for upstream verification")
|
||||
dest := fs.String("to", "", "destination directory for the offline mirror")
|
||||
signKeyFile := fs.String("sign-key-file", "", "path to base64 Ed25519 private signing key (or ARCHIE_CATALOGUE_SIGN_KEY_FILE)")
|
||||
authFile := fs.String("auth-file", "", "path to Docker config.json for private OCI auth (or ARCHIE_CATALOGUE_AUTH_FILE)")
|
||||
username := fs.String("username", "", "OCI registry username (or ARCHIE_CATALOGUE_USERNAME)")
|
||||
passwordFile := fs.String("password-file", "", "path to OCI registry password (or ARCHIE_CATALOGUE_PASSWORD_FILE)")
|
||||
asJSON := fs.Bool("json", false, "emit output as JSON")
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.TrimSpace(*dest) == "" {
|
||||
return errors.New("-to destination directory is required")
|
||||
}
|
||||
|
||||
signKey, _, err := envOrFile("ARCHIE_CATALOGUE_SIGN_KEY", "ARCHIE_CATALOGUE_SIGN_KEY_FILE")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if *signKeyFile != "" {
|
||||
raw, err := os.ReadFile(*signKeyFile) //nolint:gosec // operator-configured signing key path
|
||||
if err != nil {
|
||||
return fmt.Errorf("read sign key file %s: %w", *signKeyFile, err)
|
||||
}
|
||||
signKey = strings.TrimSpace(string(raw))
|
||||
}
|
||||
if signKey == "" {
|
||||
return errors.New("a signing private key is required (-sign-key-file or ARCHIE_CATALOGUE_SIGN_KEY_FILE)")
|
||||
}
|
||||
|
||||
regAuth, err := loadMirrorAuth(*username, *passwordFile, *authFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res, err := connectors.MirrorCatalogue(ctx, connectors.MirrorOptions{
|
||||
From: *from,
|
||||
VerifyPublicKey: *pubKey,
|
||||
DestDir: *dest,
|
||||
SignPrivateKey: signKey,
|
||||
Auth: regAuth,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if *asJSON {
|
||||
out, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = fmt.Fprintln(stdout, string(out))
|
||||
return err
|
||||
}
|
||||
_, err = fmt.Fprintf(stdout, "mirrored %d extension artifact(s) to %s\n", len(res.Mirrored), res.CataloguePath)
|
||||
return err
|
||||
}
|
||||
|
||||
func loadMirrorAuth(username, passwordFile, authFile string) (connectors.CatalogueAuth, error) {
|
||||
if username == "" {
|
||||
username = strings.TrimSpace(os.Getenv("ARCHIE_CATALOGUE_USERNAME"))
|
||||
}
|
||||
var password string
|
||||
if passwordFile != "" {
|
||||
raw, err := os.ReadFile(passwordFile) //nolint:gosec // operator-configured password file path
|
||||
if err != nil {
|
||||
return connectors.CatalogueAuth{}, fmt.Errorf("read password file %s: %w", passwordFile, err)
|
||||
}
|
||||
password = strings.TrimSpace(string(raw))
|
||||
} else {
|
||||
var err error
|
||||
password, _, err = envOrFile("ARCHIE_CATALOGUE_PASSWORD", "ARCHIE_CATALOGUE_PASSWORD_FILE")
|
||||
if err != nil {
|
||||
return connectors.CatalogueAuth{}, err
|
||||
}
|
||||
}
|
||||
if authFile == "" {
|
||||
authFile = strings.TrimSpace(os.Getenv("ARCHIE_CATALOGUE_AUTH_FILE"))
|
||||
}
|
||||
return connectors.CatalogueAuth{
|
||||
Username: username,
|
||||
Password: password,
|
||||
AuthFile: authFile,
|
||||
}, nil
|
||||
}
|
||||
@@ -58,6 +58,19 @@ type CatalogueConfig struct {
|
||||
Location string `toml:"location"`
|
||||
PublicKey string `toml:"public_key"`
|
||||
DisableAutoUpdate bool `toml:"disable_auto_update"`
|
||||
Username string `toml:"username"`
|
||||
Password string `toml:"password"`
|
||||
AuthFile string `toml:"auth_file"`
|
||||
}
|
||||
|
||||
// Auth returns the OCI registry authentication settings configured for the
|
||||
// extension catalogue.
|
||||
func (c CatalogueConfig) Auth() connectors.CatalogueAuth {
|
||||
return connectors.CatalogueAuth{
|
||||
Username: c.Username,
|
||||
Password: c.Password,
|
||||
AuthFile: c.AuthFile,
|
||||
}
|
||||
}
|
||||
|
||||
// ExtensionsConfig configures remote gRPC+mTLS extension endpoints for
|
||||
@@ -262,6 +275,9 @@ func applyEnv(cfg *Config) error {
|
||||
{envKey: "ARCHIE_SHUTDOWN_GRACE", target: &cfg.ShutdownGraceRaw},
|
||||
{envKey: "ARCHIE_CATALOGUE_LOCATION", target: &cfg.Catalogue.Location},
|
||||
{envKey: "ARCHIE_CATALOGUE_PUBLIC_KEY", target: &cfg.Catalogue.PublicKey},
|
||||
{envKey: "ARCHIE_CATALOGUE_USERNAME", target: &cfg.Catalogue.Username},
|
||||
{envKey: "ARCHIE_CATALOGUE_PASSWORD", fileKey: "ARCHIE_CATALOGUE_PASSWORD_FILE", target: &cfg.Catalogue.Password},
|
||||
{envKey: "ARCHIE_CATALOGUE_AUTH_FILE", target: &cfg.Catalogue.AuthFile},
|
||||
{envKey: "ARCHIE_EXTENSIONS_CA_FILE", target: &cfg.Extensions.CAFile},
|
||||
{envKey: "ARCHIE_EXTENSIONS_CERT_FILE", target: &cfg.Extensions.CertFile},
|
||||
{envKey: "ARCHIE_EXTENSIONS_KEY_FILE", target: &cfg.Extensions.KeyFile},
|
||||
@@ -347,6 +363,9 @@ func (c Config) validate() error {
|
||||
if (c.Catalogue.Location == "") != (c.Catalogue.PublicKey == "") {
|
||||
errs = append(errs, errors.New("catalogue.location and catalogue.public_key must be set together"))
|
||||
}
|
||||
if (c.Catalogue.Username != "") != (c.Catalogue.Password != "") {
|
||||
errs = append(errs, errors.New("catalogue.username and catalogue.password must be set together"))
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
|
||||
@@ -114,6 +114,35 @@ func TestLoadConfigRuntimeAndEnvFiles(t *testing.T) {
|
||||
if _, err := LoadConfig(""); err == nil || !strings.Contains(err.Error(), "not both") {
|
||||
t.Fatalf("expected ambiguity error, got %v", err)
|
||||
}
|
||||
|
||||
t.Run("catalogue auth env and file overrides", func(t *testing.T) {
|
||||
passFile := filepath.Join(dir, "registry_pass")
|
||||
authFile := filepath.Join(dir, "config.json")
|
||||
if err := os.WriteFile(passFile, []byte("oci-secret\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(authFile, []byte(`{"auths":{}}`), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("ARCHIE_DATABASE_URL", "")
|
||||
t.Setenv("ARCHIE_CATALOGUE_USERNAME", "oci-bot")
|
||||
t.Setenv("ARCHIE_CATALOGUE_PASSWORD_FILE", passFile)
|
||||
t.Setenv("ARCHIE_CATALOGUE_AUTH_FILE", authFile)
|
||||
|
||||
got, err := LoadConfig("")
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig with catalogue auth: %v", err)
|
||||
}
|
||||
if got.Catalogue.Username != "oci-bot" || got.Catalogue.Password != "oci-secret" || got.Catalogue.AuthFile != authFile {
|
||||
t.Fatalf("unexpected catalogue auth config: %+v", got.Catalogue)
|
||||
}
|
||||
|
||||
t.Setenv("ARCHIE_CATALOGUE_PASSWORD", "")
|
||||
t.Setenv("ARCHIE_CATALOGUE_PASSWORD_FILE", "")
|
||||
if _, err := LoadConfig(""); err == nil || !strings.Contains(err.Error(), "must be set together") {
|
||||
t.Fatalf("expected paired registry credential error, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestLoadMigrateConfigRequiresOnlyDatabaseRoles guards migration jobs running
|
||||
|
||||
@@ -322,6 +322,11 @@ func configureConnectors(conns *connectors.Module, cfg Config) error {
|
||||
return fmt.Errorf("default catalogue: %w", err)
|
||||
}
|
||||
}
|
||||
if authCfg := cfg.Catalogue.Auth(); !authCfg.Empty() {
|
||||
if err := conns.UseCatalogueAuth(authCfg); err != nil {
|
||||
return fmt.Errorf("catalogue auth: %w", err)
|
||||
}
|
||||
}
|
||||
if cfg.Extensions.RequireRemote || len(cfg.Extensions.Remote) > 0 {
|
||||
if err := conns.UseRemotes(connectors.RemoteConfig{
|
||||
RequireRemote: cfg.Extensions.RequireRemote,
|
||||
|
||||
Reference in new issue
Block a user