|
1 | 1 | package filter |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "regexp" |
| 7 | + |
| 8 | + v3 "github.com/cncf/xds/go/xds/type/v3" |
4 | 9 | "github.com/envoyproxy/envoy/contrib/golang/common/go/api" |
5 | 10 | "google.golang.org/protobuf/types/known/anypb" |
6 | 11 | ) |
7 | 12 |
|
8 | | -type config struct{} |
| 13 | +// hostPattern matches the host format: {port}-{namespace}--{name}.{domain} |
| 14 | +// Group 1: port (digits), Group 2: namespace--name (alphanumeric and hyphens) |
| 15 | +var hostPattern = regexp.MustCompile(`^(\d+)-([a-zA-Z0-9\-]+)\.`) |
| 16 | + |
| 17 | +const ( |
| 18 | + DefaultHostHeaderName = "Host" |
| 19 | + DefaultSandboxHeaderName = "e2b-sandbox-id" |
| 20 | + DefaultSandboxPortHeader = "e2b-sandbox-port" |
| 21 | + DefaultSandboxPort = "49983" |
| 22 | +) |
| 23 | + |
| 24 | +// Config holds the filter configuration |
| 25 | +type Config struct { |
| 26 | + // SandboxHeaderName is the header name for sandbox ID (checked first) |
| 27 | + SandboxHeaderName string `json:"sandbox-header-name,omitempty"` |
| 28 | + // SandboxPortHeader is the header name for sandbox port |
| 29 | + SandboxPortHeader string `json:"sandbox-port-header,omitempty"` |
| 30 | + // HostHeaderName is the header name for host matching (fallback when sandbox header not found) |
| 31 | + HostHeaderName string `json:"host-header-name,omitempty"` |
| 32 | + // DefaultPort is the default port if not specified |
| 33 | + DefaultPort string `json:"default-port,omitempty"` |
| 34 | +} |
| 35 | + |
| 36 | +// DefaultConfig returns default configuration |
| 37 | +func DefaultConfig() *Config { |
| 38 | + return &Config{ |
| 39 | + SandboxHeaderName: DefaultSandboxHeaderName, |
| 40 | + SandboxPortHeader: DefaultSandboxPortHeader, |
| 41 | + HostHeaderName: DefaultHostHeaderName, |
| 42 | + DefaultPort: DefaultSandboxPort, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// Validate checks configuration validity |
| 47 | +func (c *Config) Validate() error { |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +// GetSandboxHeaderName returns the effective sandbox header name |
| 52 | +func (c *Config) GetSandboxHeaderName() string { |
| 53 | + if c.SandboxHeaderName != "" { |
| 54 | + return c.SandboxHeaderName |
| 55 | + } |
| 56 | + return DefaultSandboxHeaderName |
| 57 | +} |
| 58 | + |
| 59 | +// GetHostHeaderName returns the effective host header name |
| 60 | +func (c *Config) GetHostHeaderName() string { |
| 61 | + if c.HostHeaderName != "" { |
| 62 | + return c.HostHeaderName |
| 63 | + } |
| 64 | + return DefaultHostHeaderName |
| 65 | +} |
| 66 | + |
| 67 | +// ExtractHostInfo extracts both host key and port from the header in one regex call |
| 68 | +// Only for host mode: extracts both from the host format (<port>-<namespace>--<name>.domain) |
| 69 | +// Returns (hostKey, port) - both empty if parsing fails |
| 70 | +func (c *Config) ExtractHostInfo(headerValue string) (string, string) { |
| 71 | + if headerValue == "" { |
| 72 | + return "", "" |
| 73 | + } |
| 74 | + |
| 75 | + // Use regex to extract both port and namespace--name from host format |
| 76 | + // e.g., "8080-abc--def.example.com" -> hostKey: "abc--def", port: "8080" |
| 77 | + matches := hostPattern.FindStringSubmatch(headerValue) |
| 78 | + if len(matches) < 3 { |
| 79 | + return "", "" |
| 80 | + } |
| 81 | + return matches[2], matches[1] |
| 82 | +} |
9 | 83 |
|
10 | 84 | type ConfigParser struct{} |
11 | 85 |
|
12 | 86 | func (p *ConfigParser) Parse(any *anypb.Any, callbacks api.ConfigCallbackHandler) (interface{}, error) { |
13 | | - return &config{}, nil |
| 87 | + cfg := DefaultConfig() |
| 88 | + |
| 89 | + // Unmarshal the xds.type.v3.TypedStruct protobuf message |
| 90 | + typedStruct := &v3.TypedStruct{} |
| 91 | + if err := any.UnmarshalTo(typedStruct); err != nil { |
| 92 | + return nil, fmt.Errorf("failed to unmarshal TypedStruct: %w", err) |
| 93 | + } |
| 94 | + |
| 95 | + // Get the value from TypedStruct which contains the actual config as Struct |
| 96 | + valueStruct := typedStruct.GetValue() |
| 97 | + if valueStruct == nil { |
| 98 | + // No value field, use defaults |
| 99 | + return cfg, nil |
| 100 | + } |
| 101 | + |
| 102 | + // Convert the struct to JSON |
| 103 | + configBytes, err := json.Marshal(valueStruct.AsMap()) |
| 104 | + if err != nil { |
| 105 | + return nil, fmt.Errorf("failed to marshal config value to JSON: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + // Parse actual config from JSON |
| 109 | + if len(configBytes) > 0 && string(configBytes) != "null" { |
| 110 | + if err := json.Unmarshal(configBytes, cfg); err != nil { |
| 111 | + return nil, fmt.Errorf("failed to parse config: %w", err) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // Validate |
| 116 | + if err := cfg.Validate(); err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + |
| 120 | + return cfg, nil |
14 | 121 | } |
15 | 122 |
|
16 | 123 | func (p *ConfigParser) Merge(parent interface{}, child interface{}) interface{} { |
17 | | - return child |
| 124 | + parentCfg := parent.(*Config) |
| 125 | + childCfg := child.(*Config) |
| 126 | + |
| 127 | + // Child overrides parent for all fields |
| 128 | + merged := DefaultConfig() |
| 129 | + *merged = *parentCfg |
| 130 | + |
| 131 | + if childCfg.SandboxHeaderName != "" { |
| 132 | + merged.SandboxHeaderName = childCfg.SandboxHeaderName |
| 133 | + } |
| 134 | + if childCfg.SandboxPortHeader != "" { |
| 135 | + merged.SandboxPortHeader = childCfg.SandboxPortHeader |
| 136 | + } |
| 137 | + if childCfg.HostHeaderName != "" { |
| 138 | + merged.HostHeaderName = childCfg.HostHeaderName |
| 139 | + } |
| 140 | + if childCfg.DefaultPort != "" { |
| 141 | + merged.DefaultPort = childCfg.DefaultPort |
| 142 | + } |
| 143 | + |
| 144 | + return merged |
18 | 145 | } |
0 commit comments