Skip to content

Commit 32dcc4a

Browse files
authored
fix(sdk/go): fall back to system active gateway (#4917)
* fix(sdk/go): fall back to system active gateway Match the CLI: use the system active_gateway when the user one is missing or invalid. Signed-off-by: Eric Curtin <eric.curtin@docker.com> * test(sdk/go): cover system active gateway paths Signed-off-by: Eric Curtin <eric.curtin@docker.com> --------- Signed-off-by: Eric Curtin <eric.curtin@docker.com>
1 parent b55ad7e commit 32dcc4a

5 files changed

Lines changed: 130 additions & 8 deletions

File tree

‎docs/sdk/go.mdx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ Omit `CAFile` to use system roots. For renewable OIDC service credentials, use
7272
`oidc.NewClientCredentialsAuth` from
7373
`github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/oidc`.
7474

75+
## Discover the Active Gateway
76+
77+
`gateway.NewClient("")` and `gateway.LoadConfig("")` select the active gateway
78+
from the user `active_gateway` file first, then fall back to the system
79+
`/etc/openshell/active_gateway`. `gateway.ListGateways` marks the selected
80+
gateway as active, including a system default.
81+
7582
## Create and Use a Sandbox
7683

7784
Resource methods take an explicit workspace. The following example creates a

‎sdk/go/openshell/v1/gateway/gateway.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
// NewClient creates a fully wired SDK client from an on-disk gateway
1515
// configuration. If name is empty, the active gateway (set via
16-
// `openshell gateway use`) is used.
16+
// `openshell gateway use`, or the system default) is used.
1717
//
1818
// The function resolves the gateway directory, parses metadata.json,
1919
// loads tokens lazily, maps the auth mode to an SDK auth provider, and
@@ -66,8 +66,8 @@ func NewClient(name string, opts ...ClientOption) (*v1.Client, error) {
6666
}
6767

6868
// LoadConfig reads and parses a gateway's on-disk configuration without
69-
// creating a client connection. If name is empty, the active gateway is
70-
// used.
69+
// creating a client connection. If name is empty, the active gateway (user
70+
// selection, then system default) is used.
7171
//
7272
// The returned [Config] is an immutable snapshot; changes to the on-disk
7373
// files after this call are not reflected.
@@ -111,6 +111,7 @@ func loadConfigInternal(name string) (*Config, error) {
111111
// directories. User gateways appear first. If the same name exists in
112112
// both directories, only the user gateway is returned (user precedence).
113113
// Returns an empty slice (not an error) when no gateways are configured.
114+
// Active also reflects the system default when no user selection exists.
114115
//
115116
// ListGateways is safe for concurrent use from multiple goroutines.
116117
func ListGateways() ([]Info, error) {

‎sdk/go/openshell/v1/gateway/gateway_test.go‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,13 +424,29 @@ func TestNewClient_ActiveGateway(t *testing.T) {
424424
func TestNewClient_NoActiveGateway(t *testing.T) {
425425
tmp := t.TempDir()
426426
t.Setenv("XDG_CONFIG_HOME", tmp)
427+
t.Setenv(systemGatewayDirEnv, t.TempDir())
427428
require.NoError(t, os.MkdirAll(filepath.Join(tmp, "openshell"), 0o755))
428429

429430
_, err := NewClient("")
430431
require.Error(t, err)
431432
assert.ErrorIs(t, err, ErrNoActiveGateway)
432433
}
433434

435+
func TestLoadConfig_SystemActiveGateway(t *testing.T) {
436+
user, system := t.TempDir(), t.TempDir()
437+
t.Setenv("XDG_CONFIG_HOME", user)
438+
t.Setenv(systemGatewayDirEnv, system)
439+
writeFile(t, system, "active_gateway", "corp")
440+
gwDir := filepath.Join(system, "gateways", "corp")
441+
require.NoError(t, os.MkdirAll(gwDir, 0o755))
442+
writeFile(t, gwDir, "metadata.json", `{"gateway_endpoint":"host:443"}`)
443+
444+
cfg, err := LoadConfig("")
445+
require.NoError(t, err)
446+
assert.Equal(t, "corp", cfg.Name)
447+
assert.Equal(t, SourceSystem, cfg.Source)
448+
}
449+
434450
func TestLoadConfig_ActiveGateway(t *testing.T) {
435451
tmp := setupGateway(t, "my-active", `{"gateway_endpoint":"host:443","auth_mode":"oidc"}`)
436452
writeFile(t, filepath.Join(tmp, "openshell"), "active_gateway", "my-active")
@@ -500,3 +516,19 @@ func TestListGateways_ActiveStatus(t *testing.T) {
500516
}
501517
}
502518
}
519+
520+
func TestListGateways_SystemActiveStatus(t *testing.T) {
521+
user, system := t.TempDir(), t.TempDir()
522+
t.Setenv("XDG_CONFIG_HOME", user)
523+
t.Setenv(systemGatewayDirEnv, system)
524+
require.NoError(t, os.MkdirAll(filepath.Join(user, "openshell", "gateways", "mine"), 0o755))
525+
require.NoError(t, os.MkdirAll(filepath.Join(system, "gateways", "corp"), 0o755))
526+
writeFile(t, system, "active_gateway", "corp")
527+
528+
gateways, err := ListGateways()
529+
require.NoError(t, err)
530+
require.Len(t, gateways, 2)
531+
532+
assert.Equal(t, Info{Name: "mine", Active: false, Source: SourceUser}, gateways[0])
533+
assert.Equal(t, Info{Name: "corp", Active: true, Source: SourceSystem}, gateways[1])
534+
}

‎sdk/go/openshell/v1/gateway/paths.go‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,29 @@ func validateGatewayName(name string) error {
137137
return nil
138138
}
139139

140-
// resolveActiveGateway reads the active_gateway file from the user
141-
// config directory and returns the validated gateway name. Returns
142-
// ErrNoActiveGateway if the file is missing or empty.
140+
// resolveActiveGateway returns the validated active gateway name. It reads
141+
// the user active_gateway file and falls back to the system one when the user
142+
// file is missing, empty, or invalid, matching the CLI. Returns
143+
// ErrNoActiveGateway describing the user file when neither yields a name.
143144
func resolveActiveGateway() (string, error) {
144145
userBase, err := userConfigDir()
145146
if err != nil {
146-
return "", fmt.Errorf("%w: cannot determine config directory: %v", ErrNoActiveGateway, err)
147+
err = fmt.Errorf("%w: cannot determine config directory: %v", ErrNoActiveGateway, err)
148+
} else {
149+
var name string
150+
if name, err = readActiveGatewayFile(filepath.Join(userBase, activeGatewayFile)); err == nil {
151+
return name, nil
152+
}
153+
}
154+
155+
if name, sysErr := readActiveGatewayFile(filepath.Join(systemConfigDir(), activeGatewayFile)); sysErr == nil {
156+
return name, nil
147157
}
158+
return "", err
159+
}
148160

149-
path := filepath.Join(userBase, activeGatewayFile)
161+
// readActiveGatewayFile reads and validates the gateway name stored at path.
162+
func readActiveGatewayFile(path string) (string, error) {
150163
data, err := os.ReadFile(path)
151164
if err != nil {
152165
return "", fmt.Errorf("%w", ErrNoActiveGateway)

‎sdk/go/openshell/v1/gateway/paths_test.go‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ func TestResolveActiveGateway_WhitespaceHandling(t *testing.T) {
198198
func TestResolveActiveGateway_FileMissing(t *testing.T) {
199199
tmp := t.TempDir()
200200
t.Setenv("XDG_CONFIG_HOME", tmp)
201+
t.Setenv(systemGatewayDirEnv, t.TempDir())
201202
require.NoError(t, os.MkdirAll(filepath.Join(tmp, "openshell"), 0o755))
202203

203204
_, err := resolveActiveGateway()
@@ -208,6 +209,7 @@ func TestResolveActiveGateway_FileMissing(t *testing.T) {
208209
func TestResolveActiveGateway_EmptyFile(t *testing.T) {
209210
tmp := t.TempDir()
210211
t.Setenv("XDG_CONFIG_HOME", tmp)
212+
t.Setenv(systemGatewayDirEnv, t.TempDir())
211213
require.NoError(t, os.MkdirAll(filepath.Join(tmp, "openshell"), 0o755))
212214
require.NoError(t, os.WriteFile(filepath.Join(tmp, "openshell", "active_gateway"), []byte(" \n"), 0o644))
213215

@@ -216,6 +218,73 @@ func TestResolveActiveGateway_EmptyFile(t *testing.T) {
216218
assert.ErrorIs(t, err, ErrNoActiveGateway)
217219
}
218220

221+
// setActiveFiles points the user and system config dirs at temp dirs and
222+
// writes active_gateway files when the given content is non-empty.
223+
func setActiveFiles(t *testing.T, userContent, systemContent string) {
224+
t.Helper()
225+
user, system := t.TempDir(), t.TempDir()
226+
t.Setenv("XDG_CONFIG_HOME", user)
227+
t.Setenv(systemGatewayDirEnv, system)
228+
if userContent != "" {
229+
require.NoError(t, os.MkdirAll(filepath.Join(user, "openshell"), 0o755))
230+
writeFile(t, filepath.Join(user, "openshell"), "active_gateway", userContent)
231+
}
232+
if systemContent != "" {
233+
writeFile(t, system, "active_gateway", systemContent)
234+
}
235+
}
236+
237+
func TestResolveActiveGateway_FallsBackToSystem(t *testing.T) {
238+
setActiveFiles(t, "", "corp\n")
239+
240+
name, err := resolveActiveGateway()
241+
require.NoError(t, err)
242+
assert.Equal(t, "corp", name)
243+
}
244+
245+
func TestResolveActiveGateway_PrefersUser(t *testing.T) {
246+
setActiveFiles(t, "mine", "corp")
247+
248+
name, err := resolveActiveGateway()
249+
require.NoError(t, err)
250+
assert.Equal(t, "mine", name)
251+
}
252+
253+
func TestResolveActiveGateway_InvalidUserFallsBackToSystem(t *testing.T) {
254+
setActiveFiles(t, "../escape", "corp")
255+
256+
name, err := resolveActiveGateway()
257+
require.NoError(t, err)
258+
assert.Equal(t, "corp", name)
259+
}
260+
261+
func TestResolveActiveGateway_InvalidSystemIsIgnored(t *testing.T) {
262+
setActiveFiles(t, "bad/user", "bad/system")
263+
264+
_, err := resolveActiveGateway()
265+
require.ErrorIs(t, err, ErrNoActiveGateway)
266+
assert.Contains(t, err.Error(), "bad/user")
267+
assert.NotContains(t, err.Error(), "bad/system")
268+
}
269+
270+
func TestResolveActiveGateway_UserDirErrorFallsBackToSystem(t *testing.T) {
271+
setActiveFiles(t, "", "corp")
272+
t.Setenv("XDG_CONFIG_HOME", "relative/path")
273+
274+
name, err := resolveActiveGateway()
275+
require.NoError(t, err)
276+
assert.Equal(t, "corp", name)
277+
}
278+
279+
func TestResolveActiveGateway_UserDirErrorNoSystem(t *testing.T) {
280+
setActiveFiles(t, "", "")
281+
t.Setenv("XDG_CONFIG_HOME", "relative/path")
282+
283+
_, err := resolveActiveGateway()
284+
require.ErrorIs(t, err, ErrNoActiveGateway)
285+
assert.Contains(t, err.Error(), "cannot determine config directory")
286+
}
287+
219288
func TestValidateGatewayName_SpecialCharacters(t *testing.T) {
220289
cases := []struct {
221290
name string

0 commit comments

Comments
 (0)