Skip to content

Commit 8a759aa

Browse files
committed
Fixes Issues in encrypting and decrypting secrets in gw configurations
1 parent b177d9b commit 8a759aa

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIAdminImpl.java

+24-4
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ public List<Environment> getAllEnvironments(String tenantDomain) throws APIManag
161161
allEnvs.addAll(dynamicEnvs);
162162

163163
for (Environment env : allEnvs) {
164-
decryptGatewayConfigurationValues(env);
164+
if (env.getProvider().equalsIgnoreCase(APIConstants.EXTERNAL_GATEWAY_VENDOR)) {
165+
maskValues(env);
166+
}
165167
}
166168
return allEnvs;
167169
}
@@ -200,7 +202,7 @@ public Environment addEnvironment(String tenantDomain, Environment environment)
200202
validateForUniqueVhostNames(environment);
201203
Environment environmentToStore = new Environment(environment);
202204
encryptGatewayConfigurationValues(null, environmentToStore);
203-
apiMgtDAO.addEnvironment(tenantDomain, environment);
205+
apiMgtDAO.addEnvironment(tenantDomain, environmentToStore);
204206
return environment;
205207
}
206208

@@ -217,6 +219,23 @@ public void deleteEnvironment(String tenantDomain, String uuid) throws APIManage
217219
apiMgtDAO.deleteEnvironment(uuid);
218220
}
219221

222+
public Environment getEnvironmentWithoutPropertyMasking(String tenantDomain, String uuid) throws APIManagementException {
223+
// priority for configured environments over dynamic environments
224+
// name is the UUID of environments configured in api-manager.xml
225+
Environment env = APIUtil.getReadOnlyEnvironments().get(uuid);
226+
if (env == null) {
227+
env = apiMgtDAO.getEnvironment(tenantDomain, uuid);
228+
if (env == null) {
229+
String errorMessage = String.format("Failed to retrieve Environment with UUID %s. Environment not found",
230+
uuid);
231+
throw new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.from(
232+
ExceptionCodes.GATEWAY_ENVIRONMENT_NOT_FOUND, String.format("UUID '%s'", uuid))
233+
);
234+
}
235+
}
236+
return env;
237+
}
238+
220239
@Override
221240
public boolean hasExistingDeployments(String tenantDomain, String uuid) throws APIManagementException {
222241
Environment existingEnv = getEnvironment(tenantDomain, uuid);
@@ -228,7 +247,7 @@ public boolean hasExistingDeployments(String tenantDomain, String uuid) throws A
228247
@Override
229248
public Environment updateEnvironment(String tenantDomain, Environment environment) throws APIManagementException {
230249
// check if the VHost exists in the tenant domain with given UUID, throw error if not found
231-
Environment existingEnv = getEnvironment(tenantDomain, environment.getUuid());
250+
Environment existingEnv = getEnvironmentWithoutPropertyMasking(tenantDomain, environment.getUuid());
232251
if (existingEnv.isReadOnly()) {
233252
String errorMessage = String.format("Failed to update Environment with UUID '%s'. Environment is read only",
234253
environment.getUuid());
@@ -247,6 +266,7 @@ public Environment updateEnvironment(String tenantDomain, Environment environmen
247266

248267
validateForUniqueVhostNames(environment);
249268
environment.setId(existingEnv.getId());
269+
encryptGatewayConfigurationValues(existingEnv, environment);
250270
Environment updatedEnvironment = apiMgtDAO.updateEnvironment(environment);
251271
// If the update is successful without throwing an exception
252272
// Perform a separate task of updating gateway label names
@@ -849,7 +869,7 @@ private KeyManagerConfigurationDTO decryptKeyManagerConfigurationValues(
849869
return keyManagerConfigurationDTO;
850870
}
851871

852-
private Environment decryptGatewayConfigurationValues(Environment environment)
872+
public Environment decryptGatewayConfigurationValues(Environment environment)
853873
throws APIManagementException {
854874

855875
Map<String, String> additionalProperties = environment.getAdditionalProperties();

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/factory/GatewayHolder.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.wso2.carbon.apimgt.api.model.GatewayAgentConfiguration;
2727
import org.wso2.carbon.apimgt.api.model.GatewayConfiguration;
2828
import org.wso2.carbon.apimgt.api.model.GatewayDeployer;
29+
import org.wso2.carbon.apimgt.impl.APIAdminImpl;
2930
import org.wso2.carbon.apimgt.impl.APIConstants;
3031
import org.wso2.carbon.apimgt.impl.dto.GatewayDto;
3132
import org.wso2.carbon.apimgt.impl.dto.OrganizationGatewayDto;
@@ -97,12 +98,19 @@ public static GatewayDeployer getTenantGatewayInstance(String organization, Stri
9798
Map<String, Environment> environmentMap = APIUtil.getEnvironments(organization);
9899
Environment environment = environmentMap.get(gatewayName);
99100
if (environment != null) {
101+
// environment fetched from DB might have encrypted properties, hence need to decrypt before
102+
// initializing the deployer
103+
APIAdminImpl apiAdmin = new APIAdminImpl();
104+
Environment resolvedEnvironment = apiAdmin.getEnvironmentWithoutPropertyMasking(organization,
105+
environment.getUuid());
106+
resolvedEnvironment = apiAdmin.decryptGatewayConfigurationValues(resolvedEnvironment);
107+
100108
GatewayAgentConfiguration gatewayAgentConfiguration = ServiceReferenceHolder.getInstance().
101109
getExternalGatewayConnectorConfiguration(environment.getGatewayType());
102110
if (gatewayAgentConfiguration != null) {
103111
GatewayDeployer deployer = (GatewayDeployer) Class.forName(gatewayAgentConfiguration.getImplementation())
104112
.getDeclaredConstructor().newInstance();
105-
deployer.init(environment);
113+
deployer.init(resolvedEnvironment);
106114
return deployer;
107115

108116
}

0 commit comments

Comments
 (0)