Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yusuf Kanchwala
committed
Jul 31, 2020
1 parent
5f0ce1a
commit 8e15ba2
Showing
4 changed files
with
139 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
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 iacprovider | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
// map of supported IaC providers | ||
var supportedIacProviders = make(map[supportedIacType](map[supportedIacVersion]reflect.Type)) | ||
|
||
// RegisterIacProvider registers an IaC provider for terrascan | ||
// if the Iac provider does not have a version, it can be kept empty | ||
func RegisterIacProvider(iacType supportedIacType, iacVersion supportedIacVersion, iacProvider reflect.Type) { | ||
|
||
if iacVersion == "" { | ||
iacVersion = defaultIacVersion | ||
} | ||
|
||
// version support | ||
supportedTerraformVersions := make(map[supportedIacVersion]reflect.Type) | ||
supportedTerraformVersions[iacVersion] = iacProvider | ||
|
||
// type support | ||
supportedIacProviders[iacType] = supportedTerraformVersions | ||
} |
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,77 @@ | ||
package iacprovider | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
type MockIacProvider struct{} | ||
|
||
func TestRegisterIacProvider(t *testing.T) { | ||
|
||
/* | ||
table := []struct { | ||
name string | ||
iacType supportedIacType | ||
iacVersion supportedIacVersion | ||
want reflect.Type | ||
}{ | ||
{ | ||
name: "mock iac type and version", | ||
iacType: supportedIacType("mockIacType"), | ||
iacVersion: supportedIacVersion("mockIacVersion"), | ||
want: reflect.TypeOf(MockIacProvider{}), | ||
}, | ||
{ | ||
name: "mock iac type default version", | ||
iacType: supportedIacType("mockIacType"), | ||
iacVersion: supportedIacVersion(""), | ||
want: reflect.TypeOf(MockIacProvider{}), | ||
}, | ||
} | ||
*/ | ||
|
||
t.Run("mock iac provider", func(t *testing.T) { | ||
|
||
var ( | ||
iacType = supportedIacType("mockIacType") | ||
iacVersion = supportedIacVersion("mockIacVersion") | ||
want = reflect.TypeOf(MockIacProvider{}) | ||
) | ||
|
||
RegisterIacProvider(iacType, iacVersion, want) | ||
|
||
if _, present := supportedIacProviders[iacType]; !present { | ||
t.Errorf("mockIacType not registered") | ||
} | ||
got, present := supportedIacProviders[iacType][iacVersion] | ||
if !present { | ||
t.Errorf("mockIacVersion not registered") | ||
} | ||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("got: '%v', want: '%v'", got, want) | ||
} | ||
}) | ||
|
||
t.Run("mock iac default version", func(t *testing.T) { | ||
|
||
var ( | ||
iacType = supportedIacType("mockIacType") | ||
iacVersion = supportedIacVersion("") | ||
want = reflect.TypeOf(MockIacProvider{}) | ||
) | ||
|
||
RegisterIacProvider(iacType, iacVersion, want) | ||
|
||
if _, present := supportedIacProviders[iacType]; !present { | ||
t.Errorf("mockIacType not registered") | ||
} | ||
got, present := supportedIacProviders[iacType][defaultIacVersion] | ||
if !present { | ||
t.Errorf("defaultIacVersion not registered") | ||
} | ||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("got: '%v', want: '%v'", got, want) | ||
} | ||
}) | ||
} |
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,20 @@ | ||
package iacprovider | ||
|
||
import ( | ||
"reflect" | ||
|
||
tfv12 "github.com/accurics/terrascan/pkg/iac-providers/terraform/v12" | ||
) | ||
|
||
// terraform specific constants | ||
const ( | ||
terraform supportedIacType = "terraform" | ||
terraformV12 supportedIacVersion = "v12" | ||
) | ||
|
||
// register terraform as an IaC provider with terrascan | ||
func init() { | ||
|
||
// register iac provider | ||
RegisterIacProvider(terraform, terraformV12, reflect.TypeOf(tfv12.TfV12{})) | ||
} |
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