Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
cda24d9
feat(coderd/database): add the chat automation queries that dbcrypt r…
ThomasK33 Nov 12, 2026
9351c74
feat(enterprise/dbcrypt): encrypt the webhook secret of a chat automa…
ThomasK33 Nov 12, 2026
93ff993
feat(enterprise/dbcrypt): cover the webhook secret in rotate, decrypt…
ThomasK33 Nov 12, 2026
1773b93
fix(enterprise/dbcrypt): leave the stored value out of the malformed …
ThomasK33 Nov 12, 2026
b983368
docs: list the chat automation webhook secret as encrypted
ThomasK33 Nov 12, 2026
ace9c75
test(enterprise/dbcrypt): prove that the commands do not write rows t…
ThomasK33 Nov 12, 2026
cc44a06
feat(coderd/database): add queries for messages that wait for prompt …
ThomasK33 Nov 12, 2026
7a690c8
feat(coderd/x/chatd/chatstate): carry prompt_hooks_pending and add Re…
ThomasK33 Nov 12, 2026
1af35f8
test(enterprise/dbcrypt): read a rotated secret back, recheck the bla…
ThomasK33 Nov 12, 2026
38e26b7
Merge remote-tracking branch 'origin/tk/automations-schema' into tk/a…
ThomasK33 Nov 12, 2026
9156732
feat(coderd/x/chatd): run the prompt hooks when a turn starts from a …
ThomasK33 Nov 12, 2026
585e4e6
docs(coderd/x/chatd): describe the prompt hooks of a flagged message
ThomasK33 Nov 12, 2026
3ef896d
Merge remote-tracking branch 'origin/tk/automations-schema' into tk/a…
ThomasK33 Nov 12, 2026
fa45c91
refactor(coderd/x/chatd): drop the dead deleted check and tests that …
ThomasK33 Nov 12, 2026
c7f48ad
docs(coderd/x/chatd): state only what the flag means
ThomasK33 Nov 12, 2026
b9ebb76
test(enterprise/dbcrypt): cover a corrupt secret and widen the overri…
ThomasK33 Nov 12, 2026
8b376e4
fix(coderd/x/chatd): keep a message that waits for its hooks out of e…
ThomasK33 Nov 12, 2026
3e8caf6
Merge remote-tracking branch 'origin/tk/automations-schema' into tk/a…
ThomasK33 Nov 12, 2026
2898e5d
docs(coderd/x/chatd): drop the context boundary from the prompt hooks…
ThomasK33 Nov 12, 2026
1693f5c
test(coderd/x/chatd): handle the strings.Builder write error
ThomasK33 Nov 12, 2026
a18a332
Merge remote-tracking branch 'origin/tk/automations-schema' into tk/a…
ThomasK33 Nov 12, 2026
15802ed
Merge remote-tracking branch 'origin/tk/automations-schema' into tk/a…
ThomasK33 Nov 12, 2026
274ef72
test(enterprise/dbcrypt): pin what a replica without keys stores and …
ThomasK33 Nov 12, 2026
3ea108a
docs(coderd/x/chatd): name the allow outcome and say what an edit doe…
ThomasK33 Nov 12, 2026
e5ffac6
test(coderd/x/chatd): pin the reserved dispatch capacity and drop che…
ThomasK33 Nov 12, 2026
4cf23bb
feat(enterprise/cli): name chat automation webhook secrets in the dbc…
ThomasK33 Nov 13, 2026
e240bed
test(coderd/x/chatd): compare the whole denial text and find the admi…
ThomasK33 Nov 13, 2026
2b3bbc2
Merge remote-tracking branch 'origin/tk/automations-schema' into tk/a…
ThomasK33 Nov 13, 2026
357d3a2
Merge remote-tracking branch 'origin/tk/automations-prompt-hooks' int…
ThomasK33 Nov 13, 2026
dfb67d8
test: cut tests that repeat or cannot fail
ThomasK33 Nov 13, 2026
85b2153
test(enterprise/dbcrypt): keep the keyless replica case
ThomasK33 Nov 13, 2026
2bb3788
Merge remote-tracking branch 'origin/tk/automations-schema' into tk/a…
ThomasK33 Nov 13, 2026
e542056
chore: remove #32075 from this branch, it lands later with B2c
ThomasK33 Nov 13, 2026
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
16 changes: 16 additions & 0 deletions coderd/database/dbauthz/dbauthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -3386,6 +3386,14 @@ func (q *querier) GetChatAutomationsByOrganizationID(ctx context.Context, arg da
return q.db.GetAuthorizedChatAutomationsByOrganizationID(ctx, arg, prepared)
}

func (q *querier) GetChatAutomationsWithWebhookSecretByOwnerID(ctx context.Context, ownerID uuid.UUID) ([]database.ChatAutomation, error) {
// Only used by the dbcrypt rotation, which operates on every row.
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceSystem); err != nil {
return nil, err
}
return q.db.GetChatAutomationsWithWebhookSecretByOwnerID(ctx, ownerID)
}

func (q *querier) GetChatByID(ctx context.Context, id uuid.UUID) (database.Chat, error) {
return fetch(q.log, q.auth, q.db.GetChatByID)(ctx, id)
}
Expand Down Expand Up @@ -8357,6 +8365,14 @@ func (q *querier) UpdateEncryptedAIProviderSettings(ctx context.Context, arg dat
return q.db.UpdateEncryptedAIProviderSettings(ctx, arg)
}

func (q *querier) UpdateEncryptedChatAutomationWebhookSecret(ctx context.Context, arg database.UpdateEncryptedChatAutomationWebhookSecretParams) error {
// Only used by the dbcrypt rotation, which operates on every row.
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceSystem); err != nil {
return err
}
return q.db.UpdateEncryptedChatAutomationWebhookSecret(ctx, arg)
}

func (q *querier) UpdateEncryptedChatMCPServerHeaders(ctx context.Context, arg database.UpdateEncryptedChatMCPServerHeadersParams) error {
// Only used by the dbcrypt rotation, which operates on every row.
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceSystem); err != nil {
Expand Down
11 changes: 11 additions & 0 deletions coderd/database/dbauthz/dbauthz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,17 @@ func (s *MethodTestSuite) TestChats() {
// No asserts here because it re-routes through GetChatAutomationsByOrganizationID which uses SQLFilter.
check.Args(arg, emptyPreparedAuthorized{}).Asserts().Returns(rows)
}))
s.Run("GetChatAutomationsWithWebhookSecretByOwnerID", s.Mocked(func(dbm *dbmock.MockStore, faker *gofakeit.Faker, check *expects) {
ownerID := uuid.New()
rows := []database.ChatAutomation{testutil.Fake(s.T(), faker, database.ChatAutomation{OwnerID: ownerID})}
dbm.EXPECT().GetChatAutomationsWithWebhookSecretByOwnerID(gomock.Any(), ownerID).Return(rows, nil).AnyTimes()
check.Args(ownerID).Asserts(rbac.ResourceSystem, policy.ActionRead).Returns(rows)
}))
s.Run("UpdateEncryptedChatAutomationWebhookSecret", s.Mocked(func(dbm *dbmock.MockStore, faker *gofakeit.Faker, check *expects) {
arg := database.UpdateEncryptedChatAutomationWebhookSecretParams{ID: uuid.New()}
dbm.EXPECT().UpdateEncryptedChatAutomationWebhookSecret(gomock.Any(), arg).Return(nil).AnyTimes()
check.Args(arg).Asserts(rbac.ResourceSystem, policy.ActionUpdate).Returns()
}))
s.Run("CountChatAutomationsByOwnerID", s.Mocked(func(dbm *dbmock.MockStore, _ *gofakeit.Faker, check *expects) {
ownerID := uuid.New()
dbm.EXPECT().CountChatAutomationsByOwnerID(gomock.Any(), ownerID).Return(int64(3), nil).AnyTimes()
Expand Down
16 changes: 16 additions & 0 deletions coderd/database/dbmetrics/querymetrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions coderd/database/dbmock/dbmock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 85 additions & 0 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions coderd/database/queries/chatautomations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,22 @@ WHERE owner_id = @owner_id::uuid;
-- name: DeleteChatAutomationByID :exec
DELETE FROM chat_automations
WHERE id = @id::uuid;

-- name: GetChatAutomationsWithWebhookSecretByOwnerID :many
-- Returns the automations of one owner that hold a webhook secret, secret
-- included. Used only by the dbcrypt key rotation utility.
SELECT *
FROM chat_automations
WHERE owner_id = @owner_id::uuid
AND webhook_secret IS NOT NULL
ORDER BY id;

-- name: UpdateEncryptedChatAutomationWebhookSecret :exec
-- Rewrites only the secret and its key ID. The version stays, because
-- re-encrypting does not change the secret. Used only by the dbcrypt key
-- rotation utility to re-encrypt or decrypt rows in place.
UPDATE chat_automations
SET
webhook_secret = sqlc.narg('webhook_secret')::text,
webhook_secret_key_id = sqlc.narg('webhook_secret_key_id')::text
WHERE id = @id::uuid;
1 change: 1 addition & 0 deletions docs/admin/security/database-encryption.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The following database fields are currently encrypted:
- `user_secrets.value`
- `gitsshkeys.private_key`
- `chat_mcp_servers.headers`
- `chat_automations.webhook_secret`

Additional database fields might be encrypted in the future.

Expand Down
1 change: 1 addition & 0 deletions enterprise/cli/server_dbcrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func (*RootCmd) dbcryptDeleteCmd() *serpent.Command {
msg := `All encrypted data will be deleted from the database:
- Encrypted user OAuth access and refresh tokens
- Encrypted user Git authentication access and refresh tokens
- Encrypted chat automation webhook secrets (the automations stay, with an empty secret)

Are you sure you want to continue?`
if _, err := cliui.Prompt(inv, cliui.PromptOptions{
Expand Down
40 changes: 40 additions & 0 deletions enterprise/cli/server_dbcrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import (
"golang.org/x/xerrors"

"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbgen"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/enterprise/cli"
"github.com/coder/coder/v2/enterprise/dbcrypt"
"github.com/coder/coder/v2/pty/ptytest"
"github.com/coder/coder/v2/testutil"
)

Expand Down Expand Up @@ -199,6 +201,14 @@ func TestServerDBCrypt(t *testing.T) {
chatMCPServers, err := db.GetChatMCPServersByChatOwnerID(ctx, usr.ID)
require.NoError(t, err, "failed to get chat MCP servers for user %s", usr.ID)
require.Empty(t, chatMCPServers)

// Chat automation rows are preserved, since a webhook row must hold
// a secret. Only the ciphertext is blanked.
automations, err := db.GetChatAutomationsWithWebhookSecretByOwnerID(ctx, usr.ID)
require.NoError(t, err, "failed to get chat automations for user %s", usr.ID)
require.Len(t, automations, 1)
require.Equal(t, sql.NullString{Valid: true}, automations[0].WebhookSecret)
require.False(t, automations[0].WebhookSecretKeyID.Valid)
}

// Validate that the key has been revoked in the database.
Expand Down Expand Up @@ -259,6 +269,11 @@ func genData(t *testing.T, db database.Store) []database.User {
ChatID: chat.ID,
Headers: "headers-" + usr.ID.String(),
})
_ = dbgen.ChatAutomation(t, db, database.ChatAutomation{
OrganizationID: chatModel.OrganizationID,
OwnerID: usr.ID,
WebhookSecret: sql.NullString{String: "webhook-" + usr.ID.String(), Valid: true},
})
now := time.Now()
_, err := db.UpsertUserAIProviderKey(context.Background(), database.UpsertUserAIProviderKeyParams{
ID: uuid.New(),
Expand Down Expand Up @@ -352,6 +367,12 @@ func requireEncryptedWithCipher(ctx context.Context, t *testing.T, db database.S
requireEncryptedEquals(t, c, "headers-"+userID.String(), chatMCPServers[0].Headers)
require.Equal(t, c.HexDigest(), chatMCPServers[0].HeadersKeyID.String)

automations, err := db.GetChatAutomationsWithWebhookSecretByOwnerID(ctx, userID)
require.NoError(t, err, "failed to get chat automations for user %s", userID)
require.Len(t, automations, 1)
requireEncryptedEquals(t, c, "webhook-"+userID.String(), automations[0].WebhookSecret.String)
require.Equal(t, c.HexDigest(), automations[0].WebhookSecretKeyID.String)

providers, err := db.GetAIProviders(ctx, database.GetAIProvidersParams{
IncludeDeleted: true,
IncludeDisabled: true,
Expand Down Expand Up @@ -494,3 +515,22 @@ func (*nullCipher) HexDigest() string {
}

var _ dbcrypt.Cipher = (*nullCipher)(nil)

// TestServerDBCryptDeletePrompt checks that the confirmation of dbcrypt delete
// names the chat automation webhook secrets it blanks, and that answering no
// stops the command before it connects to the database.
func TestServerDBCryptDeletePrompt(t *testing.T) {
t.Parallel()

inv, _ := newCLI(t, "server", "dbcrypt", "delete", "--postgres-url", "postgres://unused")
pty := ptytest.New(t).Attach(inv)
ctx := testutil.Context(t, testutil.WaitLong)
done := make(chan error, 1)
go func() {
done <- inv.WithContext(ctx).Run()
}()

pty.ExpectMatch(ctx, "chat automation webhook secrets")
pty.WriteLine("no")
require.ErrorIs(t, testutil.RequireReceive(ctx, t, done), cliui.ErrCanceled)
}
Loading
Loading