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"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"maps"
|
"maps"
|
||||||
"net"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"slices"
|
"slices"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -74,11 +72,7 @@ func (m *openShellModelEgress) Open(ctx context.Context, token, org string, prov
|
|||||||
var session daemon.ModelSession
|
var session daemon.ModelSession
|
||||||
for _, name := range slices.Sorted(maps.Keys(providers)) {
|
for _, name := range slices.Sorted(maps.Keys(providers)) {
|
||||||
p := providers[name]
|
p := providers[name]
|
||||||
keyEnv := p.KeyEnv(name)
|
mp, err := m.openProvider(ctx, token, org, name, p.KeyEnv(name), p)
|
||||||
if keyEnv == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
mp, err := m.openProvider(ctx, token, org, name, keyEnv, p)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.Close(ctx, token)
|
m.Close(ctx, token)
|
||||||
return daemon.ModelSession{}, err
|
return daemon.ModelSession{}, err
|
||||||
@@ -93,6 +87,10 @@ func (m *openShellModelEgress) openProvider(ctx context.Context, token, org, nam
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return daemon.ModelProvider{}, fmt.Errorf("provider %q: %w", name, err)
|
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)
|
key, err := m.key(ctx, token, org, name, p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return daemon.ModelProvider{}, err
|
return daemon.ModelProvider{}, err
|
||||||
@@ -263,18 +261,7 @@ func (m *openShellModelEgress) deleteProvider(ctx context.Context, name string)
|
|||||||
return err
|
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) {
|
func providerEndpoint(p config.Provider) (string, int, error) {
|
||||||
host, err := egress.ProviderHost(p.Class, p.BaseURL)
|
return egress.ProviderEndpoint(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
|
|
||||||
}
|
}
|
||||||
@@ -64,6 +64,7 @@ func TestOpenShellModelEgressHoldsKeysOnTheGateway(t *testing.T) {
|
|||||||
providers map[string]config.Provider
|
providers map[string]config.Provider
|
||||||
wantKeys map[string]string // key env -> real key held on the gateway
|
wantKeys map[string]string // key env -> real key held on the gateway
|
||||||
wantHosts []string
|
wantHosts []string
|
||||||
|
wantPorts []int // defaults to 443 for each host
|
||||||
wantHeader string // header the profile places the key in; empty means a bearer token
|
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"},
|
wantHosts: []string{"api.alpha.example"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "org binding, and a provider with no key is skipped",
|
name: "org binding",
|
||||||
providers: map[string]config.Provider{
|
providers: map[string]config.Provider{
|
||||||
"beta": {Class: "openai", BaseURL: "https://api.beta.example", APIKeyEnv: "BETA_KEY"},
|
"beta": {Class: "openai", BaseURL: "https://api.beta.example", APIKeyEnv: "BETA_KEY"},
|
||||||
"local": {Class: "openai", BaseURL: "https://local.example"},
|
|
||||||
},
|
},
|
||||||
wantKeys: map[string]string{"BETA_KEY": "bound-fake-key"},
|
wantKeys: map[string]string{"BETA_KEY": "bound-fake-key"},
|
||||||
wantHosts: []string{"api.beta.example"},
|
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",
|
name: "anthropic keys travel in x-api-key",
|
||||||
providers: map[string]config.Provider{"claude": {Class: "anthropic", APIKeyEnv: "CLAUDE_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{}
|
held := map[string]string{}
|
||||||
for i, mp := range session.Providers {
|
for i, mp := range session.Providers {
|
||||||
if mp.Host != tc.wantHosts[i] || mp.Port != 443 {
|
wantPort := 443
|
||||||
t.Errorf("provider %d = %s:%d, want %s:443", i, mp.Host, mp.Port, tc.wantHosts[i])
|
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") {
|
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)
|
t.Errorf("gateway name %q leaks the credential or key", mp.Gateway)
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
"slices"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"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
|
// its class's default. An unknown class without a base URL has none, and
|
||||||
// cannot be served through the proxy.
|
// cannot be served through the proxy.
|
||||||
func ProviderHost(class, baseURL string) (string, error) {
|
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 == "" {
|
if baseURL == "" {
|
||||||
baseURL = classBaseURL[class]
|
baseURL = classBaseURL[class]
|
||||||
}
|
}
|
||||||
if baseURL == "" {
|
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)
|
u, err := url.Parse(baseURL)
|
||||||
if err != nil || u.Hostname() == "" {
|
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
|
// 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)
|
rules["archie-state-store"] = serviceRule("archie-state-store", stateStore)
|
||||||
}
|
}
|
||||||
for _, p := range l.Providers {
|
for _, p := range l.Providers {
|
||||||
name := "model-" + p.Gateway
|
name, rule := providerRule(p)
|
||||||
rules[name] = v1.NetworkPolicyRule{
|
rules[name] = rule
|
||||||
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}},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if len(l.Hosts) > 0 {
|
if len(l.Hosts) > 0 {
|
||||||
var endpoints []v1.PolicyNetworkEndpoint
|
var endpoints []v1.PolicyNetworkEndpoint
|
||||||
@@ -305,9 +298,31 @@ func (r *Runtime) spec(l Launch) (*v1.SandboxSpec, error) {
|
|||||||
}, nil
|
}, 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 {
|
func providerNames(providers []Provider) []string {
|
||||||
var names []string
|
var names []string
|
||||||
for _, p := range providers {
|
for _, p := range providers {
|
||||||
|
if p.Gateway == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
names = append(names, p.Gateway)
|
names = append(names, p.Gateway)
|
||||||
}
|
}
|
||||||
return names
|
return names
|
||||||
|
|||||||
Reference in new issue
Block a user