diff --git a/pkg/cli/scan.go b/pkg/cli/scan.go index fd9f731c4..257fe9afb 100644 --- a/pkg/cli/scan.go +++ b/pkg/cli/scan.go @@ -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" ) @@ -57,9 +62,9 @@ func scan(cmd *cobra.Command, args []string) { } func init() { - scanCmd.Flags().StringVarP(&PolicyType, "policy-type", "t", "", " 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(" 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") diff --git a/pkg/iac-providers/providers.go b/pkg/iac-providers/providers.go index ef7e0fb20..5e015d8c4 100644 --- a/pkg/iac-providers/providers.go +++ b/pkg/iac-providers/providers.go @@ -19,6 +19,8 @@ package iacprovider import ( "fmt" "reflect" + "sort" + "strings" "go.uber.org/zap" ) @@ -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 +} diff --git a/pkg/iac-providers/providers_test.go b/pkg/iac-providers/providers_test.go index 191c6d5eb..e152ab7e7 100644 --- a/pkg/iac-providers/providers_test.go +++ b/pkg/iac-providers/providers_test.go @@ -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) + } + }) +} diff --git a/pkg/policy/cloud-providers.go b/pkg/policy/cloud-providers.go index a6b7a9fc5..c3f24b926 100644 --- a/pkg/policy/cloud-providers.go +++ b/pkg/policy/cloud-providers.go @@ -17,6 +17,8 @@ package policy import ( + "sort" + "github.com/accurics/terrascan/pkg/config" ) @@ -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 +} diff --git a/pkg/policy/cloud-providers_test.go b/pkg/policy/cloud-providers_test.go new file mode 100644 index 000000000..e67a9c22d --- /dev/null +++ b/pkg/policy/cloud-providers_test.go @@ -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) + } + }) +}