Skip to content

Commit 921ea61

Browse files
committed
simplify CloudFOundryPostProcessor
1 parent a67bf5a commit 921ea61

File tree

3 files changed

+144
-84
lines changed

3 files changed

+144
-84
lines changed

src/autoscaler/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/conf/CloudFoundryConfigurationProcessor.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@ public void postProcessEnvironment(
5151
"Found scheduler-config service in VCAP_SERVICES, applying configuration overrides");
5252
allConfigs.putAll(schedulerConfig);
5353
}
54-
// Process VCAP_APPLICATION for org GUID (this should override scheduler-config)
55-
Map<String, Object> vcapAppConfig = extractVcapApplicationConfig(environment, allConfigs);
56-
if (vcapAppConfig != null && !vcapAppConfig.isEmpty()) {
57-
logger.info("Found VCAP_APPLICATION, applying cfserver configuration overrides");
58-
allConfigs.putAll(vcapAppConfig);
54+
55+
Map<String, Object> vcapOrgAndGuid = getOrgAndSpaceGuidFromVcap(environment);
56+
Map<String, Object> cfServerConfig = (Map<String, Object>) allConfigs.get("cfserver");
57+
if (cfServerConfig != null && !cfServerConfig.isEmpty()) {
58+
logger.info(
59+
"Found org and space guid service in VCAP_APPLICATIONS, applying configuration"
60+
+ " overrides");
61+
cfServerConfig.putAll(vcapOrgAndGuid);
5962
}
6063

6164
// Process database services
@@ -89,7 +92,8 @@ public void postProcessEnvironment(
8992
}
9093
}
9194

92-
private Map<String, Object> extractVcapApplicationConfig(ConfigurableEnvironment environment, Map<String, Object> allConfigs) {
95+
private Map<String, Object> getOrgAndSpaceGuidFromVcap(ConfigurableEnvironment environment) {
96+
Map<String, Object> orgSpaceMap;
9397
try {
9498
String vcapApplication = environment.getProperty(VCAP_APPLICATION);
9599

@@ -100,23 +104,16 @@ private Map<String, Object> extractVcapApplicationConfig(ConfigurableEnvironment
100104
TypeReference<Map<String, Object>> typeRef = new TypeReference<>() {};
101105
Map<String, Object> vcapApp = objectMapper.readValue(vcapApplication, typeRef);
102106

103-
Map<String, Object> config = new java.util.HashMap<>();
104-
boolean foundConfig = false;
105-
106-
Map<String, Object> cfserverConfig = (Map<String, Object>) allConfigs.get("cfserver");
107-
if (cfserverConfig == null) {
108-
cfserverConfig = new java.util.HashMap<>();
109-
config.put("cfserver", cfserverConfig);
110-
}
107+
orgSpaceMap = new java.util.HashMap<>();
111108

112109
// Extract organization_id
113110
Object organizationId = vcapApp.get("organization_id");
114111
if (organizationId instanceof String && !((String) organizationId).trim().isEmpty()) {
115112
String orgGuid = (String) organizationId;
116113
logger.info(
117114
"Setting cfserver.validOrgGuid from VCAP_APPLICATION organization_id: {}", orgGuid);
118-
cfserverConfig.put("validOrgGuid", orgGuid);
119-
foundConfig = true;
115+
orgSpaceMap.put("validOrgGuid", orgGuid);
116+
120117
} else {
121118
logger.warn("organization_id not found or empty in VCAP_APPLICATION");
122119
}
@@ -127,17 +124,15 @@ private Map<String, Object> extractVcapApplicationConfig(ConfigurableEnvironment
127124
String spaceGuid = (String) spaceId;
128125
logger.info(
129126
"Setting cfserver.validSpaceGuid from VCAP_APPLICATION space_id: {}", spaceGuid);
130-
cfserverConfig.put("validSpaceGuid", spaceGuid);
131-
foundConfig = true;
127+
orgSpaceMap.put("validSpaceGuid", spaceGuid);
132128
} else {
133129
logger.warn("space_id not found or empty in VCAP_APPLICATION");
134130
}
135-
136-
return foundConfig ? config : null;
137131
} catch (Exception e) {
138132
logger.error("Failed to parse VCAP_APPLICATION JSON", e);
139133
return null;
140134
}
135+
return orgSpaceMap;
141136
}
142137

143138
private Map<String, Object> extractSchedulerConfig(String vcapServices) {

src/autoscaler/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/filter/HttpAuthFilter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ protected void doFilterInternal(
4444
request.getMethod(),
4545
request.getScheme(),
4646
forwardedProto,
47-
isHealthEndpoint, cfServerConfiguration.getHealthserver().getUsername());
47+
isHealthEndpoint,
48+
cfServerConfiguration.getHealthserver().getUsername());
4849

4950
if (isHealthEndpoint) {
5051
handleHealthEndpoint(request, response);

src/autoscaler/scheduler/src/test/java/org/cloudfoundry/autoscaler/scheduler/conf/CloudFoundryConfigurationProcessorTest.java

Lines changed: 127 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public void testVcapServicesWithDatabaseService() {
161161
}
162162
]
163163
}
164-
""";
164+
""";
165165

166166
environment
167167
.getPropertySources()
@@ -236,25 +236,25 @@ public void testVcapServicesWithDatabaseServiceNoSsl() {
236236
public void testVcapServicesWithClientCertCredentialMapping() {
237237
String vcapServices =
238238
"""
239-
{
240-
"postgresql-db": [
241-
{
242-
"label": "postgresql-db",
243-
"name": "autoscaler-db",
244-
"tags": ["relational", "binding_db"],
245-
"credentials": {
246-
"username": "dbuser",
247-
"password": "dbpass",
248-
"hostname": "db-host.example.com",
249-
"dbname": "autoscaler_db",
250-
"port": "5432",
251-
"client_cert": "-----BEGIN CERTIFICATE-----\\nMIICert...\\n-----END CERTIFICATE-----",
252-
"client_key": "-----BEGIN PRIVATE KEY-----\\nMIIKey...\\n-----END PRIVATE KEY-----",
253-
"sslrootcert": "-----BEGIN CERTIFICATE-----\\nMIIRoot...\\n-----END CERTIFICATE-----"
254-
}
255-
}
256-
]
257-
}
239+
{
240+
"postgresql-db": [
241+
{
242+
"label": "postgresql-db",
243+
"name": "autoscaler-db",
244+
"tags": ["relational", "binding_db"],
245+
"credentials": {
246+
"username": "dbuser",
247+
"password": "dbpass",
248+
"hostname": "db-host.example.com",
249+
"dbname": "autoscaler_db",
250+
"port": "5432",
251+
"client_cert": "-----BEGIN CERTIFICATE-----\\nMIICert...\\n-----END CERTIFICATE-----",
252+
"client_key": "-----BEGIN PRIVATE KEY-----\\nMIIKey...\\n-----END PRIVATE KEY-----",
253+
"sslrootcert": "-----BEGIN CERTIFICATE-----\\nMIIRoot...\\n-----END CERTIFICATE-----"
254+
}
255+
}
256+
]
257+
}
258258
""";
259259

260260
environment
@@ -280,23 +280,23 @@ public void testVcapServicesWithClientCertCredentialMapping() {
280280
public void testVcapServicesWithClientCertOnlyCredentialMapping() {
281281
String vcapServices =
282282
"""
283-
{
284-
"postgresql-db": [
285-
{
286-
"label": "postgresql-db",
287-
"name": "autoscaler-db",
288-
"tags": ["relational", "binding_db"],
289-
"credentials": {
290-
"username": "dbuser",
291-
"password": "dbpass",
292-
"hostname": "db-host.example.com",
293-
"dbname": "autoscaler_db",
294-
"port": "5432",
295-
"client_cert": "-----BEGIN CERTIFICATE-----\\nMIICert...\\n-----END CERTIFICATE-----"
296-
}
297-
}
298-
]
299-
}
283+
{
284+
"postgresql-db": [
285+
{
286+
"label": "postgresql-db",
287+
"name": "autoscaler-db",
288+
"tags": ["relational", "binding_db"],
289+
"credentials": {
290+
"username": "dbuser",
291+
"password": "dbpass",
292+
"hostname": "db-host.example.com",
293+
"dbname": "autoscaler_db",
294+
"port": "5432",
295+
"client_cert": "-----BEGIN CERTIFICATE-----\\nMIICert...\\n-----END CERTIFICATE-----"
296+
}
297+
}
298+
]
299+
}
300300
""";
301301

302302
environment
@@ -317,26 +317,26 @@ public void testVcapServicesWithClientCertOnlyCredentialMapping() {
317317
public void testVcapServicesPrefersSslcertOverClientCert() {
318318
String vcapServices =
319319
"""
320-
{
321-
"postgresql-db": [
322-
{
323-
"label": "postgresql-db",
324-
"name": "autoscaler-db",
325-
"tags": ["relational", "binding_db"],
326-
"credentials": {
327-
"username": "dbuser",
328-
"password": "dbpass",
329-
"hostname": "db-host.example.com",
330-
"dbname": "autoscaler_db",
331-
"port": "5432",
332-
"sslcert": "-----BEGIN CERTIFICATE-----\\nMIISSLCert...\\n-----END CERTIFICATE-----",
333-
"sslkey": "-----BEGIN PRIVATE KEY-----\\nMIISSLKey...\\n-----END PRIVATE KEY-----",
334-
"client_cert": "-----BEGIN CERTIFICATE-----\\nMIICert...\\n-----END CERTIFICATE-----",
335-
"client_key": "-----BEGIN PRIVATE KEY-----\\nMIIKey...\\n-----END PRIVATE KEY-----"
336-
}
337-
}
338-
]
339-
}
320+
{
321+
"postgresql-db": [
322+
{
323+
"label": "postgresql-db",
324+
"name": "autoscaler-db",
325+
"tags": ["relational", "binding_db"],
326+
"credentials": {
327+
"username": "dbuser",
328+
"password": "dbpass",
329+
"hostname": "db-host.example.com",
330+
"dbname": "autoscaler_db",
331+
"port": "5432",
332+
"sslcert": "-----BEGIN CERTIFICATE-----\\nMIISSLCert...\\n-----END CERTIFICATE-----",
333+
"sslkey": "-----BEGIN PRIVATE KEY-----\\nMIISSLKey...\\n-----END PRIVATE KEY-----",
334+
"client_cert": "-----BEGIN CERTIFICATE-----\\nMIICert...\\n-----END CERTIFICATE-----",
335+
"client_key": "-----BEGIN PRIVATE KEY-----\\nMIIKey...\\n-----END PRIVATE KEY-----"
336+
}
337+
}
338+
]
339+
}
340340
""";
341341

342342
environment
@@ -358,7 +358,20 @@ public void testVcapApplicationWithOrgGuid() {
358358
String vcapServices =
359359
"""
360360
{
361-
"user-provided": []
361+
"user-provided": [
362+
{
363+
"name": "scheduler-config-service",
364+
"tags": ["scheduler-config"],
365+
"credentials": {
366+
"cfserver": {
367+
"healthserver": {
368+
"username": "health-user",
369+
"password": "health-password"
370+
}
371+
}
372+
}
373+
}
374+
]
362375
}
363376
""";
364377

@@ -392,7 +405,20 @@ public void testVcapApplicationWithoutOrgGuid() {
392405
String vcapServices =
393406
"""
394407
{
395-
"user-provided": []
408+
"user-provided": [
409+
{
410+
"name": "scheduler-config-service",
411+
"tags": ["scheduler-config"],
412+
"credentials": {
413+
"cfserver": {
414+
"healthserver": {
415+
"username": "health-user",
416+
"password": "health-password"
417+
}
418+
}
419+
}
420+
}
421+
]
396422
}
397423
""";
398424

@@ -546,7 +572,20 @@ public void testVcapApplicationWithOnlySpaceGuid() {
546572
String vcapServices =
547573
"""
548574
{
549-
"user-provided": []
575+
"user-provided": [
576+
{
577+
"name": "scheduler-config-service",
578+
"tags": ["scheduler-config"],
579+
"credentials": {
580+
"cfserver": {
581+
"healthserver": {
582+
"username": "health-user",
583+
"password": "health-password"
584+
}
585+
}
586+
}
587+
}
588+
]
550589
}
551590
""";
552591

@@ -579,7 +618,20 @@ public void testVcapApplicationWithEmptySpaceGuid() {
579618
String vcapServices =
580619
"""
581620
{
582-
"user-provided": []
621+
"user-provided": [
622+
{
623+
"name": "scheduler-config-service",
624+
"tags": ["scheduler-config"],
625+
"credentials": {
626+
"cfserver": {
627+
"healthserver": {
628+
"username": "health-user",
629+
"password": "health-password"
630+
}
631+
}
632+
}
633+
}
634+
]
583635
}
584636
""";
585637

@@ -613,10 +665,22 @@ public void testVcapApplicationWithoutSpaceGuid() {
613665
String vcapServices =
614666
"""
615667
{
616-
"user-provided": []
668+
"user-provided": [
669+
{
670+
"name": "scheduler-config-service",
671+
"tags": ["scheduler-config"],
672+
"credentials": {
673+
"cfserver": {
674+
"healthserver": {
675+
"username": "health-user",
676+
"password": "health-password"
677+
}
678+
}
679+
}
680+
}
681+
]
617682
}
618683
""";
619-
620684
String vcapApplication =
621685
"""
622686
{

0 commit comments

Comments
 (0)