bmux_cluster_plugin 0.0.1-alpha.3

Shipped server clusters plugin for bmux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//! Production voter-set reconciliation for membership workflows.

use crate::consensus_network::ConsensusNodeRegistry;
use crate::consensus_runtime::{ConsensusNode, ConsensusWriteError};
use crate::membership::NodeId;
use bmux_cluster_plugin_api::cluster_types::{
    ClusterConsensusRole, ClusterMember, ClusterMemberState, ConsensusVoterChangeAction,
    ConsensusVoterChangeAuthorization,
};
use openraft::BasicNode;
use sha2::Digest as _;
use std::collections::BTreeMap;

const VOTER_CHANGE_DOMAIN: &[u8] = b"bmux.cluster.voter-change.v1\0";

fn authorization_payload(
    cluster_id: &str,
    action: ConsensusVoterChangeAction,
    target_node_id: &str,
    actor_node_id: &str,
) -> Result<Vec<u8>, String> {
    let action = match action {
        ConsensusVoterChangeAction::Add => b"add".as_slice(),
        ConsensusVoterChangeAction::Remove => b"remove".as_slice(),
    };
    let mut payload = VOTER_CHANGE_DOMAIN.to_vec();
    for field in [
        cluster_id.as_bytes(),
        action,
        target_node_id.as_bytes(),
        actor_node_id.as_bytes(),
    ] {
        let length = u64::try_from(field.len())
            .map_err(|_| "voter-change authorization field is too large".to_string())?;
        payload.extend_from_slice(&length.to_be_bytes());
        payload.extend_from_slice(field);
    }
    Ok(payload)
}

/// Signs one voter-set transition authorization with the local node identity.
///
/// # Errors
///
/// Returns an error if canonical authorization encoding fails.
pub fn authorize_voter_change(
    cluster_id: &str,
    action: ConsensusVoterChangeAction,
    target_node_id: &str,
    identity: &crate::membership::NodeIdentity,
) -> Result<ConsensusVoterChangeAuthorization, String> {
    let actor_node_id = identity.node_id().to_string();
    let signature = identity.sign(&authorization_payload(
        cluster_id,
        action,
        target_node_id,
        &actor_node_id,
    )?);
    Ok(ConsensusVoterChangeAuthorization {
        cluster_id: cluster_id.to_string(),
        action,
        target_node_id: target_node_id.to_string(),
        actor_node_id,
        signature,
    })
}

/// Verifies that a voter-change authorization is signed by an active voter.
///
/// # Errors
///
/// Returns cluster, action, target, actor, credential, or signature failures.
pub fn verify_voter_change_authorization(
    authorization: &ConsensusVoterChangeAuthorization,
    expected_cluster_id: &str,
    expected_action: ConsensusVoterChangeAction,
    expected_target_node_id: NodeId,
    members: &[ClusterMember],
) -> Result<(), String> {
    if authorization.cluster_id != expected_cluster_id {
        return Err("voter-change authorization belongs to another cluster".to_string());
    }
    if authorization.action != expected_action {
        return Err("voter-change authorization has the wrong action".to_string());
    }
    if authorization.target_node_id != expected_target_node_id.to_string() {
        return Err("voter-change authorization has the wrong target".to_string());
    }
    let actor = members
        .iter()
        .find(|member| member.node_id == authorization.actor_node_id)
        .ok_or_else(|| "voter-change authorization actor is not a member".to_string())?;
    if actor.state != ClusterMemberState::Active
        || actor.capabilities.consensus_role != ClusterConsensusRole::Voter
    {
        return Err("voter-change authorization actor is not an active voter".to_string());
    }
    crate::membership::verify_membership_credential(actor, crate::now_unix_ms())?;
    crate::membership::verify_node_signature(
        &actor.node_id,
        &authorization_payload(
            &authorization.cluster_id,
            authorization.action,
            &authorization.target_node_id,
            &authorization.actor_node_id,
        )?,
        &authorization.signature,
    )
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VoterSetPlan {
    pub voters: BTreeMap<NodeId, BasicNode>,
}

/// Builds the exact active voter set from authenticated membership records.
///
/// # Errors
///
/// Fails closed for malformed node IDs, expired/invalid credentials, or active
/// voters without a stable endpoint.
pub fn plan_active_voters_ref<'a>(
    members: impl IntoIterator<Item = &'a ClusterMember>,
) -> Result<VoterSetPlan, String> {
    let mut voters = BTreeMap::new();
    for member in members {
        if member.state != ClusterMemberState::Active
            || member.capabilities.consensus_role != ClusterConsensusRole::Voter
        {
            continue;
        }
        crate::membership::verify_membership_credential(member, crate::now_unix_ms())?;
        let node_id = member.node_id.parse::<NodeId>()?;
        let endpoint = member
            .endpoint
            .as_deref()
            .map(str::trim)
            .filter(|endpoint| !endpoint.is_empty())
            .ok_or_else(|| format!("active voter {node_id} has no advertised endpoint"))?;
        crate::membership::validate_advertised_endpoint(endpoint)?;
        if let Some((existing_id, _)) = voters
            .iter()
            .find(|(_, existing): &(&NodeId, &BasicNode)| existing.addr == endpoint)
        {
            return Err(format!(
                "active voters {existing_id} and {node_id} share advertised endpoint '{endpoint}'"
            ));
        }
        if voters.insert(node_id, BasicNode::new(endpoint)).is_some() {
            return Err(format!("duplicate active voter {node_id}"));
        }
    }
    if voters.is_empty() {
        return Err("membership transition would remove every consensus voter".to_string());
    }
    Ok(VoterSetPlan { voters })
}

/// Owned-member convenience wrapper for [`plan_active_voters_ref`].
///
/// # Errors
///
/// Returns the same validation failures as [`plan_active_voters_ref`].
pub fn plan_active_voters(
    members: impl IntoIterator<Item = ClusterMember>,
) -> Result<VoterSetPlan, String> {
    let members = members.into_iter().collect::<Vec<_>>();
    plan_active_voters_ref(&members)
}

/// Validates one exact voter-set transition.
///
/// Existing active voter endpoints are immutable within the transition; adding
/// and removing voters remains allowed.
///
/// # Errors
///
/// Returns credential, endpoint, identity, empty-set, or endpoint-change errors.
pub fn validate_membership_transition(
    current: &[ClusterMember],
    prospective: &[ClusterMember],
) -> Result<VoterSetPlan, String> {
    let current_plan = plan_active_voters_ref(current)?;
    let prospective_plan = plan_active_voters_ref(prospective)?;
    for (node_id, node) in &current_plan.voters {
        if let Some(replacement) = prospective_plan.voters.get(node_id)
            && replacement != node
        {
            return Err(format!(
                "consensus voter {node_id} endpoint cannot change during a membership transition"
            ));
        }
    }
    Ok(prospective_plan)
}

/// Reconciles a provided membership snapshot through the active local node.
///
/// This transaction primitive lets join, leave, and revoke transition Raft
/// around publication of plugin membership as required by each workflow.
///
/// # Errors
///
/// Returns registry, leader, quorum, credential, endpoint, or membership errors.
pub async fn reconcile_members(
    members: impl IntoIterator<Item = ClusterMember>,
    local_node_id: NodeId,
    nodes: &ConsensusNodeRegistry,
) -> Result<(), String> {
    let plan = plan_active_voters(members)?;
    change_voters(
        nodes
            .active(local_node_id)
            .map_err(|error| service_error(&error))?,
        plan,
    )
    .await
}

/// Safely removes one voter from the current authenticated membership plan.
///
/// The supplied node may already be absent from the voter set, making retries
/// idempotent. The transition rejects removal of the final voter and validates
/// all remaining voter credentials and endpoints before contacting `OpenRaft`.
///
/// # Errors
///
/// Returns when the node is not an active voter, when the resulting plan is
/// invalid, or when the consensus transition cannot commit.
pub async fn remove_voter(
    members: impl IntoIterator<Item = ClusterMember>,
    remove_node_id: NodeId,
    local_node_id: NodeId,
    nodes: &ConsensusNodeRegistry,
) -> Result<(), String> {
    let mut known = false;
    let prospective = members.into_iter().map(|mut member| {
        if member.node_id == remove_node_id.to_string() {
            known = true;
            if member.state == ClusterMemberState::Active
                && member.capabilities.consensus_role == ClusterConsensusRole::Voter
            {
                member.state = ClusterMemberState::Left;
            }
        }
        member
    });
    let plan = plan_active_voters(prospective)?;
    if !known {
        return Err(format!("consensus member {remove_node_id} is unknown"));
    }
    change_voters(
        nodes
            .active(local_node_id)
            .map_err(|error| service_error(&error))?,
        plan,
    )
    .await
}

/// Publishes signed membership records through deterministic idempotent
/// control commands.
///
/// # Errors
///
/// Returns control-plane leader, quorum, validation, or storage failures.
pub async fn publish_members(
    node: ConsensusNode,
    principal_id: &str,
    members: impl IntoIterator<Item = ClusterMember>,
) -> Result<(), String> {
    let mut members = members.into_iter().collect::<Vec<_>>();
    members.sort_by(|left, right| left.node_id.cmp(&right.node_id));
    for member in members {
        let command_id = membership_command_id(&member)?;
        let command = bmux_cluster_plugin_api::cluster_types::ControlCommand {
            schema_version: crate::control_state::CONTROL_SCHEMA_VERSION,
            principal_id: principal_id.to_string(),
            command_id: bmux_cluster_plugin_api::cluster_types::CommandId { value: command_id },
            issued_at_unix_ms: member.updated_at_unix_ms,
            request: bmux_cluster_plugin_api::cluster_types::ControlCommandRequest::UpsertMember {
                member,
            },
        };
        node.mutate(command)
            .await
            .map_err(|error| format!("replicated membership mutation failed: {error:?}"))?;
    }
    Ok(())
}

fn membership_command_id(member: &ClusterMember) -> Result<uuid::Uuid, String> {
    let mut digest = sha2::Sha256::new();
    digest.update(b"bmux.cluster.membership-command.v1\0");
    digest.update(member.cluster_id.as_bytes());
    digest.update(member.node_id.as_bytes());
    digest.update(member.credential_serial.as_bytes());
    digest.update(format!("{:?}", member.state).as_bytes());
    let bytes: [u8; 32] = digest.finalize().into();
    Ok(uuid::Uuid::from_bytes(bytes[..16].try_into().map_err(
        |_| "membership command digest does not contain a UUID prefix".to_string(),
    )?))
}

/// Applies a previously validated voter plan.
///
/// # Errors
///
/// Returns leader, quorum, storage, or membership transition failures.
pub async fn change_voters(node: ConsensusNode, plan: VoterSetPlan) -> Result<(), String> {
    node.change_voters(plan.voters)
        .await
        .map(|_| ())
        .map_err(|error| write_error(&error))
}

fn service_error(error: &bmux_cluster_plugin_api::cluster_types::ControlServiceError) -> String {
    format!("consensus voter transition is unavailable: {error:?}")
}

fn write_error(error: &ConsensusWriteError) -> String {
    format!("consensus voter transition failed: {error}")
}

#[cfg(test)]
mod tests {
    use super::*;
    use bmux_cluster_plugin_api::cluster_types::{
        ClusterNegotiatedProtocol, ClusterNodeCapabilities,
    };

    fn member(id: NodeId, role: ClusterConsensusRole, state: ClusterMemberState) -> ClusterMember {
        let identity = crate::membership::NodeIdentity::new_for_test(u64::from(id.as_bytes()[31]));
        let cluster_id = "cluster:00000000-0000-0000-0000-000000000001"
            .parse::<crate::membership::ClusterId>()
            .unwrap();
        let now = crate::now_unix_ms();
        let mut member = crate::membership::issue_membership_credential(
            &identity,
            cluster_id,
            id.to_string(),
            id.public_key().unwrap().to_string(),
            ClusterNodeCapabilities {
                consensus_role: role,
                worker: true,
                ingress: false,
            },
            ClusterNegotiatedProtocol {
                wire_epoch: 1,
                peer_revision: 1,
                schema_version: 1,
                local_plugin_version: "test".to_string(),
                remote_plugin_version: "test".to_string(),
                features: Vec::new(),
            },
            now,
        )
        .unwrap();
        member.state = state;
        member.endpoint = Some(format!("tls://node-{}:7443", id.as_bytes()[31]));
        member
    }

    #[test]
    fn plans_only_active_voters_and_requires_endpoints() {
        let first = NodeId::from(1);
        let second = NodeId::from(2);
        let plan = plan_active_voters([
            member(
                first,
                ClusterConsensusRole::Voter,
                ClusterMemberState::Active,
            ),
            member(
                second,
                ClusterConsensusRole::ObserverEdge,
                ClusterMemberState::Active,
            ),
        ])
        .unwrap();
        assert_eq!(plan.voters.keys().copied().collect::<Vec<_>>(), vec![first]);

        let mut missing = member(
            first,
            ClusterConsensusRole::Voter,
            ClusterMemberState::Active,
        );
        missing.endpoint = None;
        assert!(
            plan_active_voters([missing])
                .unwrap_err()
                .contains("endpoint")
        );
    }

    #[test]
    fn refuses_empty_voter_set() {
        let observer = member(
            NodeId::from(2),
            ClusterConsensusRole::ObserverEdge,
            ClusterMemberState::Active,
        );
        assert!(
            plan_active_voters([observer])
                .unwrap_err()
                .contains("every consensus voter")
        );
    }

    #[test]
    fn ignores_revoked_and_left_voters() {
        let active = NodeId::from(1);
        let plan = plan_active_voters([
            member(
                active,
                ClusterConsensusRole::Voter,
                ClusterMemberState::Active,
            ),
            member(
                NodeId::from(2),
                ClusterConsensusRole::Voter,
                ClusterMemberState::Revoked,
            ),
            member(
                NodeId::from(3),
                ClusterConsensusRole::Voter,
                ClusterMemberState::Left,
            ),
        ])
        .unwrap();
        assert_eq!(
            plan.voters.keys().copied().collect::<Vec<_>>(),
            vec![active]
        );
    }

    #[test]
    fn rejects_invalid_active_voter_credential() {
        let mut voter = member(
            NodeId::from(1),
            ClusterConsensusRole::Voter,
            ClusterMemberState::Active,
        );
        voter.credential_signature = "00".to_string();
        assert!(
            plan_active_voters([voter])
                .unwrap_err()
                .contains("signature")
        );
    }

    #[test]
    fn voter_change_authorization_is_target_and_action_bound() {
        let current = member(
            NodeId::from(1),
            ClusterConsensusRole::Voter,
            ClusterMemberState::Active,
        );
        let target = member(
            NodeId::from(2),
            ClusterConsensusRole::Voter,
            ClusterMemberState::Active,
        );
        let actor_identity = crate::membership::NodeIdentity::new_for_test(1);
        let authorization = authorize_voter_change(
            &current.cluster_id,
            ConsensusVoterChangeAction::Add,
            &target.node_id,
            &actor_identity,
        )
        .unwrap();
        verify_voter_change_authorization(
            &authorization,
            &current.cluster_id,
            ConsensusVoterChangeAction::Add,
            target.node_id.parse().unwrap(),
            std::slice::from_ref(&current),
        )
        .unwrap();

        let mut tampered = authorization;
        tampered.target_node_id = current.node_id.clone();
        assert!(
            verify_voter_change_authorization(
                &tampered,
                &current.cluster_id,
                ConsensusVoterChangeAction::Add,
                target.node_id.parse().unwrap(),
                std::slice::from_ref(&current),
            )
            .is_err()
        );
    }

    #[test]
    fn membership_command_identity_is_stable_and_transition_specific() {
        let active = member(
            NodeId::from(1),
            ClusterConsensusRole::Voter,
            ClusterMemberState::Active,
        );
        assert_eq!(
            membership_command_id(&active).unwrap(),
            membership_command_id(&active).unwrap()
        );
        let mut left = active.clone();
        left.state = ClusterMemberState::Left;
        assert_ne!(
            membership_command_id(&active).unwrap(),
            membership_command_id(&left).unwrap()
        );
        let mut rotated = active.clone();
        rotated.credential_serial = "rotated".to_string();
        assert_ne!(
            membership_command_id(&active).unwrap(),
            membership_command_id(&rotated).unwrap()
        );
    }

    #[test]
    fn active_voter_plan_rejects_duplicate_and_noncanonical_endpoints() {
        let first = member(
            NodeId::from(1),
            ClusterConsensusRole::Voter,
            ClusterMemberState::Active,
        );
        let mut duplicate = member(
            NodeId::from(2),
            ClusterConsensusRole::Voter,
            ClusterMemberState::Active,
        );
        duplicate.endpoint.clone_from(&first.endpoint);
        assert!(
            plan_active_voters([first.clone(), duplicate])
                .unwrap_err()
                .contains("share advertised endpoint")
        );

        let mut wrong_identity = first.clone();
        wrong_identity.node_id = "node:not-a-key".to_string();
        assert!(plan_active_voters([wrong_identity]).is_err());

        let mut local = first.clone();
        local.endpoint = Some("local".to_string());
        assert!(
            plan_active_voters([local])
                .unwrap_err()
                .contains("node-local")
        );
        let mut malformed = first;
        malformed.endpoint = Some("tls://".to_string());
        assert!(
            plan_active_voters([malformed])
                .unwrap_err()
                .contains("authority")
        );
    }

    #[test]
    fn transition_allows_add_remove_and_rejects_endpoint_rewrite() {
        let first = member(
            NodeId::from(1),
            ClusterConsensusRole::Voter,
            ClusterMemberState::Active,
        );
        let second = member(
            NodeId::from(2),
            ClusterConsensusRole::Voter,
            ClusterMemberState::Active,
        );
        validate_membership_transition(
            std::slice::from_ref(&first),
            &[first.clone(), second.clone()],
        )
        .unwrap();
        let mut removed = first.clone();
        removed.state = ClusterMemberState::Left;
        validate_membership_transition(&[first, second.clone()], &[removed, second.clone()])
            .unwrap();

        let mut rewritten = second.clone();
        rewritten.endpoint = Some("tls://different:7443".to_string());
        assert!(
            validate_membership_transition(std::slice::from_ref(&second), &[rewritten])
                .unwrap_err()
                .contains("cannot change")
        );
    }
}