2020-12-09 15:31:34 +00:00
|
|
|
package analyser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-05-04 15:08:54 +00:00
|
|
|
"fmt"
|
2021-03-26 18:10:38 +00:00
|
|
|
"sort"
|
2021-03-29 09:21:45 +00:00
|
|
|
"strings"
|
2021-04-28 10:42:06 +00:00
|
|
|
"time"
|
2021-03-26 18:10:38 +00:00
|
|
|
|
|
|
|
"github.com/r3labs/diff/v2"
|
2020-12-09 15:31:34 +00:00
|
|
|
|
2020-12-16 12:02:02 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/alerter"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
)
|
|
|
|
|
2021-01-08 17:14:26 +00:00
|
|
|
type Change struct {
|
|
|
|
diff.Change
|
2021-03-26 08:44:55 +00:00
|
|
|
Computed bool `json:"computed"`
|
|
|
|
JsonString bool `json:"-"`
|
2021-01-08 17:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Changelog []Change
|
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
type Difference struct {
|
|
|
|
Res resource.Resource
|
2021-01-08 17:14:26 +00:00
|
|
|
Changelog Changelog
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Summary struct {
|
|
|
|
TotalResources int `json:"total_resources"`
|
2021-04-12 13:42:18 +00:00
|
|
|
TotalDrifted int `json:"total_changed"`
|
2020-12-09 15:31:34 +00:00
|
|
|
TotalUnmanaged int `json:"total_unmanaged"`
|
2021-03-29 09:51:03 +00:00
|
|
|
TotalDeleted int `json:"total_missing"`
|
2020-12-09 15:31:34 +00:00
|
|
|
TotalManaged int `json:"total_managed"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Analysis struct {
|
|
|
|
unmanaged []resource.Resource
|
|
|
|
managed []resource.Resource
|
|
|
|
deleted []resource.Resource
|
|
|
|
differences []Difference
|
|
|
|
summary Summary
|
2020-12-16 12:02:02 +00:00
|
|
|
alerts alerter.Alerts
|
2021-04-28 10:42:06 +00:00
|
|
|
Duration time.Duration
|
2021-06-10 13:25:54 +00:00
|
|
|
Date time.Time
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type serializableDifference struct {
|
|
|
|
Res resource.SerializableResource `json:"res"`
|
2021-01-08 17:14:26 +00:00
|
|
|
Changelog Changelog `json:"changelog"`
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type serializableAnalysis struct {
|
2021-02-12 21:30:36 +00:00
|
|
|
Summary Summary `json:"summary"`
|
|
|
|
Managed []resource.SerializableResource `json:"managed"`
|
|
|
|
Unmanaged []resource.SerializableResource `json:"unmanaged"`
|
2021-03-29 09:51:03 +00:00
|
|
|
Deleted []resource.SerializableResource `json:"missing"`
|
2021-02-12 21:30:36 +00:00
|
|
|
Differences []serializableDifference `json:"differences"`
|
|
|
|
Coverage int `json:"coverage"`
|
|
|
|
Alerts map[string][]alerter.SerializableAlert `json:"alerts"`
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 15:08:54 +00:00
|
|
|
type GenDriftIgnoreOptions struct {
|
|
|
|
ExcludeUnmanaged bool
|
|
|
|
ExcludeDeleted bool
|
|
|
|
ExcludeDrifted bool
|
|
|
|
InputPath string
|
|
|
|
}
|
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
func (a Analysis) MarshalJSON() ([]byte, error) {
|
|
|
|
bla := serializableAnalysis{}
|
|
|
|
for _, m := range a.managed {
|
|
|
|
bla.Managed = append(bla.Managed, resource.SerializableResource{Resource: m})
|
|
|
|
}
|
|
|
|
for _, u := range a.unmanaged {
|
|
|
|
bla.Unmanaged = append(bla.Unmanaged, resource.SerializableResource{Resource: u})
|
|
|
|
}
|
|
|
|
for _, d := range a.deleted {
|
|
|
|
bla.Deleted = append(bla.Deleted, resource.SerializableResource{Resource: d})
|
|
|
|
}
|
|
|
|
for _, di := range a.differences {
|
|
|
|
bla.Differences = append(bla.Differences, serializableDifference{
|
|
|
|
Res: resource.SerializableResource{Resource: di.Res},
|
|
|
|
Changelog: di.Changelog,
|
|
|
|
})
|
|
|
|
}
|
2021-02-12 21:30:36 +00:00
|
|
|
if len(a.alerts) > 0 {
|
|
|
|
bla.Alerts = make(map[string][]alerter.SerializableAlert)
|
|
|
|
for k, v := range a.alerts {
|
|
|
|
for _, al := range v {
|
|
|
|
bla.Alerts[k] = append(bla.Alerts[k], alerter.SerializableAlert{Alert: al})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
bla.Summary = a.summary
|
|
|
|
bla.Coverage = a.Coverage()
|
|
|
|
|
|
|
|
return json.Marshal(bla)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Analysis) UnmarshalJSON(bytes []byte) error {
|
|
|
|
bla := serializableAnalysis{}
|
|
|
|
if err := json.Unmarshal(bytes, &bla); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, u := range bla.Unmanaged {
|
2021-05-21 14:09:45 +00:00
|
|
|
a.AddUnmanaged(&resource.SerializedResource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: u.TerraformId(),
|
|
|
|
Type: u.TerraformType(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, d := range bla.Deleted {
|
2021-05-21 14:09:45 +00:00
|
|
|
a.AddDeleted(&resource.SerializedResource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: d.TerraformId(),
|
|
|
|
Type: d.TerraformType(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, m := range bla.Managed {
|
2021-05-21 14:09:45 +00:00
|
|
|
a.AddManaged(&resource.SerializedResource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: m.TerraformId(),
|
|
|
|
Type: m.TerraformType(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, di := range bla.Differences {
|
|
|
|
a.AddDifference(Difference{
|
2021-05-21 14:09:45 +00:00
|
|
|
Res: &resource.SerializedResource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: di.Res.TerraformId(),
|
|
|
|
Type: di.Res.TerraformType(),
|
|
|
|
},
|
|
|
|
Changelog: di.Changelog,
|
|
|
|
})
|
|
|
|
}
|
2021-02-12 21:30:36 +00:00
|
|
|
if len(bla.Alerts) > 0 {
|
|
|
|
a.alerts = make(alerter.Alerts)
|
|
|
|
for k, v := range bla.Alerts {
|
|
|
|
for _, al := range v {
|
|
|
|
a.alerts[k] = append(a.alerts[k], &alerter.SerializedAlert{
|
|
|
|
Msg: al.Message(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Analysis) IsSync() bool {
|
|
|
|
return a.summary.TotalDrifted == 0 && a.summary.TotalUnmanaged == 0 && a.summary.TotalDeleted == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Analysis) AddDeleted(resources ...resource.Resource) {
|
|
|
|
a.deleted = append(a.deleted, resources...)
|
|
|
|
a.summary.TotalResources += len(resources)
|
|
|
|
a.summary.TotalDeleted += len(resources)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Analysis) AddUnmanaged(resources ...resource.Resource) {
|
|
|
|
a.unmanaged = append(a.unmanaged, resources...)
|
|
|
|
a.summary.TotalResources += len(resources)
|
|
|
|
a.summary.TotalUnmanaged += len(resources)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Analysis) AddManaged(resources ...resource.Resource) {
|
|
|
|
a.managed = append(a.managed, resources...)
|
|
|
|
a.summary.TotalResources += len(resources)
|
|
|
|
a.summary.TotalManaged += len(resources)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Analysis) AddDifference(diffs ...Difference) {
|
|
|
|
a.differences = append(a.differences, diffs...)
|
|
|
|
a.summary.TotalDrifted += len(diffs)
|
|
|
|
}
|
|
|
|
|
2021-01-11 16:33:23 +00:00
|
|
|
func (a *Analysis) SetAlerts(alerts alerter.Alerts) {
|
2020-12-16 12:02:02 +00:00
|
|
|
a.alerts = alerts
|
|
|
|
}
|
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
func (a *Analysis) Coverage() int {
|
|
|
|
if a.summary.TotalResources > 0 {
|
|
|
|
return int((float32(a.summary.TotalManaged) / float32(a.summary.TotalResources)) * 100.0)
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Analysis) Managed() []resource.Resource {
|
|
|
|
return a.managed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Analysis) Unmanaged() []resource.Resource {
|
|
|
|
return a.unmanaged
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Analysis) Deleted() []resource.Resource {
|
|
|
|
return a.deleted
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Analysis) Differences() []Difference {
|
|
|
|
return a.differences
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Analysis) Summary() Summary {
|
|
|
|
return a.summary
|
|
|
|
}
|
2020-12-16 12:02:02 +00:00
|
|
|
|
|
|
|
func (a *Analysis) Alerts() alerter.Alerts {
|
|
|
|
return a.alerts
|
|
|
|
}
|
2021-03-19 18:20:57 +00:00
|
|
|
|
|
|
|
func (a *Analysis) SortResources() {
|
|
|
|
a.unmanaged = resource.Sort(a.unmanaged)
|
|
|
|
a.deleted = resource.Sort(a.deleted)
|
2021-03-26 18:10:38 +00:00
|
|
|
a.differences = SortDifferences(a.differences)
|
|
|
|
}
|
|
|
|
|
2021-05-04 15:08:54 +00:00
|
|
|
func (a *Analysis) DriftIgnoreList(opts GenDriftIgnoreOptions) (int, string) {
|
|
|
|
var list []string
|
|
|
|
|
|
|
|
resourceCount := 0
|
|
|
|
|
|
|
|
addResources := func(res ...resource.Resource) {
|
|
|
|
for _, r := range res {
|
|
|
|
list = append(list, fmt.Sprintf("%s.%s", r.TerraformType(), escapeKey(r.TerraformId())))
|
|
|
|
}
|
|
|
|
resourceCount += len(res)
|
|
|
|
}
|
|
|
|
addDifferences := func(diff ...Difference) {
|
|
|
|
for _, d := range diff {
|
|
|
|
addResources(d.Res)
|
|
|
|
}
|
|
|
|
resourceCount += len(diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !opts.ExcludeUnmanaged && a.Summary().TotalUnmanaged > 0 {
|
|
|
|
list = append(list, "# Resources not covered by IaC")
|
|
|
|
addResources(a.Unmanaged()...)
|
|
|
|
}
|
|
|
|
if !opts.ExcludeDeleted && a.Summary().TotalDeleted > 0 {
|
|
|
|
list = append(list, "# Missing resources")
|
|
|
|
addResources(a.Deleted()...)
|
|
|
|
}
|
|
|
|
if !opts.ExcludeDrifted && a.Summary().TotalDrifted > 0 {
|
|
|
|
list = append(list, "# Changed resources")
|
|
|
|
addDifferences(a.Differences()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceCount, strings.Join(list, "\n")
|
|
|
|
}
|
|
|
|
|
2021-03-26 18:10:38 +00:00
|
|
|
func SortDifferences(diffs []Difference) []Difference {
|
|
|
|
sort.SliceStable(diffs, func(i, j int) bool {
|
|
|
|
if diffs[i].Res.TerraformType() != diffs[j].Res.TerraformType() {
|
|
|
|
return diffs[i].Res.TerraformType() < diffs[j].Res.TerraformType()
|
|
|
|
}
|
|
|
|
return diffs[i].Res.TerraformId() < diffs[j].Res.TerraformId()
|
|
|
|
})
|
2021-03-29 09:21:45 +00:00
|
|
|
|
|
|
|
for _, d := range diffs {
|
|
|
|
SortChanges(d.Changelog)
|
|
|
|
}
|
|
|
|
|
2021-03-26 18:10:38 +00:00
|
|
|
return diffs
|
2021-03-19 18:20:57 +00:00
|
|
|
}
|
2021-03-29 09:21:45 +00:00
|
|
|
|
|
|
|
func SortChanges(changes []Change) []Change {
|
|
|
|
sort.SliceStable(changes, func(i, j int) bool {
|
|
|
|
return strings.Join(changes[i].Path, ".") < strings.Join(changes[j].Path, ".")
|
|
|
|
})
|
|
|
|
return changes
|
|
|
|
}
|
2021-05-04 15:08:54 +00:00
|
|
|
|
|
|
|
func escapeKey(line string) string {
|
|
|
|
line = strings.ReplaceAll(line, `\`, `\\`)
|
|
|
|
line = strings.ReplaceAll(line, `.`, `\.`)
|
|
|
|
|
|
|
|
return line
|
|
|
|
}
|