-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(contrib/fvuls) Add commands to obtained CPE information of network devices by executing snmp2cpe and upload to Fvuls server #1721
Merged
Merged
Changes from 17 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
b88c905
add: README.md
ccf6596
add: commands(discover,add-server,add-cpe)
b46841a
add: implements(discover,add-server,add-cpe)
813a861
fix: changed os.Exit(1) in main.go to return an error
8c425fd
fix: lint error
32bb08e
delete: trivy-to-vuls stdIn
5a15fc5
fix: Incomprehesible error logs
c8b1da8
fix: according to review
40bf88f
add: function converts old config to latest one
f9ad423
delete: add-server
53d5f5a
fix: lint error
670bbfc
fix
14334a9
fix: remote scan error in Windows
cc6981f
fix: lint error
38b39fb
fix
88522e3
fix: lint error
3d02e7c
fix: CONFLICT
ef5520d
fix: lint error
b2a48cf
fix: lint error
61fc240
add: scanner/scanner.go test normalizeHomeDirForWindows()
70f3930
fix
96e7f7c
fix
843c6f2
fix
f070bd0
fix
a9d6ecb
Merge branch 'master' of https://github.com/wadda0714/vuls into wada/…
090c0d3
fix
16efe8e
fix
f7d5de4
fix
679364a
fix
6e1d563
fix: lint error
0ea9fa2
Merge branch 'master' into master
wadda0714 efebcdd
Merge branch 'master' of https://github.com/wadda0714/vuls
39964f5
fix: error log
37aeed8
fix
ba10b2e
refactor(fvuls)
sadayuki-matsuno 9bde04f
merge
sadayuki-matsuno 7f08b42
Refactor (#2)
sadayuki-matsuno dcae998
Refactor (#3)
wadda0714 34cb069
fix: CONFLICT
211187a
fix
f0c2739
fix: lint error
4e3f45a
fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
@@ -1,29 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
c "github.com/3th1nk/cidr" | ||
"github.com/future-architect/vuls/config" | ||
"github.com/future-architect/vuls/models" | ||
"github.com/future-architect/vuls/saas" | ||
"github.com/future-architect/vuls/contrib/future-vuls/pkg/cpe" | ||
"github.com/future-architect/vuls/contrib/future-vuls/pkg/discover" | ||
"github.com/future-architect/vuls/contrib/future-vuls/pkg/saas" | ||
"github.com/future-architect/vuls/contrib/future-vuls/pkg/schema" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
configFile string | ||
stdIn bool | ||
jsonDir string | ||
serverUUID string | ||
groupID int64 | ||
token string | ||
tags []string | ||
url string | ||
configFile string | ||
stdIn bool | ||
jsonDir string | ||
serverUUID string | ||
groupID int64 | ||
token string | ||
tags []string | ||
url string | ||
outputFile string | ||
cidr string | ||
snmpVersion string | ||
proxy string | ||
) | ||
|
||
func main() { | ||
|
@@ -32,15 +39,14 @@ | |
Use: "upload", | ||
Short: "Upload to FutureVuls", | ||
Long: `Upload to FutureVuls`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(serverUUID) == 0 { | ||
serverUUID = os.Getenv("VULS_SERVER_UUID") | ||
} | ||
if groupID == 0 { | ||
envGroupID := os.Getenv("VULS_GROUP_ID") | ||
if groupID, err = strconv.ParseInt(envGroupID, 10, 64); err != nil { | ||
fmt.Printf("Invalid GroupID: %s\n", envGroupID) | ||
return | ||
return fmt.Errorf("invalid GroupID: %s", envGroupID) | ||
} | ||
} | ||
if len(url) == 0 { | ||
|
@@ -52,44 +58,22 @@ | |
if len(tags) == 0 { | ||
tags = strings.Split(os.Getenv("VULS_TAGS"), ",") | ||
} | ||
|
||
var scanResultJSON []byte | ||
if stdIn { | ||
reader := bufio.NewReader(os.Stdin) | ||
buf := new(bytes.Buffer) | ||
if _, err = buf.ReadFrom(reader); err != nil { | ||
return | ||
if _, err := buf.ReadFrom(reader); err != nil { | ||
return fmt.Errorf("failed to read from stdIn. err: %v", err) | ||
} | ||
scanResultJSON = buf.Bytes() | ||
} else { | ||
fmt.Println("use --stdin option") | ||
os.Exit(1) | ||
return | ||
} | ||
|
||
var scanResult models.ScanResult | ||
if err = json.Unmarshal(scanResultJSON, &scanResult); err != nil { | ||
fmt.Println("Failed to parse json", err) | ||
os.Exit(1) | ||
return | ||
} | ||
scanResult.ServerUUID = serverUUID | ||
if 0 < len(tags) { | ||
if scanResult.Optional == nil { | ||
scanResult.Optional = map[string]interface{}{} | ||
} | ||
scanResult.Optional["VULS_TAGS"] = tags | ||
return fmt.Errorf("use --stdin option") | ||
} | ||
|
||
config.Conf.Saas.GroupID = groupID | ||
config.Conf.Saas.Token = token | ||
config.Conf.Saas.URL = url | ||
if err = (saas.Writer{}).Write(scanResult); err != nil { | ||
fmt.Println(err) | ||
if err := saas.UploadToFvuls(serverUUID, groupID, url, token, tags, scanResultJSON); err != nil { | ||
fmt.Printf("%v", err) | ||
os.Exit(1) | ||
return | ||
} | ||
return | ||
return nil | ||
}, | ||
} | ||
var cmdVersion = &cobra.Command{ | ||
|
@@ -100,19 +84,86 @@ | |
fmt.Printf("future-vuls-%s-%s\n", config.Version, config.Revision) | ||
}, | ||
} | ||
|
||
var cmdDiscover = &cobra.Command{ | ||
Use: "discover --cidr <CIDR_RANGE> --output <OUTPUT_FILE>", | ||
Short: "discover hosts with CIDR range. Run snmp2cpe on active host to get CPE. Default outputFile is ./discover_list.toml", | ||
Example: "future-vuls discover --cidr 192.168.0.0/24 --output discover_list.toml", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(outputFile) == 0 { | ||
outputFile = schema.FileName | ||
} | ||
if len(cidr) == 0 { | ||
return fmt.Errorf("please specify cidr range") | ||
} | ||
if _, err := c.Parse(cidr); err != nil { | ||
return fmt.Errorf("Invalid cidr range") | ||
} | ||
if len(snmpVersion) == 0 { | ||
snmpVersion = schema.SnmpVersion | ||
} | ||
if snmpVersion != "v1" && snmpVersion != "v2c" && snmpVersion != "v3" { | ||
return fmt.Errorf("Invalid snmpVersion") | ||
} | ||
if err := discover.ActiveHosts(cidr, outputFile, snmpVersion); err != nil { | ||
fmt.Printf("%v", err) | ||
os.Exit(1) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
var cmdAddCpe = &cobra.Command{ | ||
Use: "add-cpe --token <VULS_TOKEN> --output <OUTPUT_FILE>", | ||
Short: "Create a pseudo server in Fvuls and register CPE. Default outputFile is ./discover_list.toml", | ||
Example: "future-vuls add-cpe --token <VULS_TOKEN>", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(token) == 0 { | ||
token = os.Getenv("VULS_TOKEN") | ||
if len(token) == 0 { | ||
return fmt.Errorf("token not specified") | ||
} | ||
} | ||
if len(outputFile) == 0 { | ||
outputFile = schema.FileName | ||
} | ||
url := os.Getenv("VULS_URL") | ||
if len(url) == 0 { | ||
url = schema.RestEndPoint | ||
} | ||
if err := cpe.AddServerToFvuls(token, outputFile, proxy, url); err != nil { | ||
fmt.Printf("%v", err) | ||
os.Exit(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reason for exiting without returning err? |
||
} | ||
if err := cpe.AddCpeDataToFvuls(token, outputFile, proxy, url); err != nil { | ||
fmt.Printf("%v", err) | ||
os.Exit(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reason for exiting without returning err? |
||
} | ||
return nil | ||
}, | ||
} | ||
|
||
cmdDiscover.PersistentFlags().StringVar(&cidr, "cidr", "", "cidr range") | ||
cmdDiscover.PersistentFlags().StringVar(&outputFile, "output", "", "output file") | ||
cmdDiscover.PersistentFlags().StringVar(&snmpVersion, "snmp-version", "", "snmp version v1,v2c and v3. default: v2c ") | ||
cmdAddCpe.PersistentFlags().StringVarP(&token, "token", "t", "", "future vuls token ENV: VULS_TOKEN") | ||
cmdAddCpe.PersistentFlags().StringVar(&outputFile, "output", "", "output file") | ||
cmdAddCpe.PersistentFlags().StringVar(&proxy, "http-proxy", "", "proxy url") | ||
|
||
cmdFvulsUploader.PersistentFlags().StringVar(&serverUUID, "uuid", "", "server uuid. ENV: VULS_SERVER_UUID") | ||
cmdFvulsUploader.PersistentFlags().StringVar(&configFile, "config", "", "config file (default is $HOME/.cobra.yaml)") | ||
cmdFvulsUploader.PersistentFlags().BoolVarP(&stdIn, "stdin", "s", false, "input from stdin. ENV: VULS_STDIN") | ||
// TODO Read JSON file from directory | ||
// cmdFvulsUploader.Flags().StringVarP(&jsonDir, "results-dir", "d", "./", "vuls scan results json dir") | ||
cmdFvulsUploader.PersistentFlags().Int64VarP(&groupID, "group-id", "g", 0, "future vuls group id, ENV: VULS_GROUP_ID") | ||
cmdFvulsUploader.PersistentFlags().StringVarP(&token, "token", "t", "", "future vuls token") | ||
cmdFvulsUploader.PersistentFlags().StringVar(&url, "url", "", "future vuls upload url") | ||
|
||
var rootCmd = &cobra.Command{Use: "future-vuls"} | ||
rootCmd.AddCommand(cmdDiscover) | ||
rootCmd.AddCommand(cmdAddCpe) | ||
rootCmd.AddCommand(cmdFvulsUploader) | ||
rootCmd.AddCommand(cmdVersion) | ||
if err = rootCmd.Execute(); err != nil { | ||
fmt.Println("Failed to execute command", err) | ||
fmt.Println("Failed to execute command") | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for exiting without returning err?