Skip to content

Commit

Permalink
Derive value of opc path for the IMDS url
Browse files Browse the repository at this point in the history
  • Loading branch information
klustria committed Jan 5, 2024
1 parent 51cfddf commit 84b4c58
Showing 1 changed file with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
import com.oracle.bmc.Region;
import jakarta.inject.Singleton;

import static com.oracle.bmc.auth.AbstractFederationClientAuthenticationDetailsProviderBuilder.METADATA_SERVICE_BASE_URL;

/**
* This (overridable) implementation will check the {@link OciConfig} for {@code IMDS} availability. And if it is found to be
* available, will also perform a secondary check on {@link Region#getRegionFromImds()} to ensure it returns a non-null value.
*/
@Singleton
@Weight(ServiceInfoBasics.DEFAULT_INJECT_WEIGHT)
class OciAvailabilityDefault implements OciAvailability {
static String OPC_PATH = getOpcPath();

@Override
public boolean isRunningOnOci(OciConfig ociConfig) {
Expand All @@ -46,7 +49,7 @@ static boolean runningOnOci(OciConfig ociConfig) {
return false;
}

return (Region.getRegionFromImds("http://" + ociConfig.imdsHostName() + "/opc/v2/") != null);
return (Region.getRegionFromImds("http://" + ociConfig.imdsHostName() + OPC_PATH) != null);
}

static boolean canReach(String address,
Expand All @@ -71,4 +74,15 @@ static boolean canReach(String address,
}
}

static String getOpcPath() {
String input = METADATA_SERVICE_BASE_URL;
int index = -1;
for (int nth = 3; nth > 0; nth--) {
index = input.indexOf("/", index + 1);
if (index == -1) {
return null;
}
}
return METADATA_SERVICE_BASE_URL.substring(index);
}
}

0 comments on commit 84b4c58

Please sign in to comment.