Skip to content

Commit

Permalink
Merge pull request #336 from accurics/auto-pickup-supportedtypes
Browse files Browse the repository at this point in the history
Automatically populate usage with supported IaC providers, versions, and policies
  • Loading branch information
Cesar Rodriguez authored Sep 18, 2020
2 parents 570cc9b + 3508fe5 commit 0b22a65
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pkg/cli/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
package cli

import (
"fmt"
"strings"

iacProvider "github.com/accurics/terrascan/pkg/iac-providers"
"github.com/accurics/terrascan/pkg/policy"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -57,9 +62,9 @@ func scan(cmd *cobra.Command, args []string) {
}

func init() {
scanCmd.Flags().StringVarP(&PolicyType, "policy-type", "t", "", "<required> policy type (aws, azure, gcp, k8s)")
scanCmd.Flags().StringVarP(&IacType, "iac-type", "i", "", "iac type (terraform, k8s)")
scanCmd.Flags().StringVarP(&IacVersion, "iac-version", "", "", "iac version terraform:(v12) k8s:(v1)")
scanCmd.Flags().StringVarP(&PolicyType, "policy-type", "t", "", fmt.Sprintf("<required> policy type (%v)", strings.Join(policy.SupportedPolicyTypes(), ", ")))
scanCmd.Flags().StringVarP(&IacType, "iac-type", "i", "", fmt.Sprintf("iac type (%v)", strings.Join(iacProvider.SupportedIacProviders(), ", ")))
scanCmd.Flags().StringVarP(&IacVersion, "iac-version", "", "", fmt.Sprintf("iac version (%v)", strings.Join(iacProvider.SupportedIacVersions(), ", ")))
scanCmd.Flags().StringVarP(&IacFilePath, "iac-file", "f", "", "path to a single IaC file")
scanCmd.Flags().StringVarP(&IacDirPath, "iac-dir", "d", ".", "path to a directory containing one or more IaC files")
scanCmd.Flags().StringVarP(&PolicyPath, "policy-path", "p", "", "policy path directory")
Expand Down
27 changes: 27 additions & 0 deletions pkg/iac-providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package iacprovider
import (
"fmt"
"reflect"
"sort"
"strings"

"go.uber.org/zap"
)
Expand Down Expand Up @@ -50,3 +52,28 @@ func IsIacSupported(iacType, iacVersion string) bool {
}
return true
}

// SupportedIacProviders returns list of Iac Providers supported in terrascan
func SupportedIacProviders() []string {
var iacTypes []string
for k := range supportedIacProviders {
iacTypes = append(iacTypes, string(k))
}
sort.Strings(iacTypes)
return iacTypes
}

// SupportedIacVersions retuns a string of Iac providers and corresponding supported versions
func SupportedIacVersions() []string {
var iacVersions []string
for iac, versions := range supportedIacProviders {
var versionSlice []string
for k := range versions {
versionSlice = append(versionSlice, string(k))
}
versionString := strings.Join(versionSlice, ", ")
iacVersions = append(iacVersions, fmt.Sprintf("%s: %s", string(iac), versionString))
}
sort.Strings(iacVersions)
return iacVersions
}
13 changes: 13 additions & 0 deletions pkg/iac-providers/providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,16 @@ func TestIsIacSupported(t *testing.T) {
})
}
}

func TestSupportedIacProviders(t *testing.T) {
t.Run("supported iac providers", func(t *testing.T) {
var want []string
for k := range supportedIacProviders {
want = append(want, string(k))
}
got := SupportedIacProviders()
if !reflect.DeepEqual(got, want) {
t.Errorf("got: '%v', want: '%v'", got, want)
}
})
}
12 changes: 12 additions & 0 deletions pkg/policy/cloud-providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package policy

import (
"sort"

"github.com/accurics/terrascan/pkg/config"
)

Expand Down Expand Up @@ -70,3 +72,13 @@ func GetDefaultIacType(cloudType string) string {
func GetDefaultIacVersion(cloudType string) string {
return string(defaultIacVersion[supportedCloudType(cloudType)])
}

// SupportedPolicyTypes returns the list of policies supported in terrascan
func SupportedPolicyTypes() []string {
var policyTypes []string
for k := range supportedCloudProvider {
policyTypes = append(policyTypes, string(k))
}
sort.Strings(policyTypes)
return policyTypes
}
37 changes: 37 additions & 0 deletions pkg/policy/cloud-providers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright (C) 2020 Accurics, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package policy

import (
"reflect"
"sort"
"testing"
)

func TestSupportedPolicyTypes(t *testing.T) {
t.Run("supported policy types", func(t *testing.T) {
var want []string
for k := range supportedCloudProvider {
want = append(want, string(k))
}
sort.Strings(want)
got := SupportedPolicyTypes()
if !reflect.DeepEqual(got, want) {
t.Errorf("got: '%v', want: '%v'", got, want)
}
})
}

0 comments on commit 0b22a65

Please sign in to comment.