Skip to content

Commit

Permalink
Merge pull request aquasecurity#278 from aquasecurity/liamg-adapter-t…
Browse files Browse the repository at this point in the history
…ests

fix: Add github adapter tests and fix line numbers
  • Loading branch information
liamg authored Feb 18, 2022
2 parents 13cf97f + 79a00b5 commit 91ad861
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 144 deletions.
33 changes: 14 additions & 19 deletions adapters/terraform/github/repositories/adapt.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,25 @@ func adaptRepositories(modules terraform.Modules) []github.Repository {

func adaptRepository(resource *terraform.Block) github.Repository {

// visibility overrides private
visibilityAttr := resource.GetAttribute("visibility")
if visibilityAttr.Equals("private") || visibilityAttr.Equals("internal") {
return github.Repository{
Metadata: resource.GetMetadata(),
Public: types.Bool(false, resource.GetMetadata()),
}
} else if visibilityAttr.Equals("public") {
return github.Repository{
Metadata: resource.GetMetadata(),
Public: types.Bool(true, resource.GetMetadata()),
}
repo := github.Repository{
Metadata: resource.GetMetadata(),
Public: types.Bool(true, resource.GetMetadata()),
}

privateAttr := resource.GetAttribute("private")
if privateAttr.IsTrue() {
return github.Repository{
Metadata: resource.GetMetadata(),
Public: types.Bool(false, resource.GetMetadata()),
}
repo.Public = types.Bool(false, privateAttr.GetMetadata())
} else if privateAttr.IsFalse() {
repo.Public = types.Bool(true, privateAttr.GetMetadata())
}

return github.Repository{
Metadata: resource.GetMetadata(),
Public: types.Bool(true, resource.GetMetadata()),
// visibility overrides private
visibilityAttr := resource.GetAttribute("visibility")
if visibilityAttr.Equals("private") || visibilityAttr.Equals("internal") {
repo.Public = types.Bool(false, visibilityAttr.GetMetadata())
} else if visibilityAttr.Equals("public") {
repo.Public = types.Bool(true, visibilityAttr.GetMetadata())
}

return repo
}
128 changes: 57 additions & 71 deletions adapters/terraform/github/repositories/adapt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,73 @@ import (
"testing"

"github.com/aquasecurity/defsec/adapters/terraform/testutil"

"github.com/aquasecurity/defsec/providers/github"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_Adapt(t *testing.T) {
t.SkipNow()
tests := []struct {
name string
terraform string
expected []github.Repository
}{
{
name: "basic",
terraform: `
resource "" "example" {
func Test_AdaptDefaults(t *testing.T) {

src := `
resource "github_repository" "my-repo" {
}
`,
expected: []github.Repository{},
},
}
`
modules := testutil.CreateModulesFromSource(src, ".tf", t)
repositories := Adapt(modules)
require.Len(t, repositories, 1)
repo := repositories[0]

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
modules := testutil.CreateModulesFromSource(test.terraform, ".tf", t)
adapted := Adapt(modules)
testutil.AssertDefsecEqual(t, test.expected, adapted)
})
}
assert.True(t, repo.Public.IsTrue())
}

func Test_adaptRepositories(t *testing.T) {
t.SkipNow()
tests := []struct {
name string
terraform string
expected []github.Repository
}{
{
name: "basic",
terraform: `
resource "" "example" {
func Test_Adapt_Private(t *testing.T) {

src := `
resource "github_repository" "my-repo" {
private = true
}
`,
expected: []github.Repository{},
},
}
`
modules := testutil.CreateModulesFromSource(src, ".tf", t)
repositories := Adapt(modules)
require.Len(t, repositories, 1)
repo := repositories[0]

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
modules := testutil.CreateModulesFromSource(test.terraform, ".tf", t)
adapted := adaptRepositories(modules)
testutil.AssertDefsecEqual(t, test.expected, adapted)
})
}
assert.False(t, repo.Public.IsTrue())
assert.Equal(t, 3, repo.Public.GetMetadata().Range().GetStartLine())
assert.Equal(t, 3, repo.Public.GetMetadata().Range().GetEndLine())
}

func Test_adaptRepository(t *testing.T) {
t.SkipNow()
tests := []struct {
name string
terraform string
expected github.Repository
}{
{
name: "basic",
terraform: `
resource "" "example" {
func Test_Adapt_Public(t *testing.T) {

src := `
resource "github_repository" "my-repo" {
private = false
}
`
modules := testutil.CreateModulesFromSource(src, ".tf", t)
repositories := Adapt(modules)
require.Len(t, repositories, 1)
repo := repositories[0]

assert.True(t, repo.Public.IsTrue())
assert.Equal(t, 3, repo.Public.GetMetadata().Range().GetStartLine())
assert.Equal(t, 3, repo.Public.GetMetadata().Range().GetEndLine())
}

func Test_Adapt_VisibilityOverride(t *testing.T) {

src := `
resource "github_repository" "my-repo" {
private = true
visibility = "public"
}
`,
expected: github.Repository{},
},
}
`
modules := testutil.CreateModulesFromSource(src, ".tf", t)
repositories := Adapt(modules)
require.Len(t, repositories, 1)
repo := repositories[0]

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
modules := testutil.CreateModulesFromSource(test.terraform, ".tf", t)
adapted := adaptRepository(modules.GetBlocks()[0])
testutil.AssertDefsecEqual(t, test.expected, adapted)
})
}
assert.True(t, repo.Public.IsTrue())
assert.Equal(t, 4, repo.Public.GetMetadata().Range().GetStartLine())
assert.Equal(t, 4, repo.Public.GetMetadata().Range().GetEndLine())
}
83 changes: 29 additions & 54 deletions adapters/terraform/github/secrets/adapt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,11 @@ import (
"testing"

"github.com/aquasecurity/defsec/adapters/terraform/testutil"

"github.com/aquasecurity/defsec/parsers/types"
"github.com/aquasecurity/defsec/providers/github"
)

func Test_Adapt(t *testing.T) {
t.SkipNow()
tests := []struct {
name string
terraform string
expected []github.EnvironmentSecret
}{
{
name: "basic",
terraform: `
resource "" "example" {
}
`,
expected: []github.EnvironmentSecret{},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
modules := testutil.CreateModulesFromSource(test.terraform, ".tf", t)
adapted := Adapt(modules)
testutil.AssertDefsecEqual(t, test.expected, adapted)
})
}
}

func Test_adaptSecrets(t *testing.T) {
t.SkipNow()
tests := []struct {
name string
terraform string
Expand All @@ -45,45 +17,48 @@ func Test_adaptSecrets(t *testing.T) {
{
name: "basic",
terraform: `
resource "" "example" {
resource "github_actions_environment_secret" "example" {
}
`,
expected: []github.EnvironmentSecret{},
expected: []github.EnvironmentSecret{
{
Metadata: types.NewTestMetadata(),
Environment: types.String("", types.NewTestMetadata()),
SecretName: types.String("", types.NewTestMetadata()),
PlainTextValue: types.String("", types.NewTestMetadata()),
EncryptedValue: types.String("", types.NewTestMetadata()),
Repository: types.String("", types.NewTestMetadata()),
},
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
modules := testutil.CreateModulesFromSource(test.terraform, ".tf", t)
adapted := adaptSecrets(modules)
testutil.AssertDefsecEqual(t, test.expected, adapted)
})
}
}

func Test_adaptSecret(t *testing.T) {
t.SkipNow()
tests := []struct {
name string
terraform string
expected github.EnvironmentSecret
}{
{
name: "basic",
terraform: `
resource "" "example" {
resource "github_actions_environment_secret" "example" {
secret_name = "a"
plaintext_value = "b"
environment = "c"
encrypted_value = "d"
repository = "e"
}
`,
expected: github.EnvironmentSecret{},
expected: []github.EnvironmentSecret{
{
Metadata: types.NewTestMetadata(),
SecretName: types.String("a", types.NewTestMetadata()),
PlainTextValue: types.String("b", types.NewTestMetadata()),
Environment: types.String("c", types.NewTestMetadata()),
EncryptedValue: types.String("d", types.NewTestMetadata()),
Repository: types.String("e", types.NewTestMetadata()),
},
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
modules := testutil.CreateModulesFromSource(test.terraform, ".tf", t)
adapted := adaptSecret(modules.GetBlocks()[0])
adapted := Adapt(modules)
testutil.AssertDefsecEqual(t, test.expected, adapted)
})
}
Expand Down

0 comments on commit 91ad861

Please sign in to comment.