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 11, 2022
1 parent b1cf4b5 commit c5eec8b
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 23 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 "UNKNOWN_ERROR"
}

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
18 changes: 12 additions & 6 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 Expand Up @@ -203,7 +206,7 @@ loop:
return results, runner.Err()
}

func (e *CloudEnumerator) List(typ string) ([]*resource.Resource, error) {
func (e *CloudEnumerator) List(typ string) (*enumeration.RefreshOutput, error) {
enumInput := &enumeration.EnumerateInput{ResourceTypes: []string{typ}}
enumerate, err := e.Enumerate(enumInput)
if err != nil {
Expand All @@ -215,7 +218,10 @@ func (e *CloudEnumerator) List(typ string) ([]*resource.Resource, error) {
if err != nil {
return nil, err
}
return refresh.Resources[typ], nil

refresh.Diagnostics = append(refresh.Diagnostics, enumerate.Diagnostics...)

return refresh, nil
}

type sliceAlerter struct {
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 c5eec8b

Please sign in to comment.