|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package table |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "errors" |
| 19 | + |
| 20 | + ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors" |
| 21 | + ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log" |
| 22 | + svcsdk "github.com/aws/aws-sdk-go-v2/service/dynamodb" |
| 23 | + svcsdktypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" |
| 24 | +) |
| 25 | + |
| 26 | +// syncResourcePolicy updates a DynamoDB table's resource-based policy. |
| 27 | +func (rm *resourceManager) syncResourcePolicy( |
| 28 | + ctx context.Context, |
| 29 | + desired *resource, |
| 30 | + latest *resource, |
| 31 | +) (err error) { |
| 32 | + rlog := ackrtlog.FromContext(ctx) |
| 33 | + exit := rlog.Trace("rm.syncResourcePolicy") |
| 34 | + defer func(err error) { exit(err) }(err) |
| 35 | + |
| 36 | + if desired.ko.Spec.ResourcePolicy == nil { |
| 37 | + return rm.deleteResourcePolicy(ctx, latest) |
| 38 | + } |
| 39 | + |
| 40 | + return rm.putResourcePolicy(ctx, desired) |
| 41 | +} |
| 42 | + |
| 43 | +// putResourcePolicy attaches or updates a resource-based policy to a DynamoDB table. |
| 44 | +func (rm *resourceManager) putResourcePolicy( |
| 45 | + ctx context.Context, |
| 46 | + r *resource, |
| 47 | +) (err error) { |
| 48 | + rlog := ackrtlog.FromContext(ctx) |
| 49 | + exit := rlog.Trace("rm.putResourcePolicy") |
| 50 | + defer func(err error) { exit(err) }(err) |
| 51 | + |
| 52 | + if r.ko.Spec.ResourcePolicy == nil { |
| 53 | + return nil |
| 54 | + } |
| 55 | + |
| 56 | + tableARN := (*string)(r.ko.Status.ACKResourceMetadata.ARN) |
| 57 | + if tableARN == nil || *tableARN == "" { |
| 58 | + return errors.New("table ARN is required to put resource policy") |
| 59 | + } |
| 60 | + |
| 61 | + _, err = rm.sdkapi.PutResourcePolicy( |
| 62 | + ctx, |
| 63 | + &svcsdk.PutResourcePolicyInput{ |
| 64 | + ResourceArn: tableARN, |
| 65 | + Policy: r.ko.Spec.ResourcePolicy, |
| 66 | + }, |
| 67 | + ) |
| 68 | + rm.metrics.RecordAPICall("UPDATE", "PutResourcePolicy", err) |
| 69 | + return err |
| 70 | +} |
| 71 | + |
| 72 | +// deleteResourcePolicy removes a resource-based policy from a DynamoDB table. |
| 73 | +func (rm *resourceManager) deleteResourcePolicy( |
| 74 | + ctx context.Context, |
| 75 | + r *resource, |
| 76 | +) (err error) { |
| 77 | + rlog := ackrtlog.FromContext(ctx) |
| 78 | + exit := rlog.Trace("rm.deleteResourcePolicy") |
| 79 | + defer func(err error) { exit(err) }(err) |
| 80 | + |
| 81 | + tableARN := (*string)(r.ko.Status.ACKResourceMetadata.ARN) |
| 82 | + if tableARN == nil || *tableARN == "" { |
| 83 | + return errors.New("table ARN is required to delete resource policy") |
| 84 | + } |
| 85 | + |
| 86 | + _, err = rm.sdkapi.DeleteResourcePolicy( |
| 87 | + ctx, |
| 88 | + &svcsdk.DeleteResourcePolicyInput{ |
| 89 | + ResourceArn: tableARN, |
| 90 | + }, |
| 91 | + ) |
| 92 | + |
| 93 | + if err != nil { |
| 94 | + var policyNotFoundErr *svcsdktypes.PolicyNotFoundException |
| 95 | + if errors.As(err, &policyNotFoundErr) { |
| 96 | + // Policy already doesn't exist, this is a success case |
| 97 | + return nil |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + rm.metrics.RecordAPICall("DELETE", "DeleteResourcePolicy", err) |
| 102 | + return err |
| 103 | +} |
| 104 | + |
| 105 | +// getResourcePolicyWithContext retrieves the resource-based policy of a DynamoDB table. |
| 106 | +func (rm *resourceManager) getResourcePolicyWithContext( |
| 107 | + ctx context.Context, |
| 108 | + tableARN *string, |
| 109 | +) (*string, error) { |
| 110 | + var err error |
| 111 | + rlog := ackrtlog.FromContext(ctx) |
| 112 | + exit := rlog.Trace("rm.getResourcePolicyWithContext") |
| 113 | + defer func(err error) { exit(err) }(err) |
| 114 | + |
| 115 | + if tableARN == nil || *tableARN == "" { |
| 116 | + return nil, errors.New("table ARN is required to get resource policy") |
| 117 | + } |
| 118 | + |
| 119 | + res, err := rm.sdkapi.GetResourcePolicy( |
| 120 | + ctx, |
| 121 | + &svcsdk.GetResourcePolicyInput{ |
| 122 | + ResourceArn: tableARN, |
| 123 | + }, |
| 124 | + ) |
| 125 | + |
| 126 | + if err != nil { |
| 127 | + if awsErr, ok := ackerr.AWSError(err); ok && awsErr.ErrorCode() == "PolicyNotFoundException" { |
| 128 | + return nil, nil |
| 129 | + } |
| 130 | + rm.metrics.RecordAPICall("GET", "GetResourcePolicy", err) |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + |
| 134 | + rm.metrics.RecordAPICall("GET", "GetResourcePolicy", nil) |
| 135 | + return res.Policy, nil |
| 136 | +} |
0 commit comments