Search in sources :

Example 1 with EncryptedWorkspace

use of com.structurizr.encryption.EncryptedWorkspace in project java by structurizr.

the class EncryptedJsonTests method test_write_and_read.

@Test
public void test_write_and_read() throws Exception {
    final Workspace workspace1 = new Workspace("Name", "Description");
    // output the model as JSON
    EncryptedJsonWriter jsonWriter = new EncryptedJsonWriter(true);
    AesEncryptionStrategy encryptionStrategy = new AesEncryptionStrategy("password");
    final EncryptedWorkspace encryptedWorkspace1 = new EncryptedWorkspace(workspace1, encryptionStrategy);
    StringWriter stringWriter = new StringWriter();
    jsonWriter.write(encryptedWorkspace1, stringWriter);
    // and read it back again
    EncryptedJsonReader jsonReader = new EncryptedJsonReader();
    StringReader stringReader = new StringReader(stringWriter.toString());
    final EncryptedWorkspace encryptedWorkspace2 = jsonReader.read(stringReader);
    assertEquals("Name", encryptedWorkspace2.getName());
    assertEquals("Description", encryptedWorkspace2.getDescription());
    encryptedWorkspace2.getEncryptionStrategy().setPassphrase(encryptionStrategy.getPassphrase());
    final Workspace workspace2 = encryptedWorkspace2.getWorkspace();
    assertEquals("Name", workspace2.getName());
    assertEquals("Description", workspace2.getDescription());
}
Also used : AesEncryptionStrategy(com.structurizr.encryption.AesEncryptionStrategy) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) EncryptedWorkspace(com.structurizr.encryption.EncryptedWorkspace) EncryptedWorkspace(com.structurizr.encryption.EncryptedWorkspace) Workspace(com.structurizr.Workspace) Test(org.junit.Test)

Example 2 with EncryptedWorkspace

use of com.structurizr.encryption.EncryptedWorkspace in project java by structurizr.

the class EncryptedJsonWriterTests method test_write_ThrowsAnIllegalArgumentException_WhenANullWriterIsSpecified.

@Test
public void test_write_ThrowsAnIllegalArgumentException_WhenANullWriterIsSpecified() throws Exception {
    try {
        EncryptedJsonWriter writer = new EncryptedJsonWriter(true);
        Workspace workspace = new Workspace("Name", "Description");
        EncryptedWorkspace encryptedWorkspace = new EncryptedWorkspace(workspace, new AesEncryptionStrategy("password"));
        writer.write(encryptedWorkspace, null);
        fail();
    } catch (IllegalArgumentException e) {
        assertEquals("Writer cannot be null.", e.getMessage());
    }
}
Also used : AesEncryptionStrategy(com.structurizr.encryption.AesEncryptionStrategy) EncryptedWorkspace(com.structurizr.encryption.EncryptedWorkspace) EncryptedWorkspace(com.structurizr.encryption.EncryptedWorkspace) Workspace(com.structurizr.Workspace) Test(org.junit.Test)

Example 3 with EncryptedWorkspace

use of com.structurizr.encryption.EncryptedWorkspace in project java by structurizr.

the class StructurizrClient method putWorkspace.

/**
 * Updates the given workspace.
 *
 * @param workspaceId the workspace ID
 * @param workspace   the workspace instance to update
 * @throws StructurizrClientException   if there are problems related to the network, authorization, JSON serialization, etc
 */
public void putWorkspace(long workspaceId, Workspace workspace) throws StructurizrClientException {
    if (workspace == null) {
        throw new IllegalArgumentException("The workspace must not be null.");
    } else if (workspaceId <= 0) {
        throw new IllegalArgumentException("The workspace ID must be a positive integer.");
    }
    try (CloseableHttpClient httpClient = HttpClients.createSystem()) {
        if (mergeFromRemote) {
            Workspace remoteWorkspace = getWorkspace(workspaceId);
            if (remoteWorkspace != null) {
                workspace.getViews().copyLayoutInformationFrom(remoteWorkspace.getViews());
                workspace.getViews().getConfiguration().copyConfigurationFrom(remoteWorkspace.getViews().getConfiguration());
            }
        }
        workspace.setId(workspaceId);
        workspace.setThumbnail(null);
        workspace.setLastModifiedDate(new Date());
        workspace.setLastModifiedAgent(agent);
        workspace.setLastModifiedUser(getUser());
        workspace.countAndLogWarnings();
        HttpPut httpPut = new HttpPut(url + WORKSPACE_PATH + workspaceId);
        StringWriter stringWriter = new StringWriter();
        if (encryptionStrategy == null) {
            JsonWriter jsonWriter = new JsonWriter(false);
            jsonWriter.write(workspace, stringWriter);
        } else {
            EncryptedWorkspace encryptedWorkspace = new EncryptedWorkspace(workspace, encryptionStrategy);
            encryptionStrategy.setLocation(EncryptionLocation.Client);
            EncryptedJsonWriter jsonWriter = new EncryptedJsonWriter(false);
            jsonWriter.write(encryptedWorkspace, stringWriter);
        }
        StringEntity stringEntity = new StringEntity(stringWriter.toString(), ContentType.APPLICATION_JSON);
        httpPut.setEntity(stringEntity);
        addHeaders(httpPut, EntityUtils.toString(stringEntity), ContentType.APPLICATION_JSON.toString());
        debugRequest(httpPut, EntityUtils.toString(stringEntity));
        log.info("Putting workspace with ID " + workspaceId);
        try (CloseableHttpResponse response = httpClient.execute(httpPut)) {
            String json = EntityUtils.toString(response.getEntity());
            if (response.getCode() == HttpStatus.SC_OK) {
                debugResponse(response);
                log.info(json);
            } else {
                ApiResponse apiResponse = ApiResponse.parse(json);
                throw new StructurizrClientException(apiResponse.getMessage());
            }
        }
    } catch (Exception e) {
        log.error(e);
        throw new StructurizrClientException(e);
    }
}
Also used : EncryptedJsonWriter(com.structurizr.io.json.EncryptedJsonWriter) CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) EncryptedJsonWriter(com.structurizr.io.json.EncryptedJsonWriter) JsonWriter(com.structurizr.io.json.JsonWriter) Date(java.util.Date) HttpPut(org.apache.hc.client5.http.classic.methods.HttpPut) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) StringWriter(java.io.StringWriter) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) EncryptedWorkspace(com.structurizr.encryption.EncryptedWorkspace) EncryptedWorkspace(com.structurizr.encryption.EncryptedWorkspace) Workspace(com.structurizr.Workspace)

Example 4 with EncryptedWorkspace

use of com.structurizr.encryption.EncryptedWorkspace in project java by structurizr.

the class StructurizrClientIntegrationTests method test_putAndGetWorkspace_WithEncryption.

@Test
public void test_putAndGetWorkspace_WithEncryption() throws Exception {
    structurizrClient.setEncryptionStrategy(new AesEncryptionStrategy("password"));
    Workspace workspace = new Workspace("Structurizr client library tests - with encryption", "A test workspace for the Structurizr client library");
    SoftwareSystem softwareSystem = workspace.getModel().addSoftwareSystem("Software System", "Description");
    Person person = workspace.getModel().addPerson("Person", "Description");
    person.uses(softwareSystem, "Uses");
    SystemContextView systemContextView = workspace.getViews().createSystemContextView(softwareSystem, "SystemContext", "Description");
    systemContextView.addAllElements();
    structurizrClient.putWorkspace(20081, workspace);
    workspace = structurizrClient.getWorkspace(20081);
    assertNotNull(workspace.getModel().getSoftwareSystemWithName("Software System"));
    assertNotNull(workspace.getModel().getPersonWithName("Person"));
    assertEquals(1, workspace.getModel().getRelationships().size());
    assertEquals(1, workspace.getViews().getSystemContextViews().size());
    // and check the archive version is readable
    EncryptedWorkspace archivedWorkspace = new EncryptedJsonReader().read(new FileReader(getArchivedWorkspace()));
    assertEquals(20081, archivedWorkspace.getId());
    assertEquals("Structurizr client library tests - with encryption", archivedWorkspace.getName());
    assertTrue(archivedWorkspace.getEncryptionStrategy() instanceof AesEncryptionStrategy);
    assertEquals(1, workspaceArchiveLocation.listFiles().length);
}
Also used : AesEncryptionStrategy(com.structurizr.encryption.AesEncryptionStrategy) EncryptedJsonReader(com.structurizr.io.json.EncryptedJsonReader) SystemContextView(com.structurizr.view.SystemContextView) SoftwareSystem(com.structurizr.model.SoftwareSystem) EncryptedWorkspace(com.structurizr.encryption.EncryptedWorkspace) FileReader(java.io.FileReader) Person(com.structurizr.model.Person) Workspace(com.structurizr.Workspace) EncryptedWorkspace(com.structurizr.encryption.EncryptedWorkspace) Test(org.junit.Test)

Example 5 with EncryptedWorkspace

use of com.structurizr.encryption.EncryptedWorkspace in project java by structurizr.

the class StructurizrClient method getWorkspace.

/**
 * Gets the workspace with the given ID.
 *
 * @param workspaceId the workspace ID
 * @return a Workspace instance
 * @throws StructurizrClientException   if there are problems related to the network, authorization, JSON deserialization, etc
 */
public Workspace getWorkspace(long workspaceId) throws StructurizrClientException {
    if (workspaceId <= 0) {
        throw new IllegalArgumentException("The workspace ID must be a positive integer.");
    }
    try (CloseableHttpClient httpClient = HttpClients.createSystem()) {
        log.info("Getting workspace with ID " + workspaceId);
        HttpGet httpGet = new HttpGet(url + WORKSPACE_PATH + workspaceId);
        addHeaders(httpGet, "", "");
        debugRequest(httpGet, null);
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            debugResponse(response);
            String json = EntityUtils.toString(response.getEntity());
            if (response.getCode() == HttpStatus.SC_OK) {
                archiveWorkspace(workspaceId, json);
                if (encryptionStrategy == null) {
                    if (json.contains("\"encryptionStrategy\"") && json.contains("\"ciphertext\"")) {
                        log.warn("The JSON may contain a client-side encrypted workspace, but no passphrase has been specified.");
                    }
                    JsonReader jsonReader = new JsonReader();
                    jsonReader.setIdGenerator(idGenerator);
                    return jsonReader.read(new StringReader(json));
                } else {
                    EncryptedWorkspace encryptedWorkspace = new EncryptedJsonReader().read(new StringReader(json));
                    if (encryptedWorkspace.getEncryptionStrategy() != null) {
                        encryptedWorkspace.getEncryptionStrategy().setPassphrase(encryptionStrategy.getPassphrase());
                        return encryptedWorkspace.getWorkspace();
                    } else {
                        // this workspace isn't encrypted, even though the client has an encryption strategy set
                        JsonReader jsonReader = new JsonReader();
                        jsonReader.setIdGenerator(idGenerator);
                        return jsonReader.read(new StringReader(json));
                    }
                }
            } else {
                ApiResponse apiResponse = ApiResponse.parse(json);
                throw new StructurizrClientException(apiResponse.getMessage());
            }
        }
    } catch (Exception e) {
        log.error(e);
        throw new StructurizrClientException(e);
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) EncryptedJsonReader(com.structurizr.io.json.EncryptedJsonReader) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) StringReader(java.io.StringReader) JsonReader(com.structurizr.io.json.JsonReader) EncryptedJsonReader(com.structurizr.io.json.EncryptedJsonReader) EncryptedWorkspace(com.structurizr.encryption.EncryptedWorkspace) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Aggregations

EncryptedWorkspace (com.structurizr.encryption.EncryptedWorkspace)5 Workspace (com.structurizr.Workspace)4 AesEncryptionStrategy (com.structurizr.encryption.AesEncryptionStrategy)3 Test (org.junit.Test)3 EncryptedJsonReader (com.structurizr.io.json.EncryptedJsonReader)2 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2 UnknownHostException (java.net.UnknownHostException)2 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)2 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)2 EncryptedJsonWriter (com.structurizr.io.json.EncryptedJsonWriter)1 JsonReader (com.structurizr.io.json.JsonReader)1 JsonWriter (com.structurizr.io.json.JsonWriter)1 Person (com.structurizr.model.Person)1 SoftwareSystem (com.structurizr.model.SoftwareSystem)1 SystemContextView (com.structurizr.view.SystemContextView)1 FileReader (java.io.FileReader)1 Date (java.util.Date)1 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)1