-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |