This commit is contained in:
+22
-5
@@ -28,6 +28,12 @@ public class MinioStorageConfig {
|
|||||||
*/
|
*/
|
||||||
private String publicEndpoint;
|
private String publicEndpoint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Optional MinIO region used when signing URLs. When blank, the region is resolved once
|
||||||
|
* through the internal endpoint so that signing never depends on public DNS.
|
||||||
|
*/
|
||||||
|
private String region;
|
||||||
|
|
||||||
private String bucketName;
|
private String bucketName;
|
||||||
|
|
||||||
private String accessKey;
|
private String accessKey;
|
||||||
@@ -43,22 +49,33 @@ public class MinioStorageConfig {
|
|||||||
return buildClient(endpoint);
|
return buildClient(endpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
public MinioClient createSigningClient() {
|
/**
|
||||||
|
* Builds the client used to sign URLs. The region must be passed explicitly, otherwise the
|
||||||
|
* MinIO SDK queries the endpoint for the bucket region before signing.
|
||||||
|
*/
|
||||||
|
public MinioClient createSigningClient(String signingRegion) {
|
||||||
if (!Boolean.TRUE.equals(enabled) || !StringUtils.hasText(publicEndpoint)) {
|
if (!Boolean.TRUE.equals(enabled) || !StringUtils.hasText(publicEndpoint)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return buildClient(publicEndpoint);
|
return buildClient(publicEndpoint, signingRegion);
|
||||||
}
|
}
|
||||||
|
|
||||||
private MinioClient buildClient(String clientEndpoint) {
|
private MinioClient buildClient(String clientEndpoint) {
|
||||||
|
return buildClient(clientEndpoint, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private MinioClient buildClient(String clientEndpoint, String clientRegion) {
|
||||||
if (!StringUtils.hasText(clientEndpoint)
|
if (!StringUtils.hasText(clientEndpoint)
|
||||||
|| !StringUtils.hasText(accessKey)
|
|| !StringUtils.hasText(accessKey)
|
||||||
|| !StringUtils.hasText(secretKey)) {
|
|| !StringUtils.hasText(secretKey)) {
|
||||||
throw new IllegalStateException("MinIO is enabled but endpoint/accessKey/secretKey is incomplete");
|
throw new IllegalStateException("MinIO is enabled but endpoint/accessKey/secretKey is incomplete");
|
||||||
}
|
}
|
||||||
return MinioClient.builder()
|
MinioClient.Builder builder = MinioClient.builder()
|
||||||
.endpoint(clientEndpoint)
|
.endpoint(clientEndpoint)
|
||||||
.credentials(accessKey, secretKey)
|
.credentials(accessKey, secretKey);
|
||||||
.build();
|
if (StringUtils.hasText(clientRegion)) {
|
||||||
|
builder.region(clientRegion.trim());
|
||||||
|
}
|
||||||
|
return builder.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+63
-2
@@ -17,6 +17,8 @@ import org.springframework.util.StringUtils;
|
|||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@@ -24,10 +26,20 @@ public class MinioObjectStorageProvider implements ObjectStorageProvider {
|
|||||||
|
|
||||||
private static final long UNKNOWN_SIZE_PART_SIZE = 10 * 1024 * 1024L;
|
private static final long UNKNOWN_SIZE_PART_SIZE = 10 * 1024 * 1024L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Region probe key: only used to compute a signature locally, no object is written or read.
|
||||||
|
*/
|
||||||
|
private static final String REGION_PROBE_OBJECT = "integration-probe/region-probe.txt";
|
||||||
|
|
||||||
|
private static final Pattern CREDENTIAL_SCOPE = Pattern.compile(
|
||||||
|
"X-Amz-Credential=[^&]*?%2F\\d{8}%2F([^%&]+)%2Fs3%2Faws4_request", Pattern.CASE_INSENSITIVE);
|
||||||
|
|
||||||
private final ObjectProvider<MinioClient> minioClientProvider;
|
private final ObjectProvider<MinioClient> minioClientProvider;
|
||||||
private final MinioClient signingClient;
|
private final MinioClient signingClient;
|
||||||
private final MinioStorageConfig minioStorageConfig;
|
private final MinioStorageConfig minioStorageConfig;
|
||||||
private final ObjectStorageProperties objectStorageProperties;
|
private final ObjectStorageProperties objectStorageProperties;
|
||||||
|
private volatile MinioClient resolvedSigningClient;
|
||||||
|
private volatile String resolvedSigningRegion;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public MinioObjectStorageProvider(
|
public MinioObjectStorageProvider(
|
||||||
@@ -35,7 +47,7 @@ public class MinioObjectStorageProvider implements ObjectStorageProvider {
|
|||||||
MinioStorageConfig minioStorageConfig,
|
MinioStorageConfig minioStorageConfig,
|
||||||
ObjectStorageProperties objectStorageProperties
|
ObjectStorageProperties objectStorageProperties
|
||||||
) {
|
) {
|
||||||
this(minioClientProvider, minioStorageConfig, objectStorageProperties, minioStorageConfig.createSigningClient());
|
this(minioClientProvider, minioStorageConfig, objectStorageProperties, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
MinioObjectStorageProvider(
|
MinioObjectStorageProvider(
|
||||||
@@ -107,7 +119,7 @@ public class MinioObjectStorageProvider implements ObjectStorageProvider {
|
|||||||
if (!Boolean.TRUE.equals(minioStorageConfig.getPrivateBucket())) {
|
if (!Boolean.TRUE.equals(minioStorageConfig.getPrivateBucket())) {
|
||||||
return getFileUrl(objectName);
|
return getFileUrl(objectName);
|
||||||
}
|
}
|
||||||
return (signingClient == null ? requireClient() : signingClient).getPresignedObjectUrl(
|
return signingClientForPresign().getPresignedObjectUrl(
|
||||||
GetPresignedObjectUrlArgs.builder()
|
GetPresignedObjectUrlArgs.builder()
|
||||||
.method(Method.GET)
|
.method(Method.GET)
|
||||||
.bucket(minioStorageConfig.getBucketName())
|
.bucket(minioStorageConfig.getBucketName())
|
||||||
@@ -117,6 +129,55 @@ public class MinioObjectStorageProvider implements ObjectStorageProvider {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private MinioClient signingClientForPresign() throws Exception {
|
||||||
|
if (signingClient != null) {
|
||||||
|
return signingClient;
|
||||||
|
}
|
||||||
|
MinioClient cached = resolvedSigningClient;
|
||||||
|
if (cached != null) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
synchronized (this) {
|
||||||
|
if (resolvedSigningClient == null) {
|
||||||
|
MinioClient client = StringUtils.hasText(minioStorageConfig.getPublicEndpoint())
|
||||||
|
? minioStorageConfig.createSigningClient(resolveSigningRegion())
|
||||||
|
: null;
|
||||||
|
resolvedSigningClient = client == null ? requireClient() : client;
|
||||||
|
}
|
||||||
|
return resolvedSigningClient;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves the bucket region through the internal endpoint, so that signing does not depend on
|
||||||
|
* the public endpoint being resolvable from this host.
|
||||||
|
*/
|
||||||
|
private String resolveSigningRegion() throws Exception {
|
||||||
|
String configured = minioStorageConfig.getRegion();
|
||||||
|
if (StringUtils.hasText(configured)) {
|
||||||
|
return configured.trim();
|
||||||
|
}
|
||||||
|
if (resolvedSigningRegion != null) {
|
||||||
|
return resolvedSigningRegion;
|
||||||
|
}
|
||||||
|
String probeUrl = requireClient().getPresignedObjectUrl(
|
||||||
|
GetPresignedObjectUrlArgs.builder()
|
||||||
|
.method(Method.GET)
|
||||||
|
.bucket(minioStorageConfig.getBucketName())
|
||||||
|
.object(REGION_PROBE_OBJECT)
|
||||||
|
.expiry(1, TimeUnit.MINUTES)
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
Matcher matcher = CREDENTIAL_SCOPE.matcher(probeUrl == null ? "" : probeUrl);
|
||||||
|
if (!matcher.find()) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"MinIO region cannot be resolved from internal endpoint, please configure minio.region");
|
||||||
|
}
|
||||||
|
resolvedSigningRegion = matcher.group(1);
|
||||||
|
log.info("MinIO signing region resolved through internal endpoint, region={}", resolvedSigningRegion);
|
||||||
|
return resolvedSigningRegion;
|
||||||
|
}
|
||||||
|
|
||||||
private MinioClient requireClient() {
|
private MinioClient requireClient() {
|
||||||
MinioClient minioClient = minioClientProvider.getIfAvailable();
|
MinioClient minioClient = minioClientProvider.getIfAvailable();
|
||||||
if (minioClient == null) {
|
if (minioClient == null) {
|
||||||
|
|||||||
+38
@@ -51,6 +51,44 @@ class MinioObjectStorageProviderTest {
|
|||||||
verify(signingClient).getPresignedObjectUrl(org.mockito.ArgumentMatchers.any(GetPresignedObjectUrlArgs.class));
|
verify(signingClient).getPresignedObjectUrl(org.mockito.ArgumentMatchers.any(GetPresignedObjectUrlArgs.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void resolvesSigningRegionThroughInternalEndpointInsteadOfPublicEndpoint() throws Exception {
|
||||||
|
MinioClient internalClient = mock(MinioClient.class);
|
||||||
|
ObjectProvider<MinioClient> internalClientProvider = mock(ObjectProvider.class);
|
||||||
|
when(internalClientProvider.getIfAvailable()).thenReturn(internalClient);
|
||||||
|
when(internalClient.getPresignedObjectUrl(org.mockito.ArgumentMatchers.any(GetPresignedObjectUrlArgs.class)))
|
||||||
|
.thenReturn("http://192.168.10.14:9000/jayucloud-salon-sit/integration-probe/region-probe.txt"
|
||||||
|
+ "?X-Amz-Algorithm=AWS4-HMAC-SHA256"
|
||||||
|
+ "&X-Amz-Credential=access-key%2F20261108%2Fcn-sit-1%2Fs3%2Faws4_request"
|
||||||
|
+ "&X-Amz-Date=20261108T000000Z&X-Amz-Expires=60&X-Amz-SignedHeaders=host"
|
||||||
|
+ "&X-Amz-Signature=probe");
|
||||||
|
|
||||||
|
MinioStorageConfig config = new MinioStorageConfig();
|
||||||
|
config.setEnabled(true);
|
||||||
|
config.setEndpoint("http://192.168.10.14:9000");
|
||||||
|
config.setPublicEndpoint("https://sit.minio.yunail.art");
|
||||||
|
config.setAccessKey("access-key");
|
||||||
|
config.setSecretKey("secret-key");
|
||||||
|
config.setBucketName("jayucloud-salon-sit");
|
||||||
|
config.setPrivateBucket(true);
|
||||||
|
|
||||||
|
MinioObjectStorageProvider provider = new MinioObjectStorageProvider(
|
||||||
|
internalClientProvider,
|
||||||
|
config,
|
||||||
|
new ObjectStorageProperties()
|
||||||
|
);
|
||||||
|
|
||||||
|
String signedUrl = provider.getPresignedUrl("style.jpg", 60);
|
||||||
|
String signedAgain = provider.getPresignedUrl("style.jpg", 60);
|
||||||
|
|
||||||
|
assertTrue(signedUrl.startsWith("https://sit.minio.yunail.art/jayucloud-salon-sit/style.jpg?"));
|
||||||
|
assertTrue(signedUrl.contains("X-Amz-Credential=access-key%2F"));
|
||||||
|
assertTrue(signedUrl.contains("cn-sit-1%2Fs3%2Faws4_request"));
|
||||||
|
assertTrue(signedAgain.contains("cn-sit-1%2Fs3%2Faws4_request"));
|
||||||
|
verify(internalClient, org.mockito.Mockito.times(1))
|
||||||
|
.getPresignedObjectUrl(org.mockito.ArgumentMatchers.any(GetPresignedObjectUrlArgs.class));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void usesInternalMinioClientForUploadDownloadAndDelete() throws Exception {
|
void usesInternalMinioClientForUploadDownloadAndDelete() throws Exception {
|
||||||
MinioClient internalClient = mock(MinioClient.class);
|
MinioClient internalClient = mock(MinioClient.class);
|
||||||
|
|||||||
Reference in New Issue
Block a user