Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
refactor: include configuration and merged configuration
We want to be able to pick customizations that are only specified in the
top-level customizations stanza. That is, we do not want to accidentally
pick something specified in a feature (such as the agent name).

This refactor picks the agent name from configuration instead of the
merged configuration to ensure a feature cannot influence it.
  • Loading branch information
DanielleMaywood committed Jun 19, 2025
commit b738124e4760c4b48e285c54a20ecb6a12c127d4
6 changes: 2 additions & 4 deletions agent/agentcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1177,12 +1177,10 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
}

// NOTE(DanielleMaywood):
// We only want to take an agent name specified in the very last customization layer.
// We only want to take an agent name specified in the root customization layer.
// This restricts the ability for a feature to specify the agent name. We may revisit
// this in the future, but for now we want to restrict this behavior.
if len(coderCustomization) > 0 {
name := coderCustomization[len(coderCustomization)-1].Name

if name := config.Configuration.Customizations.Coder.Name; name != "" {
// We only want to pick this name if it is a valid name.
if provisioner.AgentNameRegex.Match([]byte(name)) {
possibleAgentName = name
Expand Down
56 changes: 30 additions & 26 deletions agent/agentcontainers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1556,17 +1556,18 @@ func TestAPI(t *testing.T) {
}

tests := []struct {
name string
customization []agentcontainers.CoderCustomization
afterCreate func(t *testing.T, subAgent agentcontainers.SubAgent)
name string
customization agentcontainers.CoderCustomization
mergedCustomizations []agentcontainers.CoderCustomization
afterCreate func(t *testing.T, subAgent agentcontainers.SubAgent)
}{
{
name: "WithoutCustomization",
customization: nil,
name: "WithoutCustomization",
mergedCustomizations: nil,
},
{
name: "WithDefaultDisplayApps",
customization: []agentcontainers.CoderCustomization{},
name: "WithDefaultDisplayApps",
mergedCustomizations: []agentcontainers.CoderCustomization{},
afterCreate: func(t *testing.T, subAgent agentcontainers.SubAgent) {
require.Len(t, subAgent.DisplayApps, 4)
assert.Contains(t, subAgent.DisplayApps, codersdk.DisplayAppVSCodeDesktop)
Expand All @@ -1577,7 +1578,7 @@ func TestAPI(t *testing.T) {
},
{
name: "WithAllDisplayApps",
customization: []agentcontainers.CoderCustomization{
mergedCustomizations: []agentcontainers.CoderCustomization{
{
DisplayApps: map[codersdk.DisplayApp]bool{
codersdk.DisplayAppSSH: true,
Expand All @@ -1599,7 +1600,7 @@ func TestAPI(t *testing.T) {
},
{
name: "WithSomeDisplayAppsDisabled",
customization: []agentcontainers.CoderCustomization{
mergedCustomizations: []agentcontainers.CoderCustomization{
{
DisplayApps: map[codersdk.DisplayApp]bool{
codersdk.DisplayAppSSH: false,
Expand Down Expand Up @@ -1631,7 +1632,7 @@ func TestAPI(t *testing.T) {
},
{
name: "WithApps",
customization: []agentcontainers.CoderCustomization{
mergedCustomizations: []agentcontainers.CoderCustomization{
{
Apps: []agentcontainers.SubAgentApp{
{
Expand Down Expand Up @@ -1699,7 +1700,7 @@ func TestAPI(t *testing.T) {
},
{
name: "AppDeduplication",
customization: []agentcontainers.CoderCustomization{
mergedCustomizations: []agentcontainers.CoderCustomization{
{
Apps: []agentcontainers.SubAgentApp{
{
Expand Down Expand Up @@ -1741,47 +1742,45 @@ func TestAPI(t *testing.T) {
},
{
name: "Name",
customization: []agentcontainers.CoderCustomization{
customization: agentcontainers.CoderCustomization{
Name: "this-name",
},
mergedCustomizations: []agentcontainers.CoderCustomization{
{
Name: "not-this-name",
},
{
Name: "custom-name",
Name: "or-this-name",
},
},
afterCreate: func(t *testing.T, subAgent agentcontainers.SubAgent) {
require.Equal(t, "custom-name", subAgent.Name)
require.Equal(t, "this-name", subAgent.Name)
},
},
{
name: "NameIsOnlyUsedWhenInLastLayer",
customization: []agentcontainers.CoderCustomization{
name: "NameIsOnlyUsedFromRoot",
mergedCustomizations: []agentcontainers.CoderCustomization{
{
Name: "custom-name",
},
{},
},
afterCreate: func(t *testing.T, subAgent agentcontainers.SubAgent) {
require.NotEqual(t, "custom-name", subAgent.Name)
},
},
{
name: "EmptyNameIsIgnored",
customization: []agentcontainers.CoderCustomization{
{
Name: "",
},
customization: agentcontainers.CoderCustomization{
Name: "",
},
afterCreate: func(t *testing.T, subAgent agentcontainers.SubAgent) {
require.NotEmpty(t, subAgent.Name)
},
},
{
name: "InvalidNameIsIgnored",
customization: []agentcontainers.CoderCustomization{
{
Name: "This--Is_An_Invalid--Name",
},
customization: agentcontainers.CoderCustomization{
Name: "This--Is_An_Invalid--Name",
},
afterCreate: func(t *testing.T, subAgent agentcontainers.SubAgent) {
require.NotEqual(t, "This--Is_An_Invalid--Name", subAgent.Name)
Expand All @@ -1804,11 +1803,16 @@ func TestAPI(t *testing.T) {
}
fDCCLI = &fakeDevcontainerCLI{
readConfig: agentcontainers.DevcontainerConfig{
MergedConfiguration: agentcontainers.DevcontainerConfiguration{
Configuration: agentcontainers.DevcontainerConfiguration{
Customizations: agentcontainers.DevcontainerCustomizations{
Coder: tt.customization,
},
},
MergedConfiguration: agentcontainers.DevcontainerMergedConfiguration{
Customizations: agentcontainers.DevcontainerMergedCustomizations{
Coder: tt.mergedCustomizations,
},
},
},
execErrC: make(chan func(cmd string, args ...string) error, 1),
}
Expand Down
13 changes: 11 additions & 2 deletions agent/agentcontainers/devcontainercli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,24 @@ import (
// Unfortunately we cannot make use of `dcspec` as the output doesn't appear to
// match.
type DevcontainerConfig struct {
MergedConfiguration DevcontainerConfiguration `json:"mergedConfiguration"`
MergedConfiguration DevcontainerMergedConfiguration `json:"mergedConfiguration"`
Configuration DevcontainerConfiguration `json:"configuration"`
}

type DevcontainerMergedConfiguration struct {
Customizations DevcontainerMergedCustomizations `json:"customizations,omitempty"`
}

type DevcontainerMergedCustomizations struct {
Coder []CoderCustomization `json:"coder,omitempty"`
}

type DevcontainerConfiguration struct {
Customizations DevcontainerCustomizations `json:"customizations,omitempty"`
}

type DevcontainerCustomizations struct {
Coder []CoderCustomization `json:"coder,omitempty"`
Coder CoderCustomization `json:"coder,omitempty"`
}

type CoderCustomization struct {
Expand Down
8 changes: 4 additions & 4 deletions agent/agentcontainers/devcontainercli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ func TestDevcontainerCLI_ArgsAndParsing(t *testing.T) {
wantArgs: "read-configuration --include-merged-configuration --workspace-folder /test/workspace",
wantError: false,
wantConfig: agentcontainers.DevcontainerConfig{
MergedConfiguration: agentcontainers.DevcontainerConfiguration{
Customizations: agentcontainers.DevcontainerCustomizations{
MergedConfiguration: agentcontainers.DevcontainerMergedConfiguration{
Customizations: agentcontainers.DevcontainerMergedCustomizations{
Coder: []agentcontainers.CoderCustomization{
{
DisplayApps: map[codersdk.DisplayApp]bool{
Expand All @@ -284,8 +284,8 @@ func TestDevcontainerCLI_ArgsAndParsing(t *testing.T) {
wantArgs: "read-configuration --include-merged-configuration --workspace-folder /test/workspace --config /test/config.json",
wantError: false,
wantConfig: agentcontainers.DevcontainerConfig{
MergedConfiguration: agentcontainers.DevcontainerConfiguration{
Customizations: agentcontainers.DevcontainerCustomizations{
MergedConfiguration: agentcontainers.DevcontainerMergedConfiguration{
Customizations: agentcontainers.DevcontainerMergedCustomizations{
Coder: nil,
},
},
Expand Down
Loading