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

adds support to use 'config_only' attribute in api server's file scan #579

Merged
merged 1 commit into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
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
25 changes: 24 additions & 1 deletion pkg/http-server/file-scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"

"github.com/accurics/terrascan/pkg/runtime"
Expand All @@ -41,6 +42,7 @@ func (g *APIHandler) scanFile(w http.ResponseWriter, r *http.Request) {
cloudType = strings.Split(params["cloud"], ",")
scanRules = []string{}
skipRules = []string{}
configOnly = false
)

// parse multipart form, 10 << 20 specifies maximum upload of 10 MB files
Expand Down Expand Up @@ -93,6 +95,18 @@ func (g *APIHandler) scanFile(w http.ResponseWriter, r *http.Request) {
// severity is the minimum severity level of violations that the user want to get informed about: low, medium or high
severity := r.FormValue("severity")

// read config_only from the form data
configOnlyValue := r.FormValue("config_only")
if configOnlyValue != "" {
configOnly, err = strconv.ParseBool(configOnlyValue)
if err != nil {
errMsg := fmt.Sprintf("error while reading 'config_only' value. error: '%v'", err)
kanchwala-yusuf marked this conversation as resolved.
Show resolved Hide resolved
zap.S().Error(errMsg)
apiErrorResponse(w, errMsg, http.StatusBadRequest)
return
}
}

if scanRulesValue != "" {
scanRules = strings.Split(scanRulesValue, ",")
}
Expand Down Expand Up @@ -127,7 +141,16 @@ func (g *APIHandler) scanFile(w http.ResponseWriter, r *http.Request) {
return
}

j, err := json.MarshalIndent(normalized, "", " ")
var output interface{}

// if config only, return resource config else return violations
if configOnly {
output = normalized.ResourceConfig
} else {
output = normalized.Violations
}

j, err := json.MarshalIndent(output, "", " ")
if err != nil {
errMsg := fmt.Sprintf("failed to create JSON. error: '%v'", err)
zap.S().Error(errMsg)
Expand Down
53 changes: 43 additions & 10 deletions pkg/http-server/file-scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"strconv"
"strings"
"testing"

Expand All @@ -23,16 +24,18 @@ func TestUpload(t *testing.T) {
testParamName := "file"

table := []struct {
name string
path string
param string
iacType string
iacVersion string
cloudType string
scanRules []string
skipRules []string
severity string
wantStatus int
name string
path string
param string
iacType string
iacVersion string
cloudType string
scanRules []string
skipRules []string
severity string
configOnly bool
invalidConfigOnly bool
wantStatus int
}{
{
name: "valid file scan",
Expand Down Expand Up @@ -178,6 +181,24 @@ func TestUpload(t *testing.T) {
"AWS.CloudFront.Logging.Medium.0567", "AWS.CloudFront.Network Security.Low.0568"},
skipRules: []string{"AWS.CloudFront.Network Security.Low.0568"},
},
{
name: "test for config only",
path: testFilePath,
param: testParamName,
iacType: testIacType,
cloudType: testCloudType,
wantStatus: http.StatusOK,
configOnly: true,
},
{
name: "test for invalid value config only",
path: testFilePath,
param: testParamName,
iacType: testIacType,
cloudType: testCloudType,
wantStatus: http.StatusBadRequest,
invalidConfigOnly: true,
},
}

for _, tt := range table {
Expand Down Expand Up @@ -220,6 +241,18 @@ func TestUpload(t *testing.T) {
}
}

if !tt.invalidConfigOnly {
if err = writer.WriteField("config_only", strconv.FormatBool(tt.configOnly)); err != nil {
writer.Close()
t.Error(err)
}
} else {
if err = writer.WriteField("config_only", "invalid"); err != nil {
writer.Close()
t.Error(err)
}
}

writer.Close()

// http request of the type "/v1/{iacType}/{iacVersion}/{cloudType}/file/scan"
Expand Down