Skip to content

Commit 08527d9

Browse files
committed
Refactor mTLS route options to RFC-0027 compliant flat format
RFC-0027 requires options values to be only strings, numbers, or booleans - not nested objects/arrays. Updated: - RegistryMessageOpts: Use flat fields (mtls_allowed_apps, mtls_allowed_spaces, mtls_allowed_orgs, mtls_allow_any) with comma-separated GUIDs instead of nested MtlsAllowedSources struct - parseCommaSeparatedGUIDs(): New helper to split comma-separated GUID strings into slices - getEffectiveMtlsAllowedSources(): Parse flat options from Options struct while maintaining top-level MtlsAllowedSources precedence for route-registrar compatibility - Tests: Updated to verify flat options parsing
1 parent f79d082 commit 08527d9

2 files changed

Lines changed: 56 additions & 21 deletions

File tree

src/code.cloudfoundry.org/gorouter/mbus/registry_message_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ var _ = Describe("RegistryMessage", func() {
100100
})
101101
})
102102

103-
Describe("With mtls_allowed_sources nested in options (CAPI/Diego format)", func() {
103+
Describe("With flat mTLS options in options (RFC-0027 compliant CAPI/Diego format)", func() {
104104
BeforeEach(func() {
105105
payload = []byte(`{
106106
"app":"app1",
@@ -111,24 +111,26 @@ var _ = Describe("RegistryMessage", func() {
111111
"private_instance_id":"private_instance_id",
112112
"options": {
113113
"loadbalancing": "round-robin",
114-
"mtls_allowed_sources": {
115-
"apps": ["nested-app-guid"],
116-
"any": true
117-
}
114+
"mtls_allowed_apps": "app-guid-1,app-guid-2",
115+
"mtls_allowed_spaces": "space-guid-1",
116+
"mtls_allowed_orgs": "org-guid-1",
117+
"mtls_allow_any": true
118118
}
119119
}`)
120120
})
121121

122-
It("parses nested mtls_allowed_sources correctly", func() {
122+
It("parses flat mTLS options correctly", func() {
123123
endpoint, err := message.MakeEndpoint(false)
124124
Expect(err).NotTo(HaveOccurred())
125125
Expect(endpoint.MtlsAllowedSources).NotTo(BeNil())
126-
Expect(endpoint.MtlsAllowedSources.Apps).To(ConsistOf("nested-app-guid"))
126+
Expect(endpoint.MtlsAllowedSources.Apps).To(ConsistOf("app-guid-1", "app-guid-2"))
127+
Expect(endpoint.MtlsAllowedSources.Spaces).To(ConsistOf("space-guid-1"))
128+
Expect(endpoint.MtlsAllowedSources.Orgs).To(ConsistOf("org-guid-1"))
127129
Expect(endpoint.MtlsAllowedSources.Any).To(BeTrue())
128130
})
129131
})
130132

131-
Describe("With mtls_allowed_sources at both top-level and nested", func() {
133+
Describe("With mtls_allowed_sources at top-level and flat options", func() {
132134
BeforeEach(func() {
133135
payload = []byte(`{
134136
"app":"app1",
@@ -141,9 +143,7 @@ var _ = Describe("RegistryMessage", func() {
141143
"apps": ["top-level-app"]
142144
},
143145
"options": {
144-
"mtls_allowed_sources": {
145-
"apps": ["nested-app"]
146-
}
146+
"mtls_allowed_apps": "flat-options-app"
147147
}
148148
}`)
149149
})

src/code.cloudfoundry.org/gorouter/mbus/subscriber.go

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ type RegistryMessage struct {
4242
}
4343

4444
type RegistryMessageOpts struct {
45-
LoadBalancingAlgorithm string `json:"loadbalancing"`
46-
HashHeaderName string `json:"hash_header"`
47-
HashBalance float64 `json:"hash_balance"`
48-
MtlsAllowedSources *MtlsAllowedSources `json:"mtls_allowed_sources,omitempty"`
45+
LoadBalancingAlgorithm string `json:"loadbalancing"`
46+
HashHeaderName string `json:"hash_header"`
47+
HashBalance float64 `json:"hash_balance"`
48+
// RFC-0027 compliant flat mTLS options (comma-separated GUIDs)
49+
MtlsAllowedApps string `json:"mtls_allowed_apps,omitempty"`
50+
MtlsAllowedSpaces string `json:"mtls_allowed_spaces,omitempty"`
51+
MtlsAllowedOrgs string `json:"mtls_allowed_orgs,omitempty"`
52+
MtlsAllowAny bool `json:"mtls_allow_any,omitempty"`
4953
}
5054

5155
// MtlsAllowedSources contains authorization rules for which sources can communicate
@@ -59,6 +63,25 @@ type MtlsAllowedSources struct {
5963
Any bool `json:"any,omitempty"`
6064
}
6165

66+
// parseCommaSeparatedGUIDs splits a comma-separated string into a slice of GUIDs
67+
func parseCommaSeparatedGUIDs(s string) []string {
68+
if s == "" {
69+
return nil
70+
}
71+
parts := strings.Split(s, ",")
72+
result := make([]string, 0, len(parts))
73+
for _, p := range parts {
74+
trimmed := strings.TrimSpace(p)
75+
if trimmed != "" {
76+
result = append(result, trimmed)
77+
}
78+
}
79+
if len(result) == 0 {
80+
return nil
81+
}
82+
return result
83+
}
84+
6285
// getMtlsAllowedSources returns the MtlsAllowedSources, or nil if not present
6386
func getMtlsAllowedSources(as *MtlsAllowedSources) *route.MtlsAllowedSources {
6487
if as == nil {
@@ -72,18 +95,30 @@ func getMtlsAllowedSources(as *MtlsAllowedSources) *route.MtlsAllowedSources {
7295
}
7396
}
7497

75-
// getEffectiveMtlsAllowedSources returns MtlsAllowedSources from either top-level or nested in options.
76-
// Top-level takes precedence (used by route-registrar), nested is used by CAPI/Diego.
98+
// getEffectiveMtlsAllowedSources returns MtlsAllowedSources from either top-level or flat options.
99+
// Top-level takes precedence (used by route-registrar), flat options are RFC-0027 compliant (used by CAPI/Diego).
77100
func (rm *RegistryMessage) getEffectiveMtlsAllowedSources() *route.MtlsAllowedSources {
78101
// Top-level mtls_allowed_sources takes precedence (route-registrar uses this)
79102
if rm.MtlsAllowedSources != nil {
80103
return getMtlsAllowedSources(rm.MtlsAllowedSources)
81104
}
82-
// Fall back to options.mtls_allowed_sources (CAPI/Diego uses this)
83-
if rm.Options.MtlsAllowedSources != nil {
84-
return getMtlsAllowedSources(rm.Options.MtlsAllowedSources)
105+
// Fall back to RFC-0027 compliant flat options
106+
apps := parseCommaSeparatedGUIDs(rm.Options.MtlsAllowedApps)
107+
spaces := parseCommaSeparatedGUIDs(rm.Options.MtlsAllowedSpaces)
108+
orgs := parseCommaSeparatedGUIDs(rm.Options.MtlsAllowedOrgs)
109+
allowAny := rm.Options.MtlsAllowAny
110+
111+
// If no mTLS options are set, return nil
112+
if apps == nil && spaces == nil && orgs == nil && !allowAny {
113+
return nil
114+
}
115+
116+
return &route.MtlsAllowedSources{
117+
Apps: apps,
118+
Spaces: spaces,
119+
Orgs: orgs,
120+
Any: allowAny,
85121
}
86-
return nil
87122
}
88123

89124
func (rm *RegistryMessage) MakeEndpoint(http2Enabled bool) (*route.Endpoint, error) {

0 commit comments

Comments
 (0)