Skip to content

Commit

Permalink
feat: implements new DataAddress validation
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Oct 31, 2023
1 parent 170bfe1 commit ee61fdb
Show file tree
Hide file tree
Showing 13 changed files with 352 additions and 111 deletions.
4 changes: 3 additions & 1 deletion extensions/common/aws/aws-s3-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ plugins {

dependencies {
api(libs.edc.spi.transfer)
api(libs.edc.spi.validation)

api(libs.failsafe.core)

api(libs.aws.iam)
api(libs.aws.s3)
api(libs.aws.sts)

testImplementation(libs.edc.junit)
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - Initial implementation
*
*/

package org.eclipse.edc.aws.s3;

import org.eclipse.edc.aws.s3.validation.S3DataAddressValidator;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.validator.spi.DataAddressValidatorRegistry;

import static org.eclipse.edc.aws.s3.S3DataAddressValidatorExtension.NAME;

@Extension(NAME)
public class S3DataAddressValidatorExtension implements ServiceExtension {
public static final String NAME = "DataAddress S3 Validator";

@Inject
private DataAddressValidatorRegistry validatorRegistry;

@Override
public String name() {
return NAME;
}

@Override
public void initialize(ServiceExtensionContext context) {
var validator = new S3DataAddressValidator();
validatorRegistry.registerDestinationValidator(S3BucketSchema.TYPE, validator);
validatorRegistry.registerSourceValidator(S3BucketSchema.TYPE, validator);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - Initial implementation
*
*/

package org.eclipse.edc.aws.s3.validation;

import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.validator.spi.ValidationResult;
import org.eclipse.edc.validator.spi.Validator;

import java.util.Objects;
import java.util.stream.Stream;

import static org.eclipse.edc.aws.s3.S3BucketSchema.ACCESS_KEY_ID;
import static org.eclipse.edc.aws.s3.S3BucketSchema.SECRET_ACCESS_KEY;
import static org.eclipse.edc.validator.spi.Violation.violation;

/**
* Validator for AmazonS3 DataAddress type when credentials are in the data address (Optional)
*/
public class S3DataAddressCredentialsValidator implements Validator<DataAddress> {
@Override
public ValidationResult validate(DataAddress dataAddress) {
var violations = Stream.of(ACCESS_KEY_ID, SECRET_ACCESS_KEY)
.map(it -> {
var value = dataAddress.getStringProperty(it);
if (value == null || value.isBlank()) {
return violation("'%s' is a mandatory attribute".formatted(it), it, value);
}
return null;
})
.filter(Objects::nonNull)
.toList();

if (violations.isEmpty()) {
return ValidationResult.success();
}

return ValidationResult.failure(violations);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - Initial implementation
*
*/

package org.eclipse.edc.aws.s3.validation;

import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.validator.spi.ValidationResult;
import org.eclipse.edc.validator.spi.Validator;

import java.util.Objects;
import java.util.stream.Stream;

import static org.eclipse.edc.aws.s3.S3BucketSchema.BUCKET_NAME;
import static org.eclipse.edc.aws.s3.S3BucketSchema.REGION;
import static org.eclipse.edc.validator.spi.Violation.violation;

/**
* Validator for AmazonS3 DataAddress type
*/
public class S3DataAddressValidator implements Validator<DataAddress> {

@Override
public ValidationResult validate(DataAddress dataAddress) {
var violations = Stream.of(BUCKET_NAME, REGION)
.map(it -> {
var value = dataAddress.getStringProperty(it);
if (value == null || value.isBlank()) {
return violation("'%s' is a mandatory attribute".formatted(it), it, value);
}
return null;
})
.filter(Objects::nonNull)
.toList();

if (violations.isEmpty()) {
return ValidationResult.success();
}

return ValidationResult.failure(violations);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
#

org.eclipse.edc.aws.s3.S3CoreExtension

org.eclipse.edc.aws.s3.S3DataAddressValidatorExtension
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - Initial implementation
*
*/

package org.eclipse.edc.aws.s3;


import org.eclipse.edc.aws.s3.validation.S3DataAddressValidator;
import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.validator.spi.DataAddressValidatorRegistry;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

@ExtendWith(DependencyInjectionExtension.class)
public class S3DataAddressValidatorExtensionTest {

private final DataAddressValidatorRegistry registry = mock();

@BeforeEach
void setup(ServiceExtensionContext context) {
context.registerService(DataAddressValidatorRegistry.class, registry);
}

@Test
void initialize(S3DataAddressValidatorExtension extension, ServiceExtensionContext context) {
extension.initialize(context);

assertThat(extension.name()).isEqualTo(S3DataAddressValidatorExtension.NAME);

verify(registry).registerDestinationValidator(eq(S3BucketSchema.TYPE), isA(S3DataAddressValidator.class));
verify(registry).registerSourceValidator(eq(S3BucketSchema.TYPE), isA(S3DataAddressValidator.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - Initial implementation
*
*/

package org.eclipse.edc.aws.s3.validation;

import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.validator.spi.ValidationFailure;
import org.eclipse.edc.validator.spi.Violation;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.aws.s3.S3BucketSchema.ACCESS_KEY_ID;
import static org.eclipse.edc.aws.s3.S3BucketSchema.SECRET_ACCESS_KEY;
import static org.eclipse.edc.aws.s3.S3BucketSchema.TYPE;
import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat;

public class S3DataAddressCredentialsValidatorTest {

private final S3DataAddressCredentialsValidator validator = new S3DataAddressCredentialsValidator();

@Test
void shouldPass_whenDataAddressIsValid() {
var dataAddress = DataAddress.Builder.newInstance()
.type(TYPE)
.property(ACCESS_KEY_ID, "ak")
.property(SECRET_ACCESS_KEY, "sk")
.build();

var result = validator.validate(dataAddress);

assertThat(result).isSucceeded();
}

@Test
void shouldFail_whenRequiredFieldsAreMissing() {
var dataAddress = DataAddress.Builder.newInstance()
.type(TYPE)
.build();

var result = validator.validate(dataAddress);

assertThat(result).isFailed()
.extracting(ValidationFailure::getViolations)
.satisfies(violations -> assertThat(violations)
.extracting(Violation::path)
.containsExactlyInAnyOrder(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - Initial implementation
*
*/

package org.eclipse.edc.aws.s3.validation;

import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.validator.spi.ValidationFailure;
import org.eclipse.edc.validator.spi.Violation;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.aws.s3.S3BucketSchema.BUCKET_NAME;
import static org.eclipse.edc.aws.s3.S3BucketSchema.REGION;
import static org.eclipse.edc.aws.s3.S3BucketSchema.TYPE;
import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat;

public class S3DataAddressValidatorTest {

private final S3DataAddressValidator validator = new S3DataAddressValidator();

@Test
void shouldPass_whenDataAddressIsValid() {
var dataAddress = DataAddress.Builder.newInstance()
.type(TYPE)
.property(BUCKET_NAME, "bucketName")
.property(REGION, "region")
.build();

var result = validator.validate(dataAddress);

assertThat(result).isSucceeded();
}

@Test
void shouldFail_whenRequiredFieldsAreMissing() {
var dataAddress = DataAddress.Builder.newInstance()
.type(TYPE)
.build();

var result = validator.validate(dataAddress);


assertThat(result).isFailed()
.extracting(ValidationFailure::getViolations)
.satisfies(violations -> assertThat(violations)
.extracting(Violation::path)
.containsExactlyInAnyOrder(BUCKET_NAME, REGION));
}
}
Loading

0 comments on commit ee61fdb

Please sign in to comment.