Skip to content

Commit f687af0

Browse files
ivanmatmatioktalz
authored andcommitted
MINOR: add domain wildcard SNI map file
1 parent 25c25c6 commit f687af0

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

k8s/gate/haproxy/frontends.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,14 @@ func (b *HaproxyConfMgrImpl) newFrontend(params newFrontendParams) (*models.Fron
234234
pathDomainWPathExactMap := b.params.mapsStorage.MapPath(frontendName, storage.PATH_EXACT_DOMAIN_WILDCARD_MAP)
235235
pathRegexMap := b.params.mapsStorage.MapPath(frontendName, storage.PATH_REGEX_MAP)
236236
sniMap := b.params.mapsStorage.MapPath(frontendName, storage.SNI_MAP)
237+
sniDomainWildcardMap := b.params.mapsStorage.MapPath(frontendName, storage.SNI_DOMAIN_WILDCARD_MAP)
237238

238239
b.params.mapsStorage.EnsureMapData(pathExactMap)
239240
b.params.mapsStorage.EnsureMapData(pathPrefixMap)
240241
b.params.mapsStorage.EnsureMapData(pathRegexMap)
241242
b.params.mapsStorage.EnsureMapData(pathDomainWPathExactMap)
242243
b.params.mapsStorage.EnsureMapData(sniMap)
244+
b.params.mapsStorage.EnsureMapData(sniDomainWildcardMap)
243245

244246
var tcpRules []*models.TCPRequestRule
245247
var httpRules []*models.HTTPRequestRule
@@ -277,11 +279,12 @@ func (b *HaproxyConfMgrImpl) newFrontend(params newFrontendParams) (*models.Fron
277279
},
278280
{
279281
// tcp-request content set-var(txn.sni_match) req_ssl_sni,regsub(^[^.]*,,),map(sni.map)
282+
// tcp-request content set-var(txn.sni_match,ifnotexists) req_ssl_sni,map_end(sniDomainWildcardMap.map)
280283
Type: "content",
281284
Action: "set-var",
282285
VarName: "sni_match",
283286
VarScope: "txn",
284-
Expr: "req_ssl_sni,regsub(^[^.]*,,),map(" + sniMap.FullPath() + ")",
287+
Expr: "req_ssl_sni,map_end(" + sniDomainWildcardMap.FullPath() + ")",
285288
},
286289
{
287290
// http-request lua.route if route_is_json

k8s/gate/haproxy/routes-maps.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,18 @@ func (b *RouteMgrImpl) fillMapsForTLSRoutes() {
6565

6666
pathSNIMap := mapsStorage.MapPath(frontendName, storage.SNI_MAP)
6767
mapSNIMap := mapsStorage.GetMapData(pathSNIMap)
68-
err = b.onDeletedTLSRoute(routeKey, route, mapSNIMap)
68+
pathSNIDomainWildcardMap := mapsStorage.MapPath(frontendName, storage.SNI_DOMAIN_WILDCARD_MAP)
69+
mapSNIDomainWildcardMap := mapsStorage.GetMapData(pathSNIDomainWildcardMap)
70+
err = b.onDeletedTLSRoute(routeKey, route, mapSNIMap, mapSNIDomainWildcardMap)
6971
// errs.Add(err)
7072
_ = err // TODO ignore error for now
7173
}
7274
}
7375
}
7476
// Managed TLSRoutes => Create / update/ delete backends
7577
for routeKey, route := range controllerStore.GateTree.TLSRoutes {
76-
for _, listeners := range route.Listeners.Iterate {
77-
for _, listener := range listeners {
78+
for _, listener := range route.Listeners.Iterate {
79+
for _, listener := range listener {
7880
frontendName, err := b.topManager.getFrontendName(listener.Owner, listener.K8sResource)
7981
if err != nil {
8082
b.topManager.logger.LogAttrs(context.Background(), slog.LevelError, "Failed to get frontend name",
@@ -86,15 +88,16 @@ func (b *RouteMgrImpl) fillMapsForTLSRoutes() {
8688
acceptedHostnamesForRoute := utils.MatchTLSHostnames(listenerHostname, routesHosnames)
8789
pathSNIMap := mapsStorage.MapPath(frontendName, storage.SNI_MAP)
8890
mapSNIMap := mapsStorage.GetMapData(pathSNIMap)
89-
91+
pathSNIDomainWildcardMap := mapsStorage.MapPath(frontendName, storage.SNI_DOMAIN_WILDCARD_MAP)
92+
mapSNIDomainWildcardMap := mapsStorage.GetMapData(pathSNIDomainWildcardMap)
9093
switch route.TreeStatus.Status {
9194
case store.StatusUnchanged:
9295
continue
9396
case store.StatusUpserted:
94-
err := b.onUpsertedTLSRoute(routeKey, route, mapSNIMap, acceptedHostnamesForRoute)
97+
err := b.onUpsertedTLSRoute(routeKey, route, mapSNIMap, mapSNIDomainWildcardMap, acceptedHostnamesForRoute)
9598
errs.Add(err)
9699
case store.StatusDeleted:
97-
err := b.onDeletedTLSRoute(routeKey, route, mapSNIMap)
100+
err := b.onDeletedTLSRoute(routeKey, route, mapSNIMap, mapSNIDomainWildcardMap)
98101
errs.Add(err)
99102
}
100103
}

k8s/gate/haproxy/routes.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ func (b *RouteMgrImpl) onUpsertedHTTPRoute(routeKey k8stypes.NamespacedName, rou
6262
}
6363

6464
func (b *RouteMgrImpl) onUpsertedTLSRoute(routeKey k8stypes.NamespacedName, route *tree.TLSRoute,
65-
sni *maps.MapData, acceptedHostnamesForRoute []string,
65+
mapSNI, mapSNIDomainWildcardMap *maps.MapData, acceptedHostnamesForRoute []string,
6666
) error {
6767
if route.Valid {
68-
return b.onValidTLSRouteUpserted(routeKey, route, sni, acceptedHostnamesForRoute)
68+
return b.onValidTLSRouteUpserted(routeKey, route, mapSNI, mapSNIDomainWildcardMap, acceptedHostnamesForRoute)
6969
}
70-
return b.onInvalidTLSRouteUpserted(routeKey, route, sni)
70+
return b.onInvalidTLSRouteUpserted(routeKey, route, mapSNI, mapSNIDomainWildcardMap)
7171
}
7272

7373
func (b *RouteMgrImpl) onValidTLSRouteUpserted(_ k8stypes.NamespacedName,
74-
tlsRoute *tree.TLSRoute, mapSNI *maps.MapData, acceptedHostnamesForRoute []string,
74+
tlsRoute *tree.TLSRoute, mapSNI, mapSNIDomainWildcardMap *maps.MapData, acceptedHostnamesForRoute []string,
7575
) error {
7676
for _, tlsRouteRule := range tlsRoute.Rules {
7777
// if !rule.Valid {
@@ -136,7 +136,11 @@ func (b *RouteMgrImpl) onValidTLSRouteUpserted(_ k8stypes.NamespacedName,
136136

137137
for _, hostname := range acceptedHostnamesForRoute {
138138
if tlsRouteRule.Valid {
139-
mapSNI.AddData(string(hostname), routeValue)
139+
if isDomainWildcard(string(hostname)) {
140+
mapSNIDomainWildcardMap.AddData(string(hostname), routeValue)
141+
} else {
142+
mapSNI.AddData(string(hostname), routeValue)
143+
}
140144
hostnamesInserted[string(hostname)] = struct{}{}
141145
} else if _, ok := hostnamesInserted[string(hostname)]; !ok {
142146
mapSNI.DeleteData(string(hostname))
@@ -146,17 +150,21 @@ func (b *RouteMgrImpl) onValidTLSRouteUpserted(_ k8stypes.NamespacedName,
146150
return nil
147151
}
148152

149-
func (RouteMgrImpl) onInvalidTLSRouteUpserted(_ k8stypes.NamespacedName, _ *tree.TLSRoute, _ *maps.MapData) error {
153+
func (RouteMgrImpl) onInvalidTLSRouteUpserted(_ k8stypes.NamespacedName, _ *tree.TLSRoute, _, _ *maps.MapData) error {
150154
// TODO we might need to remove it from the maps
151155
return nil
152156
}
153157

154158
func (RouteMgrImpl) onDeletedTLSRoute(_ k8stypes.NamespacedName, route *tree.TLSRoute,
155-
mapSNI *maps.MapData,
159+
mapSNI *maps.MapData, mapSNIDomainWildcard *maps.MapData,
156160
) error {
157161
hostnames := route.K8sResource.Spec.Hostnames
158162
for _, hostname := range hostnames {
159-
mapSNI.DeleteData(string(hostname))
163+
if isDomainWildcard(string(hostname)) {
164+
mapSNIDomainWildcard.DeleteData(string(hostname))
165+
} else {
166+
mapSNI.DeleteData(string(hostname))
167+
}
160168
}
161169
return nil
162170
}

k8s/gate/haproxy/storage/maps.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const (
3434
PATH_PREFIX_MAP = "path_prefix"
3535
PATH_REGEX_MAP = "path_regex"
3636
SNI_MAP = "sni"
37+
SNI_DOMAIN_WILDCARD_MAP = "domain_wildcard_sni"
3738
)
3839

3940
//revive:enable:var-naming

0 commit comments

Comments
 (0)