From 11e4a74762c37d883975830601628eb1e6fc6526 Mon Sep 17 00:00:00 2001 From: Sarah Alsmiller Date: Tue, 12 Mar 2024 14:53:29 -0500 Subject: [PATCH] squash --- agent/catalog_endpoint_test.go | 43 +++++---- agent/consul/catalog_endpoint_test.go | 27 +++--- agent/consul/internal_endpoint_test.go | 31 +++--- agent/consul/state/catalog_test.go | 123 ++++++++++++------------ agent/consul/state/config_entry_test.go | 3 + agent/structs/config_entry_gateways.go | 17 ++-- 6 files changed, 126 insertions(+), 118 deletions(-) diff --git a/agent/catalog_endpoint_test.go b/agent/catalog_endpoint_test.go index 10b1c8b887..5a5b2433d5 100644 --- a/agent/catalog_endpoint_test.go +++ b/agent/catalog_endpoint_test.go @@ -1938,11 +1938,12 @@ func TestCatalog_GatewayServices_Terminating(t *testing.T) { SNI: "my-domain", }, { - Name: "*", - CAFile: "ca.crt", - CertFile: "client.crt", - KeyFile: "client.key", - SNI: "my-alt-domain", + Name: "*", + CAFile: "ca.crt", + CertFile: "client.crt", + KeyFile: "client.key", + SNI: "my-alt-domain", + DisableAutoHostRewrite: true, }, }, }, @@ -1965,23 +1966,25 @@ func TestCatalog_GatewayServices_Terminating(t *testing.T) { expect := structs.GatewayServices{ { - Service: structs.NewServiceName("api", nil), - Gateway: structs.NewServiceName("terminating", nil), - GatewayKind: structs.ServiceKindTerminatingGateway, - CAFile: "api/ca.crt", - CertFile: "api/client.crt", - KeyFile: "api/client.key", - SNI: "my-domain", + Service: structs.NewServiceName("api", nil), + Gateway: structs.NewServiceName("terminating", nil), + GatewayKind: structs.ServiceKindTerminatingGateway, + CAFile: "api/ca.crt", + CertFile: "api/client.crt", + KeyFile: "api/client.key", + SNI: "my-domain", + AutoHostRewrite: true, }, { - Service: structs.NewServiceName("redis", nil), - Gateway: structs.NewServiceName("terminating", nil), - GatewayKind: structs.ServiceKindTerminatingGateway, - CAFile: "ca.crt", - CertFile: "client.crt", - KeyFile: "client.key", - SNI: "my-alt-domain", - FromWildcard: true, + Service: structs.NewServiceName("redis", nil), + Gateway: structs.NewServiceName("terminating", nil), + GatewayKind: structs.ServiceKindTerminatingGateway, + CAFile: "ca.crt", + CertFile: "client.crt", + KeyFile: "client.key", + SNI: "my-alt-domain", + FromWildcard: true, + AutoHostRewrite: false, }, } diff --git a/agent/consul/catalog_endpoint_test.go b/agent/consul/catalog_endpoint_test.go index 628ad83ae4..8c7b9827aa 100644 --- a/agent/consul/catalog_endpoint_test.go +++ b/agent/consul/catalog_endpoint_test.go @@ -3345,10 +3345,11 @@ func TestCatalog_GatewayServices_BothGateways(t *testing.T) { expect := structs.GatewayServices{ { - Service: structs.NewServiceName("api", nil), - Gateway: structs.NewServiceName("gateway", nil), - GatewayKind: structs.ServiceKindTerminatingGateway, - ServiceKind: structs.GatewayServiceKindService, + Service: structs.NewServiceName("api", nil), + Gateway: structs.NewServiceName("gateway", nil), + GatewayKind: structs.ServiceKindTerminatingGateway, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, } @@ -3568,16 +3569,18 @@ service "gateway" { expect := structs.GatewayServices{ { - Service: structs.NewServiceName("db", nil), - Gateway: structs.NewServiceName("gateway", nil), - GatewayKind: structs.ServiceKindTerminatingGateway, - ServiceKind: structs.GatewayServiceKindService, + Service: structs.NewServiceName("db", nil), + Gateway: structs.NewServiceName("gateway", nil), + GatewayKind: structs.ServiceKindTerminatingGateway, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, { - Service: structs.NewServiceName("db_replica", nil), - Gateway: structs.NewServiceName("gateway", nil), - GatewayKind: structs.ServiceKindTerminatingGateway, - ServiceKind: structs.GatewayServiceKindUnknown, + Service: structs.NewServiceName("db_replica", nil), + Gateway: structs.NewServiceName("gateway", nil), + GatewayKind: structs.ServiceKindTerminatingGateway, + ServiceKind: structs.GatewayServiceKindUnknown, + AutoHostRewrite: true, }, } diff --git a/agent/consul/internal_endpoint_test.go b/agent/consul/internal_endpoint_test.go index a7f1328101..e4b9a14b70 100644 --- a/agent/consul/internal_endpoint_test.go +++ b/agent/consul/internal_endpoint_test.go @@ -1217,10 +1217,11 @@ func TestInternal_GatewayServiceDump_Terminating(t *testing.T) { }, }, GatewayService: &structs.GatewayService{ - Gateway: structs.NewServiceName("terminating-gateway", nil), - Service: structs.NewServiceName("db", nil), - GatewayKind: "terminating-gateway", - ServiceKind: structs.GatewayServiceKindService, + Gateway: structs.NewServiceName("terminating-gateway", nil), + Service: structs.NewServiceName("db", nil), + GatewayKind: "terminating-gateway", + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, }, { @@ -1251,21 +1252,23 @@ func TestInternal_GatewayServiceDump_Terminating(t *testing.T) { }, }, GatewayService: &structs.GatewayService{ - Gateway: structs.NewServiceName("terminating-gateway", nil), - Service: structs.NewServiceName("db", nil), - GatewayKind: "terminating-gateway", - ServiceKind: structs.GatewayServiceKindService, + Gateway: structs.NewServiceName("terminating-gateway", nil), + Service: structs.NewServiceName("db", nil), + GatewayKind: "terminating-gateway", + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, }, { // Only GatewayService should be returned when linked service isn't registered GatewayService: &structs.GatewayService{ - Gateway: structs.NewServiceName("terminating-gateway", nil), - Service: structs.NewServiceName("redis", nil), - GatewayKind: "terminating-gateway", - CAFile: "/etc/certs/ca.pem", - CertFile: "/etc/certs/cert.pem", - KeyFile: "/etc/certs/key.pem", + Gateway: structs.NewServiceName("terminating-gateway", nil), + Service: structs.NewServiceName("redis", nil), + GatewayKind: "terminating-gateway", + CAFile: "/etc/certs/ca.pem", + CertFile: "/etc/certs/cert.pem", + KeyFile: "/etc/certs/key.pem", + AutoHostRewrite: true, }, }, } diff --git a/agent/consul/state/catalog_test.go b/agent/consul/state/catalog_test.go index f85844909e..cef608bc1c 100644 --- a/agent/consul/state/catalog_test.go +++ b/agent/consul/state/catalog_test.go @@ -5197,7 +5197,8 @@ func TestStateStore_GatewayServices_Terminating(t *testing.T) { CreateIndex: 21, ModifyIndex: 21, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, { Service: structs.NewServiceName("db", nil), @@ -5207,7 +5208,8 @@ func TestStateStore_GatewayServices_Terminating(t *testing.T) { CreateIndex: 21, ModifyIndex: 21, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, } assert.Equal(t, expect, out) @@ -5241,7 +5243,8 @@ func TestStateStore_GatewayServices_Terminating(t *testing.T) { CreateIndex: 21, ModifyIndex: 21, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, { Service: structs.NewServiceName("db", nil), @@ -5251,7 +5254,8 @@ func TestStateStore_GatewayServices_Terminating(t *testing.T) { CreateIndex: 21, ModifyIndex: 21, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, } assert.Equal(t, expect, out) @@ -5302,7 +5306,8 @@ func TestStateStore_GatewayServices_Terminating(t *testing.T) { CreateIndex: 22, ModifyIndex: 22, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, { Service: structs.NewServiceName("db", nil), @@ -5312,7 +5317,8 @@ func TestStateStore_GatewayServices_Terminating(t *testing.T) { CreateIndex: 22, ModifyIndex: 22, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, } assert.Equal(t, expect, out) @@ -5340,7 +5346,8 @@ func TestStateStore_GatewayServices_Terminating(t *testing.T) { CreateIndex: 22, ModifyIndex: 22, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, { Service: structs.NewServiceName("db", nil), @@ -5350,7 +5357,8 @@ func TestStateStore_GatewayServices_Terminating(t *testing.T) { CreateIndex: 22, ModifyIndex: 22, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, { Service: structs.NewServiceName("redis", nil), @@ -5365,7 +5373,8 @@ func TestStateStore_GatewayServices_Terminating(t *testing.T) { CreateIndex: 23, ModifyIndex: 23, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, } assert.Equal(t, expect, out) @@ -5393,7 +5402,8 @@ func TestStateStore_GatewayServices_Terminating(t *testing.T) { CreateIndex: 22, ModifyIndex: 22, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, { Service: structs.NewServiceName("db", nil), @@ -5403,7 +5413,8 @@ func TestStateStore_GatewayServices_Terminating(t *testing.T) { CreateIndex: 22, ModifyIndex: 22, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, } assert.Equal(t, expect, out) @@ -5435,7 +5446,8 @@ func TestStateStore_GatewayServices_Terminating(t *testing.T) { CreateIndex: 25, ModifyIndex: 25, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, } assert.Equal(t, expect, out) @@ -5467,6 +5479,7 @@ func TestStateStore_GatewayServices_Terminating(t *testing.T) { CreateIndex: 26, ModifyIndex: 26, }, + AutoHostRewrite: true, }, { Service: structs.NewServiceName("db", nil), @@ -5477,6 +5490,7 @@ func TestStateStore_GatewayServices_Terminating(t *testing.T) { CreateIndex: 26, ModifyIndex: 26, }, + AutoHostRewrite: true, }, } assert.Equal(t, expect, out) @@ -5504,6 +5518,7 @@ func TestStateStore_GatewayServices_Terminating(t *testing.T) { CreateIndex: 26, ModifyIndex: 26, }, + AutoHostRewrite: true, }, { Service: structs.NewServiceName("db", nil), @@ -5514,6 +5529,7 @@ func TestStateStore_GatewayServices_Terminating(t *testing.T) { CreateIndex: 26, ModifyIndex: 26, }, + AutoHostRewrite: true, }, { Service: structs.NewServiceName("destination1", nil), @@ -5525,6 +5541,7 @@ func TestStateStore_GatewayServices_Terminating(t *testing.T) { CreateIndex: 27, ModifyIndex: 27, }, + AutoHostRewrite: true, }, } assert.ElementsMatch(t, expectWildcardIncludesDest, out) @@ -6018,7 +6035,8 @@ func TestStateStore_GatewayServices_ServiceDeletion(t *testing.T) { CreateIndex: 19, ModifyIndex: 19, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, } assert.Equal(t, expect, out) @@ -6040,6 +6058,7 @@ func TestStateStore_GatewayServices_ServiceDeletion(t *testing.T) { CreateIndex: 20, ModifyIndex: 20, }, + AutoHostRewrite: true, }, { Service: structs.NewServiceName("db", nil), @@ -6050,6 +6069,7 @@ func TestStateStore_GatewayServices_ServiceDeletion(t *testing.T) { CreateIndex: 20, ModifyIndex: 20, }, + AutoHostRewrite: true, }, } assert.Equal(t, expect, out) @@ -6077,6 +6097,7 @@ func TestStateStore_GatewayServices_ServiceDeletion(t *testing.T) { CreateIndex: 19, ModifyIndex: 20, }, + AutoHostRewrite: true, }, } assert.Equal(t, expect, out) @@ -6097,6 +6118,7 @@ func TestStateStore_GatewayServices_ServiceDeletion(t *testing.T) { CreateIndex: 20, ModifyIndex: 20, }, + AutoHostRewrite: true, }, } assert.Equal(t, expect, out) @@ -7016,6 +7038,7 @@ func TestStateStore_DumpGatewayServices(t *testing.T) { // Read everything back. ws = memdb.NewWatchSet() idx, out, err := s.DumpGatewayServices(ws) + fmt.Println(out) assert.Nil(t, err) assert.Equal(t, idx, uint64(21)) assert.Len(t, out, 2) @@ -7033,7 +7056,8 @@ func TestStateStore_DumpGatewayServices(t *testing.T) { CreateIndex: 21, ModifyIndex: 21, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, { Service: structs.NewServiceName("db", nil), @@ -7043,7 +7067,8 @@ func TestStateStore_DumpGatewayServices(t *testing.T) { CreateIndex: 21, ModifyIndex: 21, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, } assert.Equal(t, expect, out) @@ -7077,6 +7102,7 @@ func TestStateStore_DumpGatewayServices(t *testing.T) { assert.False(t, watchFired(ws)) idx, out, err := s.DumpGatewayServices(ws) + fmt.Println(out) assert.Nil(t, err) assert.Equal(t, idx, uint64(21)) assert.Len(t, out, 2) @@ -7094,7 +7120,8 @@ func TestStateStore_DumpGatewayServices(t *testing.T) { CreateIndex: 21, ModifyIndex: 21, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, { Service: structs.NewServiceName("db", nil), @@ -7104,7 +7131,8 @@ func TestStateStore_DumpGatewayServices(t *testing.T) { CreateIndex: 21, ModifyIndex: 21, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, } assert.Equal(t, expect, out) @@ -7134,7 +7162,8 @@ func TestStateStore_DumpGatewayServices(t *testing.T) { CreateIndex: 21, ModifyIndex: 21, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, { Service: structs.NewServiceName("db", nil), @@ -7144,7 +7173,8 @@ func TestStateStore_DumpGatewayServices(t *testing.T) { CreateIndex: 21, ModifyIndex: 21, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, { Service: structs.NewServiceName("redis", nil), @@ -7159,7 +7189,8 @@ func TestStateStore_DumpGatewayServices(t *testing.T) { CreateIndex: 22, ModifyIndex: 22, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, } assert.Equal(t, expect, out) @@ -7189,7 +7220,8 @@ func TestStateStore_DumpGatewayServices(t *testing.T) { CreateIndex: 21, ModifyIndex: 21, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, { Service: structs.NewServiceName("db", nil), @@ -7199,7 +7231,8 @@ func TestStateStore_DumpGatewayServices(t *testing.T) { CreateIndex: 21, ModifyIndex: 21, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, } assert.Equal(t, expect, out) @@ -7233,7 +7266,8 @@ func TestStateStore_DumpGatewayServices(t *testing.T) { CreateIndex: 24, ModifyIndex: 24, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, } assert.Equal(t, expect, out) @@ -7291,7 +7325,8 @@ func TestStateStore_DumpGatewayServices(t *testing.T) { CreateIndex: 24, ModifyIndex: 24, }, - ServiceKind: structs.GatewayServiceKindService, + ServiceKind: structs.GatewayServiceKindService, + AutoHostRewrite: true, }, { Service: structs.NewServiceName("api", nil), @@ -9089,43 +9124,3 @@ func assertDeepEqual(t *testing.T, x, y interface{}, opts ...cmp.Option) { t.Fatalf("assertion failed: values are not equal\n--- expected\n+++ actual\n%v", diff) } } - -func Test_terminatingConfigGatewayServices(t *testing.T) { - s := testConfigStateStore(t) - - cfg := &structs.TerminatingGatewayConfigEntry{ - Kind: structs.TerminatingGateway, - Name: "terminating-gateway", - Services: []structs.LinkedService{ - { - Name: "service-default-behavior", - }, - { - Name: "service-disabled-behavior", - DisableAutoHostRewrite: true, - }, - }, - } - - expected := structs.GatewayServices{ - &structs.GatewayService{ - Gateway: structs.ServiceName{Name: "terminating-gateway"}, - Service: structs.ServiceName{Name: "service-default-behavior"}, - GatewayKind: "terminating-gateway", - AutoHostRewrite: true, - }, - &structs.GatewayService{ - Gateway: structs.ServiceName{Name: "terminating-gateway"}, - Service: structs.ServiceName{Name: "service-disabled-behavior"}, - GatewayKind: "terminating-gateway", - AutoHostRewrite: false, - }, - } - - txn := s.db.Txn(false) - - _, services, err := terminatingConfigGatewayServices(txn, structs.ServiceName{Name: "terminating-gateway"}, cfg, nil) - require.NoError(t, err) - require.Equal(t, services, expected) - -} diff --git a/agent/consul/state/config_entry_test.go b/agent/consul/state/config_entry_test.go index c1bd3e35de..f2794d5043 100644 --- a/agent/consul/state/config_entry_test.go +++ b/agent/consul/state/config_entry_test.go @@ -772,6 +772,7 @@ func TestStore_ServiceDefaults_Kind_Destination_Wildcard(t *testing.T) { CreateIndex: 8, ModifyIndex: 8, }, + AutoHostRewrite: true, }, } require.Equal(t, expected, gatewayServices) @@ -819,6 +820,7 @@ func TestStore_ServiceDefaults_Kind_Destination_Wildcard(t *testing.T) { CreateIndex: 7, ModifyIndex: 7, }, + AutoHostRewrite: true, }, } require.Equal(t, expected, gatewayServices) @@ -843,6 +845,7 @@ func TestStore_ServiceDefaults_Kind_Destination_Wildcard(t *testing.T) { CreateIndex: 7, ModifyIndex: 9, }, + AutoHostRewrite: true, }, } require.Equal(t, expected, gatewayServices) diff --git a/agent/structs/config_entry_gateways.go b/agent/structs/config_entry_gateways.go index 7f3af21932..a15b42a013 100644 --- a/agent/structs/config_entry_gateways.go +++ b/agent/structs/config_entry_gateways.go @@ -719,14 +719,15 @@ func (g *GatewayService) Clone() *GatewayService { Port: g.Port, Protocol: g.Protocol, // See https://github.com/go101/go101/wiki/How-to-efficiently-clone-a-slice%3F - Hosts: append(g.Hosts[:0:0], g.Hosts...), - CAFile: g.CAFile, - CertFile: g.CertFile, - KeyFile: g.KeyFile, - SNI: g.SNI, - FromWildcard: g.FromWildcard, - RaftIndex: g.RaftIndex, - ServiceKind: g.ServiceKind, + Hosts: append(g.Hosts[:0:0], g.Hosts...), + CAFile: g.CAFile, + CertFile: g.CertFile, + KeyFile: g.KeyFile, + SNI: g.SNI, + FromWildcard: g.FromWildcard, + RaftIndex: g.RaftIndex, + ServiceKind: g.ServiceKind, + AutoHostRewrite: g.AutoHostRewrite, } }