Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ed8af65

Browse files
committed
Merge remote-tracking branch 'origin/main' into ssncferreira/poc-prebuild-rbac-authz
2 parents f811778 + 4ceb549 commit ed8af65

File tree

104 files changed

+4799
-1118
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+4799
-1118
lines changed

agent/agent.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,7 @@ func (a *agent) updateCommandEnv(current []string) (updated []string, err error)
12971297
"CODER": "true",
12981298
"CODER_WORKSPACE_NAME": manifest.WorkspaceName,
12991299
"CODER_WORKSPACE_AGENT_NAME": manifest.AgentName,
1300+
"CODER_WORKSPACE_OWNER_NAME": manifest.OwnerName,
13001301

13011302
// Specific Coder subcommands require the agent token exposed!
13021303
"CODER_AGENT_TOKEN": *a.sessionToken.Load(),

agent/agent_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ func TestAgent_EnvironmentVariableExpansion(t *testing.T) {
12091209
func TestAgent_CoderEnvVars(t *testing.T) {
12101210
t.Parallel()
12111211

1212-
for _, key := range []string{"CODER", "CODER_WORKSPACE_NAME", "CODER_WORKSPACE_AGENT_NAME"} {
1212+
for _, key := range []string{"CODER", "CODER_WORKSPACE_NAME", "CODER_WORKSPACE_OWNER_NAME", "CODER_WORKSPACE_AGENT_NAME"} {
12131213
key := key
12141214
t.Run(key, func(t *testing.T) {
12151215
t.Parallel()
@@ -3079,6 +3079,9 @@ func setupAgent(t *testing.T, metadata agentsdk.Manifest, ptyTimeout time.Durati
30793079
if metadata.WorkspaceName == "" {
30803080
metadata.WorkspaceName = "test-workspace"
30813081
}
3082+
if metadata.OwnerName == "" {
3083+
metadata.OwnerName = "test-user"
3084+
}
30823085
if metadata.WorkspaceID == uuid.Nil {
30833086
metadata.WorkspaceID = uuid.New()
30843087
}

agent/agentcontainers/acmock/acmock.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

agent/agentcontainers/api.go

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/coder/coder/v2/coderd/httpapi"
2929
"github.com/coder/coder/v2/codersdk"
3030
"github.com/coder/coder/v2/codersdk/agentsdk"
31+
"github.com/coder/coder/v2/provisioner"
3132
"github.com/coder/quartz"
3233
)
3334

@@ -64,6 +65,9 @@ type API struct {
6465
subAgentURL string
6566
subAgentEnv []string
6667

68+
ownerName string
69+
workspaceName string
70+
6771
mu sync.RWMutex
6872
closed bool
6973
containers codersdk.WorkspaceAgentListContainersResponse // Output from the last list operation.
@@ -153,6 +157,15 @@ func WithSubAgentEnv(env ...string) Option {
153157
}
154158
}
155159

160+
// WithManifestInfo sets the owner name, and workspace name
161+
// for the sub-agent.
162+
func WithManifestInfo(owner, workspace string) Option {
163+
return func(api *API) {
164+
api.ownerName = owner
165+
api.workspaceName = workspace
166+
}
167+
}
168+
156169
// WithDevcontainers sets the known devcontainers for the API. This
157170
// allows the API to be aware of devcontainers defined in the workspace
158171
// agent manifest.
@@ -1051,6 +1064,10 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
10511064
)
10521065
return nil
10531066
}
1067+
if proc.agent.ID == uuid.Nil {
1068+
proc.agent.Architecture = arch
1069+
}
1070+
10541071
agentBinaryPath, err := os.Executable()
10551072
if err != nil {
10561073
return xerrors.Errorf("get agent binary path: %w", err)
@@ -1095,6 +1112,8 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
10951112

10961113
subAgentConfig := proc.agent.CloneConfig(dc)
10971114
if proc.agent.ID == uuid.Nil || maybeRecreateSubAgent {
1115+
subAgentConfig.Architecture = arch
1116+
10981117
// Detect workspace folder by executing `pwd` in the container.
10991118
// NOTE(mafredri): This is a quick and dirty way to detect the
11001119
// workspace folder inside the container. In the future we will
@@ -1127,9 +1146,50 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
11271146
codersdk.DisplayAppPortForward: true,
11281147
}
11291148

1130-
if config, err := api.dccli.ReadConfig(ctx, dc.WorkspaceFolder, dc.ConfigPath); err != nil {
1131-
api.logger.Error(ctx, "unable to read devcontainer config", slog.Error(err))
1132-
} else {
1149+
var appsWithPossibleDuplicates []SubAgentApp
1150+
1151+
if err := func() error {
1152+
var (
1153+
config DevcontainerConfig
1154+
configOutdated bool
1155+
)
1156+
1157+
readConfig := func() (DevcontainerConfig, error) {
1158+
return api.dccli.ReadConfig(ctx, dc.WorkspaceFolder, dc.ConfigPath, []string{
1159+
fmt.Sprintf("CODER_WORKSPACE_AGENT_NAME=%s", subAgentConfig.Name),
1160+
fmt.Sprintf("CODER_WORKSPACE_OWNER_NAME=%s", api.ownerName),
1161+
fmt.Sprintf("CODER_WORKSPACE_NAME=%s", api.workspaceName),
1162+
fmt.Sprintf("CODER_URL=%s", api.subAgentURL),
1163+
})
1164+
}
1165+
1166+
if config, err = readConfig(); err != nil {
1167+
return err
1168+
}
1169+
1170+
// NOTE(DanielleMaywood):
1171+
// We only want to take an agent name specified in the root customization layer.
1172+
// This restricts the ability for a feature to specify the agent name. We may revisit
1173+
// this in the future, but for now we want to restrict this behavior.
1174+
if name := config.Configuration.Customizations.Coder.Name; name != "" {
1175+
// We only want to pick this name if it is a valid name.
1176+
if provisioner.AgentNameRegex.Match([]byte(name)) {
1177+
subAgentConfig.Name = name
1178+
configOutdated = true
1179+
} else {
1180+
logger.Warn(ctx, "invalid name in devcontainer customization, ignoring",
1181+
slog.F("name", name),
1182+
slog.F("regex", provisioner.AgentNameRegex.String()),
1183+
)
1184+
}
1185+
}
1186+
1187+
if configOutdated {
1188+
if config, err = readConfig(); err != nil {
1189+
return err
1190+
}
1191+
}
1192+
11331193
coderCustomization := config.MergedConfiguration.Customizations.Coder
11341194

11351195
for _, customization := range coderCustomization {
@@ -1143,7 +1203,13 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
11431203
}
11441204
displayAppsMap[app] = enabled
11451205
}
1206+
1207+
appsWithPossibleDuplicates = append(appsWithPossibleDuplicates, customization.Apps...)
11461208
}
1209+
1210+
return nil
1211+
}(); err != nil {
1212+
api.logger.Error(ctx, "unable to read devcontainer config", slog.Error(err))
11471213
}
11481214

11491215
displayApps := make([]codersdk.DisplayApp, 0, len(displayAppsMap))
@@ -1154,7 +1220,27 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
11541220
}
11551221
slices.Sort(displayApps)
11561222

1223+
appSlugs := make(map[string]struct{})
1224+
apps := make([]SubAgentApp, 0, len(appsWithPossibleDuplicates))
1225+
1226+
// We want to deduplicate the apps based on their slugs here.
1227+
// As we want to prioritize later apps, we will walk through this
1228+
// backwards.
1229+
for _, app := range slices.Backward(appsWithPossibleDuplicates) {
1230+
if _, slugAlreadyExists := appSlugs[app.Slug]; slugAlreadyExists {
1231+
continue
1232+
}
1233+
1234+
appSlugs[app.Slug] = struct{}{}
1235+
apps = append(apps, app)
1236+
}
1237+
1238+
// Apps is currently in reverse order here, so by reversing it we restore
1239+
// it to the original order.
1240+
slices.Reverse(apps)
1241+
11571242
subAgentConfig.DisplayApps = displayApps
1243+
subAgentConfig.Apps = apps
11581244
}
11591245

11601246
deleteSubAgent := proc.agent.ID != uuid.Nil && maybeRecreateSubAgent && !proc.agent.EqualConfig(subAgentConfig)

0 commit comments

Comments
 (0)