Skip to content

Commit

Permalink
#406 s3-hosts is clean for qulice
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Nov 8, 2023
1 parent 7c97d39 commit fe9afb0
Show file tree
Hide file tree
Showing 40 changed files with 441 additions and 726 deletions.
11 changes: 11 additions & 0 deletions s3auth-hosts/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
Expand Down Expand Up @@ -169,12 +170,22 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.224</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-dynamo</artifactId>
<version>0.22.3</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
12 changes: 9 additions & 3 deletions s3auth-hosts/src/main/java/com/s3auth/hosts/BucketMocker.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@ public final class BucketMocker {
.builder();

/**
* Public ctor.
* Init.
* @return This object
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
public BucketMocker() {
public BucketMocker init() {
this.withName("maven.s3auth.com");
this.withBucket("s3auth");
this.withRegion("ap-southeast-1");
this.withKey("AAAAAAAAAAAAAAAAAAAA");
this.withSecret("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
this.withClient(new MkAmazonS3());
return this;
}

/**
Expand Down Expand Up @@ -127,6 +128,11 @@ public Bucket mock() {
return this.bucket.build();
}

/**
* Mock.
*
* @since 0.0.1
*/
@Builder
@SuppressWarnings({ "PMD.TooManyMethods",
"PMD.AvoidFieldNameMatchingMethodName" })
Expand Down
33 changes: 21 additions & 12 deletions s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultDynamo.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,11 @@ final class DefaultDynamo implements Dynamo {
/**
* Public ctor.
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
DefaultDynamo() {
Credentials creds = new Credentials.Simple(
Manifests.read("S3Auth-AwsDynamoKey"),
Manifests.read("S3Auth-AwsDynamoSecret")
this(
new ReRegion(new Region.Simple(DefaultDynamo.creds())),
Manifests.read("S3Auth-AwsDynamoTable")
);
if (Manifests.exists("S3Auth-AwsDynamoEntryPoint")) {
creds = new Credentials.Direct(
Credentials.Simple.class.cast(creds),
Manifests.read("S3Auth-AwsDynamoEntryPoint")
);
}
this.region = new ReRegion(new Region.Simple(creds));
this.table = Manifests.read("S3Auth-AwsDynamoTable");
}

/**
Expand Down Expand Up @@ -206,6 +197,24 @@ public boolean remove(@NotNull final Domain domain) {
return removed;
}

/**
* Create AWS credentials.
* @return Creds
*/
private static Credentials creds() {
Credentials creds = new Credentials.Simple(
Manifests.read("S3Auth-AwsDynamoKey"),
Manifests.read("S3Auth-AwsDynamoSecret")
);
if (Manifests.exists("S3Auth-AwsDynamoEntryPoint")) {
creds = new Credentials.Direct(
Credentials.Simple.class.cast(creds),
Manifests.read("S3Auth-AwsDynamoEntryPoint")
);
}
return creds;
}

/**
* Turn item into domain.
* @param item Item
Expand Down
7 changes: 6 additions & 1 deletion s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
* Default implementation of {@link Host}.
*
* @since 0.0.1
* @checkstyle ClassFanOutComplexityCheck (1000 lines)
*/
@Immutable
@EqualsAndHashCode(of = "bucket")
Expand Down Expand Up @@ -267,6 +268,7 @@ public Stats stats() {
* Convert URI to all possible S3 object names (in order of importance).
* @param uri The URI
* @return Object names
* @checkstyle NonStaticMethodCheck (20 lines)
*/
private Iterable<DefaultHost.ObjectName> names(final URI uri) {
final String name = StringUtils.strip(uri.getPath(), "/");
Expand All @@ -281,6 +283,8 @@ private Iterable<DefaultHost.ObjectName> names(final URI uri) {

/**
* Object name with a suffix from a bucket.
*
* @since 0.0.1
*/
@Loggable(Loggable.DEBUG)
private final class NameWithSuffix implements DefaultHost.ObjectName {
Expand Down Expand Up @@ -322,6 +326,7 @@ public String get() {
text.append(suffix);
return text.toString();
}

@Override
public String toString() {
return String.format("%s+suffix", this.origin);
Expand Down Expand Up @@ -376,7 +381,7 @@ private final class HostStats implements Stats {
* Public ctor.
* @param bckt The name of the bucket
*/
public HostStats(final String bckt) {
HostStats(final String bckt) {
this.bucket = bckt;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,15 @@ private long size() {

/**
* Custom IO exception.
*
* @since 0.0.1
*/
private static final class StreamingException extends IOException {
/**
* Serialization marker.
*/
private static final long serialVersionUID = 0x7529FA781E111179L;

/**
* Public ctor.
* @param cause The cause of it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@

/**
* XML Directory Listing.
*
* @since 0.0.1
* @checkstyle ClassDataAbstraction (200 lines)
*/
@Immutable
Expand Down Expand Up @@ -157,6 +159,7 @@ public String etag() {
crc.update(this.content);
return Long.toHexString(crc.getValue());
}

@Override
public Date lastModified() {
return new Date();
Expand Down
38 changes: 25 additions & 13 deletions s3auth-hosts/src/main/java/com/s3auth/hosts/Domain.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,31 @@ public interface Domain {
*/
String syslog();

/**
* Valid.
*
* @since 0.0.1
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = Domain.Validator.class)
@Documented
@interface Valid {
/**
* Message of the validation error.
* @return Message
*/
String message() default "invalid domain";

/**
* Groups.
* @return Groups
*/
Class<?>[] groups() default { };

/**
* Payload.
* @return Payload
*/
Class<? extends Payload>[] payload() default { };
}
Expand All @@ -112,63 +122,65 @@ public interface Domain {
* Validator of Domain.
*
* @since 0.0.1
* @checkstyle CyclomaticComplexity (200 lines)
* @checkstyle ExecutableStatementCount (200 lines)
* @checkstyle NPathComplexityCheck (200 lines)
*/
@SuppressWarnings("PMD.NPathComplexity")
class Validator implements ConstraintValidator<Domain.Valid, Domain> {
@Override
public void initialize(final Domain.Valid annotation) {
//nothing to do
}
// @checkstyle CyclomaticComplexity (60 lines)
// @checkstyle ExecutableStatementCount (60 lines)

@Override
public boolean isValid(final Domain domain,
final ConstraintValidatorContext ctx) {
boolean isValid = true;
boolean valid = true;
if (domain.name() == null) {
ctx.buildConstraintViolationWithTemplate(
"Domain name is mandatory and can't be NULL"
).addConstraintViolation();
isValid = false;
valid = false;
} else if (!domain.name().matches("\\s*[a-zA-Z0-9\\-.]+\\s*")) {
ctx.buildConstraintViolationWithTemplate(
String.format("Invalid domain name '%s'", domain.name())
).addPropertyNode("name").addConstraintViolation();
isValid = false;
valid = false;
}
if (domain.key() == null) {
ctx.buildConstraintViolationWithTemplate(
"AWS key is mandatory and can't be NULL"
).addConstraintViolation();
isValid = false;
valid = false;
} else if (!domain.key().matches("\\s*[A-Z0-9]{20}\\s*")) {
ctx.buildConstraintViolationWithTemplate(
String.format("Invalid AWS key '%s'", domain.key())
).addPropertyNode("key").addConstraintViolation();
isValid = false;
valid = false;
}
if (domain.region() == null) {
ctx.buildConstraintViolationWithTemplate(
"AWS S3 region is mandatory and can't be NULL"
).addConstraintViolation();
isValid = false;
valid = false;
} else if (!domain.region().matches("[a-z0-9\\-]*")) {
ctx.buildConstraintViolationWithTemplate(
String.format("Invalid AWS S3 region '%s'", domain.region())
).addPropertyNode("region").addConstraintViolation();
isValid = false;
valid = false;
}
if (domain.secret() == null) {
ctx.buildConstraintViolationWithTemplate(
"AWS secret key is mandatory and can't be NULL"
).addConstraintViolation();
isValid = false;
valid = false;
} else if (!domain.secret()
.matches("\\s*[a-zA-Z0-9\\+/]{40}\\s*")) {
ctx.buildConstraintViolationWithTemplate(
String.format("Invalid AWS secret '%s'", domain.secret())
).addPropertyNode("secret").addConstraintViolation();
isValid = false;
valid = false;
}
if (domain.syslog() != null
&& !domain.syslog()
Expand All @@ -177,9 +189,9 @@ public boolean isValid(final Domain domain,
ctx.buildConstraintViolationWithTemplate(
String.format("Invalid syslog host '%s'", domain.syslog())
).addPropertyNode("syslog").addConstraintViolation();
isValid = false;
valid = false;
}
return isValid;
return valid;
}
}

Expand Down
17 changes: 14 additions & 3 deletions s3auth-hosts/src/main/java/com/s3auth/hosts/DomainMocker.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ public final class DomainMocker {
.builder();

/**
* Public ctor.
* Init.
* @return This object
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
public DomainMocker() {
public DomainMocker init() {
this.withName("localhost");
this.withRegion("us-east-1");
this.withBucket("bucket");
this.withKey("AAAAAAAAAAAAAAAAAAAA");
this.withSecret("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
this.withSyslog("localhost:514");
return this;
}

/**
Expand Down Expand Up @@ -126,6 +127,11 @@ public Domain mock() {
return this.domain.build();
}

/**
* Mock.
*
* @since 0.0.1
*/
@Builder
@SuppressWarnings({ "PMD.TooManyMethods",
"PMD.AvoidFieldNameMatchingMethodName" })
Expand All @@ -134,22 +140,27 @@ private static class MkDomain implements Domain {
* The Domain Name.
*/
private final transient String name;

/**
* The Domain key.
*/
private final transient String key;

/**
* The Domain secret.
*/
private final transient String secret;

/**
* The Domain bucket.
*/
private final transient String bucket;

/**
* The Domain region.
*/
private final transient String region;

/**
* The Domain syslog.
*/
Expand Down
2 changes: 2 additions & 0 deletions s3auth-hosts/src/main/java/com/s3auth/hosts/Dynamo.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ interface Dynamo extends Closeable {

/**
* Client to Amazon.
*
* @since 0.0.1
*/
@Immutable
interface Client {
Expand Down
Loading

1 comment on commit fe9afb0

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on fe9afb0 Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 90-43f9c549 disappeared from s3auth-hosts/src/test/java/com/s3auth/hosts/DefaultDynamoTest.java), that's why I closed #352. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.