Skip to content

Commit

Permalink
Use helidon.oci prefix for OCI Config and allow some oci auth types t…
Browse files Browse the repository at this point in the history
…o accept federation-endpoint and tenancy-id (helidon-io#9740)

The PR fixes Issues 9681 and 9734 which includes the following:
1. Allow instance-principal, resource-principal and oke-workload-identity to accept federation-endpoint and tenancy-id as config parameters. This is originally targeted just for oke-workload-identity where Instance Metadata Service (IMDS) does not work on an OKE environment. Because of these, it is unable to assemble the target endpoint as it needs the IMDS to retrieve the region. To resolve the issue, the federation-endpoint configuration is now allowed to be explicitly specified to avoid generation of endpoint using the region from IMDS. In some examples of the use of oke-workload-identity, the tenancy id is required, so this configuration parameter is also added as an option. Furthermore, because instance-principal and resource-principal providers extends AbstractRequestingAuthenticationDetailsProvider similar to oke-workload-instance, hence they are included in the change to allow those optional parameters.
2. Fix a bug where the oci configuration does not work when prefixed with "helidon.oci".
3. Add comprehensive testing coverage for above changes.
4. Remove unnecessary Weight annotation with default value and replace WARNING message with TRACE when oci config does not use "helidon.oci"
  • Loading branch information
klustria authored and barchetta committed Feb 10, 2025
1 parent af893e4 commit 9253978
Show file tree
Hide file tree
Showing 23 changed files with 634 additions and 40 deletions.
1 change: 1 addition & 0 deletions etc/copyright-exclude.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jaxb.index
/MANIFEST.MF
/README
/CONTRIBUTING.md
.crt
.pem
.p8
.pkcs8.pem
Expand Down
4 changes: 2 additions & 2 deletions integrations/oci/authentication/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Helidon supports the following Authentication Methods:
- `config-file`: based on OCI config file
- `instance-principal`: instance principal (Such as an OCI VM)
- `resource-prinicpal`: resource principal (Such as server-less functions)
- `workload`: workload running on a k8s cluster
- `oke-workload-identity`: identity of workload running on a k8s cluster

The first two types are always available through `helidon-integrations-oci` module.
Instance, resource, and workload support can be added through modules in this project module.
Instance, resource, and workload support can be added through modules in this project module.
10 changes: 10 additions & 0 deletions integrations/oci/authentication/instance/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@
<artifactId>slf4j-jdk14</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.webserver.testing.junit5</groupId>
<artifactId>helidon-webserver-testing-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
* Copyright (c) 2024, 2025 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.
Expand All @@ -17,6 +17,7 @@
package io.helidon.integrations.oci.authentication.instance;

import java.util.Optional;
import java.util.function.Supplier;

import io.helidon.common.LazyValue;
import io.helidon.common.Weight;
Expand All @@ -42,7 +43,7 @@ class AuthenticationMethodInstancePrincipal implements OciAuthenticationMethod {
private final LazyValue<Optional<BasicAuthenticationDetailsProvider>> provider;

AuthenticationMethodInstancePrincipal(OciConfig config,
InstancePrincipalsAuthenticationDetailsProviderBuilder builder) {
Supplier<Optional<InstancePrincipalsAuthenticationDetailsProviderBuilder>> builder) {
provider = createProvider(config, builder);
}

Expand All @@ -58,10 +59,11 @@ public Optional<BasicAuthenticationDetailsProvider> provider() {

private static LazyValue<Optional<BasicAuthenticationDetailsProvider>>
createProvider(OciConfig config,
InstancePrincipalsAuthenticationDetailsProviderBuilder builder) {
Supplier<Optional<InstancePrincipalsAuthenticationDetailsProviderBuilder>> builder) {
return LazyValue.create(() -> {
if (HelidonOci.imdsAvailable(config)) {
return Optional.of(builder.build());
return builder.get()
.map(InstancePrincipalsAuthenticationDetailsProviderBuilder::build);
}
if (LOGGER.isLoggable(System.Logger.Level.TRACE)) {
LOGGER.log(System.Logger.Level.TRACE, "OCI Metadata service is not available, "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
* Copyright (c) 2024, 2025 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.
Expand Down Expand Up @@ -39,8 +39,8 @@ class InstancePrincipalBuilderProvider implements Supplier<InstancePrincipalsAut

@Override
public InstancePrincipalsAuthenticationDetailsProviderBuilder get() {
var builder = InstancePrincipalsAuthenticationDetailsProvider.builder()
.timeoutForEachRetry((int) config.authenticationTimeout().toMillis());
var builder = getBuilder();
builder.timeoutForEachRetry((int) config.authenticationTimeout().toMillis());

config.imdsDetectRetries()
.ifPresent(builder::detectEndpointRetries);
Expand All @@ -50,8 +50,14 @@ public InstancePrincipalsAuthenticationDetailsProviderBuilder get() {
config.imdsBaseUri()
.map(URI::toString)
.ifPresent(builder::metadataBaseUrl);
config.tenantId()
.ifPresent(builder::tenancyId);

return builder;
}

InstancePrincipalsAuthenticationDetailsProviderBuilder getBuilder() {
return InstancePrincipalsAuthenticationDetailsProvider.builder();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2025 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.integrations.oci.authentication.instance;

import java.util.Properties;

import io.helidon.webserver.WebServer;
import io.helidon.webserver.http.HttpRules;
import io.helidon.webserver.http.ServerRequest;
import io.helidon.webserver.http.ServerResponse;
import io.helidon.webserver.testing.junit5.ServerTest;
import io.helidon.webserver.testing.junit5.SetUpRoute;

import io.helidon.service.registry.ServiceRegistry;
import io.helidon.service.registry.ServiceRegistryManager;

import com.oracle.bmc.auth.BasicAuthenticationDetailsProvider;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

@ServerTest
public class AuthenticationMethodInstancePrincipalTest {
private static ServiceRegistryManager registryManager;
private static ServiceRegistry registry;
private static String imdsBaseUri;

AuthenticationMethodInstancePrincipalTest(WebServer server) {
imdsBaseUri = "http://localhost:%d/opc/v2/".formatted(server.port());
}

@SetUpRoute
static void routing(HttpRules rules) {
rules.get("/opc/v2/instance", ImdsEmulator::emulateImdsInstance);
}

void setUp(Properties p) {
p.put("helidon.oci.authentication-method", "instance-principal");
p.put("helidon.oci.imds-timeout", "PT1S");
p.put("helidon.oci.imds-detect-retries", "0");
p.put("helidon.oci.imds-base-uri", imdsBaseUri);
System.setProperties(p);

registryManager = ServiceRegistryManager.create();
registry = registryManager.registry();
}

@AfterEach
void cleanUp() {
registry = null;
if (registryManager != null) {
registryManager.shutdown();
}
}

@Test
public void testInstancePrincipalConfigurationAndInstantiation() {
final String FEDERATION_ENDPOINT = "https://auth.us-myregion-1.oraclecloud.com";
final String TENANT_ID = "ocid1.tenancy.oc1..mytenancyid";

Properties p = System.getProperties();
p.put("helidon.oci.federation-endpoint", FEDERATION_ENDPOINT);
p.put("helidon.oci.tenant-id", TENANT_ID);
setUp(p);

// This error indicates that the instance principal provider has been instantiated
var thrown = assertThrows(IllegalArgumentException.class,
() -> registry.get(BasicAuthenticationDetailsProvider.class));
assertThat(thrown.getMessage(),
containsString(MockedInstancePrincipalBuilderProvider.INSTANCE_PRINCIPAL_INSTANTIATION_MESSAGE));

// The following validation indicates that the instance principal provider has been configured properly
assertThat(MockedAuthenticationMethodInstancePrincipal.getBuilder().getMetadataBaseUrl(), is(imdsBaseUri));
assertThat(MockedAuthenticationMethodInstancePrincipal.getBuilder().getFederationEndpoint(), is(FEDERATION_ENDPOINT));
assertThat(MockedAuthenticationMethodInstancePrincipal.getBuilder().getTenancyId(), is(TENANT_ID));
}

public static class ImdsEmulator {
// This will allow HelidonOci.imdsAvailable() to be tested by making the server that simulates IMDS to return a JSON value
// when instance property is queried
private static final String IMDS_INSTANCE_RESPONSE = """
{
"displayName": "helidon-server"
}
""";

private static void emulateImdsInstance(ServerRequest req, ServerResponse res) {
res.send(IMDS_INSTANCE_RESPONSE);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2025 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.integrations.oci.authentication.instance;

import java.util.Optional;
import java.util.function.Supplier;

import io.helidon.common.Weight;
import io.helidon.common.Weighted;
import io.helidon.integrations.oci.OciConfig;
import io.helidon.service.registry.Service;

import com.oracle.bmc.auth.InstancePrincipalsAuthenticationDetailsProvider.InstancePrincipalsAuthenticationDetailsProviderBuilder;

@Weight(Weighted.DEFAULT_WEIGHT)
@Service.Provider
class MockedAuthenticationMethodInstancePrincipal extends AuthenticationMethodInstancePrincipal {

private static InstancePrincipalsAuthenticationDetailsProviderBuilder providerBuilder;

MockedAuthenticationMethodInstancePrincipal(OciConfig config,
Supplier<Optional<InstancePrincipalsAuthenticationDetailsProviderBuilder>>
builder) {
super(config, builder);
providerBuilder = builder.get().get();
}

static InstancePrincipalsAuthenticationDetailsProviderBuilder getBuilder() {
return providerBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2025 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.integrations.oci.authentication.instance;

import java.util.function.Supplier;

import io.helidon.common.Weight;
import io.helidon.common.Weighted;
import io.helidon.integrations.oci.OciConfig;
import io.helidon.service.registry.Service;

import com.oracle.bmc.auth.InstancePrincipalsAuthenticationDetailsProvider.InstancePrincipalsAuthenticationDetailsProviderBuilder;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;

@Weight(Weighted.DEFAULT_WEIGHT + 10)
@Service.Provider
class MockedInstancePrincipalBuilderProvider extends InstancePrincipalBuilderProvider
implements Supplier<InstancePrincipalsAuthenticationDetailsProviderBuilder> {
static final String INSTANCE_PRINCIPAL_INSTANTIATION_MESSAGE = "Instance Principal has been instantiated";
private String metadataBaseUrl = null;
private String tenancyID = null;
private String federationEndpoint = null;

MockedInstancePrincipalBuilderProvider(OciConfig config) {
super(config);
}

@Override
InstancePrincipalsAuthenticationDetailsProviderBuilder getBuilder() {
// Mock the InstancePrincipalsAuthenticationDetailsProviderBuilder
final InstancePrincipalsAuthenticationDetailsProviderBuilder builder =
mock(InstancePrincipalsAuthenticationDetailsProviderBuilder.class);

doAnswer(invocation -> {
throw new IllegalArgumentException(INSTANCE_PRINCIPAL_INSTANTIATION_MESSAGE);
}).when(builder).build();

// Process metadataBaseUrl
doAnswer(invocation -> {
metadataBaseUrl = invocation.getArgument(0);
return null;
}).when(builder).metadataBaseUrl(any());
doAnswer(invocation -> {
return metadataBaseUrl;
}).when(builder).getMetadataBaseUrl();

// Process federationEndpoint
doAnswer(invocation -> {
federationEndpoint = invocation.getArgument(0);
return null;
}).when(builder).federationEndpoint(any());
doAnswer(invocation -> {
return federationEndpoint;
}).when(builder).getFederationEndpoint();

// Process tenancyId
doAnswer(invocation -> {
tenancyID = invocation.getArgument(0);
return null;
}).when(builder).tenancyId(any());
doAnswer(invocation -> {
return tenancyID;
}).when(builder).getTenancyId();

return builder;
}
}
17 changes: 17 additions & 0 deletions integrations/oci/authentication/oke-workload/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<artifactId>slf4j-jdk14</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle.oci.sdk</groupId>
<artifactId>oci-java-sdk-common-httpclient-jersey3</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -111,6 +116,18 @@
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!--
This will allow AuthenticationMethodOkeWorkload.available() to work during testing
-->
<environmentVariables>
<OCI_KUBERNETES_SERVICE_ACCOUNT_CERT_PATH>${project.build.testResources[0].directory}/dummy-ca.crt</OCI_KUBERNETES_SERVICE_ACCOUNT_CERT_PATH>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
Expand Down
Loading

0 comments on commit 9253978

Please sign in to comment.