Skip to content

Commit

Permalink
Add workaround for nip.io for macOS, optimize for Linux (#8759)
Browse files Browse the repository at this point in the history
On Linux, the `MinioContainer` always uses `localhost` (or `<bucket-name>.localhost` respectively). On macOS it tries to use `.nip.io` - if that's not availabile it falls back to `localhost` and verified that `<bucket-name>.localhost` can be resolved.
  • Loading branch information
snazy authored Jun 6, 2024
1 parent 5a9b00b commit e4660d1
Showing 1 changed file with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,55 @@ public final class MinioContainer extends GenericContainer<MinioContainer>
*/
private static final String MINIO_DOMAIN_NIP = "s3.127-0-0-1.nip.io";

/**
* Whether random bucket names cannot be used. Randomized bucket names can only be used when
* either `*.localhost` (on Linux) or `*.s3.127-0-0-1.nip.io` (on macOS, if DNS rebind protection
* is not active) can be resolved. Otherwise we have to use a fixed bucket name and users have to
* configure that in `/etc/hosts`.
*/
private static final String FIXED_BUCKET_NAME;

static boolean canRunOnMacOs() {
return MINIO_DOMAIN_NAME.equals(MINIO_DOMAIN_NIP);
}

static {
String name;
try {
InetAddress ignored = InetAddress.getByName(MINIO_DOMAIN_NIP);
name = MINIO_DOMAIN_NIP;
} catch (UnknownHostException e) {
LOGGER.warn(
"Could not resolve '{}', falling back to 'localhost'. "
+ "This usually happens when your router or DNS provider is unable to resolve the nip.io addresses.",
MINIO_DOMAIN_NIP);
String fixedBucketName = null;
if (System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("linux")) {
name = "localhost";
} else {
try {
InetAddress ignored = InetAddress.getByName(MINIO_DOMAIN_NIP);
name = MINIO_DOMAIN_NIP;
} catch (UnknownHostException x) {
LOGGER.warn(
"Could not resolve '{}', falling back to 'localhost'. "
+ "This usually happens when your router or DNS provider is unable to resolve the nip.io addresses.",
MINIO_DOMAIN_NIP);
name = "localhost";
fixedBucketName = "miniobucket";
validateBucketHost(fixedBucketName);
}
}
MINIO_DOMAIN_NAME = name;
FIXED_BUCKET_NAME = fixedBucketName;
}

/** Validates the bucket host name, on non-Linux, if necessary. */
private static String validateBucketHost(String bucketName) {
if (FIXED_BUCKET_NAME != null) {
String test = bucketName + ".localhost";
try {
InetAddress ignored = InetAddress.getByName(test);
} catch (UnknownHostException e) {
LOGGER.warn(
"Could not resolve '{}',\n Please add the line \n '127.0.0.1 {}'\n to your local '/etc/hosts' file.\n Tests are expected to fail unless name resolution works.",
test,
test);
}
}
return bucketName;
}

private final String accessKey;
Expand Down Expand Up @@ -106,7 +138,10 @@ public MinioContainer(String image, String accessKey, String secretKey, String b
addExposedPort(DEFAULT_PORT);
this.accessKey = accessKey != null ? accessKey : randomString("access");
this.secretKey = secretKey != null ? secretKey : randomString("secret");
this.bucket = bucket != null ? bucket : randomString("bucket");
this.bucket =
bucket != null
? validateBucketHost(bucket)
: (FIXED_BUCKET_NAME != null ? FIXED_BUCKET_NAME : randomString("bucket"));
withEnv(MINIO_ACCESS_KEY, this.accessKey);
withEnv(MINIO_SECRET_KEY, this.secretKey);
// S3 SDK encodes bucket names in host names - need to tell Minio which domain to use
Expand Down

0 comments on commit e4660d1

Please sign in to comment.