Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
Add support for CloudFront public keys and CloudFront key groups (#873)
Browse files Browse the repository at this point in the history
  • Loading branch information
npellegrin committed Aug 7, 2023
1 parent d01537a commit f197cc3
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
66 changes: 66 additions & 0 deletions resources/cloudfront-key-groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package resources

import (
"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
}

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,
})
}

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)
return properties
}
66 changes: 66 additions & 0 deletions resources/cloudfront-public-keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package resources

import (
"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
}

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,
})
}

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)
return properties
}

0 comments on commit f197cc3

Please sign in to comment.