Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d18e7a7

Browse files
sadayuki-matsunokotakanbe
andauthoredMay 29, 2020
add trivy parser (future-architect#981)
* add trivy parser * fix test * format * add title and summary * add trivy parse command * add uploader * set args by env * add README * add err check * fix * fix * fix * fix test * update trivy * refactor * delete require uuid * delete uuid from trivy parser Co-authored-by: Kota Kanbe <kotakanbe@gmail.com>
1 parent 8d5ea98 commit d18e7a7

File tree

12 files changed

+6074
-19
lines changed

12 files changed

+6074
-19
lines changed
 

‎.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ results/
1515
!setup/docker/*
1616
.DS_Store
1717
dist/
18-
.idea
18+
.idea

‎GNUmakefile

+7
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,10 @@ cov:
6666
clean:
6767
echo $(PKGS) | xargs go clean || exit;
6868

69+
# trivy-to-vuls
70+
build-trivy-to-vuls: pretest fmt
71+
$(GO) build -o trivy-to-vuls contrib/trivy/cmd/*.go
72+
73+
# future-vuls
74+
build-future-vuls: pretest fmt
75+
$(GO) build -o future-vuls contrib/future-vuls/cmd/*.go

‎config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ func (c *TelegramConf) Validate() (errs []error) {
585585

586586
// SaasConf is stride config
587587
type SaasConf struct {
588-
GroupID int `json:"-"`
588+
GroupID int64 `json:"-"`
589589
Token string `json:"-"`
590590
URL string `json:"-"`
591591
}

‎contrib/future-vuls/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# future-vuls
2+
3+
## Main Features
4+
5+
- upload vuls results json to future-vuls
6+
7+
## Installation
8+
9+
```
10+
git clone https://github.com/future-architect/vuls.git
11+
make build-future-vuls
12+
```
13+
14+
## Command Reference
15+
16+
```
17+
Upload to FutureVuls
18+
19+
Usage:
20+
future-vuls upload [flags]
21+
22+
Flags:
23+
--config string config file (default is $HOME/.cobra.yaml)
24+
-g, --group-id int future vuls group id, ENV: VULS_GROUP_ID
25+
-h, --help help for upload
26+
-s, --stdin input from stdin. ENV: VULS_STDIN
27+
-t, --token string future vuls token
28+
--url string future vuls upload url
29+
--uuid string server uuid. ENV: VULS_SERVER_UUID
30+
```
31+
32+
## Usage
33+
34+
- update results json
35+
36+
```
37+
cat results.json | future-vuls upload --stdin --token xxxx --url https://xxxx --group-id 1 --uuid xxxx
38+
```

‎contrib/future-vuls/cmd/main.go

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"encoding/json"
7+
"fmt"
8+
"os"
9+
"strconv"
10+
11+
"github.com/future-architect/vuls/config"
12+
"github.com/future-architect/vuls/models"
13+
"github.com/future-architect/vuls/report"
14+
"github.com/spf13/cobra"
15+
)
16+
17+
var (
18+
configFile string
19+
stdIn bool
20+
jsonDir string
21+
serverUUID string
22+
groupID int64
23+
token string
24+
url string
25+
)
26+
27+
func main() {
28+
var err error
29+
var cmdFvulsUploader = &cobra.Command{
30+
Use: "upload",
31+
Short: "Upload to FutureVuls",
32+
Long: `Upload to FutureVuls`,
33+
Run: func(cmd *cobra.Command, args []string) {
34+
if len(serverUUID) == 0 {
35+
serverUUID = os.Getenv("VULS_SERVER_UUID")
36+
}
37+
if groupID == 0 {
38+
envGroupID := os.Getenv("VULS_GROUP_ID")
39+
if groupID, err = strconv.ParseInt(envGroupID, 10, 64); err != nil {
40+
fmt.Printf("Invalid GroupID: %s\n", envGroupID)
41+
return
42+
}
43+
}
44+
if len(url) == 0 {
45+
url = os.Getenv("VULS_URL")
46+
}
47+
if len(token) == 0 {
48+
token = os.Getenv("VULS_TOKEN")
49+
}
50+
51+
var scanResultJSON []byte
52+
if stdIn {
53+
reader := bufio.NewReader(os.Stdin)
54+
buf := new(bytes.Buffer)
55+
if _, err = buf.ReadFrom(reader); err != nil {
56+
return
57+
}
58+
scanResultJSON = buf.Bytes()
59+
} else {
60+
fmt.Println("use --stdin option")
61+
return
62+
}
63+
64+
var scanResult models.ScanResult
65+
if err = json.Unmarshal(scanResultJSON, &scanResult); err != nil {
66+
fmt.Println("Failed to parse json", err)
67+
return
68+
}
69+
scanResult.ServerUUID = serverUUID
70+
71+
config.Conf.Saas.GroupID = groupID
72+
config.Conf.Saas.Token = token
73+
config.Conf.Saas.URL = url
74+
if err = (report.SaasWriter{}).Write(scanResult); err != nil {
75+
fmt.Println("Failed to create json", err)
76+
return
77+
}
78+
return
79+
},
80+
}
81+
cmdFvulsUploader.PersistentFlags().StringVar(&serverUUID, "uuid", "", "server uuid. ENV: VULS_SERVER_UUID")
82+
cmdFvulsUploader.PersistentFlags().StringVar(&configFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
83+
cmdFvulsUploader.PersistentFlags().BoolVarP(&stdIn, "stdin", "s", false, "input from stdin. ENV: VULS_STDIN")
84+
// TODO Read JSON file from directory
85+
// cmdFvulsUploader.Flags().StringVarP(&jsonDir, "results-dir", "d", "./", "vuls scan results json dir")
86+
cmdFvulsUploader.PersistentFlags().Int64VarP(&groupID, "group-id", "g", 0, "future vuls group id, ENV: VULS_GROUP_ID")
87+
cmdFvulsUploader.PersistentFlags().StringVarP(&token, "token", "t", "", "future vuls token")
88+
cmdFvulsUploader.PersistentFlags().StringVar(&url, "url", "", "future vuls upload url")
89+
90+
var rootCmd = &cobra.Command{Use: "future-vuls"}
91+
rootCmd.AddCommand(cmdFvulsUploader)
92+
if err = rootCmd.Execute(); err != nil {
93+
fmt.Println("Failed to execute command", err)
94+
}
95+
}

‎contrib/trivy/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# trivy-to-vuls
2+
3+
## Main Features
4+
5+
- convert trivy's results json to vuls's report json
6+
7+
## Installation
8+
9+
```
10+
git clone https://github.com/future-architect/vuls.git
11+
make build-trivy-to-vuls
12+
```
13+
14+
## Command Reference
15+
16+
```
17+
Parse trivy json to vuls results
18+
19+
Usage:
20+
trivy-to-vuls parse [flags]
21+
22+
Flags:
23+
-h, --help help for parse
24+
-s, --stdin input from stdin
25+
-d, --trivy-json-dir string trivy json dir (default "./")
26+
-f, --trivy-json-file-name string trivy json file name (default "results.json")
27+
```
28+
29+
## Usage
30+
31+
- use trivy output
32+
33+
```
34+
trivy -q image -f=json python:3.4-alpine | trivy-to-vuls parse --stdin
35+
```

‎contrib/trivy/cmd/main.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"encoding/json"
7+
"fmt"
8+
"io/ioutil"
9+
"os"
10+
"path/filepath"
11+
12+
"github.com/future-architect/vuls/contrib/trivy/parser"
13+
"github.com/future-architect/vuls/models"
14+
"github.com/spf13/cobra"
15+
)
16+
17+
var (
18+
serverUUID string
19+
stdIn bool
20+
jsonDir string
21+
jsonFileName string
22+
)
23+
24+
func main() {
25+
var err error
26+
var cmdTrivyToVuls = &cobra.Command{
27+
Use: "parse",
28+
Short: "Parse trivy json to vuls results",
29+
Long: `Parse trivy json to vuls results`,
30+
Run: func(cmd *cobra.Command, args []string) {
31+
jsonFilePath := filepath.Join(jsonDir, jsonFileName)
32+
var trivyJSON []byte
33+
if stdIn {
34+
reader := bufio.NewReader(os.Stdin)
35+
buf := new(bytes.Buffer)
36+
if _, err = buf.ReadFrom(reader); err != nil {
37+
return
38+
}
39+
trivyJSON = buf.Bytes()
40+
} else {
41+
if trivyJSON, err = ioutil.ReadFile(jsonFilePath); err != nil {
42+
fmt.Println("Failed to read file", err)
43+
return
44+
}
45+
}
46+
47+
scanResult := &models.ScanResult{
48+
JSONVersion: models.JSONVersion,
49+
ScannedCves: models.VulnInfos{},
50+
}
51+
if scanResult, err = parser.Parse(trivyJSON, scanResult); err != nil {
52+
fmt.Println("Failed to execute command", err)
53+
return
54+
}
55+
var resultJSON []byte
56+
if resultJSON, err = json.MarshalIndent(scanResult, "", " "); err != nil {
57+
fmt.Println("Failed to create json", err)
58+
return
59+
}
60+
fmt.Println(string(resultJSON))
61+
return
62+
},
63+
}
64+
cmdTrivyToVuls.Flags().BoolVarP(&stdIn, "stdin", "s", false, "input from stdin")
65+
cmdTrivyToVuls.Flags().StringVarP(&jsonDir, "trivy-json-dir", "d", "./", "trivy json dir")
66+
cmdTrivyToVuls.Flags().StringVarP(&jsonFileName, "trivy-json-file-name", "f", "results.json", "trivy json file name")
67+
68+
var rootCmd = &cobra.Command{Use: "trivy-to-vuls"}
69+
rootCmd.AddCommand(cmdTrivyToVuls)
70+
if err = rootCmd.Execute(); err != nil {
71+
fmt.Println("Failed to execute command", err)
72+
}
73+
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Please sign in to comment.