|
|
@@ -22,12 +22,25 @@ const endpoint = z.object({
|
|
|
|
outerAddress: z.string().refine(value => isIP(value) === 4 && value !== "0.0.0.0", "Требуется фиксированный внешний IPv4"),
|
|
|
|
outerAddress: z.string().refine(value => isIP(value) === 4 && value !== "0.0.0.0", "Требуется фиксированный внешний IPv4"),
|
|
|
|
}).strict()
|
|
|
|
}).strict()
|
|
|
|
const schema = z.object({ endpoints: z.array(endpoint).length(2) }).strict()
|
|
|
|
const schema = z.object({ endpoints: z.array(endpoint).length(2) }).strict()
|
|
|
|
const stepSchema = schema.extend({ side: z.union([z.literal(0), z.literal(1)]), ipsecSecret: z.string().regex(/^[a-f0-9]{64}$/), beforeFingerprint: z.string() })
|
|
|
|
const writePath = z.enum([greIpsecPaths.filter, greIpsecPaths.profile, greIpsecPaths.peer, greIpsecPaths.identity, greIpsecPaths.policy])
|
|
|
|
|
|
|
|
export const greIke2StepSchema = schema.extend({
|
|
|
|
|
|
|
|
side: z.union([z.literal(0), z.literal(1)]),
|
|
|
|
|
|
|
|
ipsecSecret: z.string().regex(/^[a-f0-9]{64}$/),
|
|
|
|
|
|
|
|
beforeFingerprint: z.string(),
|
|
|
|
|
|
|
|
writes: z.array(z.discriminatedUnion("action", [
|
|
|
|
|
|
|
|
z.object({ path: writePath, action: z.literal("create") }).strict(),
|
|
|
|
|
|
|
|
z.object({ path: z.literal(greIpsecPaths.policy), action: z.literal("update"), id: z.string().regex(/^\*[a-z0-9]+$/i) }).strict(),
|
|
|
|
|
|
|
|
])).max(6),
|
|
|
|
|
|
|
|
})
|
|
|
|
type Pair = z.infer<typeof schema>
|
|
|
|
type Pair = z.infer<typeof schema>
|
|
|
|
type Side = 0 | 1
|
|
|
|
type Side = 0 | 1
|
|
|
|
type Fields = Record<string, string>
|
|
|
|
type Fields = Record<string, string>
|
|
|
|
type Catalogue = Record<string, Fields[]>
|
|
|
|
type Catalogue = Record<string, Fields[]>
|
|
|
|
type RouterWrite = { path: string; fields: Fields; id?: string }
|
|
|
|
type RouterWrite = { path: string; fields: Fields; id?: string }
|
|
|
|
|
|
|
|
export function greIke2StepInput(input: Pair, side: Side, ipsecSecret: string, beforeFingerprint: string, commands: RouterWrite[]) {
|
|
|
|
|
|
|
|
const writes = commands.map(({ path, id }) => id ? { path, id, action: "update" } : { path, action: "create" })
|
|
|
|
|
|
|
|
return greIke2StepSchema.parse({ endpoints: input.endpoints, side, ipsecSecret, beforeFingerprint, writes })
|
|
|
|
|
|
|
|
}
|
|
|
|
const sides = [0, 1] as const
|
|
|
|
const sides = [0, 1] as const
|
|
|
|
const paths = Object.values(greIpsecPaths)
|
|
|
|
const paths = Object.values(greIpsecPaths)
|
|
|
|
const runtimeFields: Readonly<Record<string, ReadonlySet<string>>> = {
|
|
|
|
const runtimeFields: Readonly<Record<string, ReadonlySet<string>>> = {
|
|
|
@@ -193,14 +206,16 @@ async function readSide(input: Pair, side: Side) {
|
|
|
|
export function registerGreIke2PairActions(register: typeof add) {
|
|
|
|
export function registerGreIke2PairActions(register: typeof add) {
|
|
|
|
registerMcpService(operation, {
|
|
|
|
registerMcpService(operation, {
|
|
|
|
current: async raw => {
|
|
|
|
current: async raw => {
|
|
|
|
const input = stepSchema.parse(raw)
|
|
|
|
const input = greIke2StepSchema.parse(raw)
|
|
|
|
return await Promise.all(sides.map(async side => (await readSide(input, side)).proof))
|
|
|
|
return await Promise.all(sides.map(async side => (await readSide(input, side)).proof))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
execute: async raw => {
|
|
|
|
execute: async raw => {
|
|
|
|
const input = stepSchema.parse(raw), side = input.side, state = await readSide(input, side)
|
|
|
|
const input = greIke2StepSchema.parse(raw), side = input.side, state = await readSide(input, side)
|
|
|
|
if (state.proof !== input.beforeFingerprint) throw new McpError("STATE_CONFLICT", "GRE или IPsec изменился после подготовки", 409)
|
|
|
|
if (state.proof !== input.beforeFingerprint) throw new McpError("STATE_CONFLICT", "GRE или IPsec изменился после подготовки", 409)
|
|
|
|
const desired = greIke2SideCommands(input, side, input.ipsecSecret)
|
|
|
|
const desired = greIke2SideCommands(input, side, input.ipsecSecret)
|
|
|
|
const commands = planGreIke2Side(input, side, state.catalogs, desired)
|
|
|
|
const commands = planGreIke2Side(input, side, state.catalogs, desired)
|
|
|
|
|
|
|
|
const reviewed = greIke2StepInput(input, side, input.ipsecSecret, state.proof, commands).writes
|
|
|
|
|
|
|
|
if (fingerprint(reviewed) !== fingerprint(input.writes)) throw new McpError("STATE_CONFLICT", "Подготовленные команды IKEv2 отличаются от плана", 409)
|
|
|
|
let before = greIke2StableCatalogues(state.catalogs), confirmed = 0, verified = 0
|
|
|
|
let before = greIke2StableCatalogues(state.catalogs), confirmed = 0, verified = 0
|
|
|
|
for (const command of commands) {
|
|
|
|
for (const command of commands) {
|
|
|
|
let dispatched = false
|
|
|
|
let dispatched = false
|
|
|
@@ -234,14 +249,14 @@ export function registerGreIke2PairActions(register: typeof add) {
|
|
|
|
return { verified: true, commandCount: commands.length, afterFingerprint, greEnabled: false, forwardingVerified: false }
|
|
|
|
return { verified: true, commandCount: commands.length, afterFingerprint, greEnabled: false, forwardingVerified: false }
|
|
|
|
},
|
|
|
|
},
|
|
|
|
verify: async (raw, result) => {
|
|
|
|
verify: async (raw, result) => {
|
|
|
|
const input = stepSchema.parse(raw)
|
|
|
|
const input = greIke2StepSchema.parse(raw)
|
|
|
|
const receipt = result as { verified?: boolean; afterFingerprint?: string } | null
|
|
|
|
const receipt = result as { verified?: boolean; afterFingerprint?: string } | null
|
|
|
|
return receipt?.verified === true && receipt.afterFingerprint === completedPairProof(input, input.side,
|
|
|
|
return receipt?.verified === true && receipt.afterFingerprint === completedPairProof(input, input.side,
|
|
|
|
(await readSide(input, input.side)).catalogs, greIke2SideCommands(input, input.side, input.ipsecSecret))
|
|
|
|
(await readSide(input, input.side)).catalogs, greIke2SideCommands(input, input.side, input.ipsecSecret))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
register(operation, "mm_gre_ikev2_pair_configure_plan", "План настройки отдельного IKEv2 для двух существующих GRE", "network", "mm:network:write", "write", "servers", schema, {
|
|
|
|
register(operation, "mm_gre_ikev2_pair_configure_plan", "План настройки отдельного IKEv2 для двух существующих GRE", "network", "mm:network:write", "write", "servers", schema, {
|
|
|
|
version: 2,
|
|
|
|
version: 3,
|
|
|
|
target: raw => target(schema.parse(raw)),
|
|
|
|
target: raw => target(schema.parse(raw)),
|
|
|
|
prepare: async raw => {
|
|
|
|
prepare: async raw => {
|
|
|
|
const input = schema.parse(raw), scope = target(input)
|
|
|
|
const input = schema.parse(raw), scope = target(input)
|
|
|
@@ -250,10 +265,9 @@ export function registerGreIke2PairActions(register: typeof add) {
|
|
|
|
validateGreIke2Defaults(greIke2DefaultProposal(catalogs[0]), greIke2DefaultProposal(catalogs[1]))
|
|
|
|
validateGreIke2Defaults(greIke2DefaultProposal(catalogs[0]), greIke2DefaultProposal(catalogs[1]))
|
|
|
|
const ipsecSecret = pairSecret(input, catalogs)
|
|
|
|
const ipsecSecret = pairSecret(input, catalogs)
|
|
|
|
const changes = sides.map(side => planGreIke2Side(input, side, catalogs[side], greIke2SideCommands(input, side, ipsecSecret)))
|
|
|
|
const changes = sides.map(side => planGreIke2Side(input, side, catalogs[side], greIke2SideCommands(input, side, ipsecSecret)))
|
|
|
|
const steps = sides.map(side => {
|
|
|
|
const steps = sides.map(side => ({ kind: "service" as const, service: operation,
|
|
|
|
const writes = changes[side].map(command => ({ path: command.path, id: command.id, action: command.id ? "update" : "create" }))
|
|
|
|
input: greIke2StepInput(input, side, ipsecSecret, before[side].proof, changes[side]),
|
|
|
|
return { kind: "service" as const, service: operation, input: { ...input, side, ipsecSecret, beforeFingerprint: before[side].proof, writes } }
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
|
|
|
const objects = (await Promise.all(before.map(state => greMutationProtectionObjects(Number(state.server.id), state.gre)))).flat()
|
|
|
|
const objects = (await Promise.all(before.map(state => greMutationProtectionObjects(Number(state.server.id), state.gre)))).flat()
|
|
|
|
return { target: scope, objects, snapshots: [], before: before.map(state => state.proof), steps, mutationCost: Math.max(1, changes.flat().length), requiresHuman: true,
|
|
|
|
return { target: scope, objects, snapshots: [], before: before.map(state => state.proof), steps, mutationCost: Math.max(1, changes.flat().length), requiresHuman: true,
|
|
|
|
warnings: [
|
|
|
|
warnings: [
|
|
|
|