Search in sources :

Example 96 with Property

use of org.wso2.carbon.identity.application.common.model.Property in project product-iots by wso2.

the class ZipUtil method getSketchArchive.

/**
 * Get agent sketch.
 *
 * @param archivesPath       Path of the zip file to create.
 * @param templateSketchPath Path of the sketch.
 * @param contextParams      Map of parameters to be included in the zip file.
 * @param zipFileName        Name of the zip file.
 * @return Created zip archive.
 * @throws DeviceManagementException
 * @throws IOException
 */
public static ZipArchive getSketchArchive(String archivesPath, String templateSketchPath, Map contextParams, String zipFileName) throws DeviceManagementException, IOException {
    String sketchPath = CarbonUtils.getCarbonHome() + File.separator + templateSketchPath;
    // clear directory
    FileUtils.deleteDirectory(new File(archivesPath));
    // clear zip
    FileUtils.deleteDirectory(new File(archivesPath + ".zip"));
    if (!new File(archivesPath).mkdirs()) {
        // new dir
        String message = "Could not create directory at path: " + archivesPath;
        throw new DeviceManagementException(message);
    }
    zipFileName = zipFileName + ".zip";
    try {
        Map<String, List<String>> properties = getProperties(sketchPath + File.separator + "sketch" + ".properties");
        List<String> templateFiles = properties.get("templates");
        for (String templateFile : templateFiles) {
            parseTemplate(templateSketchPath + File.separator + templateFile, archivesPath + File.separator + templateFile, contextParams);
        }
        // ommit copying the props file
        templateFiles.add("sketch.properties");
        copyFolder(new File(sketchPath), new File(archivesPath), templateFiles);
        createZipArchive(archivesPath);
        FileUtils.deleteDirectory(new File(archivesPath));
        File zip = new File(archivesPath + ".zip");
        return new ZipArchive(zipFileName, zip);
    } catch (IOException ex) {
        throw new DeviceManagementException("Error occurred when trying to read property " + "file sketch.properties", ex);
    }
}
Also used : DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) File(java.io.File)

Example 97 with Property

use of org.wso2.carbon.identity.application.common.model.Property in project ballerina by ballerina-lang.

the class HttpUtil method getProperty.

public static BValue[] getProperty(Context context, boolean isRequest) {
    BStruct httpMessageStruct = (BStruct) context.getRefArgument(0);
    HTTPCarbonMessage httpCarbonMessage = HttpUtil.getCarbonMsg(httpMessageStruct, HttpUtil.createHttpCarbonMessage(isRequest));
    String propertyName = context.getStringArgument(0);
    Object propertyValue = httpCarbonMessage.getProperty(propertyName);
    if (propertyValue == null) {
        return new BValue[0];
    }
    if (propertyValue instanceof String) {
        return new BValue[] { new BString((String) propertyValue) };
    } else {
        throw new BallerinaException("Property value is of unknown type : " + propertyValue.getClass().getName());
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BString(org.ballerinalang.model.values.BString) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 98 with Property

use of org.wso2.carbon.identity.application.common.model.Property in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method updateAuditApi.

/**
 * Update API Definition before retrieving Security Audit Report
 * @param apiDefinition API Definition of API
 * @param apiToken API Token to access Security Audit
 * @param auditUuid Respective UUID of API in Security Audit
 * @param baseUrl Base URL to communicate with Security Audit
 * @param isDebugEnabled Boolean whether debug is enabled
 * @throws IOException In the event of any problems with the request
 * @throws APIManagementException In the event of unexpected response
 */
private void updateAuditApi(String apiDefinition, String apiToken, String auditUuid, String baseUrl, boolean isDebugEnabled) throws IOException, APIManagementException {
    // Set the property to be attached in the body of the request
    // Attach API Definition to property called specfile to be sent in the request
    JSONObject jsonBody = new JSONObject();
    jsonBody.put("specfile", Base64Utils.encode(apiDefinition.getBytes(StandardCharsets.UTF_8)));
    // Logic for HTTP Request
    String putUrl = baseUrl + "/" + auditUuid;
    URL updateApiUrl = new URL(putUrl);
    try (CloseableHttpClient httpClient = (CloseableHttpClient) APIUtil.getHttpClient(updateApiUrl.getPort(), updateApiUrl.getProtocol())) {
        HttpPut httpPut = new HttpPut(putUrl);
        // Set the header properties of the request
        httpPut.setHeader(APIConstants.HEADER_ACCEPT, APIConstants.APPLICATION_JSON_MEDIA_TYPE);
        httpPut.setHeader(APIConstants.HEADER_CONTENT_TYPE, APIConstants.APPLICATION_JSON_MEDIA_TYPE);
        httpPut.setHeader(APIConstants.HEADER_API_TOKEN, apiToken);
        httpPut.setHeader(APIConstants.HEADER_USER_AGENT, APIConstants.USER_AGENT_APIM);
        httpPut.setEntity(new StringEntity(jsonBody.toJSONString()));
        // Code block for processing the response
        try (CloseableHttpResponse response = httpClient.execute(httpPut)) {
            if (isDebugEnabled) {
                log.debug("HTTP status " + response.getStatusLine().getStatusCode());
            }
            if (!(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)) {
                throw new APIManagementException("Error while sending data to the API Security Audit Feature. Found http status " + response.getStatusLine());
            }
        } finally {
            httpPut.releaseConnection();
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) StringEntity(org.apache.http.entity.StringEntity) JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) URL(java.net.URL) HttpPut(org.apache.http.client.methods.HttpPut)

Example 99 with Property

use of org.wso2.carbon.identity.application.common.model.Property in project carbon-apimgt by wso2.

the class SecurityConfigContextTest method testSecurityConfigContextOauth.

@Test
public void testSecurityConfigContextOauth() throws Exception {
    String json = "{\"endpoint_security\":{\n" + "  \"production\":{\n" + "    \"enabled\":true,\n" + "    \"type\":\"oauth\",\n" + "    \"clientId\":\"123-456\",\n" + "    \"clientSecret\":\"admin\",\n" + "    \"grantType\":\"client_credentials\"\n" + "  },\n" + "  \"sandbox\":{\n" + "    \"enabled\":true,\n" + "    \"type\":\"oauth\",\n" + "    \"clientId\":\"123-4567\",\n" + "    \"clientSecret\":\"admin\",\n" + "    \"grantType\":\"client_credentials\"\n" + "  }\n" + "  }\n" + "}";
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setUuid(UUID.randomUUID().toString());
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    api.setTransports(Constants.TRANSPORT_HTTP);
    api.setEndpointConfig(json);
    ConfigContext configcontext = new APIConfigContext(api);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
    SecurityConfigContext securityConfigContext = new SecurityConfigContextWrapper(configcontext, api, apiManagerConfiguration);
    securityConfigContext.validate();
    VelocityContext velocityContext = securityConfigContext.getContext();
    Assert.assertNotNull(velocityContext.get("endpoint_security"));
    Map<String, EndpointSecurityModel> endpointSecurityModelMap = (Map<String, EndpointSecurityModel>) velocityContext.get("endpoint_security");
    EndpointSecurityModel production = endpointSecurityModelMap.get("production");
    Assert.assertTrue("Property enabled cannot be false.", production.isEnabled());
    Assert.assertTrue("Property type cannot be other.", production.getType().equalsIgnoreCase("oauth"));
    Assert.assertTrue("Property clientid does not match.", "123-456".equals(production.getClientId()));
    Assert.assertEquals(production.getClientSecretAlias(), "TestAPI--v1.0.0--oauth--clientSecret--production");
    EndpointSecurityModel sandbox = endpointSecurityModelMap.get("sandbox");
    Assert.assertTrue("Property enabled cannot be false.", sandbox.isEnabled());
    Assert.assertTrue("Property type cannot be other.", sandbox.getType().equalsIgnoreCase("oauth"));
    Assert.assertTrue("Property username does not match.", "123-4567".equals(sandbox.getClientId()));
    Assert.assertEquals(sandbox.getClientSecretAlias(), "TestAPI--v1.0.0--oauth--clientSecret--sandbox");
    Assert.assertTrue("Property isSecureVaultEnabled cannot be false. ", velocityContext.get("isSecureVaultEnabled").equals(true));
}
Also used : SecurityConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.SecurityConfigContext) VelocityContext(org.apache.velocity.VelocityContext) EndpointSecurityModel(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EndpointSecurityModel) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) HashMap(java.util.HashMap) Map(java.util.Map) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) ConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext) SecurityConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.SecurityConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) Test(org.junit.Test)

Example 100 with Property

use of org.wso2.carbon.identity.application.common.model.Property in project carbon-apimgt by wso2.

the class SecurityConfigContextTest method testSecurityConfigContextPerEndpointSandbox.

@Test
public void testSecurityConfigContextPerEndpointSandbox() throws Exception {
    String json = "{\"endpoint_security\":{\n" + "  \"sandbox\":{\n" + "    \"enabled\":true,\n" + "    \"type\":\"DIGEST\",\n" + "    \"username\":\"admin\",\n" + "    \"password\":\"admin123#QA\"\n" + "  }\n" + "  }\n" + "}";
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    api.setTransports(Constants.TRANSPORT_HTTP);
    api.setEndpointConfig(json);
    ConfigContext configcontext = new APIConfigContext(api);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_SECUREVAULT_ENABLE)).thenReturn("true");
    SecurityConfigContext securityConfigContext = new SecurityConfigContextWrapper(configcontext, api, apiManagerConfiguration);
    securityConfigContext.validate();
    VelocityContext velocityContext = securityConfigContext.getContext();
    Assert.assertNotNull(velocityContext.get("endpoint_security"));
    Map<String, EndpointSecurityModel> endpointSecurityModelMap = (Map<String, EndpointSecurityModel>) velocityContext.get("endpoint_security");
    EndpointSecurityModel sandbox = endpointSecurityModelMap.get("sandbox");
    Assert.assertTrue("Property enabled cannot be false.", sandbox.isEnabled());
    Assert.assertTrue("Property type cannot be other.", sandbox.getType().equalsIgnoreCase("digest"));
    Assert.assertTrue("Property username does not match.", "admin".equals(sandbox.getUsername()));
    Assert.assertTrue("Property base64value does not match. ", new String(Base64.encodeBase64("admin:admin123#QA".getBytes())).equalsIgnoreCase(sandbox.getBase64EncodedPassword()));
    Assert.assertTrue("Property securevault_alias does not match.", "TestAPI--v1.0.0--sandbox".equalsIgnoreCase(sandbox.getAlias()));
    Assert.assertTrue("Property isSecureVaultEnabled cannot be false. ", velocityContext.get("isSecureVaultEnabled").equals(true));
    EndpointSecurityModel production = endpointSecurityModelMap.get("production");
    Assert.assertFalse("Property enabled cannot be true.", production.isEnabled());
}
Also used : SecurityConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.SecurityConfigContext) VelocityContext(org.apache.velocity.VelocityContext) EndpointSecurityModel(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.EndpointSecurityModel) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) HashMap(java.util.HashMap) Map(java.util.Map) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) ConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext) SecurityConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.SecurityConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)42 ArrayList (java.util.ArrayList)32 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)32 Resource (org.wso2.carbon.registry.core.Resource)23 Map (java.util.Map)21 Test (org.junit.Test)21 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)21 DataResponse (org.wso2.carbon.bpmn.rest.model.common.DataResponse)17 API (org.wso2.carbon.apimgt.api.model.API)16 UserStoreException (org.wso2.carbon.user.api.UserStoreException)16 Path (javax.ws.rs.Path)14 Produces (javax.ws.rs.Produces)14 JSONObject (org.json.simple.JSONObject)14 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)14 List (java.util.List)13 IOException (java.io.IOException)11 QName (javax.xml.namespace.QName)11 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)11 Properties (java.util.Properties)10 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)10