Skip to content

Commit

Permalink
feat: 支持将离线的标准扫描器转为在线扫描器 #48
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlkl authored Jul 12, 2023
1 parent bc2f852 commit 2da111c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
12 changes: 12 additions & 0 deletions standard-adapter/README.md
Original file line number Diff line number Diff line change
@@ -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"})
}
```
5 changes: 5 additions & 0 deletions standard-adapter/go.mod
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions standard-adapter/go.sum
Original file line number Diff line number Diff line change
@@ -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=
80 changes: 80 additions & 0 deletions standard-adapter/pkg/adapter.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 2da111c

Please sign in to comment.