Skip to content
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

add output-file flag #60

Merged
merged 2 commits into from
Apr 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func main() {
audit := flag.Bool("audit", false, "Runs a one-time audit.")
dashboardPort := flag.Int("dashboard-port", 8080, "Port for the dashboard webserver")
webhookPort := flag.Int("webhook-port", 9876, "Port for the webhook webserver")
auditDestination := flag.String("audit-destination", "", "Destination URL to send audit results (prints to stdout if unspecified)")
auditOutputURL := flag.String("output-url", "", "Destination URL to send audit results")
auditOutputFile := flag.String("output-file", "", "Destination file for audit results")
configPath := flag.String("config", "config.yaml", "Location of Fairwinds configuration file")

var disableWebhookConfigInstaller bool
Expand All @@ -74,7 +75,7 @@ func main() {
} else if *dashboard {
startDashboardServer(c, *dashboardPort)
} else if *audit {
runAudit(c, *auditDestination)
runAudit(c, *auditOutputFile, *auditOutputURL)
}
}

Expand Down Expand Up @@ -164,36 +165,49 @@ func startWebhookServer(c conf.Configuration, disableWebhookConfigInstaller bool
}
}

func runAudit(c conf.Configuration, destination string) {
func runAudit(c conf.Configuration, outputFile string, outputURL string) {
k, _ := kube.CreateKubeAPI()
auditData, err := validator.RunAudit(c, k)
if err != nil {
panic(err)
}

if destination != "" {
jsonData, err := json.Marshal(auditData)
if outputURL == "" && outputFile == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this whole section be restructured to something like:

if outputURL != "" {
  # send to url
} else if outputFile != "" {
  # send to file
} else {
  # send to stdout
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's I originally had it, but

  • There's some common logic for outputURL and outputFile for serializing to JSON
  • We need to output to both file and URL in some cases (e.g. the hosted version needs this)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 that makes a lot of sense

yamlBytes, err := yaml.Marshal(auditData)
if err != nil {
panic(err)
}

req, err := http.NewRequest("POST", destination, bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
os.Stdout.Write(yamlBytes)
} else {
jsonData, err := json.Marshal(auditData)
if err != nil {
panic(err)
}
defer resp.Body.Close()

body, _ := ioutil.ReadAll(resp.Body)
glog.Println(string(body))
} else {
y, err := yaml.Marshal(auditData)
if err != nil {
panic(err)
if outputFile != "" {
err := ioutil.WriteFile(outputFile, []byte(jsonData), 0644)
if err != nil {
panic(err)
}
}

if outputURL != "" {
req, err := http.NewRequest("POST", outputURL, bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading audit output URL response")
} else {
glog.Println(string(body))
}
}
fmt.Println(string(y))
}
}