fix(openshell): keyless model providers get an egress rule on their base URL's host and port (#1972)
This commit is contained in:
4 files changed
+72
-37
No files matched your search
@@ -8,10 +8,8 @@ import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"maps"
|
||||
"net"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -74,11 +72,7 @@ func (m *openShellModelEgress) Open(ctx context.Context, token, org string, prov
|
||||
var session daemon.ModelSession
|
||||
for _, name := range slices.Sorted(maps.Keys(providers)) {
|
||||
p := providers[name]
|
||||
keyEnv := p.KeyEnv(name)
|
||||
if keyEnv == "" {
|
||||
continue
|
||||
}
|
||||
mp, err := m.openProvider(ctx, token, org, name, keyEnv, p)
|
||||
mp, err := m.openProvider(ctx, token, org, name, p.KeyEnv(name), p)
|
||||
if err != nil {
|
||||
m.Close(ctx, token)
|
||||
return daemon.ModelSession{}, err
|
||||
@@ -93,6 +87,10 @@ func (m *openShellModelEgress) openProvider(ctx context.Context, token, org, nam
|
||||
if err != nil {
|
||||
return daemon.ModelProvider{}, fmt.Errorf("provider %q: %w", name, err)
|
||||
}
|
||||
if keyEnv == "" {
|
||||
// Nothing for the gateway to hold: the sandbox only needs the host.
|
||||
return daemon.ModelProvider{Host: host, Port: port}, nil
|
||||
}
|
||||
key, err := m.key(ctx, token, org, name, p)
|
||||
if err != nil {
|
||||
return daemon.ModelProvider{}, err
|
||||
@@ -263,18 +261,7 @@ func (m *openShellModelEgress) deleteProvider(ctx context.Context, name string)
|
||||
return err
|
||||
}
|
||||
|
||||
// providerEndpoint is the host and port p's key may be sent to.
|
||||
// providerEndpoint is the host and port p's requests go to.
|
||||
func providerEndpoint(p config.Provider) (string, int, error) {
|
||||
host, err := egress.ProviderHost(p.Class, p.BaseURL)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
if h, port, err := net.SplitHostPort(host); err == nil {
|
||||
n, convErr := strconv.Atoi(port)
|
||||
if convErr != nil {
|
||||
return "", 0, fmt.Errorf("provider host %q has a bad port: %w", host, convErr)
|
||||
}
|
||||
return h, n, nil
|
||||
}
|
||||
return host, 443, nil
|
||||
return egress.ProviderEndpoint(p.Class, p.BaseURL)
|
||||
}
|
||||
@@ -64,6 +64,7 @@ func TestOpenShellModelEgressHoldsKeysOnTheGateway(t *testing.T) {
|
||||
providers map[string]config.Provider
|
||||
wantKeys map[string]string // key env -> real key held on the gateway
|
||||
wantHosts []string
|
||||
wantPorts []int // defaults to 443 for each host
|
||||
wantHeader string // header the profile places the key in; empty means a bearer token
|
||||
}{
|
||||
{
|
||||
@@ -75,14 +76,22 @@ func TestOpenShellModelEgressHoldsKeysOnTheGateway(t *testing.T) {
|
||||
wantHosts: []string{"api.alpha.example"},
|
||||
},
|
||||
{
|
||||
name: "org binding, and a provider with no key is skipped",
|
||||
name: "org binding",
|
||||
providers: map[string]config.Provider{
|
||||
"beta": {Class: "openai", BaseURL: "https://api.beta.example", APIKeyEnv: "BETA_KEY"},
|
||||
"local": {Class: "openai", BaseURL: "https://local.example"},
|
||||
"beta": {Class: "openai", BaseURL: "https://api.beta.example", APIKeyEnv: "BETA_KEY"},
|
||||
},
|
||||
wantKeys: map[string]string{"BETA_KEY": "bound-fake-key"},
|
||||
wantHosts: []string{"api.beta.example"},
|
||||
},
|
||||
{
|
||||
name: "a keyless provider is reached on its base URL's host and port, holding no key",
|
||||
providers: map[string]config.Provider{
|
||||
"local": {Class: "openai", BaseURL: "http://192.0.2.7:8080/v1"},
|
||||
},
|
||||
wantKeys: map[string]string{},
|
||||
wantHosts: []string{"192.0.2.7"},
|
||||
wantPorts: []int{8080},
|
||||
},
|
||||
{
|
||||
name: "anthropic keys travel in x-api-key",
|
||||
providers: map[string]config.Provider{"claude": {Class: "anthropic", APIKeyEnv: "CLAUDE_KEY"}},
|
||||
@@ -112,8 +121,15 @@ func TestOpenShellModelEgressHoldsKeysOnTheGateway(t *testing.T) {
|
||||
}
|
||||
held := map[string]string{}
|
||||
for i, mp := range session.Providers {
|
||||
if mp.Host != tc.wantHosts[i] || mp.Port != 443 {
|
||||
t.Errorf("provider %d = %s:%d, want %s:443", i, mp.Host, mp.Port, tc.wantHosts[i])
|
||||
wantPort := 443
|
||||
if tc.wantPorts != nil {
|
||||
wantPort = tc.wantPorts[i]
|
||||
}
|
||||
if mp.Host != tc.wantHosts[i] || mp.Port != wantPort {
|
||||
t.Errorf("provider %d = %s:%d, want %s:%d", i, mp.Host, mp.Port, tc.wantHosts[i], wantPort)
|
||||
}
|
||||
if mp.Gateway == "" {
|
||||
continue
|
||||
}
|
||||
if strings.Contains(mp.Gateway, "run-credential") || strings.Contains(mp.Gateway, "fake-key") {
|
||||
t.Errorf("gateway name %q leaks the credential or key", mp.Gateway)
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -65,17 +66,33 @@ func ModelTransport(proxyURL string, hosts []string, caFile string) (*http.Trans
|
||||
// its class's default. An unknown class without a base URL has none, and
|
||||
// cannot be served through the proxy.
|
||||
func ProviderHost(class, baseURL string) (string, error) {
|
||||
host, _, err := ProviderEndpoint(class, baseURL)
|
||||
return host, err
|
||||
}
|
||||
|
||||
// ProviderEndpoint is ProviderHost with the port the base URL names, or its
|
||||
// scheme's default.
|
||||
func ProviderEndpoint(class, baseURL string) (string, int, error) {
|
||||
if baseURL == "" {
|
||||
baseURL = classBaseURL[class]
|
||||
}
|
||||
if baseURL == "" {
|
||||
return "", fmt.Errorf("provider class %q needs a base_url", class)
|
||||
return "", 0, fmt.Errorf("provider class %q needs a base_url", class)
|
||||
}
|
||||
u, err := url.Parse(baseURL)
|
||||
if err != nil || u.Hostname() == "" {
|
||||
return "", fmt.Errorf("provider base_url %q has no host", baseURL)
|
||||
return "", 0, fmt.Errorf("provider base_url %q has no host", baseURL)
|
||||
}
|
||||
return normalizeHost(u.Hostname()), nil
|
||||
port := 443
|
||||
if u.Scheme == "http" {
|
||||
port = 80
|
||||
}
|
||||
if p := u.Port(); p != "" {
|
||||
if port, err = strconv.Atoi(p); err != nil {
|
||||
return "", 0, fmt.Errorf("provider base_url %q has a bad port: %w", baseURL, err)
|
||||
}
|
||||
}
|
||||
return normalizeHost(u.Hostname()), port, nil
|
||||
}
|
||||
|
||||
// classBaseURL mirrors the default base URL each ai-sdk provider class uses
|
||||
|
||||
@@ -242,15 +242,8 @@ func (r *Runtime) spec(l Launch) (*v1.SandboxSpec, error) {
|
||||
rules["archie-state-store"] = serviceRule("archie-state-store", stateStore)
|
||||
}
|
||||
for _, p := range l.Providers {
|
||||
name := "model-" + p.Gateway
|
||||
rules[name] = v1.NetworkPolicyRule{
|
||||
Name: name,
|
||||
Endpoints: []v1.PolicyNetworkEndpoint{{
|
||||
Host: p.Host, Port: uint32(p.Port), Protocol: "rest", Access: v1.NetworkAccessPresetFull, //nolint:gosec // G115: a parsed port is 1 to 65535.
|
||||
CredentialBinding: &types.NetworkCredentialBinding{Provider: p.Gateway},
|
||||
}},
|
||||
Binaries: []v1.PolicyNetworkBinary{{Path: agentBinary}},
|
||||
}
|
||||
name, rule := providerRule(p)
|
||||
rules[name] = rule
|
||||
}
|
||||
if len(l.Hosts) > 0 {
|
||||
var endpoints []v1.PolicyNetworkEndpoint
|
||||
@@ -305,9 +298,31 @@ func (r *Runtime) spec(l Launch) (*v1.SandboxSpec, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// providerRule is the egress rule for one model provider: a credentialed one
|
||||
// is named by its gateway, a keyless one by the host and port it reaches.
|
||||
func providerRule(p Provider) (string, v1.NetworkPolicyRule) {
|
||||
name := "model-" + p.Gateway
|
||||
endpoint := v1.PolicyNetworkEndpoint{
|
||||
Host: p.Host, Port: uint32(p.Port), Protocol: "rest", Access: v1.NetworkAccessPresetFull, //nolint:gosec // G115: a parsed port is 1 to 65535.
|
||||
}
|
||||
if p.Gateway != "" {
|
||||
endpoint.CredentialBinding = &types.NetworkCredentialBinding{Provider: p.Gateway}
|
||||
} else {
|
||||
name = fmt.Sprintf("model-%s-%d", p.Host, p.Port)
|
||||
}
|
||||
return name, v1.NetworkPolicyRule{
|
||||
Name: name,
|
||||
Endpoints: []v1.PolicyNetworkEndpoint{endpoint},
|
||||
Binaries: []v1.PolicyNetworkBinary{{Path: agentBinary}},
|
||||
}
|
||||
}
|
||||
|
||||
func providerNames(providers []Provider) []string {
|
||||
var names []string
|
||||
for _, p := range providers {
|
||||
if p.Gateway == "" {
|
||||
continue
|
||||
}
|
||||
names = append(names, p.Gateway)
|
||||
}
|
||||
return names
|
||||
|
||||
Reference in new issue
Block a user