Skip to content

Commit

Permalink
fixes networknt#1453 encryped apikey is not decripted automatically w…
Browse files Browse the repository at this point in the history
…hen json format is used (networknt#1562)
  • Loading branch information
stevehu authored Jan 5, 2023
1 parent 03b32a2 commit 090afd2
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 10 deletions.
5 changes: 4 additions & 1 deletion api-key/src/main/java/com/networknt/apikey/ApiKey.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.networknt.apikey;

import com.networknt.config.ConfigInjection;

public class ApiKey {
String pathPrefix;
String headerName;
Expand All @@ -26,6 +28,7 @@ public String getApiKey() {
}

public void setApiKey(String apiKey) {
this.apiKey = apiKey;
// if apiKey is encrypted, we need to decrypted it here.
this.apiKey = (String)ConfigInjection.decryptEnvValue(ConfigInjection.getDecryptor(), apiKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class ApiKeyJsonConfigTest {
static final ApiKeyConfig config = ApiKeyConfig.load("apikey-json");

@Test
@Ignore
public void testDecryption() {
logger.debug("apiKey for /test2 = " + config.getPathPrefixAuths().get(1).getApiKey());
Assert.assertEquals("password", config.getPathPrefixAuths().get(1).getApiKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ private void populateUsers(List<Map<String, Object>> userList) {
userList.forEach(user -> {
if (user instanceof Map) {
// the password might be encrypted.
user = DecryptUtil.decryptMap(user);
UserAuth userAuth = new UserAuth();
user.forEach((k, v) -> {
if (USERNAME.equals(k)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.networknt.basicauth;

import com.networknt.config.ConfigInjection;

import java.util.List;

public class UserAuth {
Expand Down Expand Up @@ -29,7 +31,7 @@ public String getPassword() {
}

public void setPassword(String password) {
this.password = password;
this.password = (String) ConfigInjection.decryptEnvValue(ConfigInjection.getDecryptor(), password);
}

public List<String> getPaths() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static boolean isExclusionConfigFile(String configName) {
|| exclusionConfigFileList.contains(configName);
}

static Decryptor getDecryptor() {
public static Decryptor getDecryptor() {
Config myConfig = Config.getInstance();
if (myConfig == null) {
throw new RuntimeException("Unable to retrieve the configuration.");
Expand All @@ -114,7 +114,7 @@ static String convertEnvVars(String input){
return input.replaceAll("[^A-Za-z0-9]", "_").toUpperCase();
}

static Object decryptEnvValue(Decryptor decryptor, String envVal) {
public static Object decryptEnvValue(Decryptor decryptor, String envVal) {
Object decryptedEnvValue;
//checking if the value put in env is encrypted. If yes then decrypting it.
if (envVal != null && envVal.trim().startsWith(Decryptor.CRYPT_PREFIX)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception {
}
if(exchange.isResponseStarted()) {
//we can't proxy a request that has already started, this is basically a server configuration error
logger.error("Cannot proxy a request that has already started.");
UndertowLogger.REQUEST_LOGGER.cannotProxyStartedRequest(exchange);
exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
exchange.endExchange();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.networknt.proxy;

import com.networknt.config.ConfigInjection;

/**
* This is an object that contains all the authentication info for each path prefix in the pathPrefixAuth config
* section. By making it a list of objects, we can support unlimited number of APIs with different authentication
Expand Down Expand Up @@ -50,7 +52,7 @@ public String getPassword() {
}

public void setPassword(String password) {
this.password = password;
this.password = (String) ConfigInjection.decryptEnvValue(ConfigInjection.getDecryptor(), password);
}

public String getClientId() {
Expand All @@ -66,7 +68,7 @@ public String getClientSecret() {
}

public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
this.clientSecret = (String) ConfigInjection.decryptEnvValue(ConfigInjection.getDecryptor(), clientSecret);
}

public String getResponseType() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.networknt.proxy.mras;

import com.fasterxml.jackson.core.type.TypeReference;
import com.networknt.config.Config;
import com.networknt.config.ConfigException;
import com.networknt.config.JsonMapper;
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
<version.maven-version>2.7</version.maven-version>
<version.nexus-staging-maven>1.6.8</version.nexus-staging-maven>
<version.maven-source>3.0.1</version.maven-source>
<version.maven-surefire>2.19.1</version.maven-surefire>
<version.maven-surefire>2.22.2</version.maven-surefire>
<version.maven-failsafe>2.22.0</version.maven-failsafe>
<version.jacoco>0.8.3</version.jacoco>
<version.maven-compiler>3.8.0</version.maven-compiler>
Expand Down

0 comments on commit 090afd2

Please sign in to comment.