diff --git a/io.go b/io.go index 51bbc8f..0bb32e3 100644 --- a/io.go +++ b/io.go @@ -31,6 +31,10 @@ func parseRegistriesFromDir(dir string) []Registry { } + // Skip registries without auth strategy, as they are considered public + if registry.AuthStrategy == "" { + return nil + } if mapRegistriesByHost[registry.RegistryHost] { panic(fmt.Sprintf("Duplicated registry %s", registry.RegistryHost)) diff --git a/types.go b/types.go index f620cba..9f6061f 100644 --- a/types.go +++ b/types.go @@ -1,10 +1,88 @@ package main +import "encoding/json" + type Registry struct { Name string `yaml:"name" json:"name"` - RegistryHost string `yaml:"registry" json:"registry"` - AuthStrategy string `yaml:"auth_strategy" json:"auth_strategy"` + RegistryHost string `yaml:"url,omitempty" json:"registry,omitempty"` + AuthStrategy string `yaml:"auth_strategy" json:"auth_strategy,omitempty"` Default bool `yaml:"default" json:"default"` ImageTypes []string `yaml:"image_types" json:"image_types"` BasePaths map[string]string `yaml:"base_paths" json:"base_paths"` } + +// Custom YAML unmarshaling to support both 'registry' and 'url' mapping to RegistryHost +func (r *Registry) UnmarshalYAML(unmarshal func(interface{}) error) error { + var aux map[string]interface{} + if err := unmarshal(&aux); err != nil { + return err + } + if name, ok := aux["name"].(string); ok { + r.Name = name + } + if reg, ok := aux["registry"].(string); ok { + r.RegistryHost = reg + } else if url, ok := aux["url"].(string); ok { + r.RegistryHost = url + } + if auth, ok := aux["auth_strategy"].(string); ok { + r.AuthStrategy = auth + } + if def, ok := aux["default"].(bool); ok { + r.Default = def + } + if img, ok := aux["image_types"].([]interface{}); ok { + r.ImageTypes = make([]string, len(img)) + for i, v := range img { + if s, ok := v.(string); ok { + r.ImageTypes[i] = s + } + } + } + if bp, ok := aux["base_paths"].(map[string]interface{}); ok { + r.BasePaths = make(map[string]string) + for k, v := range bp { + if s, ok := v.(string); ok { + r.BasePaths[k] = s + } + } + } + return nil +} + +// Custom JSON unmarshaling to support both 'registry' and 'url' mapping to Url +func (r *Registry) UnmarshalJSON(data []byte) error { + var aux map[string]interface{} + if err := json.Unmarshal(data, &aux); err != nil { + return err + } + if name, ok := aux["name"].(string); ok { + r.Name = name + } + if reg, ok := aux["registry"].(string); ok { + r.RegistryHost = reg + } + if auth, ok := aux["auth_strategy"].(string); ok { + r.AuthStrategy = auth + } + if def, ok := aux["default"].(bool); ok { + r.Default = def + } + if img, ok := aux["image_types"].([]interface{}); ok { + r.ImageTypes = make([]string, len(img)) + for i, v := range img { + if s, ok := v.(string); ok { + r.ImageTypes[i] = s + } + } + } + if bp, ok := aux["base_paths"].(map[string]interface{}); ok { + r.BasePaths = make(map[string]string) + for k, v := range bp { + if s, ok := v.(string); ok { + r.BasePaths[k] = s + } + } + } + return nil +} diff --git a/validations.go b/validations.go index 11fe5ba..1dd501c 100644 --- a/validations.go +++ b/validations.go @@ -14,17 +14,17 @@ const SCHEMA = `{ "properties": { "name": { "type": "string" }, "registry": { "type": "string" }, + "url": { "type": "string" }, "image_types": { - "type": "array", + "type": ["array", "null"], "items": { "type": "string", "enum": ["snapshots", "releases"] } }, - "default": { "type": "boolean" }, "auth_strategy": { "type": "string", "enum": ["aws_oidc", "azure_oidc", "generic", "ghcr", "dockerhub"] }, "base_paths": { - "type": "object", + "type": ["object", "null"], "properties": { "services": { "type": "string" }, "charts": { "type": "string" } @@ -32,7 +32,7 @@ const SCHEMA = `{ "required": ["services", "charts"] } }, - "required": ["name", "registry", "image_types", "default", "auth_strategy", "base_paths"] + "required": ["name", "registry"] }` func validate() {