-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI support for discovery and nmap ``` $ cloud-discovery discover --config <path to config file> $ cloud-discovery nmap --subnet <subnet to scan> ``` Signed-off-by: liron <[email protected]>
- Loading branch information
liron
committed
Nov 11, 2018
1 parent
cabd498
commit ffc635c
Showing
29 changed files
with
5,584 additions
and
95 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 |
---|---|---|
@@ -1,33 +1,75 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/twistlock/cloud-discovery/internal/provider/aws" | ||
"github.com/twistlock/cloud-discovery/internal/nmap" | ||
"github.com/twistlock/cloud-discovery/internal/provider" | ||
"github.com/twistlock/cloud-discovery/internal/shared" | ||
"github.com/urfave/cli" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"text/tabwriter" | ||
) | ||
|
||
func main() { | ||
var ( | ||
username, password string | ||
) | ||
flag.StringVar(&username, "username", "", "Username") | ||
flag.StringVar(&password, "password", "", "Password") | ||
flag.Parse() | ||
if username == "" { | ||
panic("username is missing") | ||
app := cli.NewApp() | ||
app.Name = "cloud-discovery" | ||
app.Usage = " Cloud Discovery provides a point in time enumeration of all the cloud native platform services" | ||
app.Version = "1.0.0" | ||
|
||
var configPath, format, subnet string | ||
app.Commands = []cli.Command{ | ||
{ | ||
Name: "discover", | ||
Usage: "Discover all cloud assets", | ||
Flags: []cli.Flag{cli.StringFlag{ | ||
Name: "config", | ||
Usage: "Path to credential configuration", | ||
Destination: &configPath, | ||
}, | ||
cli.StringFlag{ | ||
Name: "format", | ||
Usage: "Output Formatting (json or csv)", | ||
Value: "csv", | ||
Destination: &format, | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
if configPath == "" { | ||
return fmt.Errorf("missing config path") | ||
} | ||
data, err := ioutil.ReadFile(configPath) | ||
if err != nil { | ||
return err | ||
} | ||
var creds []shared.Credentials | ||
if err := json.Unmarshal(data, &creds); err != nil { | ||
return err | ||
} | ||
provider.Discover(creds, os.Stdout, shared.Format(format)) | ||
return nil | ||
}, | ||
}, | ||
{ | ||
Name: "nmap", | ||
Usage: "Scan all exposed cloud assets", | ||
Flags: []cli.Flag{cli.StringFlag{ | ||
Name: "subnet", | ||
Usage: "The subnet to scan", | ||
Value: "127.0.0.1", | ||
Destination: &subnet, | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
nmap.Nmap(os.Stdout, subnet, true) | ||
return nil | ||
}, | ||
}, | ||
} | ||
if password == "" { | ||
panic("password is missing") | ||
|
||
err := app.Run(os.Args) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight|tabwriter.Debug) | ||
fmt.Fprintln(w, "Type\tRegion\tID") | ||
aws.Discover(username, password, func(result shared.CloudDiscoveryResult) { | ||
for _, asset := range result.Assets { | ||
fmt.Fprintf(w, "%s\t%s\t%s\n", result.Type, result.Region, asset.ID) | ||
} | ||
w.Flush() | ||
}) | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"github.com/twistlock/cloud-discovery/internal/provider/aws" | ||
"github.com/twistlock/cloud-discovery/internal/provider/gcp" | ||
"github.com/twistlock/cloud-discovery/internal/shared" | ||
"io" | ||
"text/tabwriter" | ||
) | ||
|
||
func Discover(creds []shared.Credentials, wr io.Writer, format shared.Format) { | ||
var writer ResponseWriter | ||
if format == shared.FormatJson { | ||
writer = shared.NewJsonResponseWriter(wr) | ||
} else { | ||
writer = NewTabResponseWriter(wr) | ||
} | ||
for _, cred := range creds { | ||
switch cred.Provider { | ||
case shared.ProviderGCP: | ||
gcp.Discover(cred.Secret, writer.Write) | ||
default: | ||
aws.Discover(cred.ID, cred.Secret, writer.Write) | ||
} | ||
} | ||
} | ||
|
||
type ResponseWriter interface { | ||
Write(shared.CloudDiscoveryResult) | ||
} | ||
|
||
type csvResponseWriter struct { | ||
tw *tabwriter.Writer | ||
} | ||
|
||
func NewTabResponseWriter(writer io.Writer) *csvResponseWriter { | ||
tw := shared.NewTabWriter(writer) | ||
fmt.Fprintf(tw, "Type\tRegion\tID\n") | ||
return &csvResponseWriter{tw: tw} | ||
} | ||
|
||
func (w *csvResponseWriter) Write(result shared.CloudDiscoveryResult) { | ||
for _, asset := range result.Assets { | ||
fmt.Fprintf(w.tw, "%s\t%s\t%s\n", result.Type, result.Region, asset.ID) | ||
} | ||
w.tw.Flush() | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.