Skip to content

Commit

Permalink
Jasypt dev UI
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnetherton committed Feb 5, 2024
1 parent 2460645 commit f60522c
Show file tree
Hide file tree
Showing 18 changed files with 445 additions and 154 deletions.
5 changes: 4 additions & 1 deletion docs/modules/ROOT/pages/reference/extensions/jasypt.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ jbang org.apache.camel:camel-jasypt:{camel-version} -c encrypt -p secret-passwor
IMPORTANT: If you choose to use a different Jasypt algorithm to the default (`PBEWithMD5AndDES`), you must provide `-a` (algorithm), `-riga` (IV generator algorithm) & `-rsga` (Salt generator algorithm)
arguments to set the correct algorithms used in encryption. Else your application will not be able to decrypt configuration values.

Alternatively, when running in dev mode, open the https://quarkus.io/guides/dev-mode-differences#dev-ui[Dev UI] and click the 'utilities' link in the Camel Jasypt pane.
Next, select either the 'Decrypt' or 'Encrypt' action, enter some text and click the submit button. The result of the action is output together with a button to copy it to the clipboard.

Configuration properties can be added to `application.properties` with the encrypted value enclosed within `ENC()` For example.

[source]
Expand Down Expand Up @@ -196,7 +199,7 @@ you must add the file to `quarkus.native.resources.includes` so that it can be l
| Configuration property | Type | Default


| [[quarkus.camel.jasypt.enabled]]`link:#quarkus.camel.jasypt.enabled[quarkus.camel.jasypt.enabled]`
|icon:lock[title=Fixed at build time] [[quarkus.camel.jasypt.enabled]]`link:#quarkus.camel.jasypt.enabled[quarkus.camel.jasypt.enabled]`

Setting this option to false will disable Jasypt integration with Quarkus SmallRye configuration. You can however, manually configure Jasypt with Camel in the 'classic' way of manually configuring JasyptPropertiesParser and PropertiesComponent. Refer to the usage section for more details.
| `boolean`
Expand Down
10 changes: 10 additions & 0 deletions extensions/jasypt/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-http-dev-ui-tests</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-http</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-direct</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import org.apache.camel.quarkus.component.jasypt.CamelJasyptBuildTimeConfig;
import org.apache.camel.quarkus.component.jasypt.CamelJasyptConfig;
import org.apache.camel.quarkus.component.jasypt.CamelJasyptRecorder;
import org.apache.camel.quarkus.component.jasypt.JasyptConfigurationCustomizer;
Expand Down Expand Up @@ -65,7 +66,7 @@ void disableCamelMainAutoConfigFromSysEnv(
@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep
RuntimeCamelContextCustomizerBuildItem propertiesComponentRuntimeCamelContextCustomizer(
CamelJasyptConfig config,
CamelJasyptBuildTimeConfig config,
CamelJasyptRecorder recorder) {
return new RuntimeCamelContextCustomizerBuildItem(recorder.createPropertiesComponentCamelContextCustomizer(config));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.component.jasypt.deployment.devui;

import java.util.function.BooleanSupplier;

import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
import io.quarkus.devui.spi.JsonRPCProvidersBuildItem;
import io.quarkus.devui.spi.page.CardPageBuildItem;
import io.quarkus.devui.spi.page.Page;
import org.apache.camel.quarkus.component.jasypt.CamelJasyptBuildTimeConfig;
import org.apache.camel.quarkus.component.jasypt.CamelJasyptDevUIService;

@BuildSteps(onlyIf = { IsDevelopment.class, JasyptUtilsDevUIProcessor.CamelJasyptEnabled.class })
public class JasyptUtilsDevUIProcessor {
@BuildStep
CardPageBuildItem create() {
CardPageBuildItem card = new CardPageBuildItem();
card.addPage(Page.webComponentPageBuilder()
.title("Utilities")
.componentLink("qwc-camel-jasypt-utils.js")
.icon("font-awesome-solid:house-lock"));
return card;
}

@BuildStep
JsonRPCProvidersBuildItem createJsonRPCServiceForCache() {
return new JsonRPCProvidersBuildItem(CamelJasyptDevUIService.class);
}

static final class CamelJasyptEnabled implements BooleanSupplier {
CamelJasyptBuildTimeConfig config;

@Override
public boolean getAsBoolean() {
return config.enabled;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* 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.
*/
import {LitElement, css, html} from 'lit';
import {JsonRpc} from 'jsonrpc';
import { notifier } from 'notifier';
import '@vaadin/icon';
import '@vaadin/button';
import '@vaadin/text-field';
import '@vaadin/select';

export class QwcCamelJasyptUtils extends LitElement {

jsonRpc = new JsonRpc(this);

static styles = css`
.container {
margin-left: 10px;
}
.result-heading {
text-transform: capitalize;
}
`;

static properties = {
_text: {type: String},
_result: {type: String},
_actions: {type: Array}
};

constructor() {
super();

this._text = "";
this._result = "";
this._action = "Encrypt";
this._actions = [
{
label: "Encrypt",
value: "encrypt"
},
{
label: "Decrypt",
value: "decrypt"
},
];
}

render() {
let results;
if (this._result.length > 0) {
results = html`
<div>
<p class="result-heading">${this._action}ed result:</p>
<p><code id="result">${this._result}</code></p>
<vaadin-button
@click="${(e) => this._copyToClipboard(e, 'Copy')}"
theme="small">
<vaadin-icon icon="font-awesome-solid:clipboard"></vaadin-icon>
Copy
</vaadin-button>
</div>
`;
}

return html`
<div class="container">
<p>A Camel Jasypt utility to encrypt or decrypt a configuration property value.</p>
<p>Jasypt is configured from <code>quarkus.camel.jasypt</code> properties in <code>application.properties</code>. Refer to the Camel Quarkus Jasypt extension documentation for details.</p>
<div>
<vaadin-select
label="Jasypt Action"
.items="${this._actions}"
.value="${this._actions[0].value}"
@value-changed="${(e) => this._selectAction(e.target.value)}" />
</div>
<div>
<vaadin-text-field
style="width: 50%;"
label="Property value to encrypt / decrypt"
.value="${this._text}"
@value-changed="${(e) => this._textChanged(e)}" />
</div>
<div>
<vaadin-button
@click="${this._doAction}"
?disabled="${this._text.trim().length === 0}">
Submit
</vaadin-button>
</div>
${results}
</div>
`;
}

_doAction() {
const successHandler = (jsonRpcResponse) => {
this._result = jsonRpcResponse.result;
this._text = '';
};

const failureHandler = () => {
this._result = '';
notifier.showErrorMessage("Failed " + this._action + "ing the property value", 'top-start');
}

if (this._action === 'encrypt') {
this.jsonRpc.encryptText({'text': this._text}).then(successHandler, failureHandler);
} else if (this._action === 'decrypt') {
this.jsonRpc.decryptText({'text': this._text}).then(successHandler, failureHandler);
}
}

_textChanged(e) {
this._text = e.detail.value.trim();
if (this._result.length > 0 && this._text.length > 0) {
this._result = '';
}
}

_selectAction(e) {
this._action = e;
this._text = '';
this._result = '';
}

_copyToClipboard(e) {
e.stopPropagation();
const text = this.shadowRoot.querySelector("#result").textContent;
const listener = function(event) {
event.clipboardData.setData("text/plain", text);
event.preventDefault();
};
document.addEventListener("copy", listener);
document.execCommand("copy");
document.removeEventListener("copy", listener);
notifier.showInfoMessage("Text copied successfully", 'top-start');
}
}

customElements.define('qwc-camel-jasypt-utils', QwcCamelJasyptUtils);
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.component.jasypt;

import java.util.Map;

import io.quarkus.devui.tests.DevUIJsonRPCTest;
import io.quarkus.test.QuarkusDevModeTest;
import org.apache.camel.util.ObjectHelper;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class JasyptDevUITest extends DevUIJsonRPCTest {
@RegisterExtension
static final QuarkusDevModeTest CONFIG = new QuarkusDevModeTest().withEmptyApplication();

@BeforeAll
public static void beforeAll() {
System.setProperty("quarkus.camel.jasypt.password", "test");
}

@AfterAll
public static void afterAll() {
System.clearProperty("quarkus.camel.jasypt.password");
}

public JasyptDevUITest() {
super("org.apache.camel.quarkus.camel-quarkus-jasypt");
}

@Test
void devUIActions() throws Exception {
String message = "Hello World";
String encrypted = super.executeJsonRPCMethod(String.class, "encryptText", Map.of("text", message));
assertTrue(ObjectHelper.isNotEmpty(encrypted) && !encrypted.equals(message));

String decrypted = super.executeJsonRPCMethod(String.class, "decryptText", Map.of("text", encrypted));
assertEquals(message, decrypted);
}
}
Loading

0 comments on commit f60522c

Please sign in to comment.