Skip to content

Commit

Permalink
fea: transform alert from enum lib into diagnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Guibert authored and eliecharra committed Aug 9, 2022
1 parent 4ecd34f commit 9556ea3
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 21 deletions.
15 changes: 14 additions & 1 deletion enumeration/alerter/alert.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package alerter

import "encoding/json"
import (
"encoding/json"

"github.com/snyk/driftctl/enumeration/resource"
)

type Alerts map[string][]Alert

type Alert interface {
Message() string
ShouldIgnoreResource() bool
Resource() *resource.Resource
}

type FakeAlert struct {
Expand All @@ -22,6 +27,10 @@ func (f *FakeAlert) ShouldIgnoreResource() bool {
return f.IgnoreResource
}

func (f *FakeAlert) Resource() *resource.Resource {
return nil
}

type SerializableAlert struct {
Alert
}
Expand All @@ -38,6 +47,10 @@ func (u *SerializedAlert) ShouldIgnoreResource() bool {
return false
}

func (s *SerializedAlert) Resource() *resource.Resource {
return nil
}

func (s *SerializableAlert) UnmarshalJSON(bytes []byte) error {
var res SerializedAlert

Expand Down
12 changes: 0 additions & 12 deletions enumeration/diagnostic.go

This file was deleted.

54 changes: 54 additions & 0 deletions enumeration/diagnostic/diagnostic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package diagnostic

import (
"github.com/snyk/driftctl/enumeration/alerter"
"github.com/snyk/driftctl/enumeration/remote/alerts"
"github.com/snyk/driftctl/enumeration/resource"
)

type Diagnostic interface {
Code() string
Message() string
ResourceType() string
Resource() *resource.Resource
}

type diagnosticImpl struct {
alert alerter.Alert
}

func (d *diagnosticImpl) Code() string {
if _, ok := d.alert.(*alerts.RemoteAccessDeniedAlert); ok {
return "ACCESS_DENIED"
}
return "NOT_IMPLEMENTED"
}

func (d *diagnosticImpl) Message() string {
return d.alert.Message()
}

func (d *diagnosticImpl) ResourceType() string {
ty := ""
if d.Resource() != nil {
ty = d.Resource().ResourceType()
}
return ty
}

func (d *diagnosticImpl) Resource() *resource.Resource {
return d.alert.Resource()
}

type Diagnostics []Diagnostic

func FromAlerts(alertMap alerter.Alerts) Diagnostics {
var results Diagnostics
for _, v := range alertMap {
for _, alert := range v {
diag := &diagnosticImpl{alert}
results = append(results, diag)
}
}
return results
}
3 changes: 2 additions & 1 deletion enumeration/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package enumeration
import (
"time"

"github.com/snyk/driftctl/enumeration/diagnostic"
"github.com/snyk/driftctl/enumeration/resource"
)

Expand All @@ -24,7 +25,7 @@ type EnumerateOutput struct {
// If the diagnostic is associated with a resource type, the ResourceType()
// call will indicate which type. If associated with a resource, the Resource()
// call will indicate which resource.
Diagnostics Diagnostics
Diagnostics diagnostic.Diagnostics
}

type Enumerator interface {
Expand Down
11 changes: 7 additions & 4 deletions enumeration/enumerator/cloud_enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"os"
"sync"

"github.com/snyk/driftctl/enumeration"

"github.com/sirupsen/logrus"
"github.com/snyk/driftctl/enumeration"
"github.com/snyk/driftctl/enumeration/alerter"
"github.com/snyk/driftctl/enumeration/diagnostic"
"github.com/snyk/driftctl/enumeration/parallel"
"github.com/snyk/driftctl/enumeration/remote"
"github.com/snyk/driftctl/enumeration/remote/common"
Expand Down Expand Up @@ -135,10 +135,12 @@ func (e *CloudEnumerator) Enumerate(input *enumeration.EnumerateInput) (*enumera

mapRes := mapByType(results)

diagnostics := diagnostic.FromAlerts(e.alerter.Alerts())

return &enumeration.EnumerateOutput{
Resources: mapRes,
Timings: nil,
Diagnostics: nil,
Diagnostics: diagnostics,
}, nil
}

Expand Down Expand Up @@ -170,10 +172,11 @@ func (e *CloudEnumerator) Refresh(input *enumeration.RefreshInput) (*enumeration
}

mapRes := mapByType(results)
diagnostics := diagnostic.FromAlerts(e.alerter.Alerts())

return &enumeration.RefreshOutput{
Resources: mapRes,
Diagnostics: nil,
Diagnostics: diagnostics,
}, nil
}

Expand Down
3 changes: 2 additions & 1 deletion enumeration/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package enumeration

import (
"github.com/hashicorp/terraform/terraform"
"github.com/snyk/driftctl/enumeration/diagnostic"
"github.com/snyk/driftctl/enumeration/resource"
)

Expand All @@ -12,7 +13,7 @@ type RefreshInput struct {

type RefreshOutput struct {
Resources map[string][]*resource.Resource
Diagnostics Diagnostics
Diagnostics diagnostic.Diagnostics
}

type GetSchemasOutput struct {
Expand Down
19 changes: 18 additions & 1 deletion enumeration/remote/alerts/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package alerts

import (
"fmt"
"strings"

"github.com/snyk/driftctl/enumeration/alerter"
"github.com/snyk/driftctl/enumeration/remote/common"
remoteerror "github.com/snyk/driftctl/enumeration/remote/error"
"github.com/snyk/driftctl/enumeration/resource"

"github.com/sirupsen/logrus"
)
Expand All @@ -21,6 +23,7 @@ type RemoteAccessDeniedAlert struct {
message string
provider string
scanningPhase ScanningPhase
resource *resource.Resource
}

func NewRemoteAccessDeniedAlert(provider string, scanErr *remoteerror.ResourceScanningError, scanningPhase ScanningPhase) *RemoteAccessDeniedAlert {
Expand All @@ -47,7 +50,17 @@ func NewRemoteAccessDeniedAlert(provider string, scanErr *remoteerror.ResourceSc
scanErr.RootCause().Error(),
)
}
return &RemoteAccessDeniedAlert{message, provider, scanningPhase}

var relatedResource *resource.Resource
resourceFQDNSSplit := strings.SplitN(scanErr.Resource(), ".", 2)
if len(resourceFQDNSSplit) == 2 {
relatedResource = &resource.Resource{
Id: resourceFQDNSSplit[1],
Type: resourceFQDNSSplit[0],
}
}

return &RemoteAccessDeniedAlert{message, provider, scanningPhase, relatedResource}
}

func (e *RemoteAccessDeniedAlert) Message() string {
Expand All @@ -58,6 +71,10 @@ func (e *RemoteAccessDeniedAlert) ShouldIgnoreResource() bool {
return true
}

func (e *RemoteAccessDeniedAlert) Resource() *resource.Resource {
return e.resource
}

func (e *RemoteAccessDeniedAlert) GetProviderMessage() string {
var message string
if e.scanningPhase == DetailsFetchingPhase {
Expand Down
4 changes: 4 additions & 0 deletions enumeration/remote/aws/sns_topic_subscription_enumerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (p *wrongArnTopicAlert) ShouldIgnoreResource() bool {
return false
}

func (p *wrongArnTopicAlert) Resource() *resource.Resource {
return nil
}

type SNSTopicSubscriptionEnumerator struct {
repository repository.SNSRepository
factory resource.ResourceFactory
Expand Down
8 changes: 8 additions & 0 deletions pkg/analyser/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func (u *UnmanagedSecurityGroupRulesAlert) ShouldIgnoreResource() bool {
return false
}

func (u *UnmanagedSecurityGroupRulesAlert) Resource() *resource.Resource {
return nil
}

type ComputedDiffAlert struct{}

func NewComputedDiffAlert() *ComputedDiffAlert {
Expand All @@ -37,6 +41,10 @@ func (c *ComputedDiffAlert) ShouldIgnoreResource() bool {
return false
}

func (c *ComputedDiffAlert) Resource() *resource.Resource {
return nil
}

type AnalyzerOptions struct {
Deep bool `json:"deep"`
OnlyManaged bool `json:"only_managed"`
Expand Down
10 changes: 9 additions & 1 deletion pkg/iac/terraform/state/alerts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package state

import "fmt"
import (
"fmt"

"github.com/snyk/driftctl/enumeration/resource"
)

type StateReadingAlert struct {
key string
Expand All @@ -18,3 +22,7 @@ func (s *StateReadingAlert) Message() string {
func (s *StateReadingAlert) ShouldIgnoreResource() bool {
return false
}

func (s *StateReadingAlert) Resource() *resource.Resource {
return nil
}

0 comments on commit 9556ea3

Please sign in to comment.