8000 chore: replace crc32 with sha256 + base32 + truncation · coder/coder@3e9f2f3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e9f2f3

Browse files
chore: replace crc32 with sha256 + base32 + truncation
1 parent 4cb1621 commit 3e9f2f3

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

coderd/agentapi/subagent.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package agentapi
22

33
import (
44
"context"
5+
"crypto/sha256"
56
"database/sql"
7+
"encoding/base32"
68
"errors"
79
"fmt"
8-
"hash/crc32"
910
"strings"
1011

1112
"github.com/google/uuid"
@@ -171,8 +172,9 @@ func (a *SubAgentAPI) CreateSubAgent(ctx context.Context, req *agentproto.Create
171172
// there is no database-layer enforcement of this constraint.
172173
// We can get around this by creating a slug that *should* be
173174
// unique (at least highly probable).
174-
slugHash := fmt.Sprintf("%08x", crc32.ChecksumIEEE([]byte(subAgent.Name+"/"+app.Slug)))
175-
computedSlug := slugHash + "-" + app.Slug
175+
slugHash := sha256.Sum256([]byte(subAgent.Name + "/" + app.Slug))
176+
slugHashEnc := base32.HexEncoding.WithPadding(base32.NoPadding).EncodeToString(slugHash[:])
177+
computedSlug := strings.ToLower(slugHashEnc[:8]) + "-" + app.Slug
176178

177179
_, err := a.Database.UpsertWorkspaceApp(ctx, database.UpsertWorkspaceAppParams{
178180
ID: uuid.New(), // NOTE: we may need to maintain the app's ID here for stability, but for now we'll leave this as-is.

coderd/agentapi/subagent_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func TestSubAgentAPI(t *testing.T) {
216216
},
217217
expectApps: []database.WorkspaceApp{
218218
{
219-
Slug: "d2b537b8-code-server",
219+
Slug: "fdqf0lpd-code-server",
220220
DisplayName: "VS Code",
221221
Icon: "/icon/code.svg",
222222
Command: sql.NullString{},
@@ -234,7 +234,7 @@ func TestSubAgentAPI(t *testing.T) {
234234
DisplayGroup: sql.NullString{},
235235
},
236236
{
237-
Slug: "9c813024-vim",
237+
Slug: "547knu0f-vim",
238238
DisplayName: "Vim",
239239
Icon: "/icon/vim.svg",
240240
Command: sql.NullString{Valid: true, String: "vim"},
@@ -377,7 +377,7 @@ func TestSubAgentAPI(t *testing.T) {
377377
},
378378
expectApps: []database.WorkspaceApp{
379379
{
380-
Slug: "7f9e8fef-valid-app",
380+
Slug: "511ctirn-valid-app",
381381
DisplayName: "Valid App",
382382
SharingLevel: database.AppSharingLevelOwner,
383383
Health: database.WorkspaceAppHealthDisabled,
@@ -410,19 +410,19 @@ func TestSubAgentAPI(t *testing.T) {
410410
},
411411
expectApps: []database.WorkspaceApp{
412412
{
413-
Slug: "51f592de-authenticated-app",
413+
Slug: "atpt261l-authenticated-app",
414414
SharingLevel: database.AppSharingLevelAuthenticated,
415415
Health: database.WorkspaceAppHealthDisabled,
416416
OpenIn: database.WorkspaceAppOpenInSlimWindow,
417417
},
418418
{
419-
Slug: "cad4f019-owner-app",
419+
Slug: "eh5gp1he-owner-app",
420420
SharingLevel: database.AppSharingLevelOwner,
421421
Health: database.WorkspaceAppHealthDisabled,
422422
OpenIn: database.WorkspaceAppOpenInSlimWindow,
423423
},
424424
{
425-
Slug: "9e367a4c-public-app",
425+
Slug: "oopjevf1-public-app",
426426
SharingLevel: database.AppSharingLevelPublic,
427427
Health: database.WorkspaceAppHealthDisabled,
428428
OpenIn: database.WorkspaceAppOpenInSlimWindow,
@@ -443,13 +443,13 @@ func TestSubAgentAPI(t *testing.T) {
443443
},
444444
expectApps: []database.WorkspaceApp{
445445
{
446-
Slug: "6cb5ae2b-tab-app",
446+
Slug: "ci9500rm-tab-app",
447447
SharingLevel: database.AppSharingLevelOwner,
448448
Health: database.WorkspaceAppHealthDisabled,
449449
OpenIn: database.WorkspaceAppOpenInTab,
450450
},
451451
{
452-
Slug: "11675927-window-app",
452+
Slug: "p17s76re-window-app",
453453
SharingLevel: database.AppSharingLevelOwner,
454454
Health: database.WorkspaceAppHealthDisabled,
455455
OpenIn: database.WorkspaceAppOpenInSlimWindow,
@@ -479,7 +479,7 @@ func TestSubAgentAPI(t *testing.T) {
479479
},
480480
expectApps: []database.WorkspaceApp{
481481
{
482-
Slug: "1067629f-full-app",
482+
Slug: "0ccdbg39-full-app",
483483
Command: sql.NullString{Valid: true, String: "echo hello"},
484484
DisplayName: "Full Featured App",
485485
External: true,
@@ -507,7 +507,7 @@ func TestSubAgentAPI(t *testing.T) {
507507
},
508508
expectApps: []database.WorkspaceApp{
509509
{
510-
Slug: "3290f004-no-health-app",
510+
Slug: "nphrhbh6-no-health-app",
511511
Health: database.WorkspaceAppHealthDisabled,
512512
SharingLevel: database.AppSharingLevelOwner,
513513
OpenIn: database.WorkspaceAppOpenInSlimWindow,
@@ -531,7 +531,7 @@ func TestSubAgentAPI(t *testing.T) {
531531
},
532532
expectApps: []database.WorkspaceApp{
533533
{
534-
Slug: "6977f2fe-duplicate-app",
534+
Slug: "uiklfckv-duplicate-app",
535535
DisplayName: "First App",
536536
SharingLevel: database.AppSharingLevelOwner,
537537
Health: database.WorkspaceAppHealthDisabled,
@@ -568,14 +568,14 @@ func TestSubAgentAPI(t *testing.T) {
568568
},
569569
expectApps: []database.WorkspaceApp{
570570
{
571-
Slug: "6977f2fe-duplicate-app",
571+
Slug: "uiklfckv-duplicate-app",
572572
DisplayName: "First Duplicate",
573573
SharingLevel: database.AppSharingLevelOwner,
574574
Health: database.WorkspaceAppHealthDisabled,
575575
OpenIn: database.WorkspaceAppOpenInSlimWindow,
576576
},
577577
{
578-
Slug: "7f9e8fef-valid-app",
578+
Slug: "511ctirn-valid-app",
579579
DisplayName: "Valid App",
580580
SharingLevel: database.AppSharingLevelOwner,
581581
Health: database.WorkspaceAppHealthDisabled,
@@ -754,7 +754,7 @@ func TestSubAgentAPI(t *testing.T) {
754754
apps, err := db.GetWorkspaceAppsByAgentID(dbauthz.AsSystemRestricted(ctx), agentID) //nolint:gocritic // this is a test.
755755
require.NoError(t, err)
756756
require.Len(t, apps, 1)
757-
require.Equal(t, "b219bd99-duplicate-slug", apps[0].Slug)
757+
require.Equal(t, "k5jd7a99-duplicate-slug", apps[0].Slug)
758758
require.Equal(t, "First Duplicate", apps[0].DisplayName)
759759
})
760760
})
@@ -1128,7 +1128,7 @@ func TestSubAgentAPI(t *testing.T) {
11281128
apps, err := api.Database.GetWorkspaceAppsByAgentID(dbauthz.AsSystemRestricted(ctx), agentID) //nolint:gocritic // this is a test.
11291129
require.NoError(t, err)
11301130
require.Len(t, apps, 1)
1131-
require.Equal(t, "9cc545fe-custom-app", apps[0].Slug)
1131+
require.Equal(t, "v4qhkq17-custom-app", apps[0].Slug)
11321132
require.Equal(t, "Custom App", apps[0].DisplayName)
11331133
})
11341134

0 commit comments

Comments
 (0)
0