Skip to content

Commit 825f698

Browse files
committed
Merge branch 'main' into fixmetadataorder
2 parents 0f3da2c + c0b251a commit 825f698

12 files changed

+57
-43
lines changed

.github/workflows/coder.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ jobs:
338338
else
339339
echo ::set-output name=cover::false
340340
fi
341-
gotestsum --junitfile="gotests.xml" --jsonfile="gotestsum.json" --packages="./..." --debug -- -parallel=8 -timeout=3m -short -failfast $COVERAGE_FLAGS
341+
gotestsum --junitfile="gotests.xml" --jsonfile="gotestsum.json" --packages="./..." --debug -- -parallel=8 -timeout=5m -short -failfast $COVERAGE_FLAGS
342342
ret=$?
343343
if ((ret)); then
344344
# Eternalize test timeout logs because "re-run failed" erases

agent/agent.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,25 @@ func (a *agent) run(ctx context.Context) error {
226226
_ = network.Close()
227227
return xerrors.New("agent is closed")
228228
}
229+
230+
// Report statistics from the created network.
231+
cl, err := a.client.AgentReportStats(ctx, a.logger, func() *codersdk.AgentStats {
232+
stats := network.ExtractTrafficStats()
233+
return convertAgentStats(stats)
234+
})
235+
if err != nil {
236+
a.logger.Error(ctx, "report stats", slog.Error(err))
237+
} else {
238+
if err = a.trackConnGoroutine(func() {
239+
// This is OK because the agent never re-creates the tailnet
240+
// and the only shutdown indicator is agent.Close().
241+
<-a.closed
242+
_ = cl.Close()
243+
}); err != nil {
244+
a.logger.Debug(ctx, "report stats goroutine", slog.Error(err))
245+
_ = cl.Close()
246+
}
247+
}
229248
} else {
230249
// Update the DERP map!
231250
network.SetDERPMap(metadata.DERPMap)
@@ -561,28 +580,6 @@ func (a *agent) init(ctx context.Context) {
561580
}
562581

563582
go a.runLoop(ctx)
564-
cl, err := a.client.AgentReportStats(ctx, a.logger, func() *codersdk.AgentStats {
565-
stats := map[netlogtype.Connection]netlogtype.Counts{}
566-
a.closeMutex.Lock()
567-
if a.network != nil {
568-
stats = a.network.ExtractTrafficStats()
569-
}
570-
a.closeMutex.Unlock()
571-
return convertAgentStats(stats)
572-
})
573-
if err != nil {
574-
a.logger.Error(ctx, "report stats", slog.Error(err))
575-
return
576-
}
577-
578-
if err = a.trackConnGoroutine(func() {
579-
<-a.closed
580-
_ = cl.Close()
581-
}); err != nil {
582-
a.logger.Error(ctx, "report stats goroutine", slog.Error(err))
583-
_ = cl.Close()
584-
return
585-
}
586583
}
587584

588585
func convertAgentStats(counts map[netlogtype.Connection]netlogtype.Counts) *codersdk.AgentStats {

coderd/database/migrations/000086_no_org_admins.down.sql

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
UPDATE
2+
organization_members
3+
SET
4+
roles = ARRAY [] :: text[]
5+
WHERE
6+
'organization-admin:'||organization_id = ANY(roles);

coderd/database/migrations/000086_resource_metadata_order.down.sql

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ALTER TABLE workspace_resource_metadata DROP COLUMN id;
2+
ALTER TABLE workspace_resource_metadata DROP CONSTRAINT workspace_resource_metadata_name;
3+
ALTER TABLE workspace_resource_metadata ADD CONSTRAINT workspace_resource_metadata_pkey PRIMARY KEY (workspace_resource_id, key);
4+

coderd/httpmw/workspaceagent.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,27 @@ func ExtractWorkspaceAgent(db database.Store) func(http.Handler) http.Handler {
3030
return func(next http.Handler) http.Handler {
3131
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
3232
ctx := r.Context()
33-
cookieValue := apiTokenFromRequest(r)
34-
if cookieValue == "" {
33+
tokenValue := apiTokenFromRequest(r)
34+
if tokenValue == "" {
3535
httpapi.Write(ctx, rw, http.StatusUnauthorized, codersdk.Response{
3636
Message: fmt.Sprintf("Cookie %q must be provided.", codersdk.SessionTokenKey),
3737
})
3838
return
3939
}
40-
token, err := uuid.Parse(cookieValue)
40+
token, err := uuid.Parse(tokenValue)
4141
if err != nil {
4242
httpapi.Write(ctx, rw, http.StatusUnauthorized, codersdk.Response{
43-
Message: "Agent token is invalid.",
43+
Message: "Workspace agent token invalid.",
44+
Detail: fmt.Sprintf("An agent token must be a valid UUIDv4. (len %d)", len(tokenValue)),
4445
})
4546
return
4647
}
4748
agent, err := db.GetWorkspaceAgentByAuthToken(ctx, token)
4849
if err != nil {
4950
if errors.Is(err, sql.ErrNoRows) {
5051
httpapi.Write(ctx, rw, http.StatusUnauthorized, codersdk.Response{
51-
Message: "Agent token is invalid.",
52+
Message: "Workspace agent not authorized.",
53+
Detail: "The agent cannot authenticate until the workspace provision job has been completed. If the job is no longer running, this agent is invalid.",
5254
})
5355
return
5456
}

coderd/organizations.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ func (api *API) postOrganizations(rw http.ResponseWriter, r *http.Request) {
7676
CreatedAt: database.Now(),
7777
UpdatedAt: database.Now(),
7878
Roles: []string{
79-
rbac.RoleOrgAdmin(organization.ID),
79+
// TODO: When organizations are allowed to be created, we should
80+
// come back to determining the default role of the person who
81+
// creates the org. Until that happens, all users in an organization
82+
// should be just regular members.
83+
rbac.RoleOrgMember(organization.ID),
8084
},
8185
})
8286
if err != nil {

coderd/users.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,11 @@ func (api *API) CreateUser(ctx context.Context, store database.Store, req Create
10711071
return xerrors.Errorf("create organization: %w", err)
10721072
}
10731073
req.OrganizationID = organization.ID
1074-
orgRoles = append(orgRoles, rbac.RoleOrgAdmin(req.OrganizationID))
1074+
// TODO: When organizations are allowed to be created, we should
1075+
// come back to determining the default role of the person who
1076+
// creates the org. Until that happens, all users in an organization
1077+
// should be just regular members.
1078+
orgRoles = append(orgRoles, rbac.RoleOrgMember(req.OrganizationID))
10751079

10761080
_, err = tx.InsertAllUsersGroup(ctx, organization.ID)
10771081
if err != nil {

coderd/users_test.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -817,15 +817,6 @@ func TestGrantSiteRoles(t *testing.T) {
817817
Error: true,
818818
StatusCode: http.StatusForbidden,
819819
},
820-
{
821-
Name: "MemberAssignMember",
822-
Client: member,
823-
OrgID: first.OrganizationID,
824-
AssignToUser: first.UserID.String(),
825-
Roles: []string{},
826-
Error: true,
827-
StatusCode: http.StatusForbidden,
828-
},
829820
{
830821
Name: "AdminUpdateOrgSelf",
831822
Client: admin,
@@ -921,7 +912,7 @@ func TestInitialRoles(t *testing.T) {
921912
}, "should be a member and admin")
922913

923914
require.ElementsMatch(t, roles.OrganizationRoles[first.OrganizationID], []string{
924-
rbac.RoleOrgAdmin(first.OrganizationID),
915+
rbac.RoleOrgMember(first.OrganizationID),
925916
}, "should be a member and admin")
926917
}
927918

provisioner/terraform/resources.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,15 @@ func ConvertResources(module *tfjson.StateModule, rawGraph string) ([]*proto.Res
218218
if agent.Id != agentID {
219219
continue
220220
}
221-
agent.Auth = &proto.Agent_InstanceId{
222-
InstanceId: instanceID,
221+
// Only apply the instance ID if the agent authentication
222+
// type is set to do so. A user ran into a bug where they
223+
// had the instance ID block, but auth was set to "token". See:
224+
// https://github.com/coder/coder/issues/4551#issuecomment-1336293468
225+
switch t := agent.Auth.(type) {
226+
case *proto.Agent_Token:
227+
continue
228+
case *proto.Agent_InstanceId:
229+
t.InstanceId = instanceID
223230
}
224231
break
225232
}

0 commit comments

Comments
 (0)