use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.
the class APIControllerUtil method handleEndpointSecurityConfigs.
/**
* This method will be used to add Endpoint security related environment parameters to imported Api object.
*
* @param envParams Env params object with required parameters
* @param endpointConfig Endpoint config object to be updated
* @throws APIManagementException If an error occurs when setting security env parameters
*/
private static void handleEndpointSecurityConfigs(JsonObject envParams, JsonObject endpointConfig) throws APIManagementException {
// If the user has set (either true or false) the enabled field under security in the params file,
// the following code should be executed.
JsonObject security = envParams.getAsJsonObject(ImportExportConstants.ENDPOINT_SECURITY_FIELD);
if (security == null) {
return;
}
String[] endpointTypes = { APIConstants.ENDPOINT_SECURITY_PRODUCTION, APIConstants.ENDPOINT_SECURITY_SANDBOX };
for (String endpointType : endpointTypes) {
if (security.has(endpointType)) {
JsonObject endpointSecurityDetails = security.get(endpointType).getAsJsonObject();
if (endpointSecurityDetails.has(APIConstants.ENDPOINT_SECURITY_ENABLED) && (endpointSecurityDetails.get(APIConstants.ENDPOINT_SECURITY_ENABLED) != null || !endpointSecurityDetails.get(APIConstants.ENDPOINT_SECURITY_ENABLED).isJsonNull())) {
boolean securityEnabled = endpointSecurityDetails.get(APIConstants.ENDPOINT_SECURITY_ENABLED).getAsBoolean();
// Set endpoint security details to API
if (securityEnabled) {
String endpointSecurityType;
if (endpointSecurityDetails.has(APIConstants.ENDPOINT_SECURITY_TYPE) && (endpointSecurityDetails.get(APIConstants.ENDPOINT_SECURITY_TYPE) != null || !endpointSecurityDetails.get(APIConstants.ENDPOINT_SECURITY_TYPE).isJsonNull())) {
// Check whether the type is defined in the params file
JsonElement type = endpointSecurityDetails.get(APIConstants.ENDPOINT_SECURITY_TYPE);
endpointSecurityType = type.getAsString();
} else {
throw new APIManagementException("You have enabled endpoint security but the type is not found " + "in the params file. Please specify type field and continue...", ExceptionCodes.ERROR_READING_PARAMS_FILE);
}
// Setup security type (basic, digest or oauth)
endpointSecurityDetails.remove(APIConstants.ENDPOINT_SECURITY_TYPE);
if (StringUtils.equals(endpointSecurityType.toLowerCase(), APIConstants.ENDPOINT_SECURITY_TYPE_DIGEST)) {
endpointSecurityDetails.addProperty(APIConstants.ENDPOINT_SECURITY_TYPE, APIConstants.ENDPOINT_SECURITY_TYPE_DIGEST.toUpperCase());
validateEndpointSecurityUsernamePassword(endpointSecurityDetails);
} else if (StringUtils.equals(endpointSecurityType.toLowerCase(), APIConstants.ENDPOINT_SECURITY_TYPE_BASIC)) {
endpointSecurityDetails.addProperty(APIConstants.ENDPOINT_SECURITY_TYPE, APIConstants.ENDPOINT_SECURITY_TYPE_BASIC.toUpperCase());
validateEndpointSecurityUsernamePassword(endpointSecurityDetails);
} else if (StringUtils.equals(endpointSecurityType.toLowerCase(), APIConstants.ENDPOINT_SECURITY_TYPE_OAUTH)) {
endpointSecurityDetails.addProperty(APIConstants.ENDPOINT_SECURITY_TYPE, APIConstants.ENDPOINT_SECURITY_TYPE_OAUTH.toUpperCase());
validateEndpointSecurityOauth(endpointSecurityDetails);
} else {
// If the type is not either basic or digest, return an error
throw new APIManagementException("Invalid endpoint security type found in the params file. " + "Should be either basic, digest or oauth. " + "Please specify correct security types field and continue...", ExceptionCodes.ERROR_READING_PARAMS_FILE);
}
} else {
endpointSecurityDetails.addProperty(APIConstants.ENDPOINT_SECURITY_TYPE, ImportExportConstants.ENDPOINT_NONE_SECURITY_TYPE);
}
}
} else {
// Even though the security field is defined, if either production/sandbox is not defined
// under that,set endpoint security to none. Otherwise the security will be blank if you
// check from the UI.
JsonObject endpointSecurityForNotDefinedEndpointType = new JsonObject();
endpointSecurityForNotDefinedEndpointType.addProperty(APIConstants.ENDPOINT_SECURITY_TYPE, ImportExportConstants.ENDPOINT_NONE_SECURITY_TYPE);
endpointSecurityForNotDefinedEndpointType.addProperty(APIConstants.ENDPOINT_SECURITY_ENABLED, Boolean.FALSE);
security.add(endpointType, endpointSecurityForNotDefinedEndpointType);
}
}
endpointConfig.add(APIConstants.ENDPOINT_SECURITY, security);
}
use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.
the class APIControllerUtil method handleEndpointValues.
/**
* This method will production and sandbox endpoint values.
*
* @param endpointConfigs Endpoint configurations to be updated
* @param updatedEndpointParams Updated endpoint parameters object
* @param defaultProductionEndpoint Default production endpoint json object
* @param defaultSandboxEndpoint Default sandbox endpoint json object
*/
private static void handleEndpointValues(JsonObject endpointConfigs, JsonObject updatedEndpointParams, JsonObject defaultProductionEndpoint, JsonObject defaultSandboxEndpoint) throws APIManagementException {
// check api params file to get provided endpoints
if (endpointConfigs == null) {
updatedEndpointParams.add(ImportExportConstants.PRODUCTION_ENDPOINTS_PROPERTY, defaultProductionEndpoint);
updatedEndpointParams.add(ImportExportConstants.SANDBOX_ENDPOINTS_PROPERTY, defaultSandboxEndpoint);
} else {
// handle production endpoints
if (endpointConfigs.get(ImportExportConstants.PRODUCTION_ENDPOINTS_JSON_PROPERTY) != null) {
updatedEndpointParams.add(ImportExportConstants.PRODUCTION_ENDPOINTS_PROPERTY, endpointConfigs.get(ImportExportConstants.PRODUCTION_ENDPOINTS_JSON_PROPERTY));
}
// handle sandbox endpoints
if (endpointConfigs.get(ImportExportConstants.SANDBOX_ENDPOINTS_JSON_PROPERTY) != null) {
updatedEndpointParams.add(ImportExportConstants.SANDBOX_ENDPOINTS_PROPERTY, endpointConfigs.get(ImportExportConstants.SANDBOX_ENDPOINTS_JSON_PROPERTY));
}
if (updatedEndpointParams.get(ImportExportConstants.SANDBOX_ENDPOINTS_PROPERTY) == null && updatedEndpointParams.get(ImportExportConstants.PRODUCTION_ENDPOINTS_PROPERTY) == null) {
throw new APIManagementException("Please specify production sandbox or endpoints for the environment and continue...", ExceptionCodes.ERROR_READING_PARAMS_FILE);
} else if ((updatedEndpointParams.get(ImportExportConstants.SANDBOX_ENDPOINTS_PROPERTY) != null) && (updatedEndpointParams.get(ImportExportConstants.SANDBOX_ENDPOINTS_PROPERTY).isJsonNull())) {
if ((updatedEndpointParams.get(ImportExportConstants.PRODUCTION_ENDPOINTS_PROPERTY) != null) && updatedEndpointParams.get(ImportExportConstants.PRODUCTION_ENDPOINTS_PROPERTY).isJsonNull()) {
throw new APIManagementException("Please specify production or sandbox endpoints for the environment and continue...", ExceptionCodes.ERROR_READING_PARAMS_FILE);
}
}
}
}
use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.
the class EnvironmentMappingUtil method fromEnvironmentCollectionToDTO.
/**
* Converts a List object of SubscribedAPIs into a DTO.
*
* @param environmentCollection a collection of Environment objects
* @return EnvironmentListDTO object containing EnvironmentDTOs
*/
public static EnvironmentListDTO fromEnvironmentCollectionToDTO(Collection<Environment> environmentCollection) {
EnvironmentListDTO environmentListDTO = new EnvironmentListDTO();
List<EnvironmentDTO> environmentDTOs = environmentListDTO.getList();
if (environmentDTOs == null) {
environmentDTOs = new ArrayList<>();
environmentListDTO.setList(environmentDTOs);
}
for (Environment environment : environmentCollection) {
environmentDTOs.add(fromEnvironmentToDTO(environment));
}
environmentListDTO.setCount(environmentDTOs.size());
return environmentListDTO;
}
use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.
the class APITemplateBuilderImpl method getConfigStringForPrototypeScriptAPI.
@Override
public String getConfigStringForPrototypeScriptAPI(Environment environment) throws APITemplateException {
StringWriter writer = new StringWriter();
try {
// build the context for template and apply the necessary decorators
ConfigContext configcontext = new APIConfigContext(this.api);
configcontext = new TransportConfigContext(configcontext, api);
configcontext = new ResourceConfigContext(configcontext, api);
configcontext = new EndpointBckConfigContext(configcontext, api);
configcontext = new EndpointConfigContext(configcontext, api);
configcontext = new SecurityConfigContext(configcontext, api);
configcontext = new JwtConfigContext(configcontext);
configcontext = new ResponseCacheConfigContext(configcontext, api);
configcontext = new HandlerConfigContex(configcontext, handlers);
configcontext = new EnvironmentConfigContext(configcontext, environment);
configcontext = new TemplateUtilContext(configcontext);
// @todo: this validation might be better to do when the builder is initialized.
configcontext.validate();
VelocityContext context = configcontext.getContext();
context.internalGetKeys();
/* first, initialize velocity engine */
VelocityEngine velocityengine = new VelocityEngine();
APIUtil.initializeVelocityContext(velocityengine);
velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
initVelocityEngine(velocityengine);
Template t = velocityengine.getTemplate(this.getPrototypeTemplatePath());
t.merge(context, writer);
} catch (Exception e) {
log.error("Velocity Error", e);
throw new APITemplateException("Velocity Error", e);
}
return writer.toString();
}
use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.
the class WebSubConfigContextTest method testWithSecretConfigContextForAPI.
@Test
public void testWithSecretConfigContextForAPI() throws Exception {
API api = new API(new APIIdentifier("admin", "websubAPI", "1.0.0"));
api.setStatus(APIConstants.CREATED);
api.setContextTemplate("/websub");
api.setTransports(Constants.TRANSPORT_HTTP);
api.setEndpointSecured(false);
api.setUriTemplates(setAPIUriTemplates());
api.setType(APIConstants.APITransportType.WEBSUB.toString());
WebsubSubscriptionConfiguration webSubConfig = new WebsubSubscriptionConfiguration(true, "9207975e1fef9c41fab41645f81dbf0f", "SHA1", "x-hub-signature");
api.setWebsubSubscriptionConfiguration(webSubConfig);
Environment environment = new Environment();
environment.setType("production");
config = Mockito.mock(APIManagerConfiguration.class);
APIManagerConfigurationService apiManagerConfigurationService = new APIManagerConfigurationServiceImpl(config);
ServiceReferenceHolder.getInstance().setAPIManagerConfigurationService(apiManagerConfigurationService);
String templatePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator;
System.setProperty("carbon.home", templatePath);
APITemplateBuilderImpl apiTemplateBuilder = new APITemplateBuilderImpl(api, null, null);
String updatedTemplate = apiTemplateBuilder.getConfigStringForTemplate(environment);
Assert.assertTrue("The websub velocity template is not updated correctly", updatedTemplate.contains("generated_signature"));
}
Aggregations