Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,9 @@ Client
├── Services() → ServiceInterface (expose, get, list, delete)
├── Providers() → ProviderInterface (CRUD + ensure)
│ ├── Profiles() → ProfileInterface (list, get, import, update, lint, delete)
│ └── Refresh() → RefreshInterface (configure, status, rotate, delete)
│ └── Refresh() → RefreshInterface (configure, status, rotate, inline secrets, delete)
├── Workspaces() → WorkspaceInterface (create, get, list, delete, members)
├── Secrets() → SecretInterface (create, get, list, update, delete)
└── Policy() → PolicyInterface (draft review, approve, reject, merge, status)
```

Expand Down
1 change: 1 addition & 0 deletions docs/src/api/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ for each resource domain and manages the underlying gRPC connection. The
| `TCP()` | `TCPInterface` | TCP port forwarding |
| `Config()` | `ConfigInterface` | Sandbox and gateway configuration |
| `Policy()` | `PolicyInterface` | Draft policy review workflow |
| `Secrets()` | `SecretInterface` | Workspace secret management (write-only values) |
| `Close()` | `error` | Close the gRPC connection |

Sub-client hierarchy: `Providers()` has two nested accessors:
Expand Down
1 change: 1 addition & 0 deletions docs/src/api/fake.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ additive concrete-client accessors such as `SandboxTemplates()`:
| `SandboxTemplates()` | `SandboxTemplateInterface` | Full CRUD |
| `Providers()` | `ProviderInterface` | Full CRUD, Ensure |
| `Workspaces()` | `WorkspaceInterface` | Full CRUD, Members |
| `Secrets()` | `SecretInterface` | Full CRUD (values are validated, never stored or returned) |
| `Health()` | `HealthInterface` | Configurable result |
| `Policy()` | `PolicyInterface` | List, GetStatus (draft ops return Unimplemented) |
| `Exec()` | `ExecInterface` | Returns Unimplemented |
Expand Down
6 changes: 6 additions & 0 deletions openshell/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type ClientInterface interface {
Config() ConfigInterface
Policy() PolicyInterface
Workspaces() WorkspaceInterface
Secrets() SecretInterface
Close() error
}

Expand Down Expand Up @@ -68,6 +69,7 @@ type Client struct {
cfg ConfigInterface
policy PolicyInterface
workspaces WorkspaceInterface
secrets SecretInterface
}

// NewClient creates a new SDK client connected to the given gateway.
Expand Down Expand Up @@ -116,6 +118,7 @@ func NewClient(cfg Config) (*Client, error) {
c.cfg = newConfigClient(conn, c.sandboxes)
c.policy = newPolicyClient(conn)
c.workspaces = newWorkspaceClient(conn)
c.secrets = newSecretClient(conn)

return c, nil
}
Expand Down Expand Up @@ -168,6 +171,9 @@ func (c *Client) Policy() PolicyInterface { return c.policy }
// Workspaces returns the workspace management sub-client.
func (c *Client) Workspaces() WorkspaceInterface { return c.workspaces }

// Secrets returns the workspace secret sub-client.
func (c *Client) Secrets() SecretInterface { return c.secrets }

// Close closes the underlying gRPC connection. Safe to call multiple times.
func (c *Client) Close() error {
c.closeOnce.Do(func() {
Expand Down
1 change: 1 addition & 0 deletions openshell/v1/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestNewClient_ValidConfig(t *testing.T) {
assert.NotNil(t, client.Config())
assert.NotNil(t, client.Policy())
assert.NotNil(t, client.Workspaces())
assert.NotNil(t, client.Secrets())

err = client.Close()
assert.NoError(t, err)
Expand Down
17 changes: 17 additions & 0 deletions openshell/v1/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Client struct {
providerStore *objectStore[*types.Provider]
workspaceStore *objectStore[*types.Workspace]
memberStore *objectStore[*types.WorkspaceMember]
secretStore *objectStore[*types.Secret]
sandboxBroadcaster *watchBroadcaster[*types.Sandbox]

sandboxes v1.SandboxInterface
Expand All @@ -38,6 +39,7 @@ type Client struct {
cfg v1.ConfigInterface
policy v1.PolicyInterface
workspaces v1.WorkspaceInterface
secrets v1.SecretInterface

closeOnce sync.Once
closed bool
Expand Down Expand Up @@ -80,6 +82,7 @@ func NewClient(opts ...ClientOption) *Client {
providerStore: newobjectStore(providerName, copyProvider),
workspaceStore: newobjectStore(workspaceName, copyWorkspace),
memberStore: newobjectStore(memberName, copyMember),
secretStore: newobjectStore(secretName, copySecret),
sandboxBroadcaster: newWatchBroadcaster[*types.Sandbox](),
}

Expand All @@ -99,6 +102,7 @@ func NewClient(opts ...ClientOption) *Client {
fc.cfg = newFakeConfigClient(fc.isClosed)
fc.policy = newFakePolicyClient(fc.isClosed)
fc.workspaces = newFakeWorkspaceClient(fc.workspaceStore, fc.memberStore, fc.isClosed)
fc.secrets = newFakeSecretClient(fc.secretStore, fc.isClosed)

options.Apply(fc, opts)

Expand Down Expand Up @@ -165,6 +169,9 @@ func (fc *Client) Policy() v1.PolicyInterface { return fc.policy }
// Workspaces returns the workspace management sub-client.
func (fc *Client) Workspaces() v1.WorkspaceInterface { return fc.workspaces }

// Secrets returns the workspace secret sub-client.
func (fc *Client) Secrets() v1.SecretInterface { return fc.secrets }

// Close marks the client as closed, stops all active watchers, and causes
// subsequent sub-client calls to return Unavailable. Safe to call multiple
// times.
Expand Down Expand Up @@ -229,6 +236,16 @@ func (fc *Client) AddMember(workspace string, m *types.WorkspaceMember) {
fc.memberStore.Insert(workspace, m)
}

// AddSecret inserts workspace secret metadata directly into the store without
// triggering any side effects. This is intended for pre-seeding test fixtures
// before the test begins. The secret is deep-copied on insert.
func (fc *Client) AddSecret(workspace string, s *types.Secret) {
if s == nil {
return
}
fc.secretStore.Insert(workspace, s)
}

// AddGlobalRevision adds a gateway-global policy revision for test seeding.
// Global revisions are returned by Policy().List() and Policy().GetStatus()
// when the global option is enabled.
Expand Down
8 changes: 8 additions & 0 deletions openshell/v1/fake/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ func (c *fakeRefreshClient) Rotate(_ context.Context, _, _, _ string) (*types.Re
return nil, &types.StatusError{Code: types.ErrorUnimplemented, Message: "Rotate is not supported by the fake client"}
}

// InlineSecrets returns Unimplemented.
func (c *fakeRefreshClient) InlineSecrets(_ context.Context, _, _, _ string) (*types.RefreshStatus, error) {
if c.closedFunc() {
return nil, &types.StatusError{Code: types.ErrorUnavailable, Message: "client is closed"}
}
return nil, &types.StatusError{Code: types.ErrorUnimplemented, Message: "InlineSecrets is not supported by the fake client"}
}

// Delete returns Unimplemented.
func (c *fakeRefreshClient) Delete(_ context.Context, _, _, _ string, _ ...v1.DeleteOptions) (*types.DeletionResult, error) {
if c.closedFunc() {
Expand Down
14 changes: 14 additions & 0 deletions openshell/v1/fake/refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func TestFakeRefresh_Rotate_ReturnsUnimplemented(t *testing.T) {
assert.True(t, types.IsUnimplemented(err))
}

func TestFakeRefresh_InlineSecrets_ReturnsUnimplemented(t *testing.T) {
c := newFakeRefreshClient(func() bool { return false })
_, err := c.InlineSecrets(context.Background(), "default", "provider-1", "cred-1")
require.Error(t, err)
assert.True(t, types.IsUnimplemented(err))
}

func TestFakeRefresh_Delete_ReturnsUnimplemented(t *testing.T) {
c := newFakeRefreshClient(func() bool { return false })
_, err := c.Delete(context.Background(), "default", "provider-1", "cred-1")
Expand Down Expand Up @@ -70,6 +77,13 @@ func TestFakeRefresh_Rotate_ClosedReturnsUnavailable(t *testing.T) {
assert.True(t, types.IsUnavailable(err))
}

func TestFakeRefresh_InlineSecrets_ClosedReturnsUnavailable(t *testing.T) {
c := newFakeRefreshClient(func() bool { return true })
_, err := c.InlineSecrets(context.Background(), "default", "provider-1", "cred-1")
require.Error(t, err)
assert.True(t, types.IsUnavailable(err))
}

func TestFakeRefresh_Delete_ClosedReturnsUnavailable(t *testing.T) {
c := newFakeRefreshClient(func() bool { return true })
_, err := c.Delete(context.Background(), "default", "provider-1", "cred-1")
Expand Down
208 changes: 208 additions & 0 deletions openshell/v1/fake/secret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package fake

import (
"context"
"slices"
"strings"
"time"

v1 "github.com/ai-openshell/sdk-go/openshell/v1"
"github.com/ai-openshell/sdk-go/openshell/v1/types"
)

// secretName extracts the name from a Secret pointer for use as the
// objectStore key function.
func secretName(s *types.Secret) string {
return s.Name
}

// copySecret returns a deep copy of a Secret pointer. All maps and slices are
// duplicated to prevent aliasing.
func copySecret(s *types.Secret) *types.Secret {
if s == nil {
return nil
}
cp := *s
cp.Labels = copyStringMap(s.Labels)
cp.Annotations = copyStringMap(s.Annotations)
if s.DeletionTimestamp != nil {
t := *s.DeletionTimestamp
cp.DeletionTimestamp = &t
}
cp.RefreshUse = copySecretRefreshUse(s.RefreshUse)
return &cp
}

func copySecretRefreshUse(u *types.SecretRefreshUse) *types.SecretRefreshUse {
if u == nil {
return nil
}
return &types.SecretRefreshUse{AllowedEndpoints: copyStringSlice(u.AllowedEndpoints)}
}

// fakeSecretClient implements v1.SecretInterface backed by an in-memory
// objectStore. Like the gateway, it keeps secret values write-only: a value is
// validated and then discarded, and no method returns one.
type fakeSecretClient struct {
store *objectStore[*types.Secret]
closedFunc func() bool
}

var _ v1.SecretInterface = (*fakeSecretClient)(nil)

// newFakeSecretClient creates a new fakeSecretClient.
func newFakeSecretClient(
store *objectStore[*types.Secret],
closedFunc func() bool,
) *fakeSecretClient {
return &fakeSecretClient{
store: store,
closedFunc: closedFunc,
}
}

// Create adds a new secret. CreatedAt, ValueUpdatedAt and ResourceVersion are
// set automatically.
func (c *fakeSecretClient) Create(_ context.Context, workspace string, secret *types.SecretCreate) (*types.Secret, error) {
if c.closedFunc() {
return nil, &types.StatusError{Code: types.ErrorUnavailable, Message: "client is closed"}
}
if secret == nil {
return nil, &types.StatusError{Code: types.ErrorInvalidArgument, Message: "secret must not be nil"}
}
if secret.Name == "" {
return nil, &types.StatusError{Code: types.ErrorInvalidArgument, Message: "secret name is required"}
}
if secret.Value == "" {
return nil, &types.StatusError{Code: types.ErrorInvalidArgument, Message: "secret value must not be empty"}
}
if secret.RefreshUse != nil && len(secret.RefreshUse.AllowedEndpoints) == 0 {
return nil, &types.StatusError{Code: types.ErrorInvalidArgument, Message: "refresh use allowed endpoints must not be empty"}
}

now := time.Now()
return c.store.Create(workspace, &types.Secret{
Name: secret.Name,
CreatedAt: now,
Labels: copyStringMap(secret.Labels),
ResourceVersion: 1,
Workspace: workspace,
Description: secret.Description,
RefreshUse: copySecretRefreshUse(secret.RefreshUse),
ValueUpdatedAt: now,
})
}

// Get retrieves secret metadata by name.
func (c *fakeSecretClient) Get(_ context.Context, workspace, name string) (*types.Secret, error) {
if c.closedFunc() {
return nil, &types.StatusError{Code: types.ErrorUnavailable, Message: "client is closed"}
}
return c.store.Get(workspace, name)
}

// List returns a lazy pager over the secrets of a workspace. Like the gateway,
// it accepts only a named workspace.
func (c *fakeSecretClient) List(workspace string, opts ...v1.ListOptions) (*v1.Pager[*types.Secret], error) {
if c.closedFunc() {
return nil, &types.StatusError{Code: types.ErrorUnavailable, Message: "client is closed"}
}
var options v1.ListOptions
if len(opts) > 0 {
options = opts[0]
if options.PageSize < 0 {
return nil, &types.StatusError{Code: types.ErrorInvalidArgument, Message: "page size must not be negative"}
}
}
if options.AllWorkspaces {
return nil, &types.StatusError{Code: types.ErrorInvalidArgument, Message: "secrets can only be listed in a named workspace"}
}
secrets := c.store.List(workspace)
slices.SortFunc(secrets, compareSecretsForList)
secrets, err := filterSecretsByLabelSelector(secrets, options.LabelSelector)
if err != nil {
return nil, err
}
return newSlicePager(secrets, options.PageSize, options.PageToken)
}

func (c *fakeSecretClient) ListAll(ctx context.Context, workspace string, opts ...v1.ListOptions) ([]*types.Secret, error) {
pager, err := c.List(workspace, opts...)
if err != nil {
return nil, err
}
return pager.All(ctx)
}

// Update applies the fields the update names and leaves the rest unchanged.
// ResourceVersion is incremented automatically, and ValueUpdatedAt moves only
// when the update carries a value.
func (c *fakeSecretClient) Update(_ context.Context, workspace, name string, update *types.SecretUpdate) (*types.Secret, error) {
if c.closedFunc() {
return nil, &types.StatusError{Code: types.ErrorUnavailable, Message: "client is closed"}
}
if update == nil {
return nil, &types.StatusError{Code: types.ErrorInvalidArgument, Message: "update must not be nil"}
}
if update.Value != nil && *update.Value == "" {
return nil, &types.StatusError{Code: types.ErrorInvalidArgument, Message: "secret value must not be empty"}
}

s, err := c.store.Get(workspace, name)
if err != nil {
return nil, err
}
if update.ExpectedResourceVersion != 0 && update.ExpectedResourceVersion != s.ResourceVersion {
return nil, &types.StatusError{Code: types.ErrorConflict, Message: "secret resource version does not match"}
}

if update.Value != nil {
s.ValueUpdatedAt = time.Now()
}
if update.Description != nil {
s.Description = *update.Description
}
if update.Labels != nil {
s.Labels = copyStringMap(update.Labels.Labels)
}
s.ResourceVersion++

return c.store.Update(workspace, s)
}

// Delete removes a secret by name.
func (c *fakeSecretClient) Delete(_ context.Context, workspace, name string, opts ...v1.DeleteOptions) (*types.DeletionResult, error) {
if c.closedFunc() {
return nil, &types.StatusError{Code: types.ErrorUnavailable, Message: "client is closed"}
}
_, existed := c.store.DeleteAndGet(workspace, name)
return deletionResult(existed, "", opts)
}

func filterSecretsByLabelSelector(secrets []*types.Secret, selector string) ([]*types.Secret, error) {
selector = strings.TrimSpace(selector)
if selector == "" {
return secrets, nil
}
labels, err := parseSimpleLabelSelector(selector)
if err != nil {
return nil, err
}
filtered := make([]*types.Secret, 0, len(secrets))
for _, secret := range secrets {
if labelsMatchSelector(secret.Labels, labels) {
filtered = append(filtered, secret)
}
}
return filtered, nil
}

func compareSecretsForList(a, b *types.Secret) int {
if c := a.CreatedAt.Compare(b.CreatedAt); c != 0 {
return c
}
return strings.Compare(a.Name, b.Name)
}
Loading