|
| 1 | +package lb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/annotations" |
| 10 | + |
| 11 | + "github.com/aws/aws-sdk-go/service/shield" |
| 12 | + "github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/albctx" |
| 13 | + "github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws" |
| 14 | + "github.com/pkg/errors" |
| 15 | + extensions "k8s.io/api/extensions/v1beta1" |
| 16 | + |
| 17 | + "k8s.io/apimachinery/pkg/util/cache" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + protectionEnabledForLBCacheMaxSize = 1024 |
| 22 | + protectionEnabledForLBCacheTTL = 10 * time.Second |
| 23 | + shieldAvailableCacheMaxSize = 1 |
| 24 | + shieldAvailableCacheTTL = 10 * time.Second |
| 25 | + shieldAvailableCacheKey = "shieldAvailable" |
| 26 | + protectionName = "managed by aws-alb-ingress-controller" |
| 27 | +) |
| 28 | + |
| 29 | +// ShieldController provides functionality to manage ALB's Shield Advanced protection. |
| 30 | +type ShieldController interface { |
| 31 | + Reconcile(ctx context.Context, lbArn string, ingress *extensions.Ingress) error |
| 32 | +} |
| 33 | + |
| 34 | +func NewShieldController(cloud aws.CloudAPI) ShieldController { |
| 35 | + return &defaultShieldController{ |
| 36 | + cloud: cloud, |
| 37 | + protectionEnabledForLBCache: cache.NewLRUExpireCache(protectionEnabledForLBCacheMaxSize), |
| 38 | + shieldAvailableCache: cache.NewLRUExpireCache(shieldAvailableCacheMaxSize), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +type defaultShieldController struct { |
| 43 | + cloud aws.CloudAPI |
| 44 | + |
| 45 | + // cache that stores protection id for LoadBalancerARN. |
| 46 | + // The cache value is string, while "" represents no protection. |
| 47 | + protectionEnabledForLBCache *cache.LRUExpireCache |
| 48 | + // cache that stores shield availability for current account. |
| 49 | + // The cache value is string, while "" represents not available. |
| 50 | + shieldAvailableCache *cache.LRUExpireCache |
| 51 | +} |
| 52 | + |
| 53 | +func (c *defaultShieldController) Reconcile(ctx context.Context, lbArn string, ingress *extensions.Ingress) error { |
| 54 | + var enableProtection bool |
| 55 | + annotationPresent, err := annotations.LoadBoolAnnocation("shield-advanced-protection", &enableProtection, ingress.Annotations) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + if !annotationPresent { |
| 60 | + return nil |
| 61 | + } |
| 62 | + |
| 63 | + available, err := c.getCurrentShieldAvailability(ctx) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + if enableProtection && !available { |
| 69 | + return fmt.Errorf("unable to create shield advanced protection for loadBalancer %v, shield advanced subscription is not active", lbArn) |
| 70 | + } |
| 71 | + |
| 72 | + protection, err := c.getCurrentProtectionStatus(ctx, lbArn) |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("failed to get shield advanced protection status for loadBalancer %v due to %v", lbArn, err) |
| 75 | + } |
| 76 | + |
| 77 | + if protection == nil && enableProtection { |
| 78 | + _, err := c.cloud.CreateProtection(ctx, aws.String(lbArn), aws.String(protectionName)) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("failed to create shield advanced protection for loadBalancer %v due to %v", lbArn, err) |
| 81 | + } |
| 82 | + albctx.GetLogger(ctx).Infof("enabled shield advanced protection for %v", lbArn) |
| 83 | + } else if protection != nil && !enableProtection { |
| 84 | + if aws.StringValue(protection.Name) == protectionName { |
| 85 | + _, err := c.cloud.DeleteProtection(ctx, protection.Id) |
| 86 | + c.protectionEnabledForLBCache.Remove(lbArn) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("failed to delete shield advanced protection for loadBalancer %v due to %v", lbArn, err) |
| 89 | + } |
| 90 | + |
| 91 | + albctx.GetLogger(ctx).Infof("deleted shield advanced protection for %v", lbArn) |
| 92 | + } else { |
| 93 | + albctx.GetLogger(ctx).Warnf("unable to delete shield advanced protection for %v, the protection name does not match \"%v\"", lbArn, protectionName) |
| 94 | + } |
| 95 | + } |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func (c *defaultShieldController) getCurrentShieldAvailability(ctx context.Context) (bool, error) { |
| 100 | + cachedShieldAvailable, exists := c.shieldAvailableCache.Get(shieldAvailableCacheKey) |
| 101 | + if exists { |
| 102 | + available, err := strconv.ParseBool(cachedShieldAvailable.(string)) |
| 103 | + if err == nil { |
| 104 | + return available, nil |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + available, err := c.cloud.ShieldAvailable(ctx) |
| 109 | + if err != nil { |
| 110 | + return false, fmt.Errorf("failed to get shield advanced subscription state %v", err) |
| 111 | + } |
| 112 | + c.shieldAvailableCache.Add(shieldAvailableCacheKey, "true", shieldAvailableCacheTTL) |
| 113 | + return available, nil |
| 114 | +} |
| 115 | + |
| 116 | +func (c *defaultShieldController) getCurrentProtectionStatus(ctx context.Context, lbArn string) (*shield.Protection, error) { |
| 117 | + cachedProtection, exists := c.protectionEnabledForLBCache.Get(lbArn) |
| 118 | + if exists { |
| 119 | + return cachedProtection.(*shield.Protection), nil |
| 120 | + } |
| 121 | + |
| 122 | + protection, err := c.cloud.GetProtection(ctx, aws.String(lbArn)) |
| 123 | + if err != nil { |
| 124 | + return nil, errors.Wrapf(err, "failed to get protection status for load balancer %v", lbArn) |
| 125 | + } |
| 126 | + |
| 127 | + c.protectionEnabledForLBCache.Add(lbArn, protection, protectionEnabledForLBCacheTTL) |
| 128 | + return protection, nil |
| 129 | +} |
0 commit comments