|
| 1 | +/* |
| 2 | + * This file is part of Dependency-Track. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * Copyright (c) OWASP Foundation. All Rights Reserved. |
| 18 | + */ |
| 19 | +package org.dependencytrack.e2e; |
| 20 | + |
| 21 | +import org.apache.commons.io.IOUtils; |
| 22 | +import org.dependencytrack.apiserver.model.BomUploadRequest; |
| 23 | +import org.dependencytrack.apiserver.model.EventProcessingResponse; |
| 24 | +import org.dependencytrack.apiserver.model.WorkflowTokenResponse; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.testcontainers.containers.Container.ExecResult; |
| 27 | +import org.testcontainers.containers.GenericContainer; |
| 28 | + |
| 29 | +import java.time.Duration; |
| 30 | +import java.util.Base64; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | +import static org.awaitility.Awaitility.await; |
| 34 | + |
| 35 | +class BomUploadProcessingWithLocalStorageE2ET extends AbstractE2ET { |
| 36 | + |
| 37 | + @Override |
| 38 | + protected void customizeApiServerContainer(final GenericContainer<?> container) { |
| 39 | + container |
| 40 | + // Ensure other storage extensions are disabled. |
| 41 | + .withEnv("BOM_UPLOAD_STORAGE_EXTENSION_DATABASE_ENABLED", "false") |
| 42 | + .withEnv("BOM_UPLOAD_STORAGE_EXTENSION_S3_ENABLED", "false") |
| 43 | + // Enable and configure local storage extension. |
| 44 | + .withEnv("BOM_UPLOAD_STORAGE_EXTENSION_LOCAL_ENABLED", "true") |
| 45 | + .withEnv("BOM_UPLOAD_STORAGE_EXTENSION_LOCAL_DIRECTORY", "/tmp/bom-uploads"); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected void customizeVulnAnalyzerContainer(final GenericContainer<?> container) { |
| 50 | + // We don't test analysis here, so don't waste any quota with the OSS Index API. |
| 51 | + container.withEnv("SCANNER_OSSINDEX_ENABLED", "false"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void test() throws Exception { |
| 56 | + // Parse and base64 encode a BOM. |
| 57 | + final byte[] bomBytes = IOUtils.resourceToByteArray("/dtrack-apiserver-4.5.0.bom.json"); |
| 58 | + final String bomBase64 = Base64.getEncoder().encodeToString(bomBytes); |
| 59 | + |
| 60 | + // Upload the BOM. |
| 61 | + final WorkflowTokenResponse response = apiServerClient.uploadBom(new BomUploadRequest("foo", "bar", true, bomBase64)); |
| 62 | + assertThat(response.token()).isNotEmpty(); |
| 63 | + |
| 64 | + // Wait up to 15sec for the BOM processing to complete. |
| 65 | + await("BOM processing") |
| 66 | + .atMost(Duration.ofSeconds(15)) |
| 67 | + .pollDelay(Duration.ofMillis(250)) |
| 68 | + .untilAsserted(() -> { |
| 69 | + final EventProcessingResponse processingResponse = apiServerClient.isEventBeingProcessed(response.token()); |
| 70 | + assertThat(processingResponse.processing()).isFalse(); |
| 71 | + }); |
| 72 | + |
| 73 | + verifyBomDeleted(); |
| 74 | + } |
| 75 | + |
| 76 | + private void verifyBomDeleted() throws Exception { |
| 77 | + final ExecResult dirExistsResult = apiServerContainer.execInContainer("test", "-d", "/tmp/bom-uploads"); |
| 78 | + assertThat(dirExistsResult.getExitCode()).withFailMessage("Storage directory was not created").isZero(); |
| 79 | + |
| 80 | + final ExecResult dirEmptyResult = apiServerContainer.execInContainer("ls", "/tmp/bom-uploads"); |
| 81 | + assertThat(dirEmptyResult.getStdout()).withFailMessage("BOM was not deleted after processing").isBlank(); |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments