org.sonatype.nexus.blobstore.api.BlobStoreConfiguration Java Examples

The following examples show how to use org.sonatype.nexus.blobstore.api.BlobStoreConfiguration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FileBlobStoreConcurrencyIT.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  Path root = util.createTempDir().toPath();
  Path content = root.resolve("content");

  when(nodeAccess.getId()).thenReturn(UUID.randomUUID().toString());
  when(dryRunPrefix.get()).thenReturn("");

  ApplicationDirectories applicationDirectories = mock(ApplicationDirectories.class);
  when(applicationDirectories.getWorkDirectory(anyString())).thenReturn(root.toFile());

  final BlobStoreConfiguration config = new MockBlobStoreConfiguration();
  config.attributes(FileBlobStore.CONFIG_KEY).set(FileBlobStore.PATH_KEY, root.toString());

  metricsStore = spy(
      new FileBlobStoreMetricsStore(new PeriodicJobServiceImpl(), nodeAccess, quotaService, QUOTA_CHECK_INTERVAL,
          fileOperations));

  this.underTest = new FileBlobStore(content,
      new DefaultBlobIdLocationResolver(),
      new SimpleFileOperations(),
      metricsStore,
      config,
      applicationDirectories, nodeAccess, dryRunPrefix);
  underTest.start();
}
 
Example #2
Source File: EncryptingAmazonS3Client.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private S3Encrypter getEncrypter(final BlobStoreConfiguration blobStoreConfig) {
  Optional<String> encryptionType = ofNullable(
      blobStoreConfig.attributes(CONFIG_KEY).get(ENCRYPTION_TYPE, String.class));
  return encryptionType.map(id -> {
    if (S3ManagedEncrypter.ID.equals(id)) {
      return new S3ManagedEncrypter();
    }
    else if (KMSEncrypter.ID.equals(id)) {
      Optional<String> key = ofNullable(
          blobStoreConfig.attributes(CONFIG_KEY).get(ENCRYPTION_KEY, String.class));
      return new KMSEncrypter(key);
    }
    else if (NoEncrypter.ID.equals(id)) {
      return NoEncrypter.INSTANCE;
    }
    else {
      throw new IllegalStateException("Failed to find encrypter for id:" + id);
    }
  }).orElse(NoEncrypter.INSTANCE);
}
 
Example #3
Source File: S3BlobStoreDescriptor.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private void validateOverlappingBucketWithConfiguration(final BlobStoreConfiguration newConfig, // NOSONAR
                                                        final BlobStoreConfiguration existingConfig) {
  String newName = newConfig.getName();
  String newBucket = newConfig.attributes(CONFIG_KEY).get(BUCKET_KEY, String.class, "");
  String newPrefix = newConfig.attributes(CONFIG_KEY).get(BUCKET_PREFIX_KEY, String.class, "");
  String newEndpoint = newConfig.attributes(CONFIG_KEY).get(ENDPOINT_KEY, String.class, "");

  if (!existingConfig.getName().equals(newName) && existingConfig.getType().equals(S3BlobStore.TYPE)) {
    String existingBucket = existingConfig.attributes(CONFIG_KEY).get(BUCKET_KEY, String.class, "");
    String existingPrefix = existingConfig.attributes(CONFIG_KEY).get(BUCKET_PREFIX_KEY, String.class, "");
    String existingEndpoint = existingConfig.attributes(CONFIG_KEY).get(ENDPOINT_KEY, String.class, "");
    if (newBucket.equals(existingBucket) &&
        newEndpoint.equals(existingEndpoint) &&
        prefixesOverlap(existingPrefix, newPrefix)) {
      String message = format("Blob Store '%s' is already using bucket '%s'", existingConfig.getName(),
          existingBucket);
      if (!newPrefix.isEmpty() || !existingPrefix.isEmpty()) {
        message = message + format(" with prefix '%s'", existingPrefix);
      }
      if (!newEndpoint.isEmpty() || !existingEndpoint.isEmpty()) {
        message = message + format(" on endpoint '%s'", existingEndpoint);
      }
      throw new ValidationException(message);
    }
  }
}
 
Example #4
Source File: FileBlobStoreResource.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private BlobStoreConfiguration getBlobStoreConfiguration(final String name) {
  if (!blobStoreManager.exists(name)) {
    BlobStoreResourceUtil.throwBlobStoreNotFoundException();
  }

  BlobStoreConfiguration configuration = blobStoreManager.get(name).getBlobStoreConfiguration();

  if (!configuration.getType().equals(FileBlobStore.TYPE)) {
    throw new WebApplicationMessageException(
        BAD_REQUEST,
        "\"Unable to read non-file blob store configuration (type was " + configuration.getType() + ")\"",
        APPLICATION_JSON
    );
  }
  return configuration;
}
 
Example #5
Source File: GoogleCloudBlobStoreITSupport.java    From nexus-blobstore-google-cloud with Eclipse Public License 1.0 6 votes vote down vote up
public BlobStoreConfiguration newConfiguration(final String name, final String bucketName,
                                               @Nullable final File credentialFile) {
  BlobStoreConfiguration configuration = blobStoreManager.newConfiguration();
  configuration.setName(name);
  configuration.setType("Google Cloud Storage");
  NestedAttributesMap configMap = configuration.attributes("google cloud storage");
  configMap.set("bucket", bucketName);
  configMap.set("location", "us-central1");
  if (credentialFile != null) {
    configMap.set("credential_file", credentialFile.getAbsolutePath());
  }
  NestedAttributesMap quotaMap = configuration.attributes(BlobStoreQuotaSupport.ROOT_KEY);
  quotaMap.set(BlobStoreQuotaSupport.TYPE_KEY, SpaceUsedQuota.ID);
  quotaMap.set(BlobStoreQuotaSupport.LIMIT_KEY, 512000L);

  return configuration;
}
 
Example #6
Source File: MemoryBlobSessionTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Before
public void setUp() {
  Blob restoredBlob = mockBlob(RESTORED_BLOB_ID);
  Blob copiedBlob = mockBlob(COPIED_BLOB_ID);

  when(blobStore.create(blobData, headers, null)).thenAnswer(this::newBlob);
  when(blobStore.create(blobData, headers, RESTORED_BLOB_ID)).thenReturn(restoredBlob);
  when(blobStore.create(sourceFile, headers, TEST_BLOB_SIZE, TEST_BLOB_HASH)).thenAnswer(this::newBlob);
  when(blobStore.copy(EXISTING_BLOB_ID, headers)).thenReturn(copiedBlob);

  when(blobStore.exists(any())).thenReturn(true);
  when(blobStore.get(any())).thenAnswer(this::getBlob);

  BlobStoreConfiguration storeConfiguration = mock(BlobStoreConfiguration.class);
  when(storeConfiguration.getName()).thenReturn("test-blob-store");
  when(blobStore.getBlobStoreConfiguration()).thenReturn(storeConfiguration);
}
 
Example #7
Source File: BlobStoreAuditor.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Subscribe
@AllowConcurrentEvents
public void on(final BlobStoreEvent event) {
  if (isRecording()) {
    BlobStore blobStore = event.getBlobStore();
    BlobStoreConfiguration configuration = blobStore.getBlobStoreConfiguration();

    AuditData data = new AuditData();
    data.setDomain(DOMAIN);
    data.setType(type(event.getClass()));
    data.setContext(configuration.getName());

    Map<String, Object> attributes = data.getAttributes();
    attributes.put("name", configuration.getName());
    attributes.put("type", configuration.getType());

    record(data);
  }
}
 
Example #8
Source File: FileBlobStoreIT.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  when(nodeAccess.getId()).thenReturn(UUID.randomUUID().toString());
  when(nodeAccess.isOldestNode()).thenReturn(true);
  when(dryRunPrefix.get()).thenReturn("");
  ApplicationDirectories applicationDirectories = mock(ApplicationDirectories.class);
  blobStoreDirectory = util.createTempDir().toPath();
  contentDirectory = blobStoreDirectory.resolve("content");
  when(applicationDirectories.getWorkDirectory(anyString())).thenReturn(blobStoreDirectory.toFile());

  fileOperations = spy(new SimpleFileOperations());

  metricsStore = new FileBlobStoreMetricsStore(new PeriodicJobServiceImpl(), nodeAccess, quotaService,
      QUOTA_CHECK_INTERVAL, fileOperations);

  blobIdResolver = new DefaultBlobIdLocationResolver();

  final BlobStoreConfiguration config = new MockBlobStoreConfiguration();
  config.attributes(FileBlobStore.CONFIG_KEY).set(FileBlobStore.PATH_KEY, blobStoreDirectory.toString());
  underTest = new FileBlobStore(blobIdResolver,
      fileOperations,
      applicationDirectories,
      metricsStore, nodeAccess, dryRunPrefix);
  underTest.init(config);
  underTest.start();
}
 
Example #9
Source File: BlobStoreGroupDescriptor.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void validateConfig(final BlobStoreConfiguration config) {
  super.validateConfig(config);
  validateEnabled();
  String name = config.getName();

  String fillPolicy = config.attributes(CONFIG_KEY).get(FILL_POLICY_KEY, String.class);
  if (StringUtils.isBlank(fillPolicy)) {
    throw new ValidationErrorsException("Blob store group requires a fill policy configuration");
  }

  List<String> memberNames = config.attributes(CONFIG_KEY).get(MEMBERS_KEY, List.class);
  validateNotEmptyOrSelfReferencing(name, memberNames);
  validateEligibleMembers(name, memberNames);
  validateOnlyEmptyOrNotWritableExistingMembersRemoved(name, memberNames);
}
 
Example #10
Source File: BlobStoreGroupDescriptor.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private void validateEligibleMembers(final String name, final List<String> memberNames) {
  for (String memberName : memberNames) {
    BlobStore member = blobStoreManager.get(memberName);
    if (!member.isGroupable()) {
      BlobStoreConfiguration memberConfig = member.getBlobStoreConfiguration();
      throw new ValidationErrorsException(
          format("Blob Store '%s' is of type '%s' and is not eligible to be a group member", memberName,
              memberConfig.getType()));
    }

    // target member may not be a member of a different group
    Predicate<String> sameGroup = name::equals;
    blobStoreManager.getParent(memberName).filter(sameGroup.negate()).ifPresent(groupName -> {
      throw new ValidationErrorsException(
          format("Blob Store '%s' is already a member of Blob Store Group '%s'", memberName, groupName));
    });

    // target member may not be set as repository storage
    int repoCount = blobStoreUtil.usageCount(memberName);
    if (repoCount > 0) {
      throw new ValidationErrorsException(format(
          "Blob Store '%s' is set as storage for %s repositories and is not eligible to be a group member",
          memberName, repoCount));
    }
  }
}
 
Example #11
Source File: BlobStoreGroupDescriptor.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private void validateOnlyEmptyOrNotWritableExistingMembersRemoved(final String name, final List<String> memberNames) {
  BlobStore blobStore = blobStoreManager.get(name);
  if (blobStore != null) {
    BlobStoreConfiguration currentConfiguration = blobStore.getBlobStoreConfiguration();
    if (currentConfiguration != null && currentConfiguration.getType().equals(BlobStoreGroup.TYPE)) {
      for (String existingMemberName : memberNames(currentConfiguration)) {
        if (!memberNames.contains(existingMemberName)) {
          BlobStore existingMember = blobStoreManager.get(existingMemberName);
          if (existingMember.isWritable() || !existingMember.isEmpty()) {
            throw new ValidationErrorsException(
                format("Blob Store '%s' cannot be removed from Blob Store Group '%s', " +
                    "use 'Admin - Remove a member from a blob store group' task instead",
                    existingMemberName, name));
          }
        }
      }
    }
  }
}
 
Example #12
Source File: BlobStoreRule.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
public BlobStore createFile(String name) throws Exception {
  BlobStoreConfiguration config = blobStoreManagerProvider.get().newConfiguration();
  config.setName(name);
  config.setType(FileBlobStore.TYPE);
  config.attributes(FileBlobStore.CONFIG_KEY).set(PATH_KEY, name);
  BlobStore blobStore = blobStoreManagerProvider.get().create(config);
  blobStoreNames.add(name);
  return blobStore;
}
 
Example #13
Source File: S3BlobStoreApiResource.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@POST
@Override
@RequiresAuthentication
@RequiresPermissions("nexus:blobstores:create")
public Response createBlobStore(@Valid final S3BlobStoreApiModel request) throws Exception {
  final BlobStoreConfiguration blobStoreConfiguration = MODEL_MAPPER.apply(blobStoreManager.newConfiguration(), request);
  blobStoreManager.create(blobStoreConfiguration);
  return status(CREATED).build();
}
 
Example #14
Source File: BlobStoreConfigurationStoreImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Transactional
@Override
public Optional<BlobStoreConfiguration> findParent(final String name) {
  return dao().findCandidateParents(name).stream()
      .filter(config -> memberNames(config).contains(name))
      .findFirst();
}
 
Example #15
Source File: OrientBlobStoreConfigurationStore.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@Guarded(by = STARTED)
public void create(final BlobStoreConfiguration configuration) {
  checkBlobStoreConfiguration(configuration);

  inTxRetry(databaseInstance).run(db -> entityAdapter.addEntity(db, (OrientBlobStoreConfiguration) configuration));
}
 
Example #16
Source File: BucketManager.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isExpirationLifecycleConfigurationPresent(final BucketLifecycleConfiguration lifecycleConfiguration,
                                                          final BlobStoreConfiguration blobStoreConfiguration) {
  String bucketPrefix = getBucketPrefix(blobStoreConfiguration);
  int expirationInDays = getConfiguredExpirationInDays(blobStoreConfiguration);
  return lifecycleConfiguration != null &&
      lifecycleConfiguration.getRules() != null &&
      lifecycleConfiguration.getRules().stream()
      .filter(r -> r.getExpirationInDays() == expirationInDays)
      .anyMatch(r -> isDeletedTagPredicate(r.getFilter().getPredicate(), bucketPrefix));
}
 
Example #17
Source File: BlobStoreQuotaServiceImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Nullable
@Override
public BlobStoreQuotaResult checkQuota(final BlobStore blobStore) {
  checkNotNull(blobStore);
  BlobStoreConfiguration config = blobStore.getBlobStoreConfiguration();

  return getQuota(config).map(quota -> {
    log.debug("Checking blob store {} for quota {}", config.getName(), quota);
    return quota.check(blobStore);
  }).orElse(null);
}
 
Example #18
Source File: S3BlobStoreDescriptor.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void validateConfig(final BlobStoreConfiguration config) {
  super.validateConfig(config);
  for (BlobStore existingBlobStore : blobStoreManager.browse()) {
    validateOverlappingBucketWithConfiguration(config, existingBlobStore.getBlobStoreConfiguration());
  }
}
 
Example #19
Source File: S3BlobStoreApiConfigurationMapper.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private static BlobStoreApiSoftQuota createSoftQuota(final BlobStoreConfiguration configuration) {
  final NestedAttributesMap softQuotaAttributes = configuration.attributes(ROOT_KEY);
  if (!softQuotaAttributes.isEmpty()) {
    final BlobStoreApiSoftQuota blobStoreApiSoftQuota = new BlobStoreApiSoftQuota();
    final String quotaType = getValue(softQuotaAttributes, TYPE_KEY);
    final String quotaLimit = getValue(softQuotaAttributes, LIMIT_KEY);
    if (nonNull(quotaType) && nonNull(quotaLimit)) {
      blobStoreApiSoftQuota.setType(quotaType);
      blobStoreApiSoftQuota.setLimit(parseLong(quotaLimit) / ONE_MILLION);
      return blobStoreApiSoftQuota;
    }
  }
  return null;
}
 
Example #20
Source File: GoogleCloudDatastoreFactory.java    From nexus-blobstore-google-cloud with Eclipse Public License 1.0 5 votes vote down vote up
Datastore create(final BlobStoreConfiguration configuration) throws Exception {
  DatastoreOptions.Builder builder = DatastoreOptions.newBuilder().setTransportOptions(transportOptions());

  String credentialFile = configuration.attributes(CONFIG_KEY).get(CREDENTIAL_FILE_KEY, String.class);
  if (StringUtils.hasText(credentialFile)) {
    ServiceAccountCredentials credentials = ServiceAccountCredentials.fromStream(new FileInputStream(credentialFile));
    logger.debug("loaded {} from {} for Google Datastore client", credentials, credentialFile);
    builder.setCredentials(credentials);
    builder.setProjectId(getProjectId(credentialFile));
  }

  return builder.build().getService();
}
 
Example #21
Source File: OrientBlobStoreConfigurationStore.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@Guarded(by = STARTED)
public void delete(final BlobStoreConfiguration configuration) {
  checkBlobStoreConfiguration(configuration);

  inTxRetry(databaseInstance).run(db -> entityAdapter.deleteEntity(db, (OrientBlobStoreConfiguration) configuration));
}
 
Example #22
Source File: BlobStoreManagerImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@Guarded(by = STARTED)
public BlobStore create(final BlobStoreConfiguration configuration) throws Exception {
  checkNotNull(configuration);
  log.debug("Creating BlobStore: {} with attributes: {}", configuration.getName(),
      configuration.getAttributes());
  BlobStoreDescriptor blobStoreDescriptor = blobStoreDescriptors.get(configuration.getType());
  blobStoreDescriptor.sanitizeConfig(configuration);
  blobStoreDescriptor.validateConfig(configuration);

  BlobStore blobStore = blobStorePrototypes.get(configuration.getType()).get();
  blobStore.init(configuration);

  if (!EventHelper.isReplicating()) {
    try {
      store.create(configuration);
    }
    catch (Exception e) {
      try {
        blobStore.remove();
      }
      catch (Exception removeException) {
        // if an error occurs on remove log and rethrow original to avoid losing the root cause
        log.error("Error removing BlobStore {} after create failed", configuration.getName(), removeException);
      }
      throw e;
    }
  }

  track(configuration.getName(), blobStore);

  blobStore.start();

  eventManager.post(new BlobStoreCreatedEvent(blobStore));

  return blobStore;
}
 
Example #23
Source File: FileBlobStoreApiModel.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public BlobStoreConfiguration toBlobStoreConfiguration(final BlobStoreConfiguration configuration) {
  BlobStoreConfiguration newConfig = super.toBlobStoreConfiguration(configuration);
  newConfig.setType(FileBlobStore.TYPE);
  newConfig.attributes(FileBlobStore.CONFIG_KEY).set(FileBlobStore.PATH_KEY, path);
  return newConfig;
}
 
Example #24
Source File: FileBlobStoreResource.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
@RequiresAuthentication
@RequiresPermissions("nexus:blobstores:create")
@POST
@Path("/")
@Validate
public void createFileBlobStore(@Valid final FileBlobStoreApiCreateRequest request) throws Exception {
  BlobStoreConfiguration configuration = request.toBlobStoreConfiguration(blobStoreManager.newConfiguration());

  blobStoreManager.create(configuration);
}
 
Example #25
Source File: BlobStoreQuotaServiceImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void validateSoftQuotaConfig(final BlobStoreConfiguration config) {
  getQuotaType(config).ifPresent(type -> {
    if(!quotas.containsKey(type)) {
      throw new ValidationErrorsException("To enable Soft Quota, you must select a Type of Quota");
    }
  });
  getQuota(config).ifPresent(quota -> quota.validateConfig(config));
}
 
Example #26
Source File: FileBlobStoreConfigurationBuilder.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a new builder using the specified name for the resulting blob store. Unless customized, the name is also
 * used as the path for the blob store.
 */
public FileBlobStoreConfigurationBuilder(final String name,
                                         final Supplier<BlobStoreConfiguration> configurationSupplier)
{
  this.name = checkNotNull(name);
  this.configurationSupplier = checkNotNull(configurationSupplier);
  this.path = name;
}
 
Example #27
Source File: FileBlobStore.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@VisibleForTesting
public FileBlobStore(final Path contentDir, //NOSONAR
                     final BlobIdLocationResolver blobIdLocationResolver,
                     final FileOperations fileOperations,
                     final FileBlobStoreMetricsStore metricsStore,
                     final BlobStoreConfiguration configuration,
                     final ApplicationDirectories directories,
                     final NodeAccess nodeAccess,
                     final DryRunPrefix dryRunPrefix)
{
  this(blobIdLocationResolver, fileOperations, directories, metricsStore, nodeAccess, dryRunPrefix);
  this.contentDir = checkNotNull(contentDir);
  this.blobStoreConfiguration = checkNotNull(configuration);
}
 
Example #28
Source File: FileBlobStoreDescriptorTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void descriptorValidationCallPathValidation() throws Exception {
  BlobStoreConfiguration config = new MockBlobStoreConfiguration();
  String tempDir = Files.createTempDirectory("test").toString();
  config.attributes(CONFIG_KEY).set(PATH_KEY, tempDir);
  descriptor.validateConfig(config);
  verify(blobStoreUtil, times(1)).validateFilePath(tempDir, MAX_NAME_LENGTH);
}
 
Example #29
Source File: BlobStoreApiModel.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private BlobStoreApiSoftQuota createSoftQuota(BlobStoreConfiguration configuration) {
  if (configuration.attributes(BlobStoreQuotaSupport.ROOT_KEY).isEmpty()) {
    return null;
  }

  BlobStoreApiSoftQuota newSoftQuota = new BlobStoreApiSoftQuota();
  newSoftQuota.setType(BlobStoreQuotaSupport.getType(configuration));
  newSoftQuota.setLimit(BlobStoreQuotaSupport.getLimit(configuration));
  return newSoftQuota;
}
 
Example #30
Source File: BlobStoreGroup.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void init(final BlobStoreConfiguration configuration) {
  this.blobStoreConfiguration = configuration;
  this.members = Suppliers.memoize(new MembersSupplier());
  String fillPolicyName = BlobStoreGroupConfigurationHelper.fillPolicyName(configuration);
  if (fillPolicyProviders.containsKey(fillPolicyName)) {
    this.fillPolicy = fillPolicyProviders.get(fillPolicyName).get();
  }
  else {
    log.warn("Unable to find fill policy {} for Blob Store Group {}, using fill policy {}",
        fillPolicyName, configuration.getName(), FALLBACK_FILL_POLICY_TYPE);
    this.fillPolicy = fillPolicyProviders.get(FALLBACK_FILL_POLICY_TYPE).get();
  }
}