2024-12-24 15:25:16 +09:00
|
|
|
import { BPSet, BPSetMetadata } from "./types";
|
|
|
|
import { readdir, readFile } from 'node:fs/promises'
|
|
|
|
import path from 'node:path'
|
|
|
|
|
|
|
|
export class BPManager {
|
|
|
|
private static _instance = new BPManager()
|
|
|
|
|
|
|
|
public static getInstance = () =>
|
|
|
|
this._instance
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
private readonly bpSets:
|
2024-12-26 13:06:44 +09:00
|
|
|
Record<string, BPSet> = {}
|
2024-12-24 15:25:16 +09:00
|
|
|
|
|
|
|
private readonly bpSetMetadatas:
|
|
|
|
Record<string, BPSetMetadata> = {}
|
|
|
|
|
|
|
|
private constructor() {
|
|
|
|
this.loadBPSets()
|
|
|
|
this.loadBPSetMetadatas()
|
|
|
|
}
|
|
|
|
|
|
|
|
private async loadBPSets() {
|
|
|
|
const bpSetFiles = await readdir('./dist/bpsets', {
|
|
|
|
recursive: true,
|
|
|
|
withFileTypes: true
|
|
|
|
})
|
|
|
|
|
|
|
|
for (const bpSetFile of bpSetFiles) {
|
|
|
|
if (bpSetFile.isDirectory())
|
|
|
|
continue
|
|
|
|
|
|
|
|
const bpSetPath = path.join(bpSetFile.parentPath, bpSetFile.name)
|
2024-12-26 13:06:44 +09:00
|
|
|
const bpSetClasses = await import('../' + bpSetPath) as Record<string, new () => BPSet>
|
2024-12-24 15:25:16 +09:00
|
|
|
|
|
|
|
for (const bpSetClass of Object.keys(bpSetClasses))
|
2024-12-26 13:06:44 +09:00
|
|
|
this.bpSets[bpSetClass] = new bpSetClasses[bpSetClass]()
|
2024-12-24 15:25:16 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async loadBPSetMetadatas() {
|
|
|
|
const bpSetMetadatasRaw = await readFile('./bpset_metadata.json')
|
|
|
|
const bpSetMetadatas = JSON.parse(bpSetMetadatasRaw.toString('utf-8')) as BPSetMetadata[]
|
|
|
|
|
|
|
|
for (const [idx, bpSetMetadata] of bpSetMetadatas.entries()) {
|
|
|
|
this.bpSetMetadatas[bpSetMetadata.name] = {
|
|
|
|
...bpSetMetadata,
|
|
|
|
nonCompliantResources: [],
|
|
|
|
compliantResources: [],
|
|
|
|
status:'LOADED',
|
2024-12-26 13:06:44 +09:00
|
|
|
errorMessage: [],
|
2024-12-24 15:25:16 +09:00
|
|
|
idx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-30 11:19:53 +09:00
|
|
|
public runCheckOnce(name: string) {
|
|
|
|
return this
|
|
|
|
.bpSets[name].check()
|
|
|
|
.catch((err) => {
|
|
|
|
this.bpSetMetadatas[name].status = 'ERROR'
|
|
|
|
this.bpSetMetadatas[name].errorMessage.push({
|
|
|
|
date: new Date(),
|
|
|
|
message: err
|
2024-12-26 13:06:44 +09:00
|
|
|
})
|
|
|
|
|
2024-12-30 11:19:53 +09:00
|
|
|
return undefined
|
|
|
|
})
|
|
|
|
.then((result) => {
|
|
|
|
if (result === undefined)
|
|
|
|
return
|
2024-12-26 13:06:44 +09:00
|
|
|
|
2024-12-30 11:19:53 +09:00
|
|
|
this.bpSetMetadatas[name].compliantResources = result.compliantResources
|
|
|
|
this.bpSetMetadatas[name].nonCompliantResources = result.nonCompliantResources
|
|
|
|
this.bpSetMetadatas[name].status = 'FINISHED'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
public runCheckAll(finished = (name: string) => {}) {
|
|
|
|
const checkJobs =
|
|
|
|
Object
|
|
|
|
.values(this.bpSetMetadatas)
|
|
|
|
.map(({ name }) => {
|
|
|
|
this.bpSetMetadatas[name].status = 'CHECKING'
|
|
|
|
|
|
|
|
return this
|
|
|
|
.bpSets[name].check()
|
|
|
|
.catch((err) => {
|
|
|
|
this.bpSetMetadatas[name].status = 'ERROR'
|
|
|
|
this.bpSetMetadatas[name].errorMessage.push({
|
|
|
|
date: new Date(),
|
|
|
|
message: err
|
|
|
|
})
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
})
|
|
|
|
.then((result) => {
|
|
|
|
if (result === undefined)
|
|
|
|
return
|
|
|
|
|
|
|
|
this.bpSetMetadatas[name].compliantResources = result.compliantResources
|
|
|
|
this.bpSetMetadatas[name].nonCompliantResources = result.nonCompliantResources
|
|
|
|
this.bpSetMetadatas[name].status = 'FINISHED'
|
|
|
|
finished(name)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return Promise.all(checkJobs)
|
|
|
|
}
|
|
|
|
|
|
|
|
public runFix(name: string, requiredParametersForFix: { name: string, value: string }[]) {
|
|
|
|
return this
|
|
|
|
.bpSets[name]
|
|
|
|
.fix(
|
|
|
|
this.bpSetMetadatas[name].nonCompliantResources,
|
|
|
|
requiredParametersForFix
|
|
|
|
)
|
2024-12-26 13:06:44 +09:00
|
|
|
}
|
|
|
|
|
2024-12-24 15:25:16 +09:00
|
|
|
public readonly getBPSet = (name: string) =>
|
|
|
|
this.bpSets[name]
|
|
|
|
|
|
|
|
public readonly getBPSetMetadata = (name: string) =>
|
|
|
|
this.bpSetMetadatas[name]
|
|
|
|
|
|
|
|
public readonly getBPSets = () =>
|
|
|
|
Object.values(this.bpSets)
|
|
|
|
|
|
|
|
public readonly getBPSetMetadatas = () =>
|
|
|
|
Object.values(this.bpSetMetadatas)
|
|
|
|
}
|