forked from apache/camel-k-runtime
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to load generic secrets and reference entries from integrations a…
- Loading branch information
1 parent
bf98bff
commit a870cb9
Showing
8 changed files
with
188 additions
and
0 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
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
64 changes: 64 additions & 0 deletions
64
...runtime-core/src/main/java/org/apache/camel/k/listener/PropertiesFunctionsConfigurer.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,64 @@ | ||
/* | ||
* 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.k.listener; | ||
|
||
import org.apache.camel.k.Constants; | ||
import org.apache.camel.k.Runtime; | ||
import org.apache.camel.k.support.KubernetesPropertiesFunction; | ||
import org.apache.camel.spi.PropertiesComponent; | ||
|
||
public class PropertiesFunctionsConfigurer extends AbstractPhaseListener { | ||
public PropertiesFunctionsConfigurer() { | ||
super(Runtime.Phase.Starting); | ||
} | ||
|
||
@Override | ||
protected void accept(Runtime runtime) { | ||
// | ||
// Register properties functions to resolve k8s secrets or config maps like: | ||
// | ||
// {{secret:name/key}} | ||
// {{configmap:name/key}} | ||
// | ||
PropertiesComponent pc = runtime.getCamelContext().getPropertiesComponent(); | ||
if (pc instanceof org.apache.camel.component.properties.PropertiesComponent) { | ||
// | ||
// ConfigMap | ||
// | ||
String cmPath = System.getProperty(Constants.PROPERTY_CAMEL_K_MOUNT_PATH_CONFIGMAPS); | ||
if (cmPath == null) { | ||
cmPath = System.getenv(Constants.ENV_CAMEL_K_MOUNT_PATH_CONFIGMAPS); | ||
} | ||
|
||
((org.apache.camel.component.properties.PropertiesComponent)pc).addFunction( | ||
new KubernetesPropertiesFunction(cmPath, "configmap") | ||
); | ||
|
||
// | ||
// Secret | ||
// | ||
String secretPath = System.getProperty(Constants.PROPERTY_CAMEL_K_MOUNT_PATH_SECRETS); | ||
if (secretPath == null) { | ||
secretPath = System.getenv(Constants.ENV_CAMEL_K_MOUNT_PATH_SECRETS); | ||
} | ||
|
||
((org.apache.camel.component.properties.PropertiesComponent)pc).addFunction( | ||
new KubernetesPropertiesFunction(secretPath, "secret") | ||
); | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...k-runtime-core/src/main/java/org/apache/camel/k/support/KubernetesPropertiesFunction.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,66 @@ | ||
/* | ||
* 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.k.support; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import org.apache.camel.component.properties.PropertiesFunction; | ||
import org.apache.camel.util.StringHelper; | ||
|
||
public class KubernetesPropertiesFunction implements PropertiesFunction { | ||
private final String name; | ||
private final Path root; | ||
|
||
public KubernetesPropertiesFunction(String path, String name) { | ||
this.root = path != null ? Paths.get(path) : null; | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
@Override | ||
public String apply(String remainder) { | ||
if (this.root == null) { | ||
return remainder; | ||
} | ||
|
||
final String name = StringHelper.before(remainder, "/"); | ||
final String property = StringHelper.after(remainder, "/"); | ||
|
||
if (name == null || property == null) { | ||
return remainder; | ||
} | ||
|
||
Path file = this.root.resolve(name.toLowerCase()).resolve(property); | ||
if (Files.exists(file) && !Files.isDirectory(file)) { | ||
try { | ||
return new String(Files.readAllBytes(file), StandardCharsets.UTF_8); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
return remainder; | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
...ime-core/src/test/java/org/apache/camel/k/listener/PropertiesFunctionsConfigurerTest.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,47 @@ | ||
/* | ||
* 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.k.listener; | ||
|
||
import java.util.Properties; | ||
|
||
import org.apache.camel.CamelContext; | ||
import org.apache.camel.impl.DefaultCamelContext; | ||
import org.apache.camel.k.Runtime; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class PropertiesFunctionsConfigurerTest { | ||
@Test | ||
public void testConfigMapFunction() { | ||
Properties properties = new Properties(); | ||
properties.setProperty("my.property", "{{secret:my-secret/my-property}}"); | ||
|
||
CamelContext context = new DefaultCamelContext(); | ||
context.getPropertiesComponent().setInitialProperties(properties); | ||
|
||
new PropertiesFunctionsConfigurer().accept(Runtime.on(context)); | ||
|
||
assertThat(context.resolvePropertyPlaceholders("{{secret:my-secret/my-property}}")).isEqualTo("my-secret-property"); | ||
assertThat(context.resolvePropertyPlaceholders("{{secret:none/my-property}}")).isEqualTo("none/my-property"); | ||
|
||
assertThat(context.resolvePropertyPlaceholders("{{configmap:my-cm/my-property}}")).isEqualTo("my-cm-property"); | ||
assertThat(context.resolvePropertyPlaceholders("{{configmap:none/my-property}}")).isEqualTo("none/my-property"); | ||
|
||
assertThat(context.resolvePropertyPlaceholders("{{my.property}}")).isEqualTo("my-secret-property"); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
camel-k-runtime-core/src/test/resources/configmaps/my-cm/my-property
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 @@ | ||
my-cm-property |
1 change: 1 addition & 0 deletions
1
camel-k-runtime-core/src/test/resources/secrets/my-secret/my-property
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 @@ | ||
my-secret-property |