From f8fc2e5532cbb1860116c3394d878394ba0feab0 Mon Sep 17 00:00:00 2001 From: Nicolas Pellegrin Date: Thu, 24 Aug 2023 17:04:00 +0200 Subject: [PATCH] Add support for CloudFront public keys and CloudFront key groups (#873) (#1065) --- resources/cloudfront-key-groups.go | 74 +++++++++++++++++++++++++++++ resources/cloudfront-public-keys.go | 74 +++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 resources/cloudfront-key-groups.go create mode 100644 resources/cloudfront-public-keys.go diff --git a/resources/cloudfront-key-groups.go b/resources/cloudfront-key-groups.go new file mode 100644 index 000000000..8a790ed37 --- /dev/null +++ b/resources/cloudfront-key-groups.go @@ -0,0 +1,74 @@ +package resources + +import ( + "time" + + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/cloudfront" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type CloudFrontKeyGroup struct { + svc *cloudfront.CloudFront + ID *string + name *string + lastModifiedTime *time.Time +} + +func init() { + register("CloudFrontKeyGroup", ListCloudFrontKeyGroups) +} + +func ListCloudFrontKeyGroups(sess *session.Session) ([]Resource, error) { + svc := cloudfront.New(sess) + resources := []Resource{} + params := &cloudfront.ListKeyGroupsInput{} + + for { + resp, err := svc.ListKeyGroups(params) + if err != nil { + return nil, err + } + + for _, item := range resp.KeyGroupList.Items { + resources = append(resources, &CloudFrontKeyGroup{ + svc: svc, + ID: item.KeyGroup.Id, + name: item.KeyGroup.KeyGroupConfig.Name, + lastModifiedTime: item.KeyGroup.LastModifiedTime, + }) + } + + if resp.KeyGroupList.NextMarker == nil { + break + } + + params.Marker = resp.KeyGroupList.NextMarker + } + + return resources, nil +} + +func (f *CloudFrontKeyGroup) Remove() error { + resp, err := f.svc.GetKeyGroup(&cloudfront.GetKeyGroupInput{ + Id: f.ID, + }) + if err != nil { + return err + } + + _, err = f.svc.DeleteKeyGroup(&cloudfront.DeleteKeyGroupInput{ + Id: f.ID, + IfMatch: resp.ETag, + }) + + return err +} + +func (f *CloudFrontKeyGroup) Properties() types.Properties { + properties := types.NewProperties() + properties.Set("ID", f.ID) + properties.Set("Name", f.name) + properties.Set("LastModifiedTime", f.lastModifiedTime.Format(time.RFC3339)) + return properties +} diff --git a/resources/cloudfront-public-keys.go b/resources/cloudfront-public-keys.go new file mode 100644 index 000000000..3c5cd6e7b --- /dev/null +++ b/resources/cloudfront-public-keys.go @@ -0,0 +1,74 @@ +package resources + +import ( + "time" + + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/cloudfront" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type CloudFrontPublicKey struct { + svc *cloudfront.CloudFront + ID *string + name *string + createdTime *time.Time +} + +func init() { + register("CloudFrontPublicKey", ListCloudFrontPublicKeys) +} + +func ListCloudFrontPublicKeys(sess *session.Session) ([]Resource, error) { + svc := cloudfront.New(sess) + resources := []Resource{} + params := &cloudfront.ListPublicKeysInput{} + + for { + resp, err := svc.ListPublicKeys(params) + if err != nil { + return nil, err + } + + for _, item := range resp.PublicKeyList.Items { + resources = append(resources, &CloudFrontPublicKey{ + svc: svc, + ID: item.Id, + name: item.Name, + createdTime: item.CreatedTime, + }) + } + + if resp.PublicKeyList.NextMarker == nil { + break + } + + params.Marker = resp.PublicKeyList.NextMarker + } + + return resources, nil +} + +func (f *CloudFrontPublicKey) Remove() error { + resp, err := f.svc.GetPublicKey(&cloudfront.GetPublicKeyInput{ + Id: f.ID, + }) + if err != nil { + return err + } + + _, err = f.svc.DeletePublicKey(&cloudfront.DeletePublicKeyInput{ + Id: f.ID, + IfMatch: resp.ETag, + }) + + return err +} + +func (f *CloudFrontPublicKey) Properties() types.Properties { + properties := types.NewProperties() + properties.Set("ID", f.ID) + properties.Set("Name", f.name) + properties.Set("CreatedTime", f.createdTime.Format(time.RFC3339)) + return properties +}