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

camel-jolokia-starter: Enables discovery on local environment #1289

Merged
merged 1 commit into from
Nov 20, 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 @@ -29,6 +29,7 @@ Moreover, it is possible to configure the Jolokia server accessing to the config
according to https://jolokia.org/reference/html/manual/agents.html#jvm-agent-installation[Jolokia agent configuration]
using `camel.component.jolokia.server-config` map configuration,
i.e. `camel.component.jolokia.server-config.port=8779` or `camel.component.jolokia.server-config.authMode=jaas`
The value of the `discoveryEnabled` configuration key is set to `true` on JVM local environment (not on Kubernetes), to allow the Hawtio process to discover the Jolokia agent. It is possible to disable the default configuration by setting `camel.component.jolokia.server-config.discoveryEnabled=false`.

To avoid to expose all the JMX MBeans (see https://jolokia.org/reference/html/manual/security.html[Security] considerations),
it is provided a default Jolokia https://jolokia.org/reference/html/manual/security.html#security-restrictor[Restrictor]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public SpringJolokiaConfigHolder camelConfigHolder() {
setDefaultConfigValue(ConfigKey.RESTRICTOR_CLASS.getKeyValue(),
"org.apache.camel.component.jolokia.springboot.restrictor.CamelRestrictor");
}
boolean k8sSet = false;
if (configuration.isKubernetesDiscover()) {
LOG.debug("trying to discover k8s environment");
final String caCert = configuration.isKubernetesUseDefaultCa() ? DEFAULT_CA_ON_K8S
Expand All @@ -97,10 +98,15 @@ public SpringJolokiaConfigHolder camelConfigHolder() {
setDefaultConfigValue("protocol", "https");
setDefaultConfigValue("useSslClientAuthentication", "true");
setDefaultConfigValue("caCert", caCert);
k8sSet = true;
} else {
LOG.debug("kubernetesDiscover is enabled but the file {} does not exist, no additional properties will be set", caCert);
}
}
//enable discovery on local JVM not in a POD
if (!k8sSet && !Files.exists(Path.of(DEFAULT_CA_ON_K8S))) {
setDefaultConfigValue("discoveryEnabled", "true");
}
springJolokiaConfigHolder.setConfig(configuration.getServerConfig());
return springJolokiaConfigHolder;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.component.jolokia.springboot;

import org.apache.camel.spring.boot.CamelAutoConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = {CamelAutoConfiguration.class, JolokiaComponentAutoConfiguration.class},
properties = {"camel.component.jolokia.serverConfig.port=0"
, "camel.component.jolokia.server-config.discoveryEnabled=false"})
public class JolokiaComponentAutoConfigurationDiscoveryDisabledTest extends JolokiaComponentTestBase {

@Test
void discoveryDisabledByPropertyTest() {
assertDiscoveryEnabled(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,7 @@ void sslConfigurationTest() {
.hasFieldOrPropertyWithValue("context", "/jolokia/")
.hasFieldOrPropertyWithValue("useSslClientAuthentication", true)
.hasFieldOrProperty("caCert").isNotNull();

assertDiscoveryEnabled(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ void defaultConfigurationTest() {
.asInstanceOf(InstanceOfAssertFactories.map(String.class, String.class))
.containsEntry("host", "0.0.0.0")
.containsEntry("autoStart", "true")
.containsEntry("restrictorClass", CamelRestrictor.class.getCanonicalName());
.containsEntry("restrictorClass", CamelRestrictor.class.getCanonicalName())
.containsEntry("discoveryEnabled", "true");

Assertions.assertThat(agent).as("check default agent/server configuration")
.hasFieldOrPropertyWithValue("lookupConfig", false)
Expand All @@ -89,5 +90,7 @@ void defaultConfigurationTest() {
.extracting("config")
.hasFieldOrPropertyWithValue("protocol", "http")
.hasFieldOrPropertyWithValue("context", "/jolokia/");

assertDiscoveryEnabled(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

import org.junit.jupiter.api.BeforeEach;

import org.assertj.core.api.Assertions;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.jolokia.server.core.config.ConfigKey;
import org.jolokia.support.spring.SpringJolokiaAgent;
import org.springframework.beans.factory.annotation.Autowired;

Expand All @@ -32,4 +35,15 @@ class JolokiaComponentTestBase {
void checkAgentIsAutoWired() {
assertThat(agent).isNotNull();
}

protected void assertDiscoveryEnabled(boolean value) {
Assertions.assertThat(agent).as("check Discovery feature configuration")
.extracting("serviceManager")
.extracting("configuration")
.extracting("configMap")
.asInstanceOf(InstanceOfAssertFactories.map(ConfigKey.class, String.class))
.extractingByKey(ConfigKey.DISCOVERY_ENABLED)
.isNotNull()
.isEqualTo(String.valueOf(value));
}
}