Files
zeekayandHanzo Dev 53350699b6 deps(vendor): fold the krakend/lura module set into internal/
Every import in this module now resolves to github.com/hanzoai/gateway/v2.
The 34 github.com/krakend/* + github.com/luraproject/lura modules that
supplied the engine and its components are vendored under internal/ and
their 91 packages rewritten onto our own path.

Why fold rather than mirror into 34 hanzoai/* repos:

  - It removes the exposure outright. 34 rows leave 34 rows of go.sum to
    reason about; zero modules leave nothing. `go list -m all` and go.sum
    now contain no krakend/lura entry at all.
  - Cross-references cannot go wrong. 28 of the 34 import lura (97 files
    inside lura self-reference it). Mirroring means rewriting every one of
    those to our lura, and a single miss silently links a SECOND copy of
    lura into the binary. With one module that is structurally impossible.
  - It creates no duplicates. These are lura-coupled gateway glue with no
    consumer outside this binary; publishing 34 repos would be 34 new
    things to maintain for zero second caller. internal/ makes that
    guarantee mechanical — Go forbids importing it from outside this
    module, so none of it can become a competing public package. Verified
    no repo outside gateway imports any hanzoai/gateway/v2 subpackage.

Layout mirrors provenance so an upstream re-sync stays a diff:

  luraproject/lura/v2  -> internal/lura         (the engine, 20 pkgs)
  krakend/krakend-<x>  -> internal/plugin/<x>   (28 components)
  krakend/<x>          -> internal/pkg/<x>      (6 standalone libs)

This commit is PURELY MECHANICAL: 196 of the 197 vendored files are
byte-identical to their upstream original once the import rewrite is
applied. The single exception is bloomfilter's subpackage, which upstream
names `krakend` — the one package name that could not survive as-is; it
becomes internal/pkg/bloomfilter/register. Hand edits land separately in
the next commit so they are readable on their own.

Per `go mod vendor` semantics, dependencies' _test.go files are not
vendored. They test upstream code at a pinned version that we do not
modify, several cannot run at all from a module distribution (they need
TLS certs and compiled .so plugins upstream generates but never ships),
and one is a 100k-request stress test that exhausts the ephemeral port
range and fails every package running beside it. Fold integrity is proven
by the byte-diff above instead, which is the stronger check.

Preserved deliberately — each of these is behaviour, not branding:
  - the `github.com/devopsfaith/krakend*` extra_config namespace strings
    (35 of them). These are config-surface keys, not import paths;
    renaming one silently stops that component finding its config. The
    jose validator namespace backs `auth/validator` on 212 endpoints.
  - core.KrakendVersion / KrakendUserAgent / the X-KRAKEND consts, whose
    VALUES rebrand.go overwrites — that seam is unchanged.
  - the KRAKEND_ koanf env prefix (a hardcoded upstream const).
  - every vendored module's LICENSE (34 files, Apache-2.0/MIT).

The Makefile/CI -X paths move with the engine. An -X naming a symbol that
does not exist is silently ignored by the linker, so a stale path here
would have shipped an "undefined" version in the backend User-Agent and
X-Api-Version with a perfectly green build. Verified live:
`gateway version` reports 2.13.0, not undefined.

Verification (GOWORK=off CGO_ENABLED=0, both tag modes):
  go build ./...              OK      go build -tags legacy ./...   OK
  go vet ./...                clean   go vet -tags legacy ./...     clean
  go test ./...               6/6 ok  go test -tags legacy          5/5 ok
  tests/ integration harness  47 fixtures pass
  gateway check -c configs/{hanzo,lux}/gateway.json  -> Syntax OK!
  go list -m all | grep -iE 'krakend|lura'  -> empty
  go mod verify -> all modules verified

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-25 14:57:19 -07:00

335 lines
9.8 KiB
Go

// SPDX-License-Identifier: Apache-2.0
/*
Package server provides tools to create http servers and handlers wrapping the lura router
*/
package server
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net"
"net/http"
"os"
"sync"
"time"
"github.com/hanzoai/gateway/v2/internal/lura/config"
"github.com/hanzoai/gateway/v2/internal/lura/core"
"github.com/hanzoai/gateway/v2/internal/lura/logging"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)
// ToHTTPError translates an error into a HTTP status code
type ToHTTPError func(error) int
// DefaultToHTTPError is a ToHTTPError transalator that always returns an
// internal server error
func DefaultToHTTPError(_ error) int {
return http.StatusInternalServerError
}
const (
// HeaderCompleteResponseValue is the value of the CompleteResponseHeader
// if the response is complete
HeaderCompleteResponseValue = "true"
// HeaderIncompleteResponseValue is the value of the CompleteResponseHeader
// if the response is not complete
HeaderIncompleteResponseValue = "false"
)
var (
// CompleteResponseHeaderName is the header to flag incomplete responses to the client
CompleteResponseHeaderName = "X-Krakend-Completed"
// HeadersToSend are the headers to pass from the router request to the proxy
HeadersToSend = []string{"Content-Type"}
// UserAgentHeaderValue is the value of the User-Agent header to add to the proxy request
UserAgentHeaderValue = []string{core.KrakendUserAgent}
// ErrInternalError is the error returned by the router when something went wrong
ErrInternalError = errors.New("internal server error")
// ErrPrivateKey is the error returned by the router when the private key is not defined
ErrPrivateKey = errors.New("private key not defined")
// ErrPublicKey is the error returned by the router when the public key is not defined
ErrPublicKey = errors.New("public key not defined")
loggerPrefix = "[SERVICE: HTTP Server]"
)
// InitHTTPDefaultTransport ensures the default HTTP transport is configured just once per execution
func InitHTTPDefaultTransport(cfg config.ServiceConfig) {
InitHTTPDefaultTransportWithLogger(cfg, nil)
}
func InitHTTPDefaultTransportWithLogger(cfg config.ServiceConfig, logger logging.Logger) {
if logger == nil {
logger = logging.NoOp
}
if cfg.AllowInsecureConnections {
if cfg.ClientTLS == nil {
cfg.ClientTLS = &config.ClientTLS{}
}
cfg.ClientTLS.AllowInsecureConnections = true
}
onceTransportConfig.Do(func() {
http.DefaultTransport = NewTransport(cfg, logger)
})
}
func NewTransport(cfg config.ServiceConfig, logger logging.Logger) *http.Transport {
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: cfg.DialerTimeout,
KeepAlive: cfg.DialerKeepAlive,
FallbackDelay: cfg.DialerFallbackDelay,
DualStack: true,
}).DialContext,
DisableCompression: cfg.DisableCompression,
DisableKeepAlives: cfg.DisableKeepAlives,
MaxIdleConns: cfg.MaxIdleConns,
MaxIdleConnsPerHost: cfg.MaxIdleConnsPerHost,
IdleConnTimeout: cfg.IdleConnTimeout,
ResponseHeaderTimeout: cfg.ResponseHeaderTimeout,
ExpectContinueTimeout: cfg.ExpectContinueTimeout,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: ParseClientTLSConfigWithLogger(cfg.ClientTLS, logger),
}
}
// RunServer runs a http.Server with the given handler and configuration.
// It configures the TLS layer if required by the received configuration.
func RunServer(ctx context.Context, cfg config.ServiceConfig, handler http.Handler) error {
return RunServerWithLoggerFactory(nil)(ctx, cfg, handler)
}
func RunServerWithLoggerFactory(l logging.Logger) func(context.Context, config.ServiceConfig, http.Handler) error {
return func(ctx context.Context, cfg config.ServiceConfig, handler http.Handler) error {
done := make(chan error)
s := NewServerWithLogger(cfg, handler, l)
if s.TLSConfig == nil {
go func() {
done <- s.ListenAndServe()
}()
} else {
if cfg.TLS.PublicKey != "" || cfg.TLS.PrivateKey != "" {
cfg.TLS.Keys = append(cfg.TLS.Keys, config.TLSKeyPair{
PublicKey: cfg.TLS.PublicKey,
PrivateKey: cfg.TLS.PrivateKey,
})
}
if len(cfg.TLS.Keys) == 0 {
return ErrPublicKey
}
for _, k := range cfg.TLS.Keys {
if k.PublicKey == "" {
return ErrPublicKey
}
if k.PrivateKey == "" {
return ErrPrivateKey
}
cert, err := tls.LoadX509KeyPair(k.PublicKey, k.PrivateKey)
if err != nil {
return err
}
s.TLSConfig.Certificates = append(s.TLSConfig.Certificates, cert)
}
go func() {
// since we already use the list of certificates in the config
// we do not need to specify the files for public and private key here
done <- s.ListenAndServeTLS("", "")
}()
}
select {
case err := <-done:
return err
case <-ctx.Done():
if cfg.MaxShutdownDuration <= 0 {
return s.Shutdown(context.Background())
}
withTimeout, cancel := context.WithTimeout(context.Background(), cfg.MaxShutdownDuration)
defer cancel()
return s.Shutdown(withTimeout)
}
}
}
// NewServer returns a http.Server ready to serve the injected handler
func NewServer(cfg config.ServiceConfig, handler http.Handler) *http.Server {
return NewServerWithLogger(cfg, handler, nil)
}
func NewServerWithLogger(cfg config.ServiceConfig, handler http.Handler, logger logging.Logger) *http.Server {
if cfg.UseH2C {
handler = h2c.NewHandler(handler, &http2.Server{})
}
return &http.Server{
Addr: net.JoinHostPort(cfg.Address, fmt.Sprintf("%d", cfg.Port)),
Handler: handler,
ReadTimeout: cfg.ReadTimeout,
WriteTimeout: cfg.WriteTimeout,
ReadHeaderTimeout: cfg.ReadHeaderTimeout,
IdleTimeout: cfg.IdleTimeout,
TLSConfig: ParseTLSConfigWithLogger(cfg.TLS, logger),
MaxHeaderBytes: cfg.MaxHeaderBytes,
}
}
// ParseTLSConfig creates a tls.Config from the TLS section of the service configuration
func ParseTLSConfig(cfg *config.TLS) *tls.Config {
return ParseTLSConfigWithLogger(cfg, nil)
}
func ParseTLSConfigWithLogger(cfg *config.TLS, logger logging.Logger) *tls.Config {
if cfg == nil {
return nil
}
if cfg.IsDisabled {
return nil
}
if logger == nil {
logger = logging.NoOp
}
tlsConfig := &tls.Config{
MinVersion: parseTLSVersion(cfg.MinVersion),
MaxVersion: parseTLSVersion(cfg.MaxVersion),
CurvePreferences: parseCurveIDs(cfg.CurvePreferences),
CipherSuites: parseCipherSuites(cfg.CipherSuites),
}
if !cfg.EnableMTLS {
return tlsConfig
}
certPool := loadCertPool(cfg.DisableSystemCaPool, cfg.CaCerts, logger)
for _, cert := range cfg.Keys {
caCert, err := os.ReadFile(cert.PublicKey)
if err != nil {
logger.Error(fmt.Sprintf("%s Cannot load public key %s: %s", loggerPrefix, cert.PublicKey, err.Error()))
continue
}
certPool.AppendCertsFromPEM(caCert)
}
tlsConfig.ClientCAs = certPool
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
return tlsConfig
}
func ParseClientTLSConfigWithLogger(cfg *config.ClientTLS, logger logging.Logger) *tls.Config {
if cfg == nil {
return nil
}
return &tls.Config{
InsecureSkipVerify: cfg.AllowInsecureConnections,
RootCAs: loadCertPool(cfg.DisableSystemCaPool, cfg.CaCerts, logger),
MinVersion: parseTLSVersion(cfg.MinVersion),
MaxVersion: parseTLSVersion(cfg.MaxVersion),
CurvePreferences: parseCurveIDs(cfg.CurvePreferences),
CipherSuites: parseCipherSuites(cfg.CipherSuites),
Certificates: loadClientCerts(cfg.ClientCerts, logger),
}
}
func loadCertPool(disableSystemCaPool bool, caCerts []string, logger logging.Logger) *x509.CertPool {
certPool := x509.NewCertPool()
if !disableSystemCaPool {
if systemCertPool, err := x509.SystemCertPool(); err == nil {
certPool = systemCertPool
} else {
logger.Error(fmt.Sprintf("%s Cannot load system CA pool: %s", loggerPrefix, err.Error()))
}
}
for _, path := range caCerts {
if ca, err := os.ReadFile(path); err == nil {
certPool.AppendCertsFromPEM(ca)
} else {
logger.Error(fmt.Sprintf("%s Cannot load certificate CA %s: %s", loggerPrefix, path, err.Error()))
}
}
return certPool
}
func loadClientCerts(certFiles []config.ClientTLSCert, logger logging.Logger) []tls.Certificate {
certs := make([]tls.Certificate, 0, len(certFiles))
for _, certAndKey := range certFiles {
cert, err := tls.LoadX509KeyPair(certAndKey.Certificate, certAndKey.PrivateKey)
if err != nil {
logger.Error(fmt.Sprintf("%s Cannot load client certificate %s, %s: %s",
loggerPrefix, certAndKey.Certificate, certAndKey.PrivateKey, err.Error()))
continue
}
certs = append(certs, cert)
}
return certs
}
func parseTLSVersion(key string) uint16 {
if v, ok := versions[key]; ok {
return v
}
return tls.VersionTLS13
}
func parseCurveIDs(curvePreferences []uint16) []tls.CurveID {
l := len(curvePreferences)
if l == 0 {
return defaultCurves
}
curves := make([]tls.CurveID, len(curvePreferences))
for i := range curves {
curves[i] = tls.CurveID(curvePreferences[i])
}
return curves
}
func parseCipherSuites(cipherSuites []uint16) []uint16 {
l := len(cipherSuites)
if l == 0 {
return defaultCipherSuites
}
cs := make([]uint16, l)
for i := range cs {
cs[i] = uint16(cipherSuites[i])
}
return cs
}
var (
onceTransportConfig sync.Once
defaultCurves = []tls.CurveID{
tls.CurveP521,
tls.CurveP384,
tls.CurveP256,
}
defaultCipherSuites = []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
}
versions = map[string]uint16{
"SSL3.0": tls.VersionSSL30,
"TLS10": tls.VersionTLS10,
"TLS11": tls.VersionTLS11,
"TLS12": tls.VersionTLS12,
"TLS13": tls.VersionTLS13,
}
)