Skip to content

Commit 56e7858

Browse files
authored
feat(coderd): add retention policy configuration (#21021)
Add `RetentionConfig` with server flags for configuring data retention: - `--audit-logs-retention`: retention for audit log entries - `--connection-logs-retention`: retention for connection logs - `--api-keys-retention`: retention for expired API keys (default 7d) Updates #20743
1 parent 74d0c39 commit 56e7858

File tree

11 files changed

+314
-0
lines changed

11 files changed

+314
-0
lines changed

cli/testdata/coder_server_--help.golden

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,27 @@ updating, and deleting workspace resources.
696696
Number of provisioner daemons to create on start. If builds are stuck
697697
in queued state for a long time, consider increasing this.
698698

699+
RETENTION OPTIONS:
700+
Configure data retention policies for various database tables. Retention
701+
policies automatically purge old data to reduce database size and improve
702+
performance. Setting a retention duration to 0 disables automatic purging for
703+
that data type.
704+
705+
--api-keys-retention duration, $CODER_API_KEYS_RETENTION (default: 7d)
706+
How long expired API keys are retained before being deleted. Keeping
707+
expired keys allows the backend to return a more helpful error when a
708+
user tries to use an expired key. Set to 0 to disable automatic
709+
deletion of expired keys.
710+
711+
--audit-logs-retention duration, $CODER_AUDIT_LOGS_RETENTION (default: 0)
712+
How long audit log entries are retained. Set to 0 to disable (keep
713+
indefinitely). We advise keeping audit logs for at least a year, and
714+
in accordance with your compliance requirements.
715+
716+
--connection-logs-retention duration, $CODER_CONNECTION_LOGS_RETENTION (default: 0)
717+
How long connection log entries are retained. Set to 0 to disable
718+
(keep indefinitely).
719+
699720
TELEMETRY OPTIONS:
700721
Telemetry is critical to our ability to improve Coder. We strip all personal
701722
information before sending data to our servers. Please only disable telemetry

cli/testdata/server-config.yaml.golden

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,3 +742,22 @@ aibridge:
742742
# (token, prompt, tool use).
743743
# (default: 60d, type: duration)
744744
retention: 1440h0m0s
745+
# Configure data retention policies for various database tables. Retention
746+
# policies automatically purge old data to reduce database size and improve
747+
# performance. Setting a retention duration to 0 disables automatic purging for
748+
# that data type.
749+
retention:
750+
# How long audit log entries are retained. Set to 0 to disable (keep
751+
# indefinitely). We advise keeping audit logs for at least a year, and in
752+
# accordance with your compliance requirements.
753+
# (default: 0, type: duration)
754+
audit_logs: 0s
755+
# How long connection log entries are retained. Set to 0 to disable (keep
756+
# indefinitely).
757+
# (default: 0, type: duration)
758+
connection_logs: 0s
759+
# How long expired API keys are retained before being deleted. Keeping expired
760+
# keys allows the backend to return a more helpful error when a user tries to use
761+
# an expired key. Set to 0 to disable automatic deletion of expired keys.
762+
# (default: 7d, type: duration)
763+
api_keys: 168h0m0s

coderd/apidoc/docs.go

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

coderd/apidoc/swagger.json

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

codersdk/deployment.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ type DeploymentValues struct {
501501
WebTerminalRenderer serpent.String `json:"web_terminal_renderer,omitempty" typescript:",notnull"`
502502
AllowWorkspaceRenames serpent.Bool `json:"allow_workspace_renames,omitempty" typescript:",notnull"`
503503
Healthcheck HealthcheckConfig `json:"healthcheck,omitempty" typescript:",notnull"`
504+
Retention RetentionConfig `json:"retention,omitempty" typescript:",notnull"`
504505
CLIUpgradeMessage serpent.String `json:"cli_upgrade_message,omitempty" typescript:",notnull"`
505506
TermsOfServiceURL serpent.String `json:"terms_of_service_url,omitempty" typescript:",notnull"`
506507
Notifications NotificationsConfig `json:"notifications,omitempty" typescript:",notnull"`
@@ -813,6 +814,23 @@ type HealthcheckConfig struct {
813814
ThresholdDatabase serpent.Duration `json:"threshold_database" typescript:",notnull"`
814815
}
815816

817+
// RetentionConfig contains configuration for data retention policies.
818+
// These settings control how long various types of data are retained in the database
819+
// before being automatically purged. Setting a value to 0 disables retention for that
820+
// data type (data is kept indefinitely).
821+
type RetentionConfig struct {
822+
// AuditLogs controls how long audit log entries are retained.
823+
// Set to 0 to disable (keep indefinitely).
824+
AuditLogs serpent.Duration `json:"audit_logs" typescript:",notnull"`
825+
// ConnectionLogs controls how long connection log entries are retained.
826+
// Set to 0 to disable (keep indefinitely).
827+
ConnectionLogs serpent.Duration `json:"connection_logs" typescript:",notnull"`
828+
// APIKeys controls how long expired API keys are retained before being deleted.
829+
// Keys are only deleted if they have been expired for at least this duration.
830+
// Defaults to 7 days to preserve existing behavior.
831+
APIKeys serpent.Duration `json:"api_keys" typescript:",notnull"`
832+
}
833+
816834
type NotificationsConfig struct {
817835
// The upper limit of attempts to send a notification.
818836
MaxSendAttempts serpent.Int64 `json:"max_send_attempts" typescript:",notnull"`
@@ -1180,6 +1198,11 @@ func (c *DeploymentValues) Options() serpent.OptionSet {
11801198
Name: "AI Bridge",
11811199
YAML: "aibridge",
11821200
}
1201+
deploymentGroupRetention = serpent.Group{
1202+
Name: "Retention",
1203+
Description: "Configure data retention policies for various database tables. Retention policies automatically purge old data to reduce database size and improve performance. Setting a retention duration to 0 disables automatic purging for that data type.",
1204+
YAML: "retention",
1205+
}
11831206
)
11841207

11851208
httpAddress := serpent.Option{
@@ -3363,6 +3386,40 @@ Write out the current server config as YAML to stdout.`,
33633386
YAML: "retention",
33643387
Annotations: serpent.Annotations{}.Mark(annotationFormatDuration, "true"),
33653388
},
3389+
// Retention settings
3390+
{
3391+
Name: "Audit Logs Retention",
3392+
Description: "How long audit log entries are retained. Set to 0 to disable (keep indefinitely). We advise keeping audit logs for at least a year, and in accordance with your compliance requirements.",
3393+
Flag: "audit-logs-retention",
3394+
Env: "CODER_AUDIT_LOGS_RETENTION",
3395+
Value: &c.Retention.AuditLogs,
3396+
Default: "0",
3397+
Group: &deploymentGroupRetention,
3398+
YAML: "audit_logs",
3399+
Annotations: serpent.Annotations{}.Mark(annotationFormatDuration, "true"),
3400+
},
3401+
{
3402+
Name: "Connection Logs Retention",
3403+
Description: "How long connection log entries are retained. Set to 0 to disable (keep indefinitely).",
3404+
Flag: "connection-logs-retention",
3405+
Env: "CODER_CONNECTION_LOGS_RETENTION",
3406+
Value: &c.Retention.ConnectionLogs,
3407+
Default: "0",
3408+
Group: &deploymentGroupRetention,
3409+
YAML: "connection_logs",
3410+
Annotations: serpent.Annotations{}.Mark(annotationFormatDuration, "true"),
3411+
},
3412+
{
3413+
Name: "API Keys Retention",
3414+
Description: "How long expired API keys are retained before being deleted. Keeping expired keys allows the backend to return a more helpful error when a user tries to use an expired key. Set to 0 to disable automatic deletion of expired keys.",
3415+
Flag: "api-keys-retention",
3416+
Env: "CODER_API_KEYS_RETENTION",
3417+
Value: &c.Retention.APIKeys,
3418+
Default: "7d",
3419+
Group: &deploymentGroupRetention,
3420+
YAML: "api_keys",
3421+
Annotations: serpent.Annotations{}.Mark(annotationFormatDuration, "true"),
3422+
},
33663423
{
33673424
Name: "Enable Authorization Recordings",
33683425
Description: "All api requests will have a header including all authorization calls made during the request. " +

codersdk/deployment_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,3 +703,65 @@ func TestNotificationsCanBeDisabled(t *testing.T) {
703703
})
704704
}
705705
}
706+
707+
func TestRetentionConfigParsing(t *testing.T) {
708+
t.Parallel()
709+
710+
tests := []struct {
711+
name string
712+
environment []serpent.EnvVar
713+
expectedAuditLogs time.Duration
714+
expectedConnectionLogs time.Duration
715+
expectedAPIKeys time.Duration
716+
}{
717+
{
718+
name: "Defaults",
719+
environment: []serpent.EnvVar{},
720+
expectedAuditLogs: 0,
721+
expectedConnectionLogs: 0,
722+
expectedAPIKeys: 7 * 24 * time.Hour, // 7 days default
723+
},
724+
{
725+
name: "IndividualRetentionSet",
726+
environment: []serpent.EnvVar{
727+
{Name: "CODER_AUDIT_LOGS_RETENTION", Value: "30d"},
728+
{Name: "CODER_CONNECTION_LOGS_RETENTION", Value: "60d"},
729+
{Name: "CODER_API_KEYS_RETENTION", Value: "14d"},
730+
},
731+
expectedAuditLogs: 30 * 24 * time.Hour,
732+
expectedConnectionLogs: 60 * 24 * time.Hour,
733+
expectedAPIKeys: 14 * 24 * time.Hour,
734+
},
735+
{
736+
name: "AllRetentionSet",
737+
environment: []serpent.EnvVar{
738+
{Name: "CODER_AUDIT_LOGS_RETENTION", Value: "365d"},
739+
{Name: "CODER_CONNECTION_LOGS_RETENTION", Value: "30d"},
740+
{Name: "CODER_API_KEYS_RETENTION", Value: "0"},
741+
},
742+
expectedAuditLogs: 365 * 24 * time.Hour,
743+
expectedConnectionLogs: 30 * 24 * time.Hour,
744+
expectedAPIKeys: 0, // Explicitly disabled
745+
},
746+
}
747+
748+
for _, tt := range tests {
749+
tt := tt
750+
t.Run(tt.name, func(t *testing.T) {
751+
t.Parallel()
752+
753+
dv := codersdk.DeploymentValues{}
754+
opts := dv.Options()
755+
756+
err := opts.SetDefaults()
757+
require.NoError(t, err)
758+
759+
err = opts.ParseEnv(tt.environment)
760+
require.NoError(t, err)
761+
762+
assert.Equal(t, tt.expectedAuditLogs, dv.Retention.AuditLogs.Value(), "audit logs retention mismatch")
763+
assert.Equal(t, tt.expectedConnectionLogs, dv.Retention.ConnectionLogs.Value(), "connection logs retention mismatch")
764+
assert.Equal(t, tt.expectedAPIKeys, dv.Retention.APIKeys.Value(), "api keys retention mismatch")
765+
})
766+
}
767+
}

docs/reference/api/general.md

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

docs/reference/api/schemas.md

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

docs/reference/cli/server.md

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

0 commit comments

Comments
 (0)