mirror of
https://github.com/hanzoai/gateway.git
synced 2026-10-30 15:04:44 +00:00
THE LIVE ESCALATION, in the path that ships. `X-User-IsAdmin` is the fleet's PLATFORM-authority header — cloud gates its cross-tenant surfaces on it and the money gates read the bit-field beside it. This edge minted it from claims.IsAdmin, which is IAM's ORG-ROLE bit: an org owner carries it inside their own org. So every org owner reached the fleet as a platform admin. I reported this fixed earlier. It was not. I patched iamauth.InjectIdentity, which has ZERO non-test callers — the shipped KrakenD middleware and the ZAP relay each carried their own inline mint, and both still read the org bit. Two more statements of the same rule, and the one I fixed was the dead one. Fixed where they run: the HTTP edge (auth_middleware.go) now applies edge.Mint, and the relay (gate.go) sets f.IsAdmin — which forward re-emits as X-User-IsAdmin — from PlatformSudo. X-User-IsOrgAdmin is minted for the first time, so org-scoped self-service has its own header to read instead of borrowing platform authority. ONE DECISION. iamauth is DELETED (834 lines) along with the shim over it. What a claim means, which org a request acts in, who pays, which headers get minted — all of it is hanzoai/authz and hanzoai/authz/edge now, and net/http's own header type satisfies edge.Headers, so applying it here is a loop rather than a second implementation. What is left is the part genuinely this deployment's: package token holds the issuer and the AUDIENCE ALLOWLIST (authz.Verify deliberately does not check `aud`, because IAM sets it to the requesting CLIENT), and identity.go holds the money bit vocabulary and the vendor-prefix strip. ONE JWT LIBRARY on the identity path. Verification is golang-jwt/v5, the library IAM SIGNS with, through authz.Verify + the edge's JWKS reader. go-jose is gone from it, and the tests now mint with the same library they verify with — a test signing with a different one is testing a signer the estate does not run. It also makes `kid` mandatory, matching IAM (signClaims always stamps it). THREE HEADERS DROPPED, because IAM never emitted the claims behind them: `roles`, `permissions` and `phone` are absent from the signed claim set and from claims_supported, so the three-shape parsers for the first two read a claim that never arrived, and X-Roles / X-Phone-Number were always empty. All three names STAY in the strip set, so a forged copy still never survives. Removing the permissions parser also closes a latent grant: a token carrying that claim used to be handed the bits it named. The vendor-prefix backstop (X-IAM-*, X-HANZO-*) stays here rather than in the estate's edge, because sweeping prefixes must ENUMERATE header names — which is transport-specific, while edge.Headers is deliberately Get/Set/Del. It is folded into one stripClaimed call with the exact-name strip, because two calls would be an ordering trap that fails silently. Falsified both ways: restoring `if cl.IsAdmin` in edge.Mint turns TestOrgOwnerIsNotAPlatformAdmin and TestAdminOrgMachineGetsNeitherAdminHeader red; granting the admin bit from the org role turns TestOrgAdminNeverMintsPlatformAuthority and TestPermissions_OrgAdminGetsNoAdminBit red. Green on both build tags, including the `legacy` tag production ships. tests/ stays red on the KrakenD fixture suite — verified identical on a stashed tree, so it is not from this change. Co-authored-by: Hanzo Dev <[email protected]>
128 lines
3.1 KiB
Go
128 lines
3.1 KiB
Go
// Package koanf defines a config parser implementation based on the koanf pkg
|
|
package koanf
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/gateway/v2/internal/lura/config"
|
|
"github.com/knadh/koanf/parsers/json"
|
|
"github.com/knadh/koanf/parsers/toml"
|
|
"github.com/knadh/koanf/parsers/yaml"
|
|
"github.com/knadh/koanf/providers/env"
|
|
"github.com/knadh/koanf/providers/file"
|
|
"github.com/knadh/koanf/v2"
|
|
)
|
|
|
|
var delimiter = "."
|
|
|
|
const (
|
|
prefix = "KRAKEND_"
|
|
)
|
|
|
|
// New creates a new parser using the koanf library
|
|
func New() Parser {
|
|
return NewWithOptions(".")
|
|
}
|
|
|
|
func NewWithOptions(delimiter string) Parser {
|
|
return Parser{koanf.New(delimiter)}
|
|
}
|
|
|
|
// Parser is a config parser using the viper library
|
|
type Parser struct {
|
|
koanf *koanf.Koanf
|
|
}
|
|
|
|
// Parse reads and parses the configFile, and if there is no
|
|
// error calls initializes the config
|
|
func (p Parser) Parse(configFile string) (config.ServiceConfig, error) {
|
|
cfg, err := p.ParseWithoutInit(configFile)
|
|
if err != nil {
|
|
return cfg, err
|
|
}
|
|
if err := cfg.Init(); err != nil {
|
|
return cfg, config.CheckErr(err, configFile)
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
// ParseWithoutInit reads and parses the configFile. The values of the file can be
|
|
// override with envvars, using the KRAKEND_ prefix
|
|
func (p Parser) ParseWithoutInit(configFile string) (config.ServiceConfig, error) {
|
|
var cfg config.ServiceConfig
|
|
|
|
var kp koanf.Parser
|
|
ext := filepath.Ext(configFile)
|
|
switch ext {
|
|
case ".yml", ".yaml":
|
|
kp = yaml.Parser()
|
|
case ".toml":
|
|
kp = toml.Parser()
|
|
default:
|
|
kp = json.Parser()
|
|
}
|
|
|
|
if err := p.koanf.Load(file.Provider(configFile), kp); err != nil {
|
|
return cfg, fmt.Errorf("'%s': %s", configFile, err.Error())
|
|
}
|
|
cb := func(s string) string {
|
|
return strings.ToLower(strings.TrimPrefix(s, prefix))
|
|
}
|
|
p.koanf.Load(env.Provider(prefix, delimiter, cb), nil)
|
|
|
|
uCfg := koanf.UnmarshalConf{
|
|
Tag: "mapstructure",
|
|
}
|
|
if err := p.koanf.UnmarshalWithConf("", &cfg, uCfg); err != nil {
|
|
return cfg, fmt.Errorf("'%s': %s", configFile, err.Error())
|
|
}
|
|
|
|
cleanupServiceConfig(&cfg)
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
// cleanupServiceConfig make sure ExtraConfig type is map[string]interface{}
|
|
func cleanupServiceConfig(cfg *config.ServiceConfig) {
|
|
cfg.ExtraConfig = cleanConfigMap(cfg.ExtraConfig)
|
|
for _, endpoint := range cfg.Endpoints {
|
|
endpoint.ExtraConfig = cleanConfigMap(endpoint.ExtraConfig)
|
|
|
|
for _, backend := range endpoint.Backend {
|
|
backend.ExtraConfig = cleanConfigMap(backend.ExtraConfig)
|
|
}
|
|
}
|
|
}
|
|
|
|
func cleanConfigMap(cfg map[string]interface{}) map[string]interface{} {
|
|
for k, v := range cfg {
|
|
cfg[k] = cleanupMapValue(v)
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
func cleanupMapValue(input interface{}) interface{} {
|
|
switch data := input.(type) {
|
|
case []interface{}:
|
|
for key, value := range data {
|
|
data[key] = cleanupMapValue(value)
|
|
}
|
|
return data
|
|
case map[string]interface{}:
|
|
for key, value := range data {
|
|
data[key] = cleanupMapValue(value)
|
|
}
|
|
return data
|
|
case map[interface{}]interface{}:
|
|
output := make(map[string]interface{})
|
|
for key, value := range data {
|
|
output[fmt.Sprintf("%v", key)] = cleanupMapValue(value)
|
|
}
|
|
return output
|
|
default:
|
|
return data
|
|
}
|
|
}
|