diff --git a/standard-adapter/README.md b/standard-adapter/README.md new file mode 100644 index 0000000..5e79136 --- /dev/null +++ b/standard-adapter/README.md @@ -0,0 +1,12 @@ +## 标准扫描器适配器 + +对仅支持解析input.json文件扫描输出output.jso文件的离线扫描器进行增强,使其支持从服务端拉取任务执行并上报结果 + +```go +// 接入示例 +func main() { + // 将会从服务端拉取任务,将任务信息写入input.json后, + // 在workdDir下执行/bin/scan --input /bkrepo/workspace/input.json --output /bkrepo/workspace/output.json + framework.Analyze(StandardAdapterExecutor{cmd: "/bin/scan", workDir: "/bkrepo/workspace"}) +} +``` diff --git a/standard-adapter/go.mod b/standard-adapter/go.mod new file mode 100644 index 0000000..5f4abb2 --- /dev/null +++ b/standard-adapter/go.mod @@ -0,0 +1,5 @@ +module github.com/TencentBlueKing/ci-repoAnalysis/standard-adapter + +go 1.20 + +require github.com/TencentBlueKing/ci-repoAnalysis/analysis-tool-sdk-golang v0.0.18 diff --git a/standard-adapter/go.sum b/standard-adapter/go.sum new file mode 100644 index 0000000..226cd72 --- /dev/null +++ b/standard-adapter/go.sum @@ -0,0 +1,2 @@ +github.com/TencentBlueKing/ci-repoAnalysis/analysis-tool-sdk-golang v0.0.18 h1:rX6zJTJD99IX8noGXNisjQ/4CSMYdI8BbDN7SDckudg= +github.com/TencentBlueKing/ci-repoAnalysis/analysis-tool-sdk-golang v0.0.18/go.mod h1:AXra//9jqgUGWl41/a0jL1vj5xG9Hw201KPYdfePmsw= diff --git a/standard-adapter/pkg/adapter.go b/standard-adapter/pkg/adapter.go new file mode 100644 index 0000000..f02e1d2 --- /dev/null +++ b/standard-adapter/pkg/adapter.go @@ -0,0 +1,80 @@ +package pkg + +import ( + "encoding/json" + "github.com/TencentBlueKing/ci-repoAnalysis/analysis-tool-sdk-golang/api" + "github.com/TencentBlueKing/ci-repoAnalysis/analysis-tool-sdk-golang/object" + "github.com/TencentBlueKing/ci-repoAnalysis/analysis-tool-sdk-golang/util" + "os" +) + +// StandardAdapterExecutor 标准扫描器适配器 +type StandardAdapterExecutor struct { + cmd string + workDir string +} + +// Execute 执行扫描 +func (e StandardAdapterExecutor) Execute(_ *object.ToolConfig, file *os.File) (*object.ToolOutput, error) { + // 将toolInput写入/bkrepo/workspace/input.json + toolInput, err := api.GetClient(object.GetArgs()).Start() + if err != nil { + return nil, err + } + newToolInput := &object.ToolInput{ + TaskId: toolInput.TaskId, + ToolConfig: toolInput.ToolConfig, + FilePath: file.Name(), + Sha256: "", + FileUrls: nil, + } + toolInputFile := util.WorkDir + "/input.json" + if err := e.writeToolInput(newToolInput, toolInputFile); err != nil { + return nil, err + } + + // 执行扫描 + toolOutputFile := util.WorkDir + "/output.json" + args := []string{ + "--input", toolInputFile, + "--output", toolOutputFile, + } + err = util.ExecAndLog(e.cmd, args, e.workDir) + if err != nil { + return nil, err + } + + // 从/bkrepo/workspace/output.json读取扫描结果 + return e.readToolOutput(toolOutputFile) +} + +func (e StandardAdapterExecutor) writeToolInput(input *object.ToolInput, f string) error { + file, err := os.Create(f) + if err != nil { + return err + } + defer file.Close() + + encoder := json.NewEncoder(file) + err = encoder.Encode(input) + if err != nil { + return err + } + util.Info("write input.json success") + return nil +} + +func (e StandardAdapterExecutor) readToolOutput(f string) (*object.ToolOutput, error) { + file, err := os.Open(f) + if err != nil { + return nil, err + } + defer file.Close() + + output := new(object.ToolOutput) + err = json.NewDecoder(file).Decode(output) + if err != nil { + return nil, err + } + return output, nil +}