forked from globocom/huskyCI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add securityTest to scan Terraform templates: tfsec (globocom#478)
* build(deps): bump github.com/onsi/gomega from 1.7.0 to 1.7.1 Bumps [github.com/onsi/gomega](https://github.com/onsi/gomega) from 1.7.0 to 1.7.1. - [Release notes](https://github.com/onsi/gomega/releases) - [Changelog](https://github.com/onsi/gomega/blob/master/CHANGELOG.md) - [Commits](onsi/gomega@v1.7.0...v1.7.1) Signed-off-by: dependabot-preview[bot] <[email protected]> * build(deps): bump github.com/onsi/ginkgo from 1.10.2 to 1.10.3 Bumps [github.com/onsi/ginkgo](https://github.com/onsi/ginkgo) from 1.10.2 to 1.10.3. - [Release notes](https://github.com/onsi/ginkgo/releases) - [Changelog](https://github.com/onsi/ginkgo/blob/master/CHANGELOG.md) - [Commits](onsi/ginkgo@v1.10.2...v1.10.3) Signed-off-by: dependabot-preview[bot] <[email protected]> * build(deps): bump github.com/spf13/viper from 1.4.0 to 1.5.0 Bumps [github.com/spf13/viper](https://github.com/spf13/viper) from 1.4.0 to 1.5.0. - [Release notes](https://github.com/spf13/viper/releases) - [Commits](spf13/viper@v1.4.0...v1.5.0) Signed-off-by: dependabot-preview[bot] <[email protected]> * Feature: Add struct TFSec * Feature: Add config for tfsec in config.yaml * Feature: Add TFSec in api context * Feature: Add TFSec in securitytest * Feature: Add TFSec in types * Feature: Add TFSec final references in api module * Feature: Add TFSec into types and output in client module * Feature: Add TFSec references in cli * Feature: Update vendors * Fix: Add comment in struct Location * Feature: Add dockerfile for TFSec * Fix: Fix TFSec output in client * Feature: Update shell scripts to build and push images * Fix: Update tfsec dockerfile to use version v0.19.0 * Bump: Update go.mod * Bump: Update golangci-lint to v1.27.0 * Lint: Fix lint in files using Println * Fix: Add missing calls of printSTDOUTOutputTFSec * Fix: Change type of fields StartLine and EndLine * Fix: Add jq and git into dockerfile * Fix: Print hcl results in client * Fix: Fix wrong securitytest name * Fix: Remove vendors * Fix: Remove go.mod and go.sum * Fix: Replace wrong comment * Fix: Fix wrong commmand to get tfsec version huskyci/tfsec:latest Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: Carlos Junior <[email protected]> Co-authored-by: carlosljr <[email protected]>
- Loading branch information
1 parent
7d63fcc
commit 22e0b14
Showing
21 changed files
with
264 additions
and
16 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
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
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,90 @@ | ||
// Copyright 2019 Globo.com authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package securitytest | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/globocom/huskyCI/api/log" | ||
"github.com/globocom/huskyCI/api/types" | ||
) | ||
|
||
// TFSecOutput is the struct that holds all data from TFSec output. | ||
type TFSecOutput struct { | ||
Warnings json.RawMessage `json:"warnings"` | ||
Results []TFSecResult `json:"results"` | ||
} | ||
|
||
// TFSecResult is the struct that holds detailed information of results from TFSec output. | ||
type TFSecResult struct { | ||
RuleID string `json:"rule_id"` | ||
Link string `json:"link"` | ||
Location Location `json:"location"` | ||
Description string `json:"description"` | ||
Severity string `json:"severity"` | ||
} | ||
|
||
// Location is the struct that holds detailed information of location from each result | ||
type Location struct { | ||
Filename string `json:"filename"` | ||
StartLine int `json:"start_line"` | ||
EndLine int `json:"end_line"` | ||
} | ||
|
||
func analyzeTFSec(tfsecScan *SecTestScanInfo) error { | ||
|
||
tfsecOutput := TFSecOutput{} | ||
|
||
// Unmarshall rawOutput into finalOutput, that is a TFSec struct. | ||
if err := json.Unmarshal([]byte(tfsecScan.Container.COutput), &tfsecOutput); err != nil { | ||
log.Error("analyzeTFSec", "TFSEC", 1040, tfsecScan.Container.COutput, err) | ||
tfsecScan.ErrorFound = err | ||
return err | ||
} | ||
tfsecScan.FinalOutput = tfsecOutput | ||
|
||
// an empty Results slice states that no Issues were found. | ||
if tfsecOutput.Results == nil { | ||
tfsecScan.prepareContainerAfterScan() | ||
return nil | ||
} | ||
|
||
// check results and prepare all vulnerabilities found | ||
tfsecScan.prepareTFSecVulns() | ||
tfsecScan.prepareContainerAfterScan() | ||
return nil | ||
} | ||
|
||
func (tfsecScan *SecTestScanInfo) prepareTFSecVulns() { | ||
|
||
huskyCItfsecResults := types.HuskyCISecurityTestOutput{} | ||
tfsecOutput := tfsecScan.FinalOutput.(TFSecOutput) | ||
|
||
for _, result := range tfsecOutput.Results { | ||
tfsecVuln := types.HuskyCIVulnerability{} | ||
tfsecVuln.Language = "HCL" | ||
tfsecVuln.SecurityTool = "TFSec" | ||
tfsecVuln.Severity = result.Severity | ||
tfsecVuln.Details = result.RuleID + " @ [" + result.Description + "]" | ||
startLine := strconv.Itoa(result.Location.StartLine) | ||
endLine := strconv.Itoa(result.Location.EndLine) | ||
tfsecVuln.Line = startLine | ||
tfsecVuln.Code = fmt.Sprintf("Code beetween Line %s and Line %s.", startLine, endLine) | ||
tfsecVuln.File = result.Location.Filename | ||
|
||
switch tfsecVuln.Severity { | ||
case "INFO": | ||
huskyCItfsecResults.LowVulns = append(huskyCItfsecResults.LowVulns, tfsecVuln) | ||
case "WARNING": | ||
huskyCItfsecResults.MediumVulns = append(huskyCItfsecResults.MediumVulns, tfsecVuln) | ||
case "ERROR": | ||
huskyCItfsecResults.HighVulns = append(huskyCItfsecResults.HighVulns, tfsecVuln) | ||
} | ||
} | ||
|
||
tfsecScan.Vulnerabilities = huskyCItfsecResults | ||
} |
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
Oops, something went wrong.