53 lines
7.0 KiB
TypeScript
53 lines
7.0 KiB
TypeScript
import assert from "node:assert/strict"
|
||
import { fileURLToPath } from "node:url"
|
||
import { canonicalWireguardSnapshot } from "./entity-snapshots.js"
|
||
import { wireguardInterfaceDependencyPaths } from "./wireguard-interface-delete.js"
|
||
import { wireguardPublicKey } from "./wireguard-interface-create.js"
|
||
import { validateWireguardRestoreSnapshot,materializeWireguardRestore,planWireguardRevisionRestore,verifyWireguardRestoreCommand,wireguardRestoreStateProof,type WireguardRestoreState } from "./wireguard-revision-restore.js"
|
||
|
||
export async function acceptWireguardRevisionRestoreModel(){
|
||
const key=Buffer.alloc(32,91).toString("base64"),publicKey=wireguardPublicKey(key)
|
||
const state:WireguardRestoreState={base:{interfaces:[{".id":"*a",name:"wg-old",disabled:"false",mtu:"1420","listen-port":"13231","private-key":key,"public-key":publicKey,opaque:"private-opaque"}],peers:[{".id":"*b",interface:"wg-old","public-key":publicKey,"allowed-address":"192.0.2.2/32",disabled:"false","endpoint-address":"old.example","endpoint-port":"54321","persistent-keepalive":"25s","client-address":"old","client-dns":"old","client-endpoint":"old",name:"old",comment:"old","preshared-key":"NEVER_TOUCH_PSK",opaque:"keep-peer"}],bindings:[],flow:{hostPublicKey:"",registered:false},serverProof:"source",proof:"base"},ipv4:[{".id":"*c",interface:"wg-old",address:"192.0.2.1/24",disabled:"false",dynamic:"false",comment:"old"}],ipv6:[],dependencies:Object.fromEntries(wireguardInterfaceDependencyPaths.map(path=>[path,[]])),filters:[],proof:""}
|
||
state.dependencies["/interface"].push({".id":"*a",name:"wg-old",type:"wg"})
|
||
const raw=canonicalWireguardSnapshot({interfaces:[{name:"wg-old",privateKey:"*****",listenPort:13232,mtu:1380,comment:"",disabled:false,address:"2001:db8::1/64",innerAddresses:[{address:"2001:db8::1/64",disabled:false,advertise:false,noDad:true,comment:""}],peers:[{publicKey,allowedAddresses:["2001:db8::2/128"],endpointAddress:"",endpointPort:"",persistentKeepalive:null,comment:"",name:"",disabled:false,privateKey:"*****",clientAddress:"",clientDns:"",clientEndpoint:""}]}]})
|
||
for(const broken of [null,{},[],{interfaces:null},{interfaces:[{}]},{...raw,foreign:true},{interfaces:[...raw.interfaces,...raw.interfaces]}])assert.throws(()=>validateWireguardRestoreSnapshot(broken))
|
||
const desired=materializeWireguardRestore(raw,state),commands=planWireguardRevisionRestore(desired,state)
|
||
assert.equal(commands[0].body?.disabled,"true");assert.equal(commands.at(-1)?.body?.disabled,"false")
|
||
assert.ok(commands.some(c=>c.method==="DELETE"&&c.id==="*c"));assert.ok(commands.some(c=>c.path==="/ipv6/address"&&c.body?.["no-dad"]==="true"))
|
||
const peerCommand=commands.find(c=>c.resource==="peers")!
|
||
for(const field of ["endpoint-address","comment","name","client-address","client-dns","client-endpoint"])assert.equal(peerCommand.body?.[field],"")
|
||
assert.equal(peerCommand.body?.["endpoint-port"],"0");assert.equal(peerCommand.body?.["persistent-keepalive"],"0")
|
||
assert.equal(peerCommand.body?.["private-key"],undefined);assert.equal(commands[0].body?.["private-key"],undefined)
|
||
const after=structuredClone(state);Object.assign(after.base.peers[0],peerCommand.body)
|
||
assert.equal(await verifyWireguardRestoreCommand(state,after,peerCommand),true)
|
||
after.base.peers[0]["preshared-key"]="changed";assert.equal(await verifyWireguardRestoreCommand(state,after,peerCommand),false)
|
||
const collateral=structuredClone(state);collateral.base.interfaces[0].opaque="changed";assert.notEqual(await wireguardRestoreStateProof(collateral),await wireguardRestoreStateProof(state))
|
||
const ipv6=structuredClone(state);ipv6.ipv6.push({".id":"*e",interface:"wg-old",address:"2001:db8::1/64",disabled:"false",dynamic:"false",advertise:"false","no-dad":"true",comment:""})
|
||
assert.match(await wireguardRestoreStateProof(ipv6),/^[a-f0-9]{64}$/,"IPv6 source is canonicalized once, never recanonicalized from a bracketed comparison key")
|
||
const ipv6Command=commands.find(c=>c.resource==="ipv6")!;assert.equal(await verifyWireguardRestoreCommand(state,ipv6,ipv6Command),true)
|
||
for(const mutate of [(s:WireguardRestoreState)=>{s.base.bindings.push({id:"user",interfaceName:"wg-old",peerPublicKey:publicKey})},(s:WireguardRestoreState)=>{s.base.interfaces[0].comment="MikrotikManager traffic-flow overlay"},(s:WireguardRestoreState)=>{s.dependencies["/interface/vlan"].push({".id":"*d",interface:"wg-old"})},(s:WireguardRestoreState)=>{s.dependencies["/routing/bgp/connection"].push({".id":"*d","local-address":"192.0.2.1"})}]){const protectedState=structuredClone(state);mutate(protectedState);assert.throws(()=>planWireguardRevisionRestore(desired,protectedState))}
|
||
const legacy=structuredClone(raw);delete legacy.interfaces[0].innerAddresses;const multiple=structuredClone(state);multiple.ipv6.push({".id":"*d",interface:"wg-old",address:"2001:db8::1/64",dynamic:"false",disabled:"false"});assert.throws(()=>materializeWireguardRestore(legacy,multiple))
|
||
assert.throws(()=>materializeWireguardRestore(raw,{...state,base:{...state.base,interfaces:[]}}))
|
||
const withoutPeer=structuredClone(desired);withoutPeer.interfaces[0].peers=[]
|
||
assert.throws(()=>planWireguardRevisionRestore(withoutPeer,state),/удалить peer WireGuard с настроенным PSK/)
|
||
assert.throws(()=>planWireguardRevisionRestore({interfaces:[]},state),/удалить peer WireGuard с настроенным PSK/)
|
||
const withPsk=structuredClone(desired);withPsk.interfaces[0].peers[0].presharedKeyConfigured=true
|
||
assert.deepEqual(validateWireguardRestoreSnapshot(withPsk),withPsk)
|
||
assert.deepEqual(planWireguardRevisionRestore(withPsk,state),commands)
|
||
const withoutPsk=structuredClone(state);delete withoutPsk.base.peers[0]["preshared-key"]
|
||
assert.throws(()=>planWireguardRevisionRestore(withPsk,withoutPsk),/существующий peer WireGuard с настроенным PSK/)
|
||
const missingPeer=structuredClone(state);missingPeer.base.peers=[]
|
||
assert.throws(()=>planWireguardRevisionRestore(withPsk,missingPeer),/существующий peer WireGuard с настроенным PSK/)
|
||
const newPeer=planWireguardRevisionRestore(desired,missingPeer).find(c=>c.resource==="peers")!
|
||
const withUnexpectedPsk=structuredClone(missingPeer)
|
||
withUnexpectedPsk.base.peers.push({".id":"*f",...newPeer.body,"preshared-key":"UNEXPECTED"})
|
||
assert.equal(await verifyWireguardRestoreCommand(missingPeer,withUnexpectedPsk,newPeer),false)
|
||
delete withUnexpectedPsk.base.peers[0]["preshared-key"]
|
||
assert.equal(await verifyWireguardRestoreCommand(missingPeer,withUnexpectedPsk,newPeer),true)
|
||
const deletions=planWireguardRevisionRestore({interfaces:[]},withoutPsk)
|
||
assert.deepEqual(deletions.map(c=>c.resource),["interfaces","peers","ipv4","interfaces"])
|
||
assert.equal(state.base.interfaces[0]["private-key"],key);assert.equal(state.base.peers[0]["preshared-key"],"NEVER_TOUCH_PSK")
|
||
console.log("WireGuard revision restore model: schema, clearing, keys, IPv6, deletion closure, dependencies and private conservation passed")
|
||
}
|
||
if(process.argv[1]===fileURLToPath(import.meta.url))await acceptWireguardRevisionRestoreModel()
|