Skip to content

Commit f1eeb3a

Browse files
chore: revert "force NLB security group" (#5551)
This reverts commit [ab6e1da](#5471) By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the Apache 2.0 License.
1 parent 2f18918 commit f1eeb3a

19 files changed

Lines changed: 563 additions & 147 deletions

File tree

internal/pkg/cli/deploy/lbws.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package deploy
55

66
import (
77
"fmt"
8-
98
"github.com/aws/copilot-cli/internal/pkg/aws/elbv2"
109

1110
"github.com/aws/aws-sdk-go/aws"
@@ -15,6 +14,7 @@ import (
1514
awsecs "github.com/aws/copilot-cli/internal/pkg/aws/ecs"
1615
"github.com/aws/copilot-cli/internal/pkg/aws/partitions"
1716
"github.com/aws/copilot-cli/internal/pkg/config"
17+
"github.com/aws/copilot-cli/internal/pkg/deploy"
1818
"github.com/aws/copilot-cli/internal/pkg/deploy/cloudformation"
1919
"github.com/aws/copilot-cli/internal/pkg/deploy/cloudformation/stack"
2020
"github.com/aws/copilot-cli/internal/pkg/deploy/upload/customresource"
@@ -43,15 +43,21 @@ var (
4343
color.HighlightCode("copilot app init --domain example.com"))
4444
)
4545

46+
// TODO(Aiden): remove when NetworkLoadBalancer is forcibly updated
47+
type publicCIDRBlocksGetter interface {
48+
PublicCIDRBlocks() ([]string, error)
49+
}
50+
4651
type elbGetter interface {
4752
LoadBalancer(nameOrARN string) (*elbv2.LoadBalancer, error)
4853
}
4954

5055
type lbWebSvcDeployer struct {
5156
*svcDeployer
52-
appVersionGetter versionGetter
53-
elbGetter elbGetter
54-
lbMft *manifest.LoadBalancedWebService
57+
appVersionGetter versionGetter
58+
publicCIDRBlocksGetter publicCIDRBlocksGetter
59+
elbGetter elbGetter
60+
lbMft *manifest.LoadBalancedWebService
5561

5662
// Overriden in tests.
5763
newAliasCertValidator func(optionalRegion *string) aliasCertValidator
@@ -70,6 +76,18 @@ func NewLBWSDeployer(in *WorkloadDeployerInput) (*lbWebSvcDeployer, error) {
7076
return nil, fmt.Errorf("new app describer for application %s: %w", in.App.Name, err)
7177
}
7278

79+
// TODO(Aiden): remove when NetworkLoadBalancer is forcibly updated
80+
deployStore, err := deploy.NewStore(in.SessionProvider, svcDeployer.store)
81+
if err != nil {
82+
return nil, fmt.Errorf("new deploy store: %w", err)
83+
}
84+
envDescriber, err := describe.NewEnvDescriber(describe.NewEnvDescriberConfig{
85+
App: in.App.Name,
86+
Env: in.Env.Name,
87+
ConfigStore: svcDeployer.store,
88+
DeployStore: deployStore,
89+
})
90+
7391
if err != nil {
7492
return nil, fmt.Errorf("create describer for environment %s in application %s: %w", in.Env.Name, in.App.Name, err)
7593
}
@@ -78,10 +96,11 @@ func NewLBWSDeployer(in *WorkloadDeployerInput) (*lbWebSvcDeployer, error) {
7896
return nil, fmt.Errorf("manifest is not of type %s", manifestinfo.LoadBalancedWebServiceType)
7997
}
8098
return &lbWebSvcDeployer{
81-
svcDeployer: svcDeployer,
82-
appVersionGetter: versionGetter,
83-
elbGetter: elbv2.New(svcDeployer.envSess),
84-
lbMft: lbMft,
99+
svcDeployer: svcDeployer,
100+
appVersionGetter: versionGetter,
101+
publicCIDRBlocksGetter: envDescriber,
102+
elbGetter: elbv2.New(svcDeployer.envSess),
103+
lbMft: lbMft,
85104
newAliasCertValidator: func(optionalRegion *string) aliasCertValidator {
86105
sess := svcDeployer.envSess.Copy(&aws.Config{
87106
Region: optionalRegion,
@@ -143,6 +162,13 @@ func (d *lbWebSvcDeployer) stackConfiguration(in *StackRuntimeConfiguration) (*s
143162
return nil, err
144163
}
145164
var opts []stack.LoadBalancedWebServiceOption
165+
if !d.lbMft.NLBConfig.IsEmpty() {
166+
cidrBlocks, err := d.publicCIDRBlocksGetter.PublicCIDRBlocks()
167+
if err != nil {
168+
return nil, fmt.Errorf("get public CIDR blocks information from the VPC of environment %s: %w", d.env.Name, err)
169+
}
170+
opts = append(opts, stack.WithNLB(cidrBlocks))
171+
}
146172
if d.lbMft.HTTPOrBool.ImportedALB != nil {
147173
lb, err := d.elbGetter.LoadBalancer(aws.StringValue(d.lbMft.HTTPOrBool.ImportedALB))
148174
if err != nil {

internal/pkg/cli/deploy/mocks/mock_lbws.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/pkg/cli/deploy/workload_test.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type deployMocks struct {
5050
mockRepositoryService *mocks.MockrepositoryService
5151
mockEndpointGetter *mocks.MockendpointGetter
5252
mockSpinner *mocks.Mockspinner
53+
mockPublicCIDRBlocksGetter *mocks.MockpublicCIDRBlocksGetter
5354
mockSNSTopicsLister *mocks.MocksnsTopicsLister
5455
mockServiceDeployer *mocks.MockserviceDeployer
5556
mockServiceForceUpdater *mocks.MockserviceForceUpdater
@@ -921,6 +922,26 @@ func TestWorkloadDeployer_DeployWorkload(t *testing.T) {
921922
},
922923
wantErr: fmt.Errorf("validate ALB runtime configuration for \"http\": validate aliases against the imported CDN certificate for env mockEnv: some error"),
923924
},
925+
"fail to get public CIDR blocks": {
926+
inNLB: manifest.NetworkLoadBalancerConfiguration{
927+
Listener: manifest.NetworkLoadBalancerListener{
928+
Port: aws.String("443/tcp"),
929+
},
930+
},
931+
inEnvironment: &config.Environment{
932+
Name: mockEnvName,
933+
Region: "us-west-2",
934+
},
935+
inApp: &config.Application{
936+
Name: mockAppName,
937+
},
938+
mock: func(m *deployMocks) {
939+
m.mockEndpointGetter.EXPECT().ServiceDiscoveryEndpoint().Return("mockApp.local", nil)
940+
m.mockEnvVersionGetter.EXPECT().Version().Return("v1.42.0", nil)
941+
m.mockPublicCIDRBlocksGetter.EXPECT().PublicCIDRBlocks().Return(nil, errors.New("some error"))
942+
},
943+
wantErr: fmt.Errorf("get public CIDR blocks information from the VPC of environment mockEnv: some error"),
944+
},
924945
"alias used while app is not associated with a domain": {
925946
inAliases: manifest.Alias{AdvancedAliases: mockAlias},
926947
inEnvironment: &config.Environment{
@@ -1340,13 +1361,14 @@ func TestWorkloadDeployer_DeployWorkload(t *testing.T) {
13401361
defer ctrl.Finish()
13411362

13421363
m := &deployMocks{
1343-
mockAppVersionGetter: mocks.NewMockversionGetter(ctrl),
1344-
mockEnvVersionGetter: mocks.NewMockversionGetter(ctrl),
1345-
mockEndpointGetter: mocks.NewMockendpointGetter(ctrl),
1346-
mockServiceDeployer: mocks.NewMockserviceDeployer(ctrl),
1347-
mockServiceForceUpdater: mocks.NewMockserviceForceUpdater(ctrl),
1348-
mockSpinner: mocks.NewMockspinner(ctrl),
1349-
mockValidator: mocks.NewMockaliasCertValidator(ctrl),
1364+
mockAppVersionGetter: mocks.NewMockversionGetter(ctrl),
1365+
mockEnvVersionGetter: mocks.NewMockversionGetter(ctrl),
1366+
mockEndpointGetter: mocks.NewMockendpointGetter(ctrl),
1367+
mockServiceDeployer: mocks.NewMockserviceDeployer(ctrl),
1368+
mockServiceForceUpdater: mocks.NewMockserviceForceUpdater(ctrl),
1369+
mockPublicCIDRBlocksGetter: mocks.NewMockpublicCIDRBlocksGetter(ctrl),
1370+
mockSpinner: mocks.NewMockspinner(ctrl),
1371+
mockValidator: mocks.NewMockaliasCertValidator(ctrl),
13501372
}
13511373
tc.mock(m)
13521374

@@ -1376,7 +1398,8 @@ func TestWorkloadDeployer_DeployWorkload(t *testing.T) {
13761398
return mockNowTime
13771399
},
13781400
},
1379-
appVersionGetter: m.mockAppVersionGetter,
1401+
appVersionGetter: m.mockAppVersionGetter,
1402+
publicCIDRBlocksGetter: m.mockPublicCIDRBlocksGetter,
13801403
newAliasCertValidator: func(region *string) aliasCertValidator {
13811404
return m.mockValidator
13821405
},

internal/pkg/cli/interfaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ type statusDescriber interface {
472472

473473
type envDescriber interface {
474474
Describe() (*describe.EnvDescription, error)
475+
PublicCIDRBlocks() ([]string, error)
475476
Manifest() ([]byte, error)
476477
ValidateCFServiceDomainAliases() error
477478
}

internal/pkg/cli/mocks/mock_interfaces.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/pkg/cli/svc_deploy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,14 @@ After fixing the deployment, you can:
350350
func (o *deploySvcOpts) RecommendActions() error {
351351
if lbMft, ok := o.appliedDynamicMft.Manifest().(*manifest.LoadBalancedWebService); ok {
352352
if !lbMft.NLBConfig.IsEmpty() {
353-
log.Warning("With v1.33.0, Copilot applies a security group to your network load balancer. ",
353+
log.Warning("Starting with v1.33.0, Copilot will start applying a security group to your network load balancer. ",
354354
"This allows more fine-grained intra-VPC access control: ",
355355
"your service won't need to allow-list the CIDR blocks of the public subnets where the NLB is deployed; ",
356356
"it only needs to allow-list the NLB, specifically.\n",
357357
"\n",
358358
"NLB security group onboarding implies resource recreation, ",
359359
"because a security group can't be added to an existing NLB that does not already have one. ",
360-
"Therefore, you may see some resource recreation related to your NLB. ",
360+
"Therefore, after v1.33.0, you might see some resource recreation related to your NLB. ",
361361
"This means:\n",
362362
"1. If you don't use DNS aliases, then the NLB's domain name will change.\n",
363363
"2. If you use DNS aliases, then the aliases will start pointing to the new NLB that is enhanced with a security group.\n",

internal/pkg/deploy/cloudformation/stack/lb_network_web_service_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func TestNetworkLoadBalancedWebService_Template(t *testing.T) {
115115
Version: "v1.29.0",
116116
},
117117
RootUserARN: "arn:aws:iam::123456789123:root",
118-
})
118+
}, stack.WithNLB([]string{"10.0.0.0/24", "10.1.0.0/24"}))
119119
tpl, err := serializer.Template()
120120
require.NoError(t, err, "template should render")
121121
regExpGUID := regexp.MustCompile(`([a-f\d]{8}-)([a-f\d]{4}-){3}([a-f\d]{12})`) // Matches random guids

internal/pkg/deploy/cloudformation/stack/lb_web_svc.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ package stack
55

66
import (
77
"fmt"
8+
"github.com/aws/copilot-cli/internal/pkg/aws/elbv2"
89
"strconv"
910
"strings"
1011

11-
"github.com/aws/copilot-cli/internal/pkg/aws/elbv2"
12-
1312
"github.com/aws/aws-sdk-go/aws"
1413
"github.com/aws/aws-sdk-go/service/cloudformation"
1514
"github.com/aws/copilot-cli/internal/pkg/config"
@@ -31,18 +30,27 @@ const (
3130
// LoadBalancedWebService represents the configuration needed to create a CloudFormation stack from a load balanced web service manifest.
3231
type LoadBalancedWebService struct {
3332
*ecsWkld
34-
manifest *manifest.LoadBalancedWebService
35-
httpsEnabled bool
36-
dnsDelegationEnabled bool
37-
importedALB *elbv2.LoadBalancer
38-
appInfo deploy.AppInformation
33+
manifest *manifest.LoadBalancedWebService
34+
httpsEnabled bool
35+
dnsDelegationEnabled bool
36+
publicSubnetCIDRBlocks []string
37+
importedALB *elbv2.LoadBalancer
38+
appInfo deploy.AppInformation
3939

4040
parser loadBalancedWebSvcReadParser
4141
}
4242

4343
// LoadBalancedWebServiceOption is used to configuring an optional field for LoadBalancedWebService.
4444
type LoadBalancedWebServiceOption func(s *LoadBalancedWebService)
4545

46+
// WithNLB enables Network Load Balancer in a LoadBalancedWebService.
47+
// TODO(Aiden): remove when NetworkLoadBalancer is forcibly updated
48+
func WithNLB(cidrBlocks []string) func(s *LoadBalancedWebService) {
49+
return func(s *LoadBalancedWebService) {
50+
s.publicSubnetCIDRBlocks = cidrBlocks
51+
}
52+
}
53+
4654
// WithImportedALB specifies an imported load balancer.
4755
func WithImportedALB(alb *elbv2.LoadBalancer) func(s *LoadBalancedWebService) {
4856
return func(s *LoadBalancedWebService) {

0 commit comments

Comments
 (0)