Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 72cf14c

Browse files
authored
S3 credentials should be optional (#501)
1 parent 5a2b260 commit 72cf14c

File tree

5 files changed

+94
-42
lines changed

5 files changed

+94
-42
lines changed

asto-core/src/main/java/com/artipie/asto/factory/Config.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public interface Config {
4242
*/
4343
Config config(String key);
4444

45+
/**
46+
* Checks that there is a config data.
47+
*
48+
* @return True if no config data.
49+
*/
50+
boolean isEmpty();
51+
4552
/**
4653
* Strict storage config throws {@code NullPointerException} when value is not exist.
4754
*
@@ -86,6 +93,11 @@ public Config config(final String key) {
8693
String.format("No config found for key %s", key)
8794
);
8895
}
96+
97+
@Override
98+
public boolean isEmpty() {
99+
return this.original == null || this.original.isEmpty();
100+
}
89101
}
90102

91103
/**
@@ -149,6 +161,11 @@ public Config config(final String key) {
149161
return new YamlStorageConfig(this.original.yamlMapping(key));
150162
}
151163

164+
@Override
165+
public boolean isEmpty() {
166+
return this.original == null || this.original.isEmpty();
167+
}
168+
152169
@Override
153170
public String toString() {
154171
return this.original.toString();

asto-s3/src/main/java/com/artipie/asto/s3/S3StorageFactory.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,40 +45,43 @@ private static S3AsyncClient s3Client(final Config cfg) {
4545
final S3AsyncClientBuilder builder = S3AsyncClient.builder();
4646
Optional.ofNullable(cfg.string("region")).ifPresent(val -> builder.region(Region.of(val)));
4747
endpoint(cfg).ifPresent(val -> builder.endpointOverride(URI.create(val)));
48-
return builder
49-
.credentialsProvider(
50-
S3StorageFactory.credentials(
51-
new Config.StrictStorageConfig(cfg)
52-
.config("credentials")
53-
)
54-
)
55-
.build();
48+
setCredentialsProvider(builder, cfg);
49+
return builder.build();
5650
}
5751

5852
/**
59-
* Creates {@link StaticCredentialsProvider} instance based on config.
53+
* Sets a credentials provider into the passed builder.
6054
*
61-
* @param cred Credentials config.
62-
* @return Credentials provider.
55+
* @param builder Builder.
56+
* @param cfg S3 storage configuration.
6357
*/
64-
private static StaticCredentialsProvider credentials(final Config cred) {
65-
final String type = cred.string("type");
66-
if ("basic".equals(type)) {
67-
return StaticCredentialsProvider.create(
68-
AwsBasicCredentials.create(
69-
cred.string("accessKeyId"),
70-
cred.string("secretAccessKey")
71-
)
72-
);
73-
} else {
74-
throw new IllegalArgumentException(
75-
String.format("Unsupported S3 credentials type: %s", type)
76-
);
58+
private static void setCredentialsProvider(
59+
final S3AsyncClientBuilder builder,
60+
final Config cfg
61+
) {
62+
final Config credentials = cfg.config("credentials");
63+
if (!credentials.isEmpty()) {
64+
final String type = credentials.string("type");
65+
if ("basic".equals(type)) {
66+
builder.credentialsProvider(
67+
StaticCredentialsProvider.create(
68+
AwsBasicCredentials.create(
69+
credentials.string("accessKeyId"),
70+
credentials.string("secretAccessKey")
71+
)
72+
)
73+
);
74+
} else {
75+
throw new IllegalArgumentException(
76+
String.format("Unsupported S3 credentials type: %s", type)
77+
);
78+
}
7779
}
7880
}
7981

8082
/**
8183
* Obtain endpoint from storage config. The parameter is optional.
84+
*
8285
* @param cfg Storage config
8386
* @return Endpoint value is present
8487
*/

asto-s3/src/test/java/com/artipie/asto/S3StorageFactoryTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public final class S3StorageFactoryTest {
2626
* @checkstyle MethodNameCheck (3 lines)
2727
*/
2828
@Test
29-
void shouldCreateS3Storage() {
29+
void shouldCreateS3StorageConfigHasCredentials() {
3030
MatcherAssert.assertThat(
3131
new StoragesLoader()
3232
.newObject(
@@ -50,4 +50,27 @@ void shouldCreateS3Storage() {
5050
new IsInstanceOf(S3Storage.class)
5151
);
5252
}
53+
54+
/**
55+
* Test for S3 storage factory.
56+
*
57+
* @checkstyle MethodNameCheck (3 lines)
58+
*/
59+
@Test
60+
void shouldCreateS3StorageConfigDoesNotHaveCredentials() {
61+
MatcherAssert.assertThat(
62+
new StoragesLoader()
63+
.newObject(
64+
"s3",
65+
new Config.YamlStorageConfig(
66+
Yaml.createYamlMappingBuilder()
67+
.add("region", "us-east-1")
68+
.add("bucket", "aaa")
69+
.add("endpoint", "http://localhost")
70+
.build()
71+
)
72+
),
73+
new IsInstanceOf(S3Storage.class)
74+
);
75+
}
5376
}

asto-s3/src/test/java/com/artipie/asto/s3/S3HeadMetaTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void readSize() {
2525
.contentLength(len)
2626
.eTag("empty")
2727
.build()
28-
).read(Meta.OP_SIZE).get(),
28+
).read(Meta.OP_SIZE).orElseThrow(IllegalStateException::new),
2929
new IsEqual<>(len)
3030
);
3131
}
@@ -39,7 +39,7 @@ void readHash() {
3939
.contentLength(0L)
4040
.eTag(hash)
4141
.build()
42-
).read(Meta.OP_MD5).get(),
42+
).read(Meta.OP_MD5).orElseThrow(IllegalStateException::new),
4343
new IsEqual<>(hash)
4444
);
4545
}

asto-s3/src/test/java/com/artipie/asto/s3/S3StorageTest.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@
1010
import com.amazonaws.services.s3.model.MultipartUpload;
1111
import com.amazonaws.services.s3.model.ObjectMetadata;
1212
import com.amazonaws.services.s3.model.S3Object;
13+
import com.amihaiemil.eoyaml.Yaml;
1314
import com.artipie.asto.Content;
1415
import com.artipie.asto.Key;
1516
import com.artipie.asto.Meta;
17+
import com.artipie.asto.Storage;
1618
import com.artipie.asto.blocking.BlockingStorage;
19+
import com.artipie.asto.factory.Config;
20+
import com.artipie.asto.factory.StoragesLoader;
1721
import com.google.common.io.ByteStreams;
1822
import io.reactivex.Flowable;
1923
import java.io.ByteArrayInputStream;
2024
import java.io.IOException;
21-
import java.net.URI;
2225
import java.util.Arrays;
2326
import java.util.Collection;
2427
import java.util.List;
@@ -35,10 +38,6 @@
3538
import org.junit.jupiter.api.condition.DisabledOnOs;
3639
import org.junit.jupiter.api.condition.OS;
3740
import org.junit.jupiter.api.extension.RegisterExtension;
38-
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
39-
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
40-
import software.amazon.awssdk.regions.Region;
41-
import software.amazon.awssdk.services.s3.S3AsyncClient;
4241

4342
/**
4443
* Tests for {@link S3Storage}.
@@ -258,15 +257,25 @@ private byte[] download(final AmazonS3 client, final String key) throws IOExcept
258257
}
259258
}
260259

261-
private S3Storage storage() {
262-
final String endpoint = String.format("http://localhost:%d", MOCK.getHttpPort());
263-
final S3AsyncClient client = S3AsyncClient.builder()
264-
.region(Region.of("us-east-1"))
265-
.credentialsProvider(
266-
StaticCredentialsProvider.create(AwsBasicCredentials.create("foo", "bar"))
267-
)
268-
.endpointOverride(URI.create(endpoint))
269-
.build();
270-
return new S3Storage(client, this.bucket, endpoint);
260+
private Storage storage() {
261+
return new StoragesLoader()
262+
.newObject(
263+
"s3",
264+
new Config.YamlStorageConfig(
265+
Yaml.createYamlMappingBuilder()
266+
.add("region", "us-east-1")
267+
.add("bucket", this.bucket)
268+
.add("endpoint", String.format("http://localhost:%d", MOCK.getHttpPort()))
269+
.add(
270+
"credentials",
271+
Yaml.createYamlMappingBuilder()
272+
.add("type", "basic")
273+
.add("accessKeyId", "foo")
274+
.add("secretAccessKey", "bar")
275+
.build()
276+
)
277+
.build()
278+
)
279+
);
271280
}
272281
}

0 commit comments

Comments
 (0)