Skip to content

Commit a03cf25

Browse files
Add configurable timeout for instance principal authentication
1 parent 541507b commit a03cf25

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

ojdbc-provider-oci/README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,12 @@ in Optional Parameters</td>
256256
<td><b>OCI_INSTANCE_PRINCIPAL</b></td>
257257
<td>Instance Principal Authentication</td>
258258
<td>&nbsp;</td>
259-
<td>&nbsp;</td>
259+
<td>
260+
<code>OCI_INSTANCE_PRINCIPAL_TIMEOUT</code> <br>
261+
<i>(Optional)</i> Specifies the maximum time, in seconds, to wait for the instance principal authentication process to complete.<br>
262+
The value must be a valid integer (e.g., <code>5</code>, <code>30</code>). Decimal values are not allowed.<br>
263+
<b>Default:</b> <code>5</code> seconds
264+
</td>
260265
</tr>
261266
<tr>
262267
<td><b>OCI_RESOURCE_PRINCIPAL</b></td>
@@ -763,6 +768,15 @@ common set of parameters.
763768
DEFAULT
764769
</td>
765770
</tr>
771+
<tr>
772+
<td>instancePrincipalTimeout</td>
773+
<td>
774+
Specifies the maximum time, in seconds, to wait for instance principal authentication to complete.<br>
775+
The value must be a valid integer (e.g., <code>5</code>, <code>10</code>). Decimal values are not accepted.
776+
</td>
777+
<td>A positive integer</td>
778+
<td><code>5</code></td>
779+
</tr>
766780
</tbody>
767781
</table>
768782

@@ -815,7 +829,8 @@ OCI configuration file
815829
<dd>
816830
Authenticate as an <a href="https://docs.oracle.com/en-us/iaas/Content/Identity/Tasks/callingservicesfrominstances.htm">
817831
instance principal
818-
</a>.
832+
</a>.<br>
833+
You may optionally configure the timeout for this authentication using the <code>instancePrincipalTimeout</code> parameter.
819834
</dd>
820835
<dt>resource-principal</dt>
821836
<dd>

ojdbc-provider-oci/src/main/java/oracle/jdbc/provider/oci/authentication/AuthenticationDetailsFactory.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ public final class AuthenticationDetailsFactory
108108
*/
109109
public static final Parameter<String> USERNAME = Parameter.create();
110110

111+
/**
112+
* Timeout in seconds for instance principal authentication.
113+
* Optional – defaults to 5 seconds if not explicitly configured.
114+
*/
115+
public static final Parameter<Integer> INSTANCE_PRINCIPAL_TIMEOUT =
116+
Parameter.create();
117+
111118
/**
112119
* <p>
113120
* An OCI region provided by instances of
@@ -187,7 +194,7 @@ private static AbstractAuthenticationDetailsProvider getAuthenticationDetails(
187194
case CLOUD_SHELL:
188195
return cloudShellAuthentication();
189196
case INSTANCE_PRINCIPAL:
190-
return instancePrincipalAuthentication();
197+
return instancePrincipalAuthentication(parameterSet);
191198
case RESOURCE_PRINCIPAL:
192199
return resourcePrincipalAuthentication();
193200
case INTERACTIVE:
@@ -312,7 +319,7 @@ private static AbstractAuthenticationDetailsProvider getAuthenticationDetails(
312319
}
313320

314321
try {
315-
return instancePrincipalAuthentication();
322+
return instancePrincipalAuthentication(parameters);
316323
}
317324
catch (RuntimeException notComputeInstance) {
318325
previousFailure.addSuppressed(
@@ -332,17 +339,23 @@ private static AbstractAuthenticationDetailsProvider getAuthenticationDetails(
332339
* </p><p>
333340
* It is thought that authentication as an instance principal should not take
334341
* more than a few seconds to complete, so this method will throw an
335-
* {@code IllegalStateException} if a timeout of 5 seconds is exceeded.
342+
* {@code IllegalStateException} if the operation exceeds the configured
343+
* timeout (5 seconds by default).
344+
* </p><p>
345+
* The timeout can be overridden using the optional
346+
* {@code INSTANCE_PRINCIPAL_TIMEOUT} parameter (value in seconds), which can
347+
* be provided in the URI query string.
336348
* </p>
337349
* @return Authentication details for an instance principal. Not null.
338350
* @throws IllegalStateException If the current environment is not a compute
339351
* instance.
340352
*/
341353
private static InstancePrincipalsAuthenticationDetailsProvider
342-
instancePrincipalAuthentication() {
354+
instancePrincipalAuthentication(ParameterSet parameters) {
355+
int timeoutSeconds = parameters.getOptional(INSTANCE_PRINCIPAL_TIMEOUT);
343356
try {
344357
return InstancePrincipalAuthenticationTask.FUTURE
345-
.get(5, TimeUnit.SECONDS);
358+
.get(timeoutSeconds, TimeUnit.SECONDS);
346359
}
347360
catch (ExecutionException exception) {
348361
throw new IllegalStateException(
@@ -356,8 +369,8 @@ private static AbstractAuthenticationDetailsProvider getAuthenticationDetails(
356369
}
357370
catch (TimeoutException timeoutException) {
358371
throw new IllegalStateException(
359-
"Authentication as an instance principal did not complete within" +
360-
" 5 seconds",
372+
"Authentication as an instance principal did not complete within "
373+
+ timeoutSeconds + " seconds",
361374
timeoutException);
362375
}
363376
}

ojdbc-provider-oci/src/main/java/oracle/jdbc/provider/oci/configuration/OciConfigurationParameters.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ private OciConfigurationParameters(){}
9292
.addParameter("OCI_FINGERPRINT", FINGERPRINT)
9393
.addParameter("OCI_KEY_FILE", PRIVATE_KEY)
9494
.addParameter("OCI_PASS_PHRASE", PASS_PHRASE)
95+
.addParameter("OCI_INSTANCE_PRINCIPAL_TIMEOUT", INSTANCE_PRINCIPAL_TIMEOUT,
96+
5,
97+
s -> {
98+
try {
99+
return Integer.parseInt(s);
100+
}catch (NumberFormatException e) {
101+
throw new IllegalArgumentException( "Invalid value for " +
102+
"OCI_INSTANCE_PRINCIPAL_TIMEOUT: " + s + ". The value must be an integer.");
103+
}
104+
})
95105
.build();
96106

97107
/**

ojdbc-provider-oci/src/main/java/oracle/jdbc/provider/oci/resource/OciResourceProvider.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ public abstract class OciResourceProvider
8080
new ResourceParameter(
8181
"username",
8282
AuthenticationDetailsFactory.USERNAME),
83+
new ResourceParameter("instancePrincipalTimeout",
84+
AuthenticationDetailsFactory.INSTANCE_PRINCIPAL_TIMEOUT,
85+
"5",
86+
s -> {
87+
try {
88+
return Integer.parseInt(s);
89+
} catch (NumberFormatException e) {
90+
throw new IllegalArgumentException("Invalid value for instancePrincipalTimeout: " + s +
91+
" – must be an integer.");
92+
}
93+
}),
8394
new ResourceParameter(
8495
"region",
8596
AuthenticationDetailsFactory.REGION,

0 commit comments

Comments
 (0)