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

[WFLY-14984] Add support for encrypted expressions in the wildfly-config.xml #45

Merged
merged 1 commit into from
Mar 1, 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 @@ -26,6 +26,8 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
Expand Down Expand Up @@ -725,13 +727,61 @@ default Expression getExpressionAttributeValue(int index, Expression.Flag... fla
final String attributeValue = getAttributeValue(index);
if (attributeValue == null) {
return null;
} else if (attributeValue.startsWith("env.")) {
String envVar = System.getenv().get(attributeValue.substring(4));

if (envVar.contains("ENC:")) {
return resolveEncryptedExpression(envVar, flags);
} else {
return Expression.compile(envVar, flags);
}
} else if (attributeValue.contains("env") || attributeValue.contains("ENV")) {
String envVar = replaceNonAlphanumericByUnderscoresAndMakeUpperCase(attributeValue);

if (envVar.contains("ENC:")) {
return resolveEncryptedExpression(envVar, flags);
} else {
return Expression.compile(envVar, flags);
}
} else if (attributeValue.startsWith("prop.")) {
String propertyValue = System.getProperty(attributeValue.substring(5));

if (propertyValue.contains("ENC:")) {
return resolveEncryptedExpression(propertyValue, flags);
} else {
return Expression.compile(propertyValue, flags);
}
} else if (attributeValue.contains("prop") || attributeValue.contains("PROP")) {
String propertyValue = System.getProperty(replaceNonAlphanumericByUnderscoresAndMakeUpperCase(attributeValue));

if (propertyValue.contains("ENC:")) {
return resolveEncryptedExpression(propertyValue, flags);
} else {
return Expression.compile(propertyValue, flags);
}
} else if (attributeValue.contains("ENC:")) {
return resolveEncryptedExpression(attributeValue, flags);
} else try {
return Expression.compile(attributeValue, flags);
} catch (IllegalArgumentException ex) {
throw msg.expressionParseException(ex, getAttributeName(index), getLocation());
}
}

default Expression resolveEncryptedExpression(String attributeValue, Expression.Flag... flags) throws ConfigXMLParseException {
try {
Iterator<ResolverProvider> resolverProviderIterator = ServiceLoader.load(ResolverProvider.class, ConfigurationXMLStreamReader.class.getClassLoader()).iterator();
if (resolverProviderIterator.hasNext()) {
final ResolverProvider resolverProvider = resolverProviderIterator.next();
return Expression.compile(resolverProvider.resolveExpression(attributeValue), flags);
} else {
throw msg.failedToLoadResolver();
}
} catch (ServiceConfigurationError e) {
throw msg.failedToLoadUsingServiceLoader(ResolverProvider.class.getName());
}
}

/**
* Get an attribute value as a {@link InetAddress}.
*
Expand Down Expand Up @@ -811,4 +861,20 @@ default CidrAddress getCidrAddressAttributeValueResolved(int index) throws Confi
return cidrAddress;
}
}

default String replaceNonAlphanumericByUnderscoresAndMakeUpperCase(final String name) {
StringBuilder sb = new StringBuilder();
int c;
for (int i = 0; i < name.length(); i += Character.charCount(c)) {
c = Character.toUpperCase(name.codePointAt(i));
if ('A' <= c && c <= 'Z' || '0' <= c && c <= '9') {
sb.appendCodePoint(c);
} else if (c == '{' || c == '}' || c == '$') {
//ignore these characters
}else {
sb.append('_');
}
}
return sb.toString();
}
}
28 changes: 28 additions & 0 deletions src/main/java/org/wildfly/client/config/ResolverProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2024 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed 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.wildfly.client.config;

/**
* An interface that allows WildFly-Client-Config to use Functions from
* Encrypted Expression Resolver without adding an Elytron dependency.
* @author <a href="mailto:[email protected]">Prarthona Paul</a>
*/
public interface ResolverProvider {
String resolveExpression(String expression);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.ServiceConfigurationError;

import javax.xml.namespace.QName;
import javax.xml.stream.Location;
Expand Down Expand Up @@ -115,4 +116,10 @@ public interface ConfigMessages {

@Message(id = 24, value = "Failed to parse CIDR address value of attribute \"%s\": \"%s\" is not a valid CIDR address")
ConfigXMLParseException cidrAddressParseException(QName attributeName, String address, @Param(Location.class) XMLLocation location);

@Message(id = 25, value = "Failed to load a resolver for the encrypted expression.")
ConfigXMLParseException failedToLoadResolver();

@Message(id = 26, value = "Failed to load the \"%s\" class using a service loader.")
ServiceConfigurationError failedToLoadUsingServiceLoader(String className);
}