|
| 1 | +package io.apicurio.registry.operator.feat; |
| 2 | + |
| 3 | +import io.apicurio.registry.operator.EnvironmentVariables; |
| 4 | +import io.apicurio.registry.operator.api.v1.ApicurioRegistry3; |
| 5 | +import io.apicurio.registry.operator.api.v1.ApicurioRegistry3Spec; |
| 6 | +import io.apicurio.registry.operator.api.v1.spec.ComponentSpec; |
| 7 | +import io.apicurio.registry.operator.resource.ResourceFactory; |
| 8 | +import io.apicurio.registry.operator.utils.IngressUtils; |
| 9 | +import io.fabric8.kubernetes.api.model.EnvVar; |
| 10 | +import io.fabric8.kubernetes.api.model.EnvVarBuilder; |
| 11 | + |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.LinkedHashMap; |
| 14 | +import java.util.Optional; |
| 15 | +import java.util.Set; |
| 16 | +import java.util.TreeSet; |
| 17 | + |
| 18 | +/** |
| 19 | + * Helper class used to handle CORS related configuration. |
| 20 | + */ |
| 21 | +public class Cors { |
| 22 | + /** |
| 23 | + * Configure the QUARKUS_HTTP_CORS_ORIGINS environment variable with the following: |
| 24 | + * <ul> |
| 25 | + * <li>Add the ingress host</li> |
| 26 | + * <li>Override if QUARKUS_HTTP_CORS_ORIGINS is configured in the "env" section</li> |
| 27 | + * </ul> |
| 28 | + * |
| 29 | + * @param primary |
| 30 | + * @param envVars |
| 31 | + */ |
| 32 | + public static void configureAllowedOrigins(ApicurioRegistry3 primary, |
| 33 | + LinkedHashMap<String, EnvVar> envVars) { |
| 34 | + TreeSet<String> allowedOrigins = new TreeSet<>(); |
| 35 | + |
| 36 | + // If the QUARKUS_HTTP_CORS_ORIGINS env var is configured in the "env" section of the CR, |
| 37 | + // then make sure to add those configured values to the set of allowed origins we want to |
| 38 | + // configure. |
| 39 | + Optional.ofNullable(primary.getSpec()).map(ApicurioRegistry3Spec::getApp).map(ComponentSpec::getEnv) |
| 40 | + .ifPresent(env -> { |
| 41 | + env.stream().filter( |
| 42 | + envVar -> envVar.getName().equals(EnvironmentVariables.QUARKUS_HTTP_CORS_ORIGINS)) |
| 43 | + .forEach(envVar -> { |
| 44 | + Optional.ofNullable(envVar.getValue()).ifPresent(envVarValue -> { |
| 45 | + Arrays.stream(envVarValue.split(",")).forEach(allowedOrigins::add); |
| 46 | + }); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + // If not, let's try to figure it out from other sources. |
| 51 | + if (allowedOrigins.isEmpty()) { |
| 52 | + // If there is a configured Ingress host for the UI or the Studio UI, add them to the allowed |
| 53 | + // origins. |
| 54 | + Set.of(ResourceFactory.COMPONENT_UI, ResourceFactory.COMPONENT_STUDIO_UI).forEach(component -> { |
| 55 | + String host = IngressUtils.getConfiguredHost(component, primary); |
| 56 | + if (host != null) { |
| 57 | + allowedOrigins.add("http://" + host); |
| 58 | + allowedOrigins.add("https://" + host); |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + // If we still do not have anything, then default to "*" |
| 64 | + if (allowedOrigins.isEmpty()) { |
| 65 | + allowedOrigins.add("*"); |
| 66 | + } |
| 67 | + |
| 68 | + // Join the values in allowedOrigins into a String and set it as the new value of the env var. |
| 69 | + String envVarValue = String.join(",", allowedOrigins); |
| 70 | + envVars.put(EnvironmentVariables.QUARKUS_HTTP_CORS_ORIGINS, new EnvVarBuilder() |
| 71 | + .withName(EnvironmentVariables.QUARKUS_HTTP_CORS_ORIGINS).withValue(envVarValue).build()); |
| 72 | + } |
| 73 | +} |
0 commit comments