Skip to content

Commit 5442494

Browse files
Add resource providers
1 parent 29c714b commit 5442494

14 files changed

+549
-307
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,30 @@
1-
/*
2-
** Copyright (c) 2023 Oracle and/or its affiliates.
3-
**
4-
** The Universal Permissive License (UPL), Version 1.0
5-
**
6-
** Subject to the condition set forth below, permission is hereby granted to any
7-
** person obtaining a copy of this software, associated documentation and/or data
8-
** (collectively the "Software"), free of charge and under any and all copyright
9-
** rights in the Software, and any and all patent rights owned or freely
10-
** licensable by each licensor hereunder covering either (i) the unmodified
11-
** Software as contributed to or provided by such licensor, or (ii) the Larger
12-
** Works (as defined below), to deal in both
13-
**
14-
** (a) the Software, and
15-
** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16-
** one is included with the Software (each a "Larger Work" to which the Software
17-
** is contributed by such licensors),
18-
**
19-
** without restriction, including without limitation the rights to copy, create
20-
** derivative works of, display, perform, and distribute the Software and make,
21-
** use, sell, offer for sale, import, export, have made, and have sold the
22-
** Software and the Larger Work(s), and to sublicense the foregoing rights on
23-
** either these or other terms.
24-
**
25-
** This license is subject to the following condition:
26-
** The above copyright notice and either this complete permission notice or at
27-
** a minimum a reference to the UPL must be included in all copies or
28-
** substantial portions of the Software.
29-
**
30-
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31-
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32-
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33-
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34-
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35-
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36-
** SOFTWARE.
37-
*/
381
package oracle.jdbc.provider.aws.resource;
392

40-
public class AwsParameterStoreResourceParameterNames {
3+
/**
4+
* Centralized parameter name constants used by AWS Parameter Store resource providers.
5+
*/
6+
public final class AwsParameterStoreResourceParameterNames {
7+
8+
private AwsParameterStoreResourceParameterNames() {}
9+
10+
/** The AWS region where the parameter is stored (e.g., eu-north-1). */
11+
public static final String AWS_REGION = "awsRegion";
12+
13+
/** The name of the parameter stored in AWS Parameter Store. */
14+
public static final String PARAMETER_NAME = "parameterName";
15+
16+
/** Optional field name to extract from a JSON parameter value. */
17+
public static final String FIELD_NAME = "fieldName";
18+
19+
/** The alias used to retrieve a connection string from tnsnames.ora. */
20+
public static final String TNS_ALIAS = "tnsAlias";
21+
22+
/** Optional password used to decrypt the wallet (for PKCS12 or encrypted PEM). */
23+
public static final String WALLET_PASSWORD = "walletPassword";
24+
25+
/** The wallet format: SSO, PKCS12, or PEM. */
26+
public static final String TYPE = "type";
27+
28+
/** Index of the credential set in the wallet */
29+
public static final String CONNECTION_STRING_INDEX = "connectionStringIndex";
4130
}
Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,58 @@
1-
/*
2-
** Copyright (c) 2023 Oracle and/or its affiliates.
3-
**
4-
** The Universal Permissive License (UPL), Version 1.0
5-
**
6-
** Subject to the condition set forth below, permission is hereby granted to any
7-
** person obtaining a copy of this software, associated documentation and/or data
8-
** (collectively the "Software"), free of charge and under any and all copyright
9-
** rights in the Software, and any and all patent rights owned or freely
10-
** licensable by each licensor hereunder covering either (i) the unmodified
11-
** Software as contributed to or provided by such licensor, or (ii) the Larger
12-
** Works (as defined below), to deal in both
13-
**
14-
** (a) the Software, and
15-
** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16-
** one is included with the Software (each a "Larger Work" to which the Software
17-
** is contributed by such licensors),
18-
**
19-
** without restriction, including without limitation the rights to copy, create
20-
** derivative works of, display, perform, and distribute the Software and make,
21-
** use, sell, offer for sale, import, export, have made, and have sold the
22-
** Software and the Larger Work(s), and to sublicense the foregoing rights on
23-
** either these or other terms.
24-
**
25-
** This license is subject to the following condition:
26-
** The above copyright notice and either this complete permission notice or at
27-
** a minimum a reference to the UPL must be included in all copies or
28-
** substantial portions of the Software.
29-
**
30-
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31-
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32-
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33-
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34-
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35-
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36-
** SOFTWARE.
37-
*/
381
package oracle.jdbc.provider.aws.resource;
392

40-
public class AwsResourceProvider {
41-
}
3+
import oracle.jdbc.provider.aws.authentication.AwsAuthenticationMethod;
4+
import oracle.jdbc.provider.aws.authentication.AwsCredentialsFactory;
5+
import oracle.jdbc.provider.resource.AbstractResourceProvider;
6+
import oracle.jdbc.provider.resource.ResourceParameter;
7+
8+
import java.util.stream.Stream;
9+
10+
import static oracle.jdbc.provider.aws.authentication.AwsAuthenticationMethod.DEFAULT;
11+
import static oracle.jdbc.provider.aws.authentication.AwsCredentialsFactory.AUTHENTICATION_METHOD;
12+
import static oracle.jdbc.provider.aws.configuration.AwsConfigurationParameters.REGION;
13+
import static oracle.jdbc.provider.aws.resource.AwsParameterStoreResourceParameterNames.AWS_REGION;
14+
15+
/**
16+
* Super class of all {@code OracleResourceProvider} implementations
17+
* that request a resource from AWS. This super class defines parameters for
18+
* authentication with AWS.
19+
*/
20+
public abstract class AwsResourceProvider extends AbstractResourceProvider {
21+
22+
private static final ResourceParameter[] PARAMETERS = {
23+
new ResourceParameter("authenticationMethod", AUTHENTICATION_METHOD,
24+
"aws-default",
25+
AwsResourceProvider::parseAuthenticationMethod),
26+
new ResourceParameter(AWS_REGION, REGION)
27+
};
28+
29+
/**
30+
* Constructs a provider identified by the name:
31+
* <pre>{@code
32+
* ojdbc-provider-aws-{resourceType}
33+
* }</pre>
34+
* @param resourceType The resource type identifier used in the provider name.
35+
* @param parameters Additional parameters specific to the subclass provider.
36+
*/
37+
protected AwsResourceProvider(String resourceType, ResourceParameter... parameters) {
38+
super("aws", resourceType,
39+
Stream.concat(Stream.of(PARAMETERS), Stream.of(parameters))
40+
.toArray(ResourceParameter[]::new));
41+
}
42+
43+
/**
44+
* Parses the "authenticationMethod" parameter as an
45+
* {@link AwsAuthenticationMethod} recognized by {@link AwsCredentialsFactory}.
46+
*
47+
* @param authenticationMethod The value to parse.
48+
* @return An {@link AwsAuthenticationMethod} enum.
49+
* @throws IllegalArgumentException if the value is unrecognized.
50+
*/
51+
private static AwsAuthenticationMethod parseAuthenticationMethod(String authenticationMethod) {
52+
switch (authenticationMethod) {
53+
case "aws-default": return DEFAULT;
54+
default:
55+
throw new IllegalArgumentException("Unrecognized authentication method: " + authenticationMethod);
56+
}
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,46 @@
1-
/*
2-
** Copyright (c) 2023 Oracle and/or its affiliates.
3-
**
4-
** The Universal Permissive License (UPL), Version 1.0
5-
**
6-
** Subject to the condition set forth below, permission is hereby granted to any
7-
** person obtaining a copy of this software, associated documentation and/or data
8-
** (collectively the "Software"), free of charge and under any and all copyright
9-
** rights in the Software, and any and all patent rights owned or freely
10-
** licensable by each licensor hereunder covering either (i) the unmodified
11-
** Software as contributed to or provided by such licensor, or (ii) the Larger
12-
** Works (as defined below), to deal in both
13-
**
14-
** (a) the Software, and
15-
** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16-
** one is included with the Software (each a "Larger Work" to which the Software
17-
** is contributed by such licensors),
18-
**
19-
** without restriction, including without limitation the rights to copy, create
20-
** derivative works of, display, perform, and distribute the Software and make,
21-
** use, sell, offer for sale, import, export, have made, and have sold the
22-
** Software and the Larger Work(s), and to sublicense the foregoing rights on
23-
** either these or other terms.
24-
**
25-
** This license is subject to the following condition:
26-
** The above copyright notice and either this complete permission notice or at
27-
** a minimum a reference to the UPL must be included in all copies or
28-
** substantial portions of the Software.
29-
**
30-
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31-
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32-
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33-
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34-
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35-
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36-
** SOFTWARE.
37-
*/
381
package oracle.jdbc.provider.aws.resource;
392

40-
public class ParameterStoreConnectionStringProvider {
3+
import oracle.jdbc.spi.ConnectionStringProvider;
4+
import oracle.jdbc.provider.resource.ResourceParameter;
5+
import oracle.jdbc.provider.util.TNSNames;
6+
7+
import java.io.ByteArrayInputStream;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.util.Map;
11+
12+
import static oracle.jdbc.provider.util.CommonParameters.TNS_ALIAS;
13+
import static oracle.jdbc.provider.util.FileUtils.decodeIfBase64;
14+
15+
/**
16+
* Provider that retrieves tnsnames.ora from AWS Parameter Store and extracts a connection string.
17+
*/
18+
public class ParameterStoreConnectionStringProvider
19+
extends ParameterStoreSecretProvider
20+
implements ConnectionStringProvider {
21+
22+
private static final ResourceParameter[] PARAMETERS = {
23+
new ResourceParameter(AwsParameterStoreResourceParameterNames.TNS_ALIAS, TNS_ALIAS)
24+
};
25+
26+
public ParameterStoreConnectionStringProvider() {
27+
super("parameterstore-tnsnames", PARAMETERS);
28+
}
29+
30+
@Override
31+
public String getConnectionString(Map<Parameter, CharSequence> parameterValues) {
32+
String alias = parseParameterValues(parameterValues).getRequired(TNS_ALIAS);
33+
byte[] fileBytes = decodeIfBase64(getSecret(parameterValues).getBytes());
34+
35+
try (InputStream inputStream = new ByteArrayInputStream(fileBytes)) {
36+
TNSNames tnsNames = TNSNames.read(inputStream);
37+
String connectionString = tnsNames.getConnectionStringByAlias(alias);
38+
if (connectionString == null) {
39+
throw new IllegalArgumentException("Alias specified does not exist in tnsnames.ora: " + alias);
40+
}
41+
return connectionString;
42+
} catch (IOException e) {
43+
throw new IllegalStateException("Failed to read tnsnames.ora content", e);
44+
}
45+
}
4146
}
Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
1-
/*
2-
** Copyright (c) 2023 Oracle and/or its affiliates.
3-
**
4-
** The Universal Permissive License (UPL), Version 1.0
5-
**
6-
** Subject to the condition set forth below, permission is hereby granted to any
7-
** person obtaining a copy of this software, associated documentation and/or data
8-
** (collectively the "Software"), free of charge and under any and all copyright
9-
** rights in the Software, and any and all patent rights owned or freely
10-
** licensable by each licensor hereunder covering either (i) the unmodified
11-
** Software as contributed to or provided by such licensor, or (ii) the Larger
12-
** Works (as defined below), to deal in both
13-
**
14-
** (a) the Software, and
15-
** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16-
** one is included with the Software (each a "Larger Work" to which the Software
17-
** is contributed by such licensors),
18-
**
19-
** without restriction, including without limitation the rights to copy, create
20-
** derivative works of, display, perform, and distribute the Software and make,
21-
** use, sell, offer for sale, import, export, have made, and have sold the
22-
** Software and the Larger Work(s), and to sublicense the foregoing rights on
23-
** either these or other terms.
24-
**
25-
** This license is subject to the following condition:
26-
** The above copyright notice and either this complete permission notice or at
27-
** a minimum a reference to the UPL must be included in all copies or
28-
** substantial portions of the Software.
29-
**
30-
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31-
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32-
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33-
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34-
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35-
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36-
** SOFTWARE.
37-
*/
381
package oracle.jdbc.provider.aws.resource;
392

40-
public class ParameterStorePasswordProvider {
3+
import oracle.jdbc.spi.PasswordProvider;
4+
5+
import java.util.Map;
6+
7+
/**
8+
* <p>
9+
* A provider of password managed as parameters in AWS Systems Manager Parameter Store.
10+
* This class inherits parameters and behavior from {@link ParameterStoreSecretProvider}
11+
* and {@link AwsResourceProvider}.
12+
* </p><p>
13+
* This class implements the {@link PasswordProvider} SPI defined by Oracle JDBC.
14+
* It is designed to be located and instantiated by {@link java.util.ServiceLoader}.
15+
* </p>
16+
*/
17+
public class ParameterStorePasswordProvider
18+
extends ParameterStoreSecretProvider
19+
implements PasswordProvider {
20+
21+
/**
22+
* A public no-arg constructor used by {@link java.util.ServiceLoader}
23+
* to construct an instance of this provider.
24+
*/
25+
public ParameterStorePasswordProvider() {
26+
super("parameterstore-password");
27+
}
28+
29+
/**
30+
* Retrieves a password stored in AWS Parameter Store.
31+
*
32+
* @param parameterValues A map of parameter names and values required for
33+
* retrieving the parameter. Must not be null.
34+
* @return The secret value as a char array. Not null.
35+
*/
36+
@Override
37+
public char[] getPassword(Map<Parameter, CharSequence> parameterValues) {
38+
return getSecret(parameterValues).toCharArray();
39+
}
4140
}

0 commit comments

Comments
 (0)