Skip to content

Commit 733f58c

Browse files
authored
chore: Force license uuids to not be null (#6012)
* chore: Force license uuids to not be null * All unit tests generate uuids for licenses * Update migration files to new numbers * Put migration in transaction
1 parent a54de60 commit 733f58c

File tree

11 files changed

+39
-25
lines changed

11 files changed

+39
-25
lines changed

coderd/database/dump.sql

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE ONLY licenses ALTER COLUMN uuid DROP NOT NULL;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
BEGIN;
2+
3+
-- We need to assign uuids to any existing licenses that don't have them.
4+
UPDATE licenses SET uuid = gen_random_uuid() WHERE uuid IS NULL;
5+
-- Assert no licenses have null uuids.
6+
ALTER TABLE ONLY licenses ALTER COLUMN uuid SET NOT NULL;
7+
8+
COMMIT;

coderd/database/models.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/sqlc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ overrides:
4343
troubleshooting_url: TroubleshootingURL
4444
default_ttl: DefaultTTL
4545
motd_file: MOTDFile
46+
uuid: UUID
4647

4748
sql:
4849
- schema: "./dump.sql"

coderd/telemetry/telemetry.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ func ConvertTemplateVersion(version database.TemplateVersion) TemplateVersion {
645645
func ConvertLicense(license database.License) License {
646646
return License{
647647
UploadedAt: license.UploadedAt,
648-
UUID: license.Uuid.UUID,
648+
UUID: license.UUID,
649649
}
650650
}
651651

coderd/telemetry/telemetry_test.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ func TestTelemetry(t *testing.T) {
7171
UploadedAt: database.Now(),
7272
JWT: "",
7373
Exp: database.Now().Add(time.Hour),
74-
Uuid: uuid.NullUUID{
75-
UUID: uuid.New(),
76-
Valid: true,
77-
},
74+
UUID: uuid.New(),
7875
})
7976
assert.NoError(t, err)
8077
_, snapshot := collectSnapshot(t, db)

enterprise/coderd/coderdenttest/coderdenttest.go

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/golang-jwt/jwt/v4"
14+
"github.com/google/uuid"
1415
"github.com/stretchr/testify/assert"
1516
"github.com/stretchr/testify/require"
1617

@@ -128,6 +129,7 @@ func GenerateLicense(t *testing.T, options LicenseOptions) string {
128129

129130
c := &license.Claims{
130131
RegisteredClaims: jwt.RegisteredClaims{
132+
ID: uuid.NewString(),
131133
Issuer: "test@testing.test",
132134
ExpiresAt: jwt.NewNumericDate(options.ExpiresAt),
133135
NotBefore: jwt.NewNumericDate(time.Now().Add(-time.Minute)),

enterprise/coderd/licenses.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,19 @@ func (api *API) postLicense(rw http.ResponseWriter, r *http.Request) {
9898
}
9999

100100
id, err := uuid.Parse(claims.ID)
101+
if err != nil {
102+
// If no uuid is in the license, we generate a random uuid.
103+
// This is not ideal, and this should be fixed to require a uuid
104+
// for all licenses. We require this patch to support older licenses.
105+
// TODO: In the future (April 2023?) we should remove this and reissue
106+
// old licenses with a uuid.
107+
id = uuid.New()
108+
}
101109
dl, err := api.Database.InsertLicense(ctx, database.InsertLicenseParams{
102110
UploadedAt: database.Now(),
103111
JWT: addLicense.License,
104112
Exp: expTime,
105-
Uuid: uuid.NullUUID{
106-
UUID: id,
107-
Valid: err == nil,
108-
},
113+
UUID: id,
109114
})
110115
if err != nil {
111116
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
@@ -229,7 +234,7 @@ func (api *API) deleteLicense(rw http.ResponseWriter, r *http.Request) {
229234
func convertLicense(dl database.License, c jwt.MapClaims) codersdk.License {
230235
return codersdk.License{
231236
ID: dl.ID,
232-
UUID: dl.Uuid.UUID,
237+
UUID: dl.UUID,
233238
UploadedAt: dl.UploadedAt,
234239
Claims: c,
235240
}

enterprise/trialer/trialer.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ func New(db database.Store, url string, keys map[string]ed25519.PublicKey) func(
6464
return xerrors.Errorf("parse claims: %w", err)
6565
}
6666
id, err := uuid.Parse(claims.ID)
67+
if err != nil {
68+
return xerrors.Errorf("parse uuid: %w", err)
69+
}
6770
_, err = db.InsertLicense(ctx, database.InsertLicenseParams{
6871
UploadedAt: database.Now(),
6972
JWT: string(raw),
7073
Exp: expTime,
71-
Uuid: uuid.NullUUID{
72-
UUID: id,
73-
Valid: err == nil,
74-
},
74+
UUID: id,
7575
})
7676
if err != nil {
7777
return xerrors.Errorf("insert license: %w", err)

0 commit comments

Comments
 (0)