mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 12:16:03 +09:00
fix(postgres): make an explicit parameter win over the audit default
Seeding pgaudit.log and the connection-logging parameters through the $defaults merge let a falsy explicit value lose: a tenant setting log_connections: 0 while audit is on had it silently overwritten with on, because sprig merge reads a zero value as unset. Seed each key only when postgresql.parameters does not already set it, so an explicit value wins unconditionally. Also enumerate the valid pgaudit.log classes in the field description, since an unrecognised class makes pgaudit fall back to none and silently disables auditing. Assisted-by: LLM Signed-off-by: IvanHunters <xorokhotnikov@gmail.com>
This commit is contained in:
7 files changed
+35
-15
No files matched your search
Generated
+1
-1
@@ -80,7 +80,7 @@ type Audit struct {
|
||||
// Enable PostgreSQL auditing. Loads the pgaudit extension through the `sharedPreloadLibraries` allowlist and turns on `log_connections`/`log_disconnections`, so who connected and the classes of statement they ran are written to the instance log and collected by the platform log stack. The audited statement classes are set by `log`. Off by default. Enabling or disabling it restarts PostgreSQL, since pgaudit is a preloaded library.
|
||||
// +kubebuilder:default:=false
|
||||
Enabled bool `json:"enabled,omitempty"`
|
||||
// pgaudit session audit classes recorded when `enabled`, as the comma-separated value of `pgaudit.log` (for example `ddl, role`, `write, ddl, role` or `all`; `all` logs every statement including reads and is heavy). Defaults to `write, ddl, role`. A `pgaudit.log` set directly in `postgresql.parameters` overrides this.
|
||||
// pgaudit session audit classes recorded when `enabled`, as the comma-separated value of `pgaudit.log`. Valid classes are `read`, `write`, `function`, `role`, `ddl`, `misc`, `misc_set`, `all` and `none`; a class may be prefixed with `-` to subtract it from `all` (for example `ddl, role`, `write, ddl, role`, or `all, -misc`). `all` logs every statement including reads and is heavy. Defaults to `write, ddl, role`. A `pgaudit.log` set in `postgresql.parameters` overrides this. An unrecognised class makes pgaudit fall back to `none` at start, which silently disables auditing, so keep to the listed names.
|
||||
// +kubebuilder:default:="write, ddl, role"
|
||||
Log string `json:"log,omitempty"`
|
||||
}
|
||||
|
||||
@@ -242,11 +242,11 @@ For `sslmode=verify-full` to work, the CA bundle retrieved above must be saved t
|
||||
|
||||
### Audit parameters
|
||||
|
||||
| Name | Description | Type | Value |
|
||||
| --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ------------------ |
|
||||
| `audit` | PostgreSQL audit configuration. | `object` | `{}` |
|
||||
| `audit.enabled` | Enable PostgreSQL auditing. Loads the pgaudit extension through the `sharedPreloadLibraries` allowlist and turns on `log_connections`/`log_disconnections`, so who connected and the classes of statement they ran are written to the instance log and collected by the platform log stack. The audited statement classes are set by `log`. Off by default. Enabling or disabling it restarts PostgreSQL, since pgaudit is a preloaded library. | `bool` | `false` |
|
||||
| `audit.log` | pgaudit session audit classes recorded when `enabled`, as the comma-separated value of `pgaudit.log` (for example `ddl, role`, `write, ddl, role` or `all`; `all` logs every statement including reads and is heavy). Defaults to `write, ddl, role`. A `pgaudit.log` set directly in `postgresql.parameters` overrides this. | `string` | `write, ddl, role` |
|
||||
| Name | Description | Type | Value |
|
||||
| --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ------------------ |
|
||||
| `audit` | PostgreSQL audit configuration. | `object` | `{}` |
|
||||
| `audit.enabled` | Enable PostgreSQL auditing. Loads the pgaudit extension through the `sharedPreloadLibraries` allowlist and turns on `log_connections`/`log_disconnections`, so who connected and the classes of statement they ran are written to the instance log and collected by the platform log stack. The audited statement classes are set by `log`. Off by default. Enabling or disabling it restarts PostgreSQL, since pgaudit is a preloaded library. | `bool` | `false` |
|
||||
| `audit.log` | pgaudit session audit classes recorded when `enabled`, as the comma-separated value of `pgaudit.log`. Valid classes are `read`, `write`, `function`, `role`, `ddl`, `misc`, `misc_set`, `all` and `none`; a class may be prefixed with `-` to subtract it from `all` (for example `ddl, role`, `write, ddl, role`, or `all, -misc`). `all` logs every statement including reads and is heavy. Defaults to `write, ddl, role`. A `pgaudit.log` set in `postgresql.parameters` overrides this. An unrecognised class makes pgaudit fall back to `none` at start, which silently disables auditing, so keep to the listed names. | `string` | `write, ddl, role` |
|
||||
|
||||
|
||||
### Quorum-based synchronous replication
|
||||
|
||||
@@ -365,14 +365,16 @@ spec:
|
||||
parameters:
|
||||
{{- $defaults := dict "max_wal_senders" "30" }}
|
||||
{{- /*
|
||||
Audit defaults seed pgaudit.log and connection logging. They go into
|
||||
$defaults, so an explicit pgaudit.log/log_connections in
|
||||
postgresql.parameters still takes precedence through the merge below.
|
||||
Audit seeds pgaudit.log and connection logging. Each key is seeded only
|
||||
when postgresql.parameters does not set it, so an explicit value wins
|
||||
unconditionally — including a falsy one like log_connections: 0, which
|
||||
merge would otherwise read as unset and overwrite with the default.
|
||||
*/}}
|
||||
{{- if $auditEnabled }}
|
||||
{{- $_ := set $defaults "pgaudit.log" (default "write, ddl, role" .Values.audit.log) }}
|
||||
{{- $_ := set $defaults "log_connections" "on" }}
|
||||
{{- $_ := set $defaults "log_disconnections" "on" }}
|
||||
{{- $userParams := default (dict) .Values.postgresql.parameters }}
|
||||
{{- if not (hasKey $userParams "pgaudit.log") }}{{- $_ := set $defaults "pgaudit.log" (default "write, ddl, role" .Values.audit.log) }}{{- end }}
|
||||
{{- if not (hasKey $userParams "log_connections") }}{{- $_ := set $defaults "log_connections" "on" }}{{- end }}
|
||||
{{- if not (hasKey $userParams "log_disconnections") }}{{- $_ := set $defaults "log_disconnections" "on" }}{{- end }}
|
||||
{{- end }}
|
||||
{{- /*
|
||||
PostgreSQL sizes shared_buffers at 128MB whatever the container gets.
|
||||
|
||||
@@ -101,3 +101,21 @@ tests:
|
||||
- equal:
|
||||
path: spec.postgresql.parameters["pgaudit.log"]
|
||||
value: "ddl"
|
||||
|
||||
- it: "an explicit falsy connection-logging value wins over the audit default"
|
||||
set:
|
||||
audit:
|
||||
enabled: true
|
||||
postgresql:
|
||||
parameters:
|
||||
log_connections: 0
|
||||
documentSelector:
|
||||
path: kind
|
||||
value: Cluster
|
||||
asserts:
|
||||
- equal:
|
||||
path: spec.postgresql.parameters.log_connections
|
||||
value: "0"
|
||||
- equal:
|
||||
path: spec.postgresql.parameters.log_disconnections
|
||||
value: "on"
|
||||
@@ -219,7 +219,7 @@
|
||||
"default": false
|
||||
},
|
||||
"log": {
|
||||
"description": "pgaudit session audit classes recorded when `enabled`, as the comma-separated value of `pgaudit.log` (for example `ddl, role`, `write, ddl, role` or `all`; `all` logs every statement including reads and is heavy). Defaults to `write, ddl, role`. A `pgaudit.log` set directly in `postgresql.parameters` overrides this.",
|
||||
"description": "pgaudit session audit classes recorded when `enabled`, as the comma-separated value of `pgaudit.log`. Valid classes are `read`, `write`, `function`, `role`, `ddl`, `misc`, `misc_set`, `all` and `none`; a class may be prefixed with `-` to subtract it from `all` (for example `ddl, role`, `write, ddl, role`, or `all, -misc`). `all` logs every statement including reads and is heavy. Defaults to `write, ddl, role`. A `pgaudit.log` set in `postgresql.parameters` overrides this. An unrecognised class makes pgaudit fall back to `none` at start, which silently disables auditing, so keep to the listed names.",
|
||||
"type": "string",
|
||||
"default": "write, ddl, role"
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ postgresql:
|
||||
|
||||
## @typedef {struct} Audit - PostgreSQL audit configuration.
|
||||
## @field {bool} [enabled] - Enable PostgreSQL auditing. Loads the pgaudit extension through the `sharedPreloadLibraries` allowlist and turns on `log_connections`/`log_disconnections`, so who connected and the classes of statement they ran are written to the instance log and collected by the platform log stack. The audited statement classes are set by `log`. Off by default. Enabling or disabling it restarts PostgreSQL, since pgaudit is a preloaded library.
|
||||
## @field {string} [log] - pgaudit session audit classes recorded when `enabled`, as the comma-separated value of `pgaudit.log` (for example `ddl, role`, `write, ddl, role` or `all`; `all` logs every statement including reads and is heavy). Defaults to `write, ddl, role`. A `pgaudit.log` set directly in `postgresql.parameters` overrides this.
|
||||
## @field {string} [log] - pgaudit session audit classes recorded when `enabled`, as the comma-separated value of `pgaudit.log`. Valid classes are `read`, `write`, `function`, `role`, `ddl`, `misc`, `misc_set`, `all` and `none`; a class may be prefixed with `-` to subtract it from `all` (for example `ddl, role`, `write, ddl, role`, or `all, -misc`). `all` logs every statement including reads and is heavy. Defaults to `write, ddl, role`. A `pgaudit.log` set in `postgresql.parameters` overrides this. An unrecognised class makes pgaudit fall back to `none` at start, which silently disables auditing, so keep to the listed names.
|
||||
|
||||
## @param {Audit} audit - PostgreSQL audit configuration.
|
||||
audit:
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ spec:
|
||||
singular: postgres
|
||||
plural: postgreses
|
||||
openAPISchema: |-
|
||||
{"title":"Chart Values","type":"object","properties":{"replicas":{"description":"Number of Postgres replicas. Under active autoscaling this is the resting/seed count, not the live count — KEDA drives the live count via `/scale`. Disabling autoscaling does NOT automatically bring the live count back to `replicas`: the client-side merge writes `instances` only when the rendered value changes, so a cluster KEDA grew past `replicas` stays there with no autoscaler left to reduce it — lower `replicas` to the count you want and it rebases down (shedding the surplus replicas + PVCs). Lowering it below the live count, or disabling/dry-running while it sits below the live count, sheds in one step past the scaleDown pacing; stage it to the live count first (suspend the HelmRelease and `kubectl scale` if needed) to avoid a transient. Must stay greater than `quorum.maxSyncReplicas`.","type":"integer","default":2},"resources":{"description":"Explicit CPU and memory configuration for each PostgreSQL replica. When omitted, the preset defined in `resourcesPreset` is applied.","type":"object","default":{},"properties":{"cpu":{"description":"CPU available to each replica.","pattern":"^(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))))?$","anyOf":[{"type":"integer"},{"type":"string"}],"x-kubernetes-int-or-string":true},"memory":{"description":"Memory (RAM) available to each replica.","pattern":"^(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))))?$","anyOf":[{"type":"integer"},{"type":"string"}],"x-kubernetes-int-or-string":true}}},"resourcesPreset":{"description":"Default sizing preset used when `resources` is omitted.","type":"string","default":"t1.micro","enum":["t1.nano","t1.micro","t1.small","t1.medium","t1.large","t1.xlarge","t1.2xlarge","t1.4xlarge","c1.nano","c1.micro","c1.small","c1.medium","c1.large","c1.xlarge","c1.2xlarge","c1.4xlarge","s1.nano","s1.micro","s1.small","s1.medium","s1.large","s1.xlarge","s1.2xlarge","s1.4xlarge","u1.nano","u1.micro","u1.small","u1.medium","u1.large","u1.xlarge","u1.2xlarge","u1.4xlarge","m1.nano","m1.micro","m1.small","m1.medium","m1.large","m1.xlarge","m1.2xlarge","m1.4xlarge","nano","micro","small","medium","large","xlarge","2xlarge"]},"size":{"description":"Persistent Volume Claim size available for application data.","default":"10Gi","pattern":"^(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))))?$","anyOf":[{"type":"integer"},{"type":"string"}],"x-kubernetes-int-or-string":true},"storageClass":{"description":"StorageClass used to store the data.","type":"string","default":"","x-kubernetes-validations":[{"rule":"self == oldSelf","message":"storageClass is immutable"}],"x-cozystack-options":{"source":"storageclass"}},"external":{"description":"Enable external access from outside the cluster.","type":"boolean","default":false},"schedulingClass":{"description":"Name of a SchedulingClass CR (cluster-scoped, group cozystack.io) used in place of the tenant's for this application's pods. A pod gets its class when it is created, so running pods keep theirs until they are recreated. Must be the tenant's own class or one listed in the tenant's `allowedSchedulingClasses`. Setting any other value is refused, and the pods of an application whose class is not, or no longer, in that list get the tenant's class. Empty means inherit from the tenant.","type":"string","default":"","maxLength":63},"version":{"description":"PostgreSQL major version to deploy","type":"string","default":"v18","enum":["v18","v17","v16","v15","v14","v13"]},"flavor":{"description":"PostgreSQL image flavor, fixed when the cluster is created. `postgresql` uses the CloudNativePG PostgreSQL image. `postgis` uses the CloudNativePG PostGIS image, which adds postgis, postgis_raster, postgis_topology, postgis_sfcgal, postgis_tiger_geocoder and address_standardizer; enable them per database in `databases.<name>.extensions`. Changing the flavor of an existing cluster is refused, and a backup restores only into a cluster of the flavor it was taken from: to change flavor, create a new cluster and move the data with a logical dump (pg_dump / pg_restore).","type":"string","default":"postgresql","enum":["postgresql","postgis"]},"tls":{"description":"TLS configuration for server connections.","type":"object","default":{},"properties":{"enabled":{"description":"Tri-state switch controlling whether the chart injects the external hostnames into the operator-managed CNPG cert via spec.certificates.serverAltDNSNames. When omitted, the chart injects them if `external: true` and skips them otherwise. Set explicitly to `true` to inject regardless of `external` (no-op when `external: false` since there is no external hostname to add). Set to `false` to skip injection. Note that CNPG keeps its built-in TLS on the wire regardless of this flag Line truncated
|
||||
{"title":"Chart Values","type":"object","properties":{"replicas":{"description":"Number of Postgres replicas. Under active autoscaling this is the resting/seed count, not the live count — KEDA drives the live count via `/scale`. Disabling autoscaling does NOT automatically bring the live count back to `replicas`: the client-side merge writes `instances` only when the rendered value changes, so a cluster KEDA grew past `replicas` stays there with no autoscaler left to reduce it — lower `replicas` to the count you want and it rebases down (shedding the surplus replicas + PVCs). Lowering it below the live count, or disabling/dry-running while it sits below the live count, sheds in one step past the scaleDown pacing; stage it to the live count first (suspend the HelmRelease and `kubectl scale` if needed) to avoid a transient. Must stay greater than `quorum.maxSyncReplicas`.","type":"integer","default":2},"resources":{"description":"Explicit CPU and memory configuration for each PostgreSQL replica. When omitted, the preset defined in `resourcesPreset` is applied.","type":"object","default":{},"properties":{"cpu":{"description":"CPU available to each replica.","pattern":"^(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))))?$","anyOf":[{"type":"integer"},{"type":"string"}],"x-kubernetes-int-or-string":true},"memory":{"description":"Memory (RAM) available to each replica.","pattern":"^(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))))?$","anyOf":[{"type":"integer"},{"type":"string"}],"x-kubernetes-int-or-string":true}}},"resourcesPreset":{"description":"Default sizing preset used when `resources` is omitted.","type":"string","default":"t1.micro","enum":["t1.nano","t1.micro","t1.small","t1.medium","t1.large","t1.xlarge","t1.2xlarge","t1.4xlarge","c1.nano","c1.micro","c1.small","c1.medium","c1.large","c1.xlarge","c1.2xlarge","c1.4xlarge","s1.nano","s1.micro","s1.small","s1.medium","s1.large","s1.xlarge","s1.2xlarge","s1.4xlarge","u1.nano","u1.micro","u1.small","u1.medium","u1.large","u1.xlarge","u1.2xlarge","u1.4xlarge","m1.nano","m1.micro","m1.small","m1.medium","m1.large","m1.xlarge","m1.2xlarge","m1.4xlarge","nano","micro","small","medium","large","xlarge","2xlarge"]},"size":{"description":"Persistent Volume Claim size available for application data.","default":"10Gi","pattern":"^(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))))?$","anyOf":[{"type":"integer"},{"type":"string"}],"x-kubernetes-int-or-string":true},"storageClass":{"description":"StorageClass used to store the data.","type":"string","default":"","x-kubernetes-validations":[{"rule":"self == oldSelf","message":"storageClass is immutable"}],"x-cozystack-options":{"source":"storageclass"}},"external":{"description":"Enable external access from outside the cluster.","type":"boolean","default":false},"schedulingClass":{"description":"Name of a SchedulingClass CR (cluster-scoped, group cozystack.io) used in place of the tenant's for this application's pods. A pod gets its class when it is created, so running pods keep theirs until they are recreated. Must be the tenant's own class or one listed in the tenant's `allowedSchedulingClasses`. Setting any other value is refused, and the pods of an application whose class is not, or no longer, in that list get the tenant's class. Empty means inherit from the tenant.","type":"string","default":"","maxLength":63},"version":{"description":"PostgreSQL major version to deploy","type":"string","default":"v18","enum":["v18","v17","v16","v15","v14","v13"]},"flavor":{"description":"PostgreSQL image flavor, fixed when the cluster is created. `postgresql` uses the CloudNativePG PostgreSQL image. `postgis` uses the CloudNativePG PostGIS image, which adds postgis, postgis_raster, postgis_topology, postgis_sfcgal, postgis_tiger_geocoder and address_standardizer; enable them per database in `databases.<name>.extensions`. Changing the flavor of an existing cluster is refused, and a backup restores only into a cluster of the flavor it was taken from: to change flavor, create a new cluster and move the data with a logical dump (pg_dump / pg_restore).","type":"string","default":"postgresql","enum":["postgresql","postgis"]},"tls":{"description":"TLS configuration for server connections.","type":"object","default":{},"properties":{"enabled":{"description":"Tri-state switch controlling whether the chart injects the external hostnames into the operator-managed CNPG cert via spec.certificates.serverAltDNSNames. When omitted, the chart injects them if `external: true` and skips them otherwise. Set explicitly to `true` to inject regardless of `external` (no-op when `external: false` since there is no external hostname to add). Set to `false` to skip injection. Note that CNPG keeps its built-in TLS on the wire regardless of this flag Line truncated
|
||||
release:
|
||||
prefix: postgres-
|
||||
labels:
|
||||
|
||||
Reference in new issue
Block a user