@yarkivaev/scada 2.5.7 → 2.5.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -208,7 +208,45 @@ SQL files in `db/migrations/` use profile prefixes:
208
208
  | `E*` | Edge only (metrics tables, retention) |
209
209
  | `C*` | Central only (central privileges, including segment DELETE) |
210
210
 
211
- Set `SINK_DB_PROFILE=central|edge`. Migrations run at supervisor-sink startup via `POSTGRES_ADMIN_URL`.
211
+ Set `SINK_DB_PROFILE=central|edge`. By default, migrations run at supervisor-sink
212
+ startup via `POSTGRES_ADMIN_URL` (`SUPERVISOR_MIGRATIONS=apply`).
213
+
214
+ After a separate administrative bootstrap has applied the image's complete
215
+ migration set, set `SUPERVISOR_MIGRATIONS=verify` and remove `POSTGRES_ADMIN_URL`
216
+ from the runtime container. Startup then uses only `POSTGRES_URL` to SELECT the
217
+ recorded versions in `public.schema_migrations`. It checks shared and selected
218
+ profile migrations, including privileged migrations, before starting consumers or
219
+ HTTP. An absent/inaccessible registry or any missing migration stops startup;
220
+ it never creates the registry or falls back to administrative credentials.
221
+ Collector task-state migration remains separately opt-in, as in apply mode.
222
+ This verifies migration records, not schema checksums or effective DML privileges.
223
+
224
+ The restricted runtime role needs database CONNECT, public schema USAGE,
225
+ `GRANT SELECT ON public.schema_migrations TO <runtime_role>`, and the DML/sequence
226
+ grants required by its enabled pipelines and APIs. It needs no schema CREATE,
227
+ registry INSERT, table ownership or elevated role attributes for verification.
228
+ Do not mount an administrative Secret into verify-mode runtime containers.
229
+ Complete each image's bootstrap and role grants before switching it; retain the
230
+ administrative identity only in the separate migration job. Use the exact main
231
+ commit whose mandatory checks passed before releasing a new image.
232
+
233
+ For chart-managed PostgreSQL, select this boundary with:
234
+
235
+ ```yaml
236
+ postgres:
237
+ migrationsMode: verify
238
+ secret: postgres-bootstrap
239
+ runtimeSecret: postgres-runtime
240
+ ```
241
+
242
+ `runtimeSecret` must differ from the bootstrap Secret and contain the restricted
243
+ connection string under `url`. The chart injects it as POSTGRES_URL, sets
244
+ SUPERVISOR_MIGRATIONS=verify, and omits POSTGRES_ADMIN_URL from both application
245
+ containers. Bootstrap credentials remain on PostgreSQL. Apply mode keeps the
246
+ legacy adminUrl reference; an optional runtimeSecret can separately supply its
247
+ POSTGRES_URL. With postgres.enabled=false, explicit application env values
248
+ continue to configure external databases without generated PostgreSQL settings.
249
+ Do not add administrative credentials through env/envFrom when selecting verify.
212
250
 
213
251
  ## Environment (supervisor-sink / siteServer)
214
252
 
@@ -216,7 +254,8 @@ Set `SINK_DB_PROFILE=central|edge`. Migrations run at supervisor-sink startup vi
216
254
  |----------|---------|
217
255
  | `STOMP_URL` | RabbitMQ STOMP broker |
218
256
  | `POSTGRES_URL` | Application DB user (`supervisor_sink`) |
219
- | `POSTGRES_ADMIN_URL` | Admin user for migrations |
257
+ | `POSTGRES_ADMIN_URL` | Admin user for apply-mode migrations; omit in verify mode |
258
+ | `SUPERVISOR_MIGRATIONS` | `apply` (default) or `verify`; other values fail startup |
220
259
  | `SINK_DB_PROFILE` | `central` or `edge` |
221
260
  | `SINK_PIPELINES` | `false` skips STOMP segment, decision, and alert consumers. The site container sets this and a `supervisor-sink` container keeps the default |
222
261
  | `SEGMENT_REPAIR_HISTORY_ON_START` | `false` preserves closed historical boundaries at ingest startup. Omitted or `true` retains the existing same-name gap repair. Live ingest and silent-open handling remain active. |
@@ -596,3 +635,15 @@ Silent Modbus backup sources use `MX210/<device>` freshness identities. AMQP tel
596
635
  ### MQTT publisher recovery
597
636
 
598
637
  An unexpected MQTT publisher close/offline/error retires the session and notifies `onFatal` once. Intentional `stop()` does not notify failure. Modbus runtime tasks propagate publisher failure to collector `/ready` and `/live` (503). Run the collector under its liveness supervisor: it must retire the failed executor and create a fresh one, which reconnects after the broker returns. Uncertain publications are not automatically replayed. Broker restart regression covers failed health, old poll cleanup, and confirmed fresh measurements from the replacement executor.
638
+
639
+ ### Broker credential Secrets
640
+
641
+ Set `rabbitmq.definitionsSecret` to a separately provisioned Secret with the key
642
+ `definitions.json`; the chart then omits definitions from its ConfigMap. Set
643
+ `rabbitmq.runtimeSecret` for `AMQP_URL`, `STOMP_LOGIN` and `STOMP_PASSCODE`.
644
+ Other clients can reference the same or narrower Secrets explicitly. Anonymous
645
+ MQTT remains compatible by default; set `rabbitmq.extraConfig["mqtt.allow_anonymous"]`
646
+ to false only after all MQTT clients have authenticated credentials. A frozen
647
+ broker PodTemplate cannot adopt these mounts: unfreeze it for a controlled
648
+ restart while preserving the claim. Definitions import does not delete an
649
+ existing guest user; retire it explicitly after all dependent clients reconnect.
@@ -63,26 +63,41 @@ volumeMode: Filesystem
63
63
  - name: POSTGRES_URL
64
64
  valueFrom:
65
65
  secretKeyRef:
66
- name: {{ .Values.postgres.secret }}
66
+ name: {{ default .Values.postgres.secret .Values.postgres.runtimeSecret }}
67
67
  key: url
68
+ {{- if eq .Values.postgres.migrationsMode "verify" }}
69
+ - name: SUPERVISOR_MIGRATIONS
70
+ value: "verify"
71
+ {{- else }}
68
72
  - name: POSTGRES_ADMIN_URL
69
73
  valueFrom:
70
74
  secretKeyRef:
71
75
  name: {{ .Values.postgres.secret }}
72
76
  key: adminUrl
73
77
  {{- end }}
78
+ {{- end }}
74
79
  {{- if .Values.rabbitmq.enabled }}
75
80
  - name: STOMP_URL
76
81
  value: {{ printf "stomp://%s-rabbitmq:61613" (include "scada.prefix" .) | quote }}
82
+ {{- if .Values.rabbitmq.runtimeSecret }}
83
+ {{- range $name := list "STOMP_LOGIN" "STOMP_PASSCODE" "AMQP_URL" }}
84
+ - name: {{ $name }}
85
+ valueFrom:
86
+ secretKeyRef:
87
+ name: {{ $.Values.rabbitmq.runtimeSecret | quote }}
88
+ key: {{ $name }}
89
+ {{- end }}
90
+ {{- else }}
77
91
  - name: STOMP_LOGIN
78
92
  value: guest
79
93
  - name: STOMP_PASSCODE
80
94
  value: guest
81
- - name: STOMP_HOST
82
- value: /
83
95
  - name: AMQP_URL
84
96
  value: {{ printf "amqp://guest:guest@%s-rabbitmq:5672" (include "scada.prefix" .) | quote }}
85
97
  {{- end }}
98
+ - name: STOMP_HOST
99
+ value: /
100
+ {{- end }}
86
101
  {{- if .Values.clickhouse.enabled }}
87
102
  - name: CLICKHOUSE_HOST
88
103
  value: {{ printf "%s-clickhouse" (include "scada.prefix" .) | quote }}
@@ -1,4 +1,10 @@
1
1
  {{- if .Values.postgres.enabled }}
2
+ {{- if eq .Values.postgres.migrationsMode "verify" }}
3
+ {{- $runtimeSecret := required "postgres.runtimeSecret is required in verify mode" .Values.postgres.runtimeSecret }}
4
+ {{- if eq $runtimeSecret .Values.postgres.secret }}
5
+ {{- fail "postgres.runtimeSecret must differ from bootstrap postgres.secret in verify mode" }}
6
+ {{- end }}
7
+ {{- end }}
2
8
  apiVersion: v1
3
9
  kind: Service
4
10
  metadata:
@@ -76,12 +76,15 @@ data:
76
76
  rabbitmq.conf: |
77
77
  load_definitions = /etc/rabbitmq/definitions.json
78
78
  mqtt.listeners.tcp.default = 1883
79
- mqtt.allow_anonymous = true
79
+ mqtt.allow_anonymous = {{ if hasKey (.Values.rabbitmq.extraConfig | default dict) "mqtt.allow_anonymous" }}{{ get .Values.rabbitmq.extraConfig "mqtt.allow_anonymous" }}{{ else }}true{{ end }}
80
80
  mqtt.vhost = /
81
81
  mqtt.exchange = scada.telemetry
82
82
  {{- range $key, $value := .Values.rabbitmq.extraConfig }}
83
+ {{- if ne $key "mqtt.allow_anonymous" }}
83
84
  {{ $key }} = {{ $value }}
84
85
  {{- end }}
86
+ {{- end }}
87
+ {{- if not .Values.rabbitmq.definitionsSecret }}
85
88
  definitions.json: |
86
89
  {{- if ne .Values.rabbitmq.definitions nil }}
87
90
  {{ .Values.rabbitmq.definitions | toPrettyJson | nindent 4 }}
@@ -89,3 +92,4 @@ data:
89
92
  {{ $definitions | toPrettyJson | nindent 4 }}
90
93
  {{- end }}
91
94
  {{- end }}
95
+ {{- end }}
@@ -1,4 +1,7 @@
1
1
  {{- if .Values.rabbitmq.enabled }}
2
+ {{- if and .Values.rabbitmq.definitionsSecret .Values.rabbitmq.podTemplate }}
3
+ {{- fail "rabbitmq.definitionsSecret requires the managed pod template; unfreeze storage templates for the controlled migration restart" }}
4
+ {{- end }}
2
5
  apiVersion: v1
3
6
  kind: Service
4
7
  metadata:
@@ -98,12 +101,20 @@ spec:
98
101
  - name: config
99
102
  mountPath: /etc/rabbitmq/rabbitmq.conf
100
103
  subPath: rabbitmq.conf
101
- - name: config
104
+ - name: {{ ternary "definitions" "config" (not (empty .Values.rabbitmq.definitionsSecret)) }}
102
105
  mountPath: /etc/rabbitmq/definitions.json
103
106
  subPath: definitions.json
104
107
  - name: data
105
108
  mountPath: /var/lib/rabbitmq
106
109
  volumes:
110
+ {{- with .Values.rabbitmq.definitionsSecret }}
111
+ - name: definitions
112
+ secret:
113
+ secretName: {{ . | quote }}
114
+ items:
115
+ - key: definitions.json
116
+ path: definitions.json
117
+ {{- end }}
107
118
  - name: config
108
119
  configMap:
109
120
  name: {{ include "scada.brokerConfigName" . }}
@@ -35,7 +35,12 @@ spec:
35
35
  {{- include "scada.linkEnv" . | nindent 12 }}
36
36
  {{- range .Values.env }}
37
37
  - name: {{ .name }}
38
+ {{- if .valueFrom }}
39
+ valueFrom:
40
+ {{- toYaml .valueFrom | nindent 16 }}
41
+ {{- else }}
38
42
  value: {{ .value | quote }}
43
+ {{- end }}
39
44
  {{- end }}
40
45
  readinessProbe:
41
46
  tcpSocket:
@@ -115,8 +115,47 @@
115
115
  "podTemplate": {
116
116
  "type": "object",
117
117
  "description": "Opaque PodTemplateSpec override for preserving existing storage pods during adoption; empty uses generated defaults."
118
+ },
119
+ "migrationsMode": {
120
+ "type": "string",
121
+ "enum": [
122
+ "apply",
123
+ "verify"
124
+ ]
125
+ },
126
+ "runtimeSecret": {
127
+ "type": "string"
118
128
  }
119
- }
129
+ },
130
+ "allOf": [
131
+ {
132
+ "if": {
133
+ "required": [
134
+ "enabled",
135
+ "migrationsMode"
136
+ ],
137
+ "properties": {
138
+ "enabled": {
139
+ "const": true
140
+ },
141
+ "migrationsMode": {
142
+ "const": "verify"
143
+ }
144
+ }
145
+ },
146
+ "then": {
147
+ "required": [
148
+ "runtimeSecret"
149
+ ],
150
+ "properties": {
151
+ "runtimeSecret": {
152
+ "type": "string",
153
+ "minLength": 1
154
+ }
155
+ }
156
+ }
157
+ }
158
+ ]
120
159
  },
121
160
  "rabbitmq": {
122
161
  "type": "object",
@@ -124,6 +163,12 @@
124
163
  "podTemplate": {
125
164
  "type": "object",
126
165
  "description": "Opaque PodTemplateSpec override for preserving existing storage pods during adoption; empty uses generated defaults."
166
+ },
167
+ "definitionsSecret": {
168
+ "type": "string"
169
+ },
170
+ "runtimeSecret": {
171
+ "type": "string"
127
172
  }
128
173
  }
129
174
  }
@@ -30,6 +30,10 @@ postgres:
30
30
  enabled: true
31
31
  image: postgres:16
32
32
  secret: scada-postgres
33
+ # apply preserves startup migrations; verify uses a separately bootstrapped DB.
34
+ migrationsMode: apply
35
+ # Required and distinct from secret in verify mode; contains only runtime url.
36
+ runtimeSecret: ""
33
37
  # Existing PVC: no PVC or volumeClaimTemplate is created.
34
38
  existingClaim: ""
35
39
  env: {}
@@ -56,6 +60,9 @@ rabbitmq:
56
60
  configName: ""
57
61
  rolloutOnConfigChange: false
58
62
  definitions: null
63
+ # Secure definitions are provisioned separately; passwords never enter ConfigMaps.
64
+ definitionsSecret: ""
65
+ runtimeSecret: ""
59
66
  plugins: [rabbitmq_stomp, rabbitmq_mqtt, rabbitmq_management]
60
67
  extraConfig: {}
61
68
  resources: {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yarkivaev/scada",
3
- "version": "2.5.7",
3
+ "version": "2.5.8",
4
4
  "description": "SCADA domain objects, state persistence, and plant monitoring",
5
5
  "repository": {
6
6
  "type": "git",
@@ -6,7 +6,7 @@ import segmentPipeline, {
6
6
  } from '../infrastructure/ingest/pipelines/segmentPipeline.js';
7
7
  import decisionPipeline from '../infrastructure/ingest/pipelines/decisionPipeline.js';
8
8
  import alertPipeline from '../infrastructure/ingest/pipelines/alertPipeline.js';
9
- import migrate, { migratePrivileged } from '../infrastructure/ingest/db/migrate.js';
9
+ import migrate, { migratePrivileged, verifyMigrations } from '../infrastructure/ingest/db/migrate.js';
10
10
  import stateDataFromPool from '../infrastructure/persistence/stateDataFromPool.js';
11
11
  import { parseRequestTimeoutMs } from '../infrastructure/ingest/parseRequestTimeoutMs.js';
12
12
  import pg from 'pg';
@@ -38,6 +38,13 @@ function historyRepair(value) {
38
38
  return value === 'true';
39
39
  }
40
40
 
41
+ function migrationMode(value = 'apply') {
42
+ if (!['apply', 'verify'].includes(value)) {
43
+ throw new TypeError('SUPERVISOR_MIGRATIONS must be apply or verify');
44
+ }
45
+ return value;
46
+ }
47
+
41
48
  export function readDeploymentConfig(env) {
42
49
  const root = path.join(appRoot, '../..');
43
50
  const prefetch = Number(env.STOMP_PREFETCH || '128');
@@ -50,6 +57,7 @@ export function readDeploymentConfig(env) {
50
57
  stomp: env.STOMP_URL,
51
58
  postgres: env.POSTGRES_URL,
52
59
  postgresAdmin: env.POSTGRES_ADMIN_URL,
60
+ migrationsMode: migrationMode(env.SUPERVISOR_MIGRATIONS),
53
61
  apiPort: parseInt(
54
62
  env.STATE_HTTP_PORT || env.INTERNAL_API_PORT || '8081',
55
63
  10
@@ -92,6 +100,23 @@ function startPipelines(cfg, pool, health) {
92
100
  return Promise.all(pipelines.map((pipeline) => { return pipeline.start(); }));
93
101
  }
94
102
 
103
+ async function prepareDatabase(cfg, pool) {
104
+ if (cfg.migrationsMode === 'verify') {
105
+ await verifyMigrations(pool, cfg.migrationsDir, cfg.sinkDbProfile);
106
+ return;
107
+ }
108
+ if (!cfg.postgresAdmin) {
109
+ throw new Error('POSTGRES_ADMIN_URL is required for supervisor-sink database migrations');
110
+ }
111
+ const adminPool = holdClient(new pg.Pool({ connectionString: cfg.postgresAdmin }));
112
+ try {
113
+ await migrate(adminPool, cfg.migrationsDir, cfg.sinkDbProfile);
114
+ await migratePrivileged(adminPool, cfg.migrationsDir, cfg.sinkDbProfile);
115
+ } finally {
116
+ await adminPool.end();
117
+ }
118
+ }
119
+
95
120
  /**
96
121
  * Wires STOMP pipelines, migrations, and state data access from env config.
97
122
  * HTTP binding uses edgeApi (metrics, checkpoint, retention).
@@ -120,16 +145,7 @@ export default function supervisorSink(env = process.env, options = {}) {
120
145
  pool,
121
146
  dataAccess,
122
147
  async run(httpServer) {
123
- if (!cfg.postgresAdmin) {
124
- throw new Error('POSTGRES_ADMIN_URL is required for supervisor-sink database migrations');
125
- }
126
- const adminPool = holdClient(new pg.Pool({ connectionString: cfg.postgresAdmin }));
127
- try {
128
- await migrate(adminPool, cfg.migrationsDir, cfg.sinkDbProfile);
129
- await migratePrivileged(adminPool, cfg.migrationsDir, cfg.sinkDbProfile);
130
- } finally {
131
- await adminPool.end();
132
- }
148
+ await prepareDatabase(cfg, pool);
133
149
  if (cfg.pipelines) {
134
150
  await startPipelines(cfg, pool, health);
135
151
  }
@@ -78,3 +78,19 @@ export async function migratePrivileged(pool, migrationsDir, profile = 'central'
78
78
  await ensureMigrationsTable(pool);
79
79
  await applyMigrationFiles(pool, migrationsDir, profile, true);
80
80
  }
81
+
82
+
83
+ /** Checks the complete selected migration manifest without DDL or writes. */
84
+ export async function verifyMigrations(pool, migrationsDir, profile = 'edge', options = {}) {
85
+ const expected = (await fs.readdir(migrationsDir)).filter((name) => {
86
+ return migrationSelected(name, profile, false, options)
87
+ || migrationSelected(name, profile, true, options);
88
+ }).sort();
89
+ const { rows } = await pool.query(
90
+ 'SELECT version FROM public.schema_migrations WHERE version = ANY($1::text[])',
91
+ [expected]
92
+ );
93
+ const applied = new Set(rows.map(({ version }) => { return version; }));
94
+ const missing = expected.filter((name) => { return !applied.has(name); });
95
+ if (missing.length) { throw new Error(`Missing database migrations: ${missing.join(', ')}`); }
96
+ }