From 51cfddf9b786bbeb636869fd22fa767a91f614db Mon Sep 17 00:00:00 2001 From: Keith Lustria Date: Thu, 4 Jan 2024 22:02:24 -0800 Subject: [PATCH 1/4] Fix issue for checking if app is running in an OCI instance which causes Instance Principal auth to fail --- .../integrations/oci/sdk/runtime/OciAvailabilityDefault.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integrations/oci/sdk/runtime/src/main/java/io/helidon/integrations/oci/sdk/runtime/OciAvailabilityDefault.java b/integrations/oci/sdk/runtime/src/main/java/io/helidon/integrations/oci/sdk/runtime/OciAvailabilityDefault.java index 4e55995ff6b..7e17d244e91 100644 --- a/integrations/oci/sdk/runtime/src/main/java/io/helidon/integrations/oci/sdk/runtime/OciAvailabilityDefault.java +++ b/integrations/oci/sdk/runtime/src/main/java/io/helidon/integrations/oci/sdk/runtime/OciAvailabilityDefault.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Oracle and/or its affiliates. + * Copyright (c) 2023, 2024 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,7 +46,7 @@ static boolean runningOnOci(OciConfig ociConfig) { return false; } - return (Region.getRegionFromImds() != null); + return (Region.getRegionFromImds("http://" + ociConfig.imdsHostName() + "/opc/v2/") != null); } static boolean canReach(String address, From 84b4c586a462127206aadb43bf57338166f75817 Mon Sep 17 00:00:00 2001 From: Keith Lustria Date: Fri, 5 Jan 2024 14:41:01 -0800 Subject: [PATCH 2/4] Derive value of opc path for the IMDS url --- .../oci/sdk/runtime/OciAvailabilityDefault.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/integrations/oci/sdk/runtime/src/main/java/io/helidon/integrations/oci/sdk/runtime/OciAvailabilityDefault.java b/integrations/oci/sdk/runtime/src/main/java/io/helidon/integrations/oci/sdk/runtime/OciAvailabilityDefault.java index 7e17d244e91..ef5ca7493c3 100644 --- a/integrations/oci/sdk/runtime/src/main/java/io/helidon/integrations/oci/sdk/runtime/OciAvailabilityDefault.java +++ b/integrations/oci/sdk/runtime/src/main/java/io/helidon/integrations/oci/sdk/runtime/OciAvailabilityDefault.java @@ -28,6 +28,8 @@ 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. @@ -35,6 +37,7 @@ @Singleton @Weight(ServiceInfoBasics.DEFAULT_INJECT_WEIGHT) class OciAvailabilityDefault implements OciAvailability { + static String OPC_PATH = getOpcPath(); @Override public boolean isRunningOnOci(OciConfig ociConfig) { @@ -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, @@ -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); + } } From 8af91867c5393788ab44632778a27e63b4ff1db8 Mon Sep 17 00:00:00 2001 From: Keith Lustria Date: Fri, 5 Jan 2024 16:29:40 -0800 Subject: [PATCH 3/4] Fix checkstyle issue --- .../integrations/oci/sdk/runtime/OciAvailabilityDefault.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integrations/oci/sdk/runtime/src/main/java/io/helidon/integrations/oci/sdk/runtime/OciAvailabilityDefault.java b/integrations/oci/sdk/runtime/src/main/java/io/helidon/integrations/oci/sdk/runtime/OciAvailabilityDefault.java index ef5ca7493c3..81a0f39c19b 100644 --- a/integrations/oci/sdk/runtime/src/main/java/io/helidon/integrations/oci/sdk/runtime/OciAvailabilityDefault.java +++ b/integrations/oci/sdk/runtime/src/main/java/io/helidon/integrations/oci/sdk/runtime/OciAvailabilityDefault.java @@ -37,7 +37,7 @@ @Singleton @Weight(ServiceInfoBasics.DEFAULT_INJECT_WEIGHT) class OciAvailabilityDefault implements OciAvailability { - static String OPC_PATH = getOpcPath(); + private static final String OPC_PATH = getOpcPath(); @Override public boolean isRunningOnOci(OciConfig ociConfig) { @@ -74,7 +74,7 @@ static boolean canReach(String address, } } - static String getOpcPath() { + private static String getOpcPath() { String input = METADATA_SERVICE_BASE_URL; int index = -1; for (int nth = 3; nth > 0; nth--) { From e1e19e9c8b343034adb4ee16347500c762687f13 Mon Sep 17 00:00:00 2001 From: Keith Lustria Date: Mon, 8 Jan 2024 22:53:27 -0800 Subject: [PATCH 4/4] Improve getOpcPath to return exception during failure and add unit test coverage --- integrations/oci/sdk/runtime/pom.xml | 7 +++++- .../sdk/runtime/OciAvailabilityDefault.java | 10 ++++---- .../oci/sdk/runtime/OciAvailabilityTest.java | 23 ++++++++++++++++++- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/integrations/oci/sdk/runtime/pom.xml b/integrations/oci/sdk/runtime/pom.xml index a7c481c44e7..158f2e10cfd 100644 --- a/integrations/oci/sdk/runtime/pom.xml +++ b/integrations/oci/sdk/runtime/pom.xml @@ -1,7 +1,7 @@