-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(operator): Add support for properly configuring the CORS allowed…
… origins header (#5906) * Add support for properly configuring the CORS allowed origins header * Fix smoke test to get expected CORS allowed origins from the actual ingress * Moved CORS test examples to test/resources. Updated Cors logic.
- Loading branch information
1 parent
2095bc2
commit 878ca8b
Showing
12 changed files
with
227 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
operator/controller/src/main/java/io/apicurio/registry/operator/feat/Cors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package io.apicurio.registry.operator.feat; | ||
|
||
import io.apicurio.registry.operator.EnvironmentVariables; | ||
import io.apicurio.registry.operator.api.v1.ApicurioRegistry3; | ||
import io.apicurio.registry.operator.api.v1.ApicurioRegistry3Spec; | ||
import io.apicurio.registry.operator.api.v1.spec.ComponentSpec; | ||
import io.apicurio.registry.operator.resource.ResourceFactory; | ||
import io.apicurio.registry.operator.utils.IngressUtils; | ||
import io.fabric8.kubernetes.api.model.EnvVar; | ||
import io.fabric8.kubernetes.api.model.EnvVarBuilder; | ||
|
||
import java.util.Arrays; | ||
import java.util.LinkedHashMap; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
|
||
/** | ||
* Helper class used to handle CORS related configuration. | ||
*/ | ||
public class Cors { | ||
/** | ||
* Configure the QUARKUS_HTTP_CORS_ORIGINS environment variable with the following: | ||
* <ul> | ||
* <li>Add the ingress host</li> | ||
* <li>Override if QUARKUS_HTTP_CORS_ORIGINS is configured in the "env" section</li> | ||
* </ul> | ||
* | ||
* @param primary | ||
* @param envVars | ||
*/ | ||
public static void configureAllowedOrigins(ApicurioRegistry3 primary, | ||
LinkedHashMap<String, EnvVar> envVars) { | ||
TreeSet<String> allowedOrigins = new TreeSet<>(); | ||
|
||
// If the QUARKUS_HTTP_CORS_ORIGINS env var is configured in the "env" section of the CR, | ||
// then make sure to add those configured values to the set of allowed origins we want to | ||
// configure. | ||
Optional.ofNullable(primary.getSpec()).map(ApicurioRegistry3Spec::getApp).map(ComponentSpec::getEnv) | ||
.ifPresent(env -> { | ||
env.stream().filter( | ||
envVar -> envVar.getName().equals(EnvironmentVariables.QUARKUS_HTTP_CORS_ORIGINS)) | ||
.forEach(envVar -> { | ||
Optional.ofNullable(envVar.getValue()).ifPresent(envVarValue -> { | ||
Arrays.stream(envVarValue.split(",")).forEach(allowedOrigins::add); | ||
}); | ||
}); | ||
}); | ||
|
||
// If not, let's try to figure it out from other sources. | ||
if (allowedOrigins.isEmpty()) { | ||
// If there is a configured Ingress host for the UI or the Studio UI, add them to the allowed | ||
// origins. | ||
Set.of(ResourceFactory.COMPONENT_UI, ResourceFactory.COMPONENT_STUDIO_UI).forEach(component -> { | ||
String host = IngressUtils.getConfiguredHost(component, primary); | ||
if (host != null) { | ||
allowedOrigins.add("http://" + host); | ||
allowedOrigins.add("https://" + host); | ||
} | ||
}); | ||
} | ||
|
||
// If we still do not have anything, then default to "*" | ||
if (allowedOrigins.isEmpty()) { | ||
allowedOrigins.add("*"); | ||
} | ||
|
||
// Join the values in allowedOrigins into a String and set it as the new value of the env var. | ||
String envVarValue = String.join(",", allowedOrigins); | ||
envVars.put(EnvironmentVariables.QUARKUS_HTTP_CORS_ORIGINS, new EnvVarBuilder() | ||
.withName(EnvironmentVariables.QUARKUS_HTTP_CORS_ORIGINS).withValue(envVarValue).build()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
operator/controller/src/test/java/io/apicurio/registry/operator/unit/CorsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.apicurio.registry.operator.unit; | ||
|
||
import io.apicurio.registry.operator.EnvironmentVariables; | ||
import io.apicurio.registry.operator.OperatorException; | ||
import io.apicurio.registry.operator.api.v1.ApicurioRegistry3; | ||
import io.apicurio.registry.operator.feat.Cors; | ||
import io.apicurio.registry.operator.resource.ResourceFactory; | ||
import io.fabric8.kubernetes.api.model.EnvVar; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.nio.charset.Charset; | ||
import java.util.LinkedHashMap; | ||
import java.util.Set; | ||
|
||
public class CorsTest { | ||
|
||
@Test | ||
public void testConfigureAllowedOrigins() throws Exception { | ||
doTestAllowedOrigins("k8s/examples/cors/example-default.yaml", "*"); | ||
doTestAllowedOrigins("k8s/examples/cors/example-ingress.yaml", | ||
"http://simple-ui.apps.cluster.example", "https://simple-ui.apps.cluster.example"); | ||
doTestAllowedOrigins("k8s/examples/cors/example-env-vars.yaml", "https://ui.example.org"); | ||
doTestAllowedOrigins("k8s/examples/cors/example-env-vars-and-ingress.yaml", "https://ui.example.org"); | ||
} | ||
|
||
private void doTestAllowedOrigins(String crPath, String... values) { | ||
ClassLoader classLoader = CorsTest.class.getClassLoader(); | ||
ApicurioRegistry3 registry = ResourceFactory.deserialize(crPath, ApicurioRegistry3.class, | ||
classLoader); | ||
|
||
LinkedHashMap<String, EnvVar> envVars = new LinkedHashMap<>(); | ||
Cors.configureAllowedOrigins(registry, envVars); | ||
Assertions.assertThat(envVars.keySet()).contains(EnvironmentVariables.QUARKUS_HTTP_CORS_ORIGINS); | ||
String allowedOriginsValue = envVars.get(EnvironmentVariables.QUARKUS_HTTP_CORS_ORIGINS).getValue(); | ||
Set<String> allowedOrigins = Set.of(allowedOriginsValue.split(",")); | ||
Assertions.assertThat(allowedOrigins).containsExactlyInAnyOrder(values); | ||
} | ||
|
||
public static String load(String path) { | ||
try (var stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path)) { | ||
return new String(stream.readAllBytes(), Charset.defaultCharset()); | ||
} catch (Exception ex) { | ||
throw new OperatorException("Could not read resource: " + path, ex); | ||
} | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
operator/controller/src/test/resources/k8s/examples/cors/example-default.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
apiVersion: registry.apicur.io/v1 | ||
kind: ApicurioRegistry3 | ||
metadata: | ||
name: simple | ||
spec: {} |
14 changes: 14 additions & 0 deletions
14
operator/controller/src/test/resources/k8s/examples/cors/example-env-vars-and-ingress.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
apiVersion: registry.apicur.io/v1 | ||
kind: ApicurioRegistry3 | ||
metadata: | ||
name: simple | ||
spec: | ||
app: | ||
ingress: | ||
host: simple-app.apps.cluster.example | ||
env: | ||
- name: QUARKUS_HTTP_CORS_ORIGINS | ||
value: https://ui.example.org | ||
ui: | ||
ingress: | ||
host: simple-ui.apps.cluster.example |
9 changes: 9 additions & 0 deletions
9
operator/controller/src/test/resources/k8s/examples/cors/example-env-vars.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: registry.apicur.io/v1 | ||
kind: ApicurioRegistry3 | ||
metadata: | ||
name: simple | ||
spec: | ||
app: | ||
env: | ||
- name: QUARKUS_HTTP_CORS_ORIGINS | ||
value: https://ui.example.org |
11 changes: 11 additions & 0 deletions
11
operator/controller/src/test/resources/k8s/examples/cors/example-ingress.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
apiVersion: registry.apicur.io/v1 | ||
kind: ApicurioRegistry3 | ||
metadata: | ||
name: simple | ||
spec: | ||
app: | ||
ingress: | ||
host: simple-app.apps.cluster.example | ||
ui: | ||
ingress: | ||
host: simple-ui.apps.cluster.example |