Feature Max Artifact Storage quota (#739)

* Enforcement of artifact storage quota
* Added REST integration tests for artifact upload
* Fix test config
* Fix failing test cases
* Fix PR review findings

Signed-off-by: Stefan Behl <stefan.behl@bosch-si.com>
This commit is contained in:
Stefan Behl
2018-10-05 13:52:46 +02:00
committed by GitHub
parent 124ef54a78
commit 086408e8f8
10 changed files with 249 additions and 118 deletions

View File

@@ -54,6 +54,8 @@ public class JpaArtifactManagement implements ArtifactManagement {
private static final String MAX_ARTIFACT_SIZE_EXCEEDED = "Quota exceeded: The artifact '%s' (%s bytes) which has been uploaded for software module '%s' exceeds the maximum artifact size of %s bytes.";
private static final String MAX_ARTIFACT_SIZE_TOTAL_EXCEEDED = "Quota exceeded: The artifact '%s' (%s bytes) cannot be uploaded. The maximum total artifact storage of %s bytes would be exceeded.";
private final LocalArtifactRepository localArtifactRepository;
private final SoftwareModuleRepository softwareModuleRepository;
@@ -104,6 +106,7 @@ public class JpaArtifactManagement implements ArtifactManagement {
assertArtifactQuota(moduleId, 1);
assertMaxArtifactSizeQuota(filename, moduleId, filesize);
assertMaxArtifactStorageQuota(filename, filesize);
try {
result = artifactRepository.store(tenantAware.getCurrentTenant(), stream, filename, contentType,
@@ -141,6 +144,21 @@ public class JpaArtifactManagement implements ArtifactManagement {
}
}
private void assertMaxArtifactStorageQuota(final String filename, final long artifactSize) {
final long maxArtifactSizeTotal = quotaManagement.getMaxArtifactStorage();
if (maxArtifactSizeTotal <= 0) {
return;
}
final Long currentlyUsed = localArtifactRepository.getSumOfUndeletedArtifactSize().orElse(0L);
if (currentlyUsed + artifactSize > maxArtifactSizeTotal) {
final String msg = String.format(MAX_ARTIFACT_SIZE_TOTAL_EXCEEDED, filename, artifactSize,
maxArtifactSizeTotal);
LOG.warn(msg);
throw new QuotaExceededException(msg);
}
}
@Override
@Transactional
@Retryable(include = {

View File

@@ -149,7 +149,8 @@ public class RepositoryApplicationConfiguration extends JpaBaseConfiguration {
}
@Bean
PropertiesQuotaManagement staticQuotaManagement(final HawkbitSecurityProperties securityProperties) {
@ConditionalOnMissingBean
QuotaManagement staticQuotaManagement(final HawkbitSecurityProperties securityProperties) {
return new PropertiesQuotaManagement(securityProperties);
}