Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.8.x] Backports #5795

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.camel.quarkus.k.core.SourceDefinition;
import org.apache.camel.quarkus.k.runtime.ApplicationProducers;
import org.apache.camel.quarkus.k.runtime.ApplicationRecorder;
import org.apache.camel.quarkus.k.runtime.ApplicationRoutes;
import org.apache.camel.quarkus.k.support.Constants;
import org.apache.camel.quarkus.k.support.RuntimeSupport;
import org.apache.camel.spi.CamelContextCustomizer;
Expand Down Expand Up @@ -88,7 +89,8 @@ CamelRuntimeTaskBuildItem registerRuntime(
@BuildStep
List<AdditionalBeanBuildItem> unremovableBeans() {
return List.of(
AdditionalBeanBuildItem.unremovableOf(ApplicationProducers.class));
AdditionalBeanBuildItem.unremovableOf(ApplicationProducers.class),
AdditionalBeanBuildItem.unremovableOf(ApplicationRoutes.class));
}

@BuildStep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,20 @@
*/
package org.apache.camel.quarkus.k.runtime;

import org.apache.camel.CamelContext;
import org.apache.camel.Route;
import org.apache.camel.impl.DefaultModelReifierFactory;
import org.apache.camel.model.RouteDefinition;

public class ApplicationModelReifierFactory extends DefaultModelReifierFactory {
@Override
public Route createRoute(CamelContext camelContext, Object routeDefinition) {
ApplicationRoutes routes = camelContext.getRegistry().findSingleByType(ApplicationRoutes.class);

if (routeDefinition instanceof RouteDefinition) {
routes.override((RouteDefinition) routeDefinition);
}

return super.createRoute(camelContext, routeDefinition);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.k.runtime;

import java.util.Objects;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.apache.camel.CamelContext;
import org.apache.camel.model.FromDefinition;
import org.apache.camel.model.RouteDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A bean to hold routes related logic.
*/
@ApplicationScoped
public class ApplicationRoutes {
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationRoutes.class);

@Inject
ApplicationRoutesConfig config;

@Inject
CamelContext context;

public ApplicationRoutesConfig config() {
return this.config;
}

public void override(RouteDefinition definition) {
if (config.overrides().isEmpty()) {
return;
}

final String id = definition.getRouteId();
final FromDefinition from = definition.getInput();

for (ApplicationRoutesConfig.RouteOverride override : config.overrides().get()) {
if (override.id().isEmpty() && override.input().from().isEmpty()) {
continue;
}
if (override.id().isPresent() && !Objects.equals(override.id().get(), id)) {
continue;
}
if (override.input().from().isPresent() && !Objects.equals(from.getEndpointUri(), override.input().from().get())) {
continue;
}

LOGGER.debug("Replace '{}' --> '{}' for route {}",
from.getEndpointUri(),
override.input().with(),
definition.getRouteId());

from.setUri(override.input().with());

break;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.k.runtime;

import java.util.List;
import java.util.Optional;

import io.smallrye.config.ConfigMapping;

@ConfigMapping(prefix = "camel.k.routes")
public interface ApplicationRoutesConfig {
/**
* A list of {@link RouteOverride} items to override some aspect of a {@link org.apache.camel.model.RouteDefinition}.
*/
Optional<List<RouteOverride>> overrides();

interface RouteOverride {
/**
* Identifies the route to be amended.
*/
Optional<String> id();

/**
* Override for the {@link org.apache.camel.model.FromDefinition} of a {@link org.apache.camel.model.RouteDefinition}.
*/
RouteInputOverride input();
}

interface RouteInputOverride {
/**
* The optional endpoint that should be replaced.
*/
Optional<String> from();

/**
* The value that should replace the endpoint.
*/
String with();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void testLoadCustomizersFallback() {
@Test
public void testLoadCustomizerOrder() {
DefaultCamelContext context = new DefaultCamelContext();
context.setName("camel");
context.getCamelContextExtension().setName("camel");
context.getRegistry().bind("c1", new CamelContextCustomizer() {
@Override
public int getOrder() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.test;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.jupiter.api.extension.ExtendWith;

/**
* Advertises that a test should be enabled if the JDK has FIPS enabled security providers present.
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ExtendWith(EnabledIfFipsModeCondition.class)
public @interface EnabledIfFipsMode {
/**
* The list of FIPS security provider names to match against for enabling the test.
* If no providers are specified, the default behaviour is to try to match any provider that has
* FIPS in its name.
*
* @return The list of security provider names.
*/
String[] providers() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.test;

import java.security.Provider;
import java.security.Security;
import java.util.List;

import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;

import static org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled;
import static org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled;
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation;

public class EnabledIfFipsModeCondition implements ExecutionCondition {
private static final ConditionEvaluationResult ENABLED_BY_DEFAULT = enabled("@EnabledIfFipsMode is not present");

@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
return findAnnotation(context.getElement(), EnabledIfFipsMode.class).map(this::map).orElse(ENABLED_BY_DEFAULT);
}

private ConditionEvaluationResult map(EnabledIfFipsMode annotation) {
List<String> providersToMatch = List.of(annotation.providers());
Provider[] jdkProviders = Security.getProviders();
int matchCount = 0;

for (Provider provider : jdkProviders) {
if (providersToMatch.isEmpty() && provider.getName().toLowerCase().contains("fips")) {
return enabled("Detected FIPS security provider " + provider.getName());
} else if (providersToMatch.contains(provider.getName())) {
matchCount++;
}
}

if (!providersToMatch.isEmpty() && matchCount == providersToMatch.size()) {
return enabled("Detected FIPS security providers");
}

return disabled("No FIPS security providers were detected");
}
}
Loading
Loading