Skip to content

Commit

Permalink
#406 more
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Nov 5, 2023
1 parent 1eb88ba commit 7c97d39
Show file tree
Hide file tree
Showing 37 changed files with 275 additions and 206 deletions.
6 changes: 6 additions & 0 deletions s3auth-hosts/src/main/java/com/s3auth/hosts/BucketMocker.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,26 +135,32 @@ private static class MkBucket implements Bucket {
* The Bucket Name.
*/
private final transient String name;

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

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

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

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

/**
* The Bucket syslog.
*/
private final transient String syslog;

/**
* The Bucket client.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,32 @@ final class DefaultDomain implements Domain {
public String toString() {
return String.format("%s/%s", name, region);
}

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

@Override
public String key() {
return key;
}

@Override
public String secret() {
return secret;
}

@Override
public String bucket() {
return bucket;
}

@Override
public String region() {
return region;
}

@Override
public String syslog() {
return syslog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ final class DefaultDynamo implements Dynamo {
/**
* Public ctor.
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
DefaultDynamo() {
Credentials creds = new Credentials.Simple(
Manifests.read("S3Auth-AwsDynamoKey"),
Expand Down
13 changes: 13 additions & 0 deletions s3auth-hosts/src/main/java/com/s3auth/hosts/DefaultHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,15 @@ private final class NameWithSuffix implements DefaultHost.ObjectName {
* Original name.
*/
private final transient String origin;

/**
* Public ctor.
* @param name The original name
*/
NameWithSuffix(final String name) {
this.origin = name;
}

@Override
public String get() {
String suffix = null;
Expand Down Expand Up @@ -328,24 +330,29 @@ public String toString() {

/**
* Object name.
*
* @since 0.0.1
*/
@EqualsAndHashCode(of = "name")
private static final class Simple implements DefaultHost.ObjectName {
/**
* Original name.
*/
private final transient String name;

/**
* Public ctor.
* @param nme The name
*/
Simple(final String nme) {
this.name = nme;
}

@Override
public String get() {
return this.name;
}

@Override
public String toString() {
return this.name;
Expand All @@ -354,6 +361,8 @@ public String toString() {

/**
* Stats for this domain.
*
* @since 0.0.1
*/
@Loggable(Loggable.DEBUG)
@EqualsAndHashCode(of = "bucket")
Expand All @@ -362,13 +371,15 @@ private final class HostStats implements Stats {
* The S3 bucket.
*/
private final transient String bucket;

/**
* Public ctor.
* @param bckt The name of the bucket
*/
public HostStats(final String bckt) {
this.bucket = bckt;
}

@Override
@Cacheable(lifetime = 30, unit = TimeUnit.MINUTES)
public long bytesTransferred() {
Expand Down Expand Up @@ -399,6 +410,8 @@ public long bytesTransferred() {

/**
* Name of an S3 Object, context dependent.
*
* @since 0.0.1
*/
private interface ObjectName {
/**
Expand Down
20 changes: 11 additions & 9 deletions s3auth-hosts/src/main/java/com/s3auth/hosts/Domain.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public interface Domain {

/**
* Region of S3 bucket.
* @return Region name/endpoint, e.g. "us-east-1"
* @return Region name/endpoint, e.g. "s3-us-east-1"
* @see <a href="https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region">S3 Regions</a>
*/
String region();
Expand Down Expand Up @@ -110,6 +110,8 @@ public interface Domain {

/**
* Validator of Domain.
*
* @since 0.0.1
*/
@SuppressWarnings("PMD.NPathComplexity")
class Validator implements ConstraintValidator<Domain.Valid, Domain> {
Expand All @@ -125,12 +127,12 @@ public boolean isValid(final Domain domain,
boolean isValid = true;
if (domain.name() == null) {
ctx.buildConstraintViolationWithTemplate(
"domain name is mandatory and can't be NULL"
"Domain name is mandatory and can't be NULL"
).addConstraintViolation();
isValid = false;
} else if (!domain.name().matches("\\s*[a-zA-Z0-9\\-\\.]+\\s*")) {
} else if (!domain.name().matches("\\s*[a-zA-Z0-9\\-.]+\\s*")) {
ctx.buildConstraintViolationWithTemplate(
String.format("invalid domain name '%s'", domain.name())
String.format("Invalid domain name '%s'", domain.name())
).addPropertyNode("name").addConstraintViolation();
isValid = false;
}
Expand All @@ -141,7 +143,7 @@ public boolean isValid(final Domain domain,
isValid = false;
} else if (!domain.key().matches("\\s*[A-Z0-9]{20}\\s*")) {
ctx.buildConstraintViolationWithTemplate(
String.format("invalid AWS key '%s'", domain.key())
String.format("Invalid AWS key '%s'", domain.key())
).addPropertyNode("key").addConstraintViolation();
isValid = false;
}
Expand All @@ -150,9 +152,9 @@ public boolean isValid(final Domain domain,
"AWS S3 region is mandatory and can't be NULL"
).addConstraintViolation();
isValid = false;
} else if (!domain.region().matches("s3[a-z0-9\\-]*")) {
} else if (!domain.region().matches("[a-z0-9\\-]*")) {
ctx.buildConstraintViolationWithTemplate(
String.format("invalid AWS S3 region '%s'", domain.region())
String.format("Invalid AWS S3 region '%s'", domain.region())
).addPropertyNode("region").addConstraintViolation();
isValid = false;
}
Expand All @@ -164,7 +166,7 @@ public boolean isValid(final Domain domain,
} else if (!domain.secret()
.matches("\\s*[a-zA-Z0-9\\+/]{40}\\s*")) {
ctx.buildConstraintViolationWithTemplate(
String.format("invalid AWS secret '%s'", domain.secret())
String.format("Invalid AWS secret '%s'", domain.secret())
).addPropertyNode("secret").addConstraintViolation();
isValid = false;
}
Expand All @@ -173,7 +175,7 @@ public boolean isValid(final Domain domain,
.matches("\\s*[a-zA-Z0-9\\-\\.]+(:\\d+)?\\s*")
) {
ctx.buildConstraintViolationWithTemplate(
String.format("invalid syslog host '%s'", domain.syslog())
String.format("Invalid syslog host '%s'", domain.syslog())
).addPropertyNode("syslog").addConstraintViolation();
isValid = false;
}
Expand Down
9 changes: 9 additions & 0 deletions s3auth-hosts/src/main/java/com/s3auth/hosts/DynamoHosts.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,21 @@ private Domain byName(final String name) throws IOException {

/**
* Wrap of domains.
*
* @since 0.0.1
*/
@Loggable(Loggable.DEBUG)
private final class Wrap extends AbstractSet<Domain> {
/**
* User.
*/
private final transient User user;

/**
* Domains.
*/
private final transient Domains domains;

/**
* Public ctor.
* @param usr User
Expand All @@ -219,22 +223,27 @@ private final class Wrap extends AbstractSet<Domain> {
this.user = usr;
this.domains = dmns;
}

@Override
public int size() {
return this.domains.size();
}

@Override
public Iterator<Domain> iterator() {
return this.domains.iterator();
}

@Override
public boolean contains(final Object obj) {
return this.domains.contains(obj);
}

@Override
public boolean add(@NotNull @Valid final Domain domain) {
return DynamoHosts.this.add(this.user.identity(), domain);
}

@Override
public boolean remove(final Object obj) {
return DynamoHosts.this.remove(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
package com.s3auth.hosts;

import com.jcabi.urn.URN;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -61,13 +60,15 @@ public Dynamo mock() {
public Map<URN, Domains> load() {
return Collections.unmodifiableMap(DynamoMocker.this.users);
}

@Override
public boolean add(final URN user, final Domain domain) {
DynamoMocker.this.users.putIfAbsent(user, new Domains());
DynamoMocker.this.users.get(user)
.add(new DefaultDomain(domain));
return true;
}

@Override
public boolean remove(final Domain domain) {
for (final Set<Domain> domains
Expand All @@ -77,6 +78,7 @@ public boolean remove(final Domain domain) {
}
return true;
}

@Override
public void close() {
assert this != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ final class H2DomainStatsData implements DomainStatsData {
H2DomainStatsData(final File file) throws IOException {
this.jdbc = String.format("jdbc:h2:file:%s", file.getAbsolutePath());
try {
this.session().sql(CREATE).execute().commit();
this.session().sql(H2DomainStatsData.CREATE).execute().commit();
} catch (final SQLException ex) {
throw new IOException(ex);
}
Expand All @@ -120,7 +120,7 @@ final class H2DomainStatsData implements DomainStatsData {
public void put(final String domain, final Stats stats) throws IOException {
try {
this.session()
.sql(INSERT)
.sql(H2DomainStatsData.INSERT)
.set(domain)
.set(stats.bytesTransferred())
.execute()
Expand All @@ -138,7 +138,7 @@ public Stats get(final String domain) throws IOException {
final Stats result = session
.sql("SELECT SUM(BYTES) FROM DOMAIN_STATS WHERE DOMAIN = ?")
.set(domain)
.select(STATS);
.select(H2DomainStatsData.STATS);
session.sql("DELETE FROM DOMAIN_STATS WHERE DOMAIN = ?")
.set(domain)
.execute()
Expand All @@ -157,7 +157,7 @@ public Map<String, Stats> all() throws IOException {
// @checkstyle LineLength (2 lines)
final Map<String, Stats> result = session
.sql("SELECT DOMAIN, SUM(BYTES) FROM DOMAIN_STATS GROUP BY DOMAIN")
.select(STATS_ALL);
.select(H2DomainStatsData.STATS_ALL);
session.sql("DELETE FROM DOMAIN_STATS").execute().commit();
return result;
} catch (final SQLException ex) {
Expand Down
10 changes: 9 additions & 1 deletion s3auth-hosts/src/main/java/com/s3auth/hosts/HostMocker.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
*/
package com.s3auth.hosts;

import java.io.IOException;
import java.net.URI;
import lombok.Builder;

Expand Down Expand Up @@ -84,6 +83,11 @@ public Host mock() {
return this.host.build();
}

/**
* Mock.
*
* @since 0.0.1
*/
@Builder
@SuppressWarnings({ "PMD.TooManyMethods",
"PMD.AvoidFieldNameMatchingMethodName" })
Expand All @@ -92,18 +96,22 @@ private static class MkHost implements Host {
* The host resource.
*/
private final transient Resource resource;

/**
* Whether the host authorized.
*/
private final transient boolean authorized;

/**
* Whether the host is hidden.
*/
private final transient boolean hidden;

/**
* The host syslog.
*/
private final transient String syslog;

/**
* The host stats.
*/
Expand Down
Loading

0 comments on commit 7c97d39

Please sign in to comment.