|
| 1 | +package ec2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + awssdk "github.com/aws/aws-sdk-go/aws" |
| 7 | + ec2sdk "github.com/aws/aws-sdk-go/service/ec2" |
| 8 | + "github.com/go-logr/logr" |
| 9 | + "k8s.io/apimachinery/pkg/util/sets" |
| 10 | + "sigs.k8s.io/aws-alb-ingress-controller/pkg/algorithm" |
| 11 | + "sigs.k8s.io/aws-alb-ingress-controller/pkg/aws/services" |
| 12 | + "sigs.k8s.io/aws-alb-ingress-controller/pkg/deploy/tagging" |
| 13 | + ec2model "sigs.k8s.io/aws-alb-ingress-controller/pkg/model/ec2" |
| 14 | + "sigs.k8s.io/aws-alb-ingress-controller/pkg/networking" |
| 15 | +) |
| 16 | + |
| 17 | +// SecurityGroupManager is responsible for create/update/delete SecurityGroup resources. |
| 18 | +type SecurityGroupManager interface { |
| 19 | + Create(ctx context.Context, resSG *ec2model.SecurityGroup) (ec2model.SecurityGroupStatus, error) |
| 20 | + |
| 21 | + Update(ctx context.Context, resSG *ec2model.SecurityGroup, sdkSG networking.SecurityGroupInfo) (ec2model.SecurityGroupStatus, error) |
| 22 | + |
| 23 | + Delete(ctx context.Context, sdkSG networking.SecurityGroupInfo) error |
| 24 | +} |
| 25 | + |
| 26 | +// NewDefaultSecurityGroupManager constructs new defaultSecurityGroupManager. |
| 27 | +func NewDefaultSecurityGroupManager(ec2Client services.EC2, taggingProvider tagging.Provider, networkingSGReconciler networking.SecurityGroupReconciler, vpcID string, logger logr.Logger) *defaultSecurityGroupManager { |
| 28 | + return &defaultSecurityGroupManager{ |
| 29 | + ec2Client: ec2Client, |
| 30 | + taggingProvider: taggingProvider, |
| 31 | + networkingSGReconciler: networkingSGReconciler, |
| 32 | + vpcID: vpcID, |
| 33 | + logger: logger, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// default implementation for SecurityGroupManager. |
| 38 | +type defaultSecurityGroupManager struct { |
| 39 | + ec2Client services.EC2 |
| 40 | + taggingProvider tagging.Provider |
| 41 | + networkingSGReconciler networking.SecurityGroupReconciler |
| 42 | + vpcID string |
| 43 | + logger logr.Logger |
| 44 | +} |
| 45 | + |
| 46 | +func (m *defaultSecurityGroupManager) Create(ctx context.Context, resSG *ec2model.SecurityGroup) (ec2model.SecurityGroupStatus, error) { |
| 47 | + sgTags := m.taggingProvider.ResourceTags(resSG.Stack(), resSG, resSG.Spec.Tags) |
| 48 | + sdkTags := convertTagsToSDKTags(sgTags) |
| 49 | + permissionInfos, err := buildIPPermissionInfos(resSG.Spec.Ingress) |
| 50 | + if err != nil { |
| 51 | + return ec2model.SecurityGroupStatus{}, err |
| 52 | + } |
| 53 | + |
| 54 | + req := &ec2sdk.CreateSecurityGroupInput{ |
| 55 | + VpcId: awssdk.String(m.vpcID), |
| 56 | + GroupName: awssdk.String(resSG.Spec.GroupName), |
| 57 | + Description: awssdk.String(resSG.Spec.Description), |
| 58 | + TagSpecifications: []*ec2sdk.TagSpecification{ |
| 59 | + { |
| 60 | + ResourceType: awssdk.String("security-group"), |
| 61 | + Tags: sdkTags, |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | + m.logger.Info("creating securityGroup", |
| 66 | + "resourceID", resSG.ID()) |
| 67 | + resp, err := m.ec2Client.CreateSecurityGroupWithContext(ctx, req) |
| 68 | + if err != nil { |
| 69 | + return ec2model.SecurityGroupStatus{}, err |
| 70 | + } |
| 71 | + sgID := awssdk.StringValue(resp.GroupId) |
| 72 | + m.logger.Info("created securityGroup", |
| 73 | + "resourceID", resSG.ID(), |
| 74 | + "securityGroupID", sgID) |
| 75 | + |
| 76 | + if err := m.networkingSGReconciler.ReconcileIngress(ctx, sgID, permissionInfos); err != nil { |
| 77 | + return ec2model.SecurityGroupStatus{}, err |
| 78 | + } |
| 79 | + |
| 80 | + return ec2model.SecurityGroupStatus{ |
| 81 | + GroupID: sgID, |
| 82 | + }, nil |
| 83 | +} |
| 84 | + |
| 85 | +func (m *defaultSecurityGroupManager) Update(ctx context.Context, resSG *ec2model.SecurityGroup, sdkSG networking.SecurityGroupInfo) (ec2model.SecurityGroupStatus, error) { |
| 86 | + permissionInfos, err := buildIPPermissionInfos(resSG.Spec.Ingress) |
| 87 | + if err != nil { |
| 88 | + return ec2model.SecurityGroupStatus{}, err |
| 89 | + } |
| 90 | + if err := m.updateSDKSecurityGroupGroupWithTags(ctx, resSG, sdkSG); err != nil { |
| 91 | + return ec2model.SecurityGroupStatus{}, err |
| 92 | + } |
| 93 | + if err := m.networkingSGReconciler.ReconcileIngress(ctx, sdkSG.SecurityGroupID, permissionInfos); err != nil { |
| 94 | + return ec2model.SecurityGroupStatus{}, err |
| 95 | + } |
| 96 | + return ec2model.SecurityGroupStatus{ |
| 97 | + GroupID: sdkSG.SecurityGroupID, |
| 98 | + }, nil |
| 99 | +} |
| 100 | + |
| 101 | +func (m *defaultSecurityGroupManager) Delete(ctx context.Context, sdkSG networking.SecurityGroupInfo) error { |
| 102 | + req := &ec2sdk.DeleteSecurityGroupInput{ |
| 103 | + GroupId: awssdk.String(sdkSG.SecurityGroupID), |
| 104 | + } |
| 105 | + m.logger.Info("deleting securityGroup", |
| 106 | + "securityGroupID", sdkSG.SecurityGroupID) |
| 107 | + if _, err := m.ec2Client.DeleteSecurityGroupWithContext(ctx, req); err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + m.logger.Info("deleted securityGroup", |
| 111 | + "securityGroupID", sdkSG.SecurityGroupID) |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func (m *defaultSecurityGroupManager) updateSDKSecurityGroupGroupWithTags(ctx context.Context, resSG *ec2model.SecurityGroup, sdkSG networking.SecurityGroupInfo) error { |
| 116 | + desiredTags := m.taggingProvider.ResourceTags(resSG.Stack(), resSG, resSG.Spec.Tags) |
| 117 | + tagsToUpdate, tagsToRemove := algorithm.DiffStringMap(desiredTags, sdkSG.Tags) |
| 118 | + if len(tagsToUpdate) > 0 { |
| 119 | + req := &ec2sdk.CreateTagsInput{ |
| 120 | + Resources: []*string{awssdk.String(sdkSG.SecurityGroupID)}, |
| 121 | + Tags: convertTagsToSDKTags(tagsToUpdate), |
| 122 | + } |
| 123 | + |
| 124 | + m.logger.Info("adding securityGroup tags", |
| 125 | + "securityGroupID", sdkSG.SecurityGroupID, |
| 126 | + "change", tagsToUpdate) |
| 127 | + if _, err := m.ec2Client.CreateTagsWithContext(ctx, req); err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + m.logger.Info("added securityGroup tags", |
| 131 | + "securityGroupID", sdkSG.SecurityGroupID) |
| 132 | + } |
| 133 | + |
| 134 | + if len(tagsToRemove) > 0 { |
| 135 | + req := &ec2sdk.DeleteTagsInput{ |
| 136 | + Resources: []*string{awssdk.String(sdkSG.SecurityGroupID)}, |
| 137 | + Tags: convertTagsToSDKTags(tagsToRemove), |
| 138 | + } |
| 139 | + |
| 140 | + m.logger.Info("removing securityGroup tags", |
| 141 | + "securityGroupID", sdkSG.SecurityGroupID, |
| 142 | + "change", tagsToRemove) |
| 143 | + if _, err := m.ec2Client.DeleteTagsWithContext(ctx, req); err != nil { |
| 144 | + return err |
| 145 | + } |
| 146 | + m.logger.Info("removed securityGroup tags", |
| 147 | + "securityGroupID", sdkSG.SecurityGroupID) |
| 148 | + } |
| 149 | + return nil |
| 150 | +} |
| 151 | + |
| 152 | +func buildIPPermissionInfos(permissions []ec2model.IPPermission) ([]networking.IPPermissionInfo, error) { |
| 153 | + permissionInfos := make([]networking.IPPermissionInfo, 0, len(permissions)) |
| 154 | + for _, permission := range permissions { |
| 155 | + permissionInfo, err := buildIPPermissionInfo(permission) |
| 156 | + if err != nil { |
| 157 | + return nil, err |
| 158 | + } |
| 159 | + permissionInfos = append(permissionInfos, permissionInfo) |
| 160 | + } |
| 161 | + return permissionInfos, nil |
| 162 | +} |
| 163 | + |
| 164 | +func buildIPPermissionInfo(permission ec2model.IPPermission) (networking.IPPermissionInfo, error) { |
| 165 | + protocol := permission.IPProtocol |
| 166 | + if len(permission.IPRanges) == 1 { |
| 167 | + labels := networking.NewIPPermissionLabelsForRawDescription(permission.IPRanges[0].Description) |
| 168 | + return networking.NewCIDRIPPermission(protocol, permission.FromPort, permission.ToPort, permission.IPRanges[0].CIDRIP, labels), nil |
| 169 | + } |
| 170 | + if len(permission.IPV6Range) == 1 { |
| 171 | + labels := networking.NewIPPermissionLabelsForRawDescription(permission.IPV6Range[0].Description) |
| 172 | + return networking.NewCIDRIPPermission(protocol, permission.FromPort, permission.ToPort, permission.IPV6Range[0].CIDRIPv6, labels), nil |
| 173 | + } |
| 174 | + if len(permission.UserIDGroupPairs) == 1 { |
| 175 | + labels := networking.NewIPPermissionLabelsForRawDescription(permission.UserIDGroupPairs[0].Description) |
| 176 | + return networking.NewGroupIDIPPermission(protocol, permission.FromPort, permission.ToPort, permission.UserIDGroupPairs[0].GroupID, labels), nil |
| 177 | + } |
| 178 | + return networking.IPPermissionInfo{}, errors.New("invalid ipPermission") |
| 179 | +} |
| 180 | + |
| 181 | +// convert tags into AWS SDK tag presentation. |
| 182 | +func convertTagsToSDKTags(tags map[string]string) []*ec2sdk.Tag { |
| 183 | + if len(tags) == 0 { |
| 184 | + return nil |
| 185 | + } |
| 186 | + sdkTags := make([]*ec2sdk.Tag, 0, len(tags)) |
| 187 | + |
| 188 | + for _, key := range sets.StringKeySet(tags).List() { |
| 189 | + sdkTags = append(sdkTags, &ec2sdk.Tag{ |
| 190 | + Key: awssdk.String(key), |
| 191 | + Value: awssdk.String(tags[key]), |
| 192 | + }) |
| 193 | + } |
| 194 | + return sdkTags |
| 195 | +} |
0 commit comments