Files
coder/coderd/agentapi/manifest_internal_test.go
T
Steven Masley 03cba25a63 feat: list a workspace build's secrets in coder secret list (#30388)
`coder secret list` now shows the current workspace build's secrets next to your user secrets, and which user secrets they replace. Part of [RELAY-7](https://linear.app/codercom/issue/RELAY-7/agent-relay-deliver-provider-credentials-via-coder-secrets-instead-of).

**Stack** (design decisions in #30248)
1. #30248 Database schema, queries, and RBAC
2. #30249 Encryption at rest
3. #30250 Set secrets on workspace builds
4. #30251 Deliver secrets to the agent
5. #30252 Docs
6. **#30388 List a build's secrets (this PR)**
7. #30459 Audit a build's secrets

- New `GET /workspacebuilds/{workspacebuild}/secrets` lists a build's secrets (metadata only, never values) and which user secret each one replaces.
- Inside a workspace, `coder secret list` includes the current build's secrets. Outside a workspace, use `--workspace` or `--build-id`.
- The agent now exposes `CODER_WORKSPACE_BUILD_ID`, so the CLI knows which build it is running in.
- The agent manifest and the new endpoint use one shared resolver, so they always agree on which secret wins.
- The user secret API is unchanged.

<details>
<summary>Implementation details</summary>

- The manifest carries `workspace_build_id`; the agent sets it as `CODER_WORKSPACE_BUILD_ID`.
- `coderd/workspacesecrets.Resolve(user, build, policy)` decides which secret is delivered on each env var and file. The manifest no longer relies on entry order.
- `codersdk.WorkspaceSecret` embeds `UserSecret` and adds `workspace_build_id`, `ephemeral`, `cleared_at`, `env_replaces`, and `file_replaces`. Secrets whose values a later build cleared are still listed, with `cleared_at` set.
- CLI columns: `source` and `replaced by` are shown by default only when a build is included; `ephemeral` is opt-in. The CLI makes two requests: user secrets, then build secrets.
- Agents that predate the build ID fall back to `CODER_WORKSPACE_ID` and its latest build.

</details>

<details>
<summary>Decision log</summary>

(existing decision log, unchanged)

</details>

---

*Created by Coder Agents on behalf of @Emyrk.*
2026-10-15 17:40:00 -05:00

160 lines
4.8 KiB
Go

package agentapi
import (
"database/sql"
"fmt"
"net/url"
"testing"
"github.com/stretchr/testify/require"
agentproto "github.com/coder/coder/v2/agent/proto"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
"github.com/coder/coder/v2/coderd/workspacesecrets"
)
func Test_dbSecretsToProto(t *testing.T) {
t.Parallel()
secrets := []database.UserSecret{
{Name: "env-only", EnvName: "ENV_ONLY", Value: "env-val", Enabled: true},
{Name: "file-only", FilePath: "~/.ssh/id_rsa", Value: "file-val", Enabled: true},
{Name: "dual", EnvName: "DUAL_ENV", FilePath: "/etc/dual", Value: "dual-val", Enabled: true},
{Name: "disabled", EnvName: "DISABLED_ENV", FilePath: "/etc/disabled", Value: "disabled-val"},
// ws-shared takes this secret's env var; its file is still written.
{Name: "overridden", EnvName: "SHARED_ENV", FilePath: "/etc/overridden", Value: "user-val", Enabled: true},
}
workspaceSecrets := []database.WorkspaceSecret{
{Name: "ws-env", EnvName: "WS_ENV", Value: sql.NullString{String: "ws-val", Valid: true}},
{Name: "ws-file", FilePath: "/etc/ws", Value: sql.NullString{String: "ws-file-val", Valid: true}},
{Name: "ws-cleared", EnvName: "WS_CLEARED"},
{Name: "ws-shared", EnvName: "SHARED_ENV", Value: sql.NullString{String: "ws-shared-val", Valid: true}},
}
cases := []struct {
name string
policy workspacesecrets.FilePathPolicy
expected []*agentproto.WorkspaceSecret
}{
{
name: "FilePathAllowed",
policy: workspacesecrets.FilePathAllowed,
expected: []*agentproto.WorkspaceSecret{
{EnvName: "ENV_ONLY", Value: []byte("env-val")},
{FilePath: "~/.ssh/id_rsa", Value: []byte("file-val")},
{EnvName: "DUAL_ENV", FilePath: "/etc/dual", Value: []byte("dual-val")},
{FilePath: "/etc/overridden", Value: []byte("user-val")},
{EnvName: "WS_ENV", Value: []byte("ws-val")},
{FilePath: "/etc/ws", Value: []byte("ws-file-val")},
{EnvName: "SHARED_ENV", Value: []byte("ws-shared-val")},
},
},
{
name: "FilePathBlocked",
policy: workspacesecrets.FilePathBlocked,
expected: []*agentproto.WorkspaceSecret{
{EnvName: "ENV_ONLY", Value: []byte("env-val")},
{EnvName: "DUAL_ENV", Value: []byte("dual-val")},
{EnvName: "WS_ENV", Value: []byte("ws-val")},
{EnvName: "SHARED_ENV", Value: []byte("ws-shared-val")},
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
t.Parallel()
got := dbSecretsToProto(secrets, workspaceSecrets, c.policy)
require.Len(t, got, len(c.expected))
for i, want := range c.expected {
require.Equal(t, want.EnvName, got[i].EnvName, "secret %d env_name", i)
require.Equal(t, want.FilePath, got[i].FilePath, "secret %d file_path", i)
require.Equal(t, want.Value, got[i].Value, "secret %d value", i)
}
})
}
}
func Test_vscodeProxyURI(t *testing.T) {
t.Parallel()
coderAccessURL, err := url.Parse("https://coder.com")
require.NoError(t, err)
accessURLWithPort, err := url.Parse("https://coder.com:8080")
require.NoError(t, err)
basicApp := appurl.ApplicationURL{
Prefix: "prefix",
AppSlugOrPort: "slug",
AgentName: "agent",
WorkspaceName: "workspace",
Username: "user",
}
cases := []struct {
Name string
App appurl.ApplicationURL
AccessURL *url.URL
AppHostname string
Expected string
}{
{
Name: "NoHostname",
AccessURL: coderAccessURL,
AppHostname: "",
App: basicApp,
Expected: "",
},
{
Name: "NoHostnameAccessURLPort",
AccessURL: accessURLWithPort,
AppHostname: "",
App: basicApp,
Expected: "",
},
{
Name: "Hostname",
AccessURL: coderAccessURL,
AppHostname: "*.apps.coder.com",
App: basicApp,
Expected: fmt.Sprintf("https://%s.apps.coder.com", basicApp.String()),
},
{
Name: "HostnameWithAccessURLPort",
AccessURL: accessURLWithPort,
AppHostname: "*.apps.coder.com",
App: basicApp,
Expected: fmt.Sprintf("https://%s.apps.coder.com:%s", basicApp.String(), accessURLWithPort.Port()),
},
{
Name: "HostnameWithPort",
AccessURL: coderAccessURL,
AppHostname: "*.apps.coder.com:4444",
App: basicApp,
Expected: fmt.Sprintf("https://%s.apps.coder.com:%s", basicApp.String(), "4444"),
},
{
// Port from hostname takes precedence over access url port.
Name: "HostnameWithPortAccessURLWithPort",
AccessURL: accessURLWithPort,
AppHostname: "*.apps.coder.com:4444",
App: basicApp,
Expected: fmt.Sprintf("https://%s.apps.coder.com:%s", basicApp.String(), "4444"),
},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
t.Parallel()
require.NotNilf(t, c.AccessURL, "AccessURL is required")
output := vscodeProxyURI(c.App, c.AccessURL, c.AppHostname)
require.Equal(t, c.Expected, output)
})
}
}