|
| 1 | +package resources |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/aws/aws-sdk-go-v2/service/lakeformation" |
| 8 | + lakeformationtypes "github.com/aws/aws-sdk-go-v2/service/lakeformation/types" |
| 9 | + |
| 10 | + "github.com/ekristen/libnuke/pkg/registry" |
| 11 | + "github.com/ekristen/libnuke/pkg/resource" |
| 12 | + "github.com/ekristen/libnuke/pkg/types" |
| 13 | + |
| 14 | + "github.com/ekristen/aws-nuke/v3/pkg/nuke" |
| 15 | +) |
| 16 | + |
| 17 | +const LakeFormationPermissionResource = "LakeFormationPermission" |
| 18 | + |
| 19 | +func init() { |
| 20 | + registry.Register(®istry.Registration{ |
| 21 | + Name: LakeFormationPermissionResource, |
| 22 | + Scope: nuke.Account, |
| 23 | + Resource: &LakeFormationPermission{}, |
| 24 | + Lister: &LakeFormationPermissionLister{}, |
| 25 | + }) |
| 26 | +} |
| 27 | + |
| 28 | +type LakeFormationPermissionLister struct{} |
| 29 | + |
| 30 | +func (l *LakeFormationPermissionLister) List(ctx context.Context, o interface{}) ([]resource.Resource, error) { |
| 31 | + opts := o.(*nuke.ListerOpts) |
| 32 | + |
| 33 | + svc := lakeformation.NewFromConfig(*opts.Config) |
| 34 | + resources := make([]resource.Resource, 0) |
| 35 | + |
| 36 | + paginator := lakeformation.NewListPermissionsPaginator(svc, &lakeformation.ListPermissionsInput{}) |
| 37 | + for paginator.HasMorePages() { |
| 38 | + page, err := paginator.NextPage(ctx) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + |
| 43 | + for _, prp := range page.PrincipalResourcePermissions { |
| 44 | + resources = append(resources, &LakeFormationPermission{ |
| 45 | + svc: svc, |
| 46 | + PrincipalARN: prp.Principal.DataLakePrincipalIdentifier, |
| 47 | + Resource: prp.Resource, |
| 48 | + Permissions: prp.Permissions, |
| 49 | + }) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return resources, nil |
| 54 | +} |
| 55 | + |
| 56 | +type LakeFormationPermission struct { |
| 57 | + svc *lakeformation.Client |
| 58 | + PrincipalARN *string `description:"The ARN of the principal to remove permissions from"` |
| 59 | + Permissions []lakeformationtypes.Permission `description:"The permissions to remove from the principal"` |
| 60 | + Resource *lakeformationtypes.Resource `description:"-"` |
| 61 | +} |
| 62 | + |
| 63 | +func (f *LakeFormationPermission) Remove(ctx context.Context) error { |
| 64 | + _, err := f.svc.RevokePermissions(ctx, &lakeformation.RevokePermissionsInput{ |
| 65 | + Principal: &lakeformationtypes.DataLakePrincipal{ |
| 66 | + DataLakePrincipalIdentifier: f.PrincipalARN, |
| 67 | + }, |
| 68 | + Resource: f.Resource, |
| 69 | + Permissions: f.Permissions, |
| 70 | + }) |
| 71 | + |
| 72 | + return err |
| 73 | +} |
| 74 | + |
| 75 | +func (r *LakeFormationPermission) Filter() error { |
| 76 | + if *r.PrincipalARN == "IAM_ALLOWED_PRINCIPALS" { |
| 77 | + return fmt.Errorf("cannot delete default setting group permissions") |
| 78 | + } |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +func (f *LakeFormationPermission) Properties() types.Properties { |
| 83 | + return types.NewPropertiesFromStruct(f) |
| 84 | +} |
0 commit comments