Skip to content

Commit ed58b6d

Browse files
authored
Merge pull request #1579 from snyk/fea/alert_into_diag
fea: transform alert from enum lib into diagnostic
2 parents b1cf4b5 + de753eb commit ed58b6d

File tree

15 files changed

+163
-40
lines changed

15 files changed

+163
-40
lines changed

enumeration/alerter/alert.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package alerter
22

3-
import "encoding/json"
3+
import (
4+
"encoding/json"
5+
6+
"github.com/snyk/driftctl/enumeration/resource"
7+
)
48

59
type Alerts map[string][]Alert
610

711
type Alert interface {
812
Message() string
913
ShouldIgnoreResource() bool
14+
Resource() *resource.Resource
1015
}
1116

1217
type FakeAlert struct {
@@ -22,6 +27,10 @@ func (f *FakeAlert) ShouldIgnoreResource() bool {
2227
return f.IgnoreResource
2328
}
2429

30+
func (f *FakeAlert) Resource() *resource.Resource {
31+
return nil
32+
}
33+
2534
type SerializableAlert struct {
2635
Alert
2736
}
@@ -38,6 +47,10 @@ func (u *SerializedAlert) ShouldIgnoreResource() bool {
3847
return false
3948
}
4049

50+
func (s *SerializedAlert) Resource() *resource.Resource {
51+
return nil
52+
}
53+
4154
func (s *SerializableAlert) UnmarshalJSON(bytes []byte) error {
4255
var res SerializedAlert
4356

enumeration/diagnostic.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

enumeration/diagnostic/diagnostic.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package diagnostic
2+
3+
import (
4+
"github.com/snyk/driftctl/enumeration/alerter"
5+
"github.com/snyk/driftctl/enumeration/remote/alerts"
6+
"github.com/snyk/driftctl/enumeration/resource"
7+
)
8+
9+
type Diagnostic interface {
10+
Code() string
11+
Message() string
12+
ResourceType() string
13+
Resource() *resource.Resource
14+
}
15+
16+
type diagnosticImpl struct {
17+
alert alerter.Alert
18+
}
19+
20+
func (d *diagnosticImpl) Code() string {
21+
if _, ok := d.alert.(*alerts.RemoteAccessDeniedAlert); ok {
22+
return "ACCESS_DENIED"
23+
}
24+
return "UNKNOWN_ERROR"
25+
}
26+
27+
func (d *diagnosticImpl) Message() string {
28+
return d.alert.Message()
29+
}
30+
31+
func (d *diagnosticImpl) ResourceType() string {
32+
ty := ""
33+
if d.Resource() != nil {
34+
ty = d.Resource().ResourceType()
35+
}
36+
return ty
37+
}
38+
39+
func (d *diagnosticImpl) Resource() *resource.Resource {
40+
return d.alert.Resource()
41+
}
42+
43+
type Diagnostics []Diagnostic
44+
45+
func FromAlerts(alertMap alerter.Alerts) Diagnostics {
46+
var results Diagnostics
47+
for _, v := range alertMap {
48+
for _, alert := range v {
49+
diag := &diagnosticImpl{alert}
50+
results = append(results, diag)
51+
}
52+
}
53+
return results
54+
}

enumeration/enum.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package enumeration
33
import (
44
"time"
55

6+
"github.com/snyk/driftctl/enumeration/diagnostic"
67
"github.com/snyk/driftctl/enumeration/resource"
78
)
89

@@ -24,7 +25,7 @@ type EnumerateOutput struct {
2425
// If the diagnostic is associated with a resource type, the ResourceType()
2526
// call will indicate which type. If associated with a resource, the Resource()
2627
// call will indicate which resource.
27-
Diagnostics Diagnostics
28+
Diagnostics diagnostic.Diagnostics
2829
}
2930

3031
type Enumerator interface {

enumeration/enumerator/cloud_enumerator.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
"os"
77
"sync"
88

9-
"github.com/snyk/driftctl/enumeration"
10-
119
"github.com/sirupsen/logrus"
10+
"github.com/snyk/driftctl/enumeration"
1211
"github.com/snyk/driftctl/enumeration/alerter"
12+
"github.com/snyk/driftctl/enumeration/diagnostic"
1313
"github.com/snyk/driftctl/enumeration/parallel"
1414
"github.com/snyk/driftctl/enumeration/remote"
1515
"github.com/snyk/driftctl/enumeration/remote/common"
@@ -27,6 +27,11 @@ type CloudEnumerator struct {
2727
to string
2828
}
2929

30+
type ListOutput struct {
31+
Resources []*resource.Resource
32+
Diagnostics diagnostic.Diagnostics
33+
}
34+
3035
type cloudEnumeratorBuilder struct {
3136
cloud string
3237
providerVersion string
@@ -58,7 +63,7 @@ func (b *cloudEnumeratorBuilder) Build() (*CloudEnumerator, error) {
5863
detailsFetcherRunner: parallel.NewParallelRunner(context.TODO(), 10),
5964
providerLibrary: terraform.NewProviderLibrary(),
6065
remoteLibrary: common.NewRemoteLibrary(),
61-
alerter: &sliceAlerter{},
66+
alerter: newSliceAlerter(),
6267
progress: &dummyCounter{},
6368
}
6469

@@ -92,6 +97,9 @@ func (e *CloudEnumerator) init(to, providerVersion, configDirectory string) erro
9297
}
9398

9499
func (e *CloudEnumerator) Enumerate(input *enumeration.EnumerateInput) (*enumeration.EnumerateOutput, error) {
100+
101+
e.alerter.alerts = alerter.Alerts{}
102+
95103
types := map[string]struct{}{}
96104
for _, resourceType := range input.ResourceTypes {
97105
types[resourceType] = struct{}{}
@@ -135,14 +143,19 @@ func (e *CloudEnumerator) Enumerate(input *enumeration.EnumerateInput) (*enumera
135143

136144
mapRes := mapByType(results)
137145

146+
diagnostics := diagnostic.FromAlerts(e.alerter.Alerts())
147+
138148
return &enumeration.EnumerateOutput{
139149
Resources: mapRes,
140150
Timings: nil,
141-
Diagnostics: nil,
151+
Diagnostics: diagnostics,
142152
}, nil
143153
}
144154

145155
func (e *CloudEnumerator) Refresh(input *enumeration.RefreshInput) (*enumeration.RefreshOutput, error) {
156+
157+
e.alerter.alerts = alerter.Alerts{}
158+
146159
for _, resByType := range input.Resources {
147160
for _, res := range resByType {
148161
res := res
@@ -170,10 +183,11 @@ func (e *CloudEnumerator) Refresh(input *enumeration.RefreshInput) (*enumeration
170183
}
171184

172185
mapRes := mapByType(results)
186+
diagnostics := diagnostic.FromAlerts(e.alerter.Alerts())
173187

174188
return &enumeration.RefreshOutput{
175189
Resources: mapRes,
176-
Diagnostics: nil,
190+
Diagnostics: diagnostics,
177191
}, nil
178192
}
179193

@@ -203,26 +217,41 @@ loop:
203217
return results, runner.Err()
204218
}
205219

206-
func (e *CloudEnumerator) List(typ string) ([]*resource.Resource, error) {
220+
func (e *CloudEnumerator) List(typ string) (*ListOutput, error) {
221+
222+
diagnostics := diagnostic.Diagnostics{}
223+
207224
enumInput := &enumeration.EnumerateInput{ResourceTypes: []string{typ}}
208225
enumerate, err := e.Enumerate(enumInput)
209226
if err != nil {
210227
return nil, err
211228
}
229+
diagnostics = append(diagnostics, enumerate.Diagnostics...)
212230

213231
refreshInput := &enumeration.RefreshInput{Resources: enumerate.Resources}
214232
refresh, err := e.Refresh(refreshInput)
215233
if err != nil {
216234
return nil, err
217235
}
218-
return refresh.Resources[typ], nil
236+
diagnostics = append(diagnostics, refresh.Diagnostics...)
237+
238+
return &ListOutput{
239+
Resources: refresh.Resources[typ],
240+
Diagnostics: diagnostics,
241+
}, nil
219242
}
220243

221244
type sliceAlerter struct {
222245
lock sync.Mutex
223246
alerts alerter.Alerts
224247
}
225248

249+
func newSliceAlerter() *sliceAlerter {
250+
return &sliceAlerter{
251+
alerts: alerter.Alerts{},
252+
}
253+
}
254+
226255
func (d *sliceAlerter) Alerts() alerter.Alerts {
227256
return d.alerts
228257
}

enumeration/refresh.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package enumeration
22

33
import (
44
"github.com/hashicorp/terraform/terraform"
5+
"github.com/snyk/driftctl/enumeration/diagnostic"
56
"github.com/snyk/driftctl/enumeration/resource"
67
)
78

@@ -12,7 +13,7 @@ type RefreshInput struct {
1213

1314
type RefreshOutput struct {
1415
Resources map[string][]*resource.Resource
15-
Diagnostics Diagnostics
16+
Diagnostics diagnostic.Diagnostics
1617
}
1718

1819
type GetSchemasOutput struct {

enumeration/remote/alerts/alerts.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package alerts
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/snyk/driftctl/enumeration/alerter"
78
"github.com/snyk/driftctl/enumeration/remote/common"
89
remoteerror "github.com/snyk/driftctl/enumeration/remote/error"
10+
"github.com/snyk/driftctl/enumeration/resource"
911

1012
"github.com/sirupsen/logrus"
1113
)
@@ -21,33 +23,44 @@ type RemoteAccessDeniedAlert struct {
2123
message string
2224
provider string
2325
scanningPhase ScanningPhase
26+
resource *resource.Resource
2427
}
2528

2629
func NewRemoteAccessDeniedAlert(provider string, scanErr *remoteerror.ResourceScanningError, scanningPhase ScanningPhase) *RemoteAccessDeniedAlert {
2730
var message string
2831
switch scanningPhase {
2932
case EnumerationPhase:
3033
message = fmt.Sprintf(
31-
"Ignoring %s from drift calculation: Listing %s is forbidden: %s",
34+
"An error occured listing %s: listing %s is forbidden: %s",
3235
scanErr.Resource(),
3336
scanErr.ListedTypeError(),
3437
scanErr.RootCause().Error(),
3538
)
3639
case DetailsFetchingPhase:
3740
message = fmt.Sprintf(
38-
"Ignoring %s from drift calculation: Reading details of %s is forbidden: %s",
41+
"An error occured listing %s: reading details of %s is forbidden: %s",
3942
scanErr.Resource(),
4043
scanErr.ListedTypeError(),
4144
scanErr.RootCause().Error(),
4245
)
4346
default:
4447
message = fmt.Sprintf(
45-
"Ignoring %s from drift calculation: %s",
48+
"An error occured listing %s: %s",
4649
scanErr.Resource(),
4750
scanErr.RootCause().Error(),
4851
)
4952
}
50-
return &RemoteAccessDeniedAlert{message, provider, scanningPhase}
53+
54+
var relatedResource *resource.Resource
55+
resourceFQDNSSplit := strings.SplitN(scanErr.Resource(), ".", 2)
56+
if len(resourceFQDNSSplit) == 2 {
57+
relatedResource = &resource.Resource{
58+
Id: resourceFQDNSSplit[1],
59+
Type: resourceFQDNSSplit[0],
60+
}
61+
}
62+
63+
return &RemoteAccessDeniedAlert{message, provider, scanningPhase, relatedResource}
5164
}
5265

5366
func (e *RemoteAccessDeniedAlert) Message() string {
@@ -58,6 +71,10 @@ func (e *RemoteAccessDeniedAlert) ShouldIgnoreResource() bool {
5871
return true
5972
}
6073

74+
func (e *RemoteAccessDeniedAlert) Resource() *resource.Resource {
75+
return e.resource
76+
}
77+
6178
func (e *RemoteAccessDeniedAlert) GetProviderMessage() string {
6279
var message string
6380
if e.scanningPhase == DetailsFetchingPhase {

enumeration/remote/aws/sns_topic_subscription_enumerator.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func (p *wrongArnTopicAlert) ShouldIgnoreResource() bool {
3333
return false
3434
}
3535

36+
func (p *wrongArnTopicAlert) Resource() *resource.Resource {
37+
return nil
38+
}
39+
3640
type SNSTopicSubscriptionEnumerator struct {
3741
repository repository.SNSRepository
3842
factory resource.ResourceFactory

pkg/analyser/analyzer.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ func (u *UnmanagedSecurityGroupRulesAlert) ShouldIgnoreResource() bool {
2323
return false
2424
}
2525

26+
func (u *UnmanagedSecurityGroupRulesAlert) Resource() *resource.Resource {
27+
return nil
28+
}
29+
2630
type ComputedDiffAlert struct{}
2731

2832
func NewComputedDiffAlert() *ComputedDiffAlert {
@@ -37,6 +41,10 @@ func (c *ComputedDiffAlert) ShouldIgnoreResource() bool {
3741
return false
3842
}
3943

44+
func (c *ComputedDiffAlert) Resource() *resource.Resource {
45+
return nil
46+
}
47+
4048
type AnalyzerOptions struct {
4149
Deep bool `json:"deep"`
4250
OnlyManaged bool `json:"only_managed"`

pkg/cmd/scan/output/testdata/output.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -681,17 +681,17 @@ <h2>Jun 10, 2021</h2>
681681

682682
<li data-kind="resource-alerts" class="resource-item">
683683

684-
<span>Ignoring aws_vpc from drift calculation: Listing aws_vpc is forbidden: dummy error</span>
684+
<span>An error occured listing aws_vpc: listing aws_vpc is forbidden: dummy error</span>
685685
</li>
686686

687687
<li data-kind="resource-alerts" class="resource-item">
688688

689-
<span>Ignoring aws_sqs from drift calculation: Listing aws_sqs is forbidden: dummy error</span>
689+
<span>An error occured listing aws_sqs: listing aws_sqs is forbidden: dummy error</span>
690690
</li>
691691

692692
<li data-kind="resource-alerts" class="resource-item">
693693

694-
<span>Ignoring aws_sns from drift calculation: Listing aws_sns is forbidden: dummy error</span>
694+
<span>An error occured listing aws_sns: listing aws_sns is forbidden: dummy error</span>
695695
</li>
696696

697697

0 commit comments

Comments
 (0)