Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[CONJ-733] adding credential service implementation
Credential service permits providing user / password just implementing CredentialPlugin interface. 3 default implementations : - using environment credential - using java system properties credential - using IAM authentication
- Loading branch information
Showing
16 changed files
with
770 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * MariaDB Client for Java | ||
| * | ||
| * Copyright (c) 2012-2014 Monty Program Ab. | ||
| * Copyright (c) 2015-2019 MariaDB Ab. | ||
| * | ||
| * This library is free software; you can redistribute it and/or modify it under | ||
| * the terms of the GNU Lesser General Public License as published by the Free | ||
| * Software Foundation; either version 2.1 of the License, or (at your option) | ||
| * any later version. | ||
| * | ||
| * This library is distributed in the hope that it will be useful, but | ||
| * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
| * for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public License along | ||
| * with this library; if not, write to Monty Program Ab info@montyprogram.com. | ||
| * | ||
| */ | ||
|
|
||
| package org.mariadb.jdbc.credential; | ||
|
|
||
| public class Credential { | ||
| private String user; | ||
| private String password; | ||
|
|
||
| public Credential(String user, String password) { | ||
| this.user = user; | ||
| this.password = password; | ||
| } | ||
|
|
||
| public String getUser() { | ||
| return user; | ||
| } | ||
|
|
||
| public String getPassword() { | ||
| return password; | ||
| } | ||
|
|
||
| public void setUser(String user) { | ||
| this.user = user; | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
src/main/java/org/mariadb/jdbc/credential/CredentialPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * MariaDB Client for Java | ||
| * | ||
| * Copyright (c) 2012-2014 Monty Program Ab. | ||
| * Copyright (c) 2015-2019 MariaDB Ab. | ||
| * | ||
| * This library is free software; you can redistribute it and/or modify it under | ||
| * the terms of the GNU Lesser General Public License as published by the Free | ||
| * Software Foundation; either version 2.1 of the License, or (at your option) | ||
| * any later version. | ||
| * | ||
| * This library is distributed in the hope that it will be useful, but | ||
| * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
| * for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public License along | ||
| * with this library; if not, write to Monty Program Ab info@montyprogram.com. | ||
| * | ||
| */ | ||
|
|
||
| package org.mariadb.jdbc.credential; | ||
|
|
||
| import java.sql.SQLException; | ||
| import java.util.function.Supplier; | ||
| import org.mariadb.jdbc.HostAddress; | ||
| import org.mariadb.jdbc.util.Options; | ||
|
|
||
| public interface CredentialPlugin extends Supplier<Credential> { | ||
|
|
||
| String name(); | ||
|
|
||
| String type(); | ||
|
|
||
| default boolean mustUseSsl() { | ||
| return false; | ||
| } | ||
|
|
||
| default String defaultAuthenticationPluginType() { | ||
| return null; | ||
| } | ||
|
|
||
| default CredentialPlugin initialize(Options options, String userName, HostAddress hostAddress) | ||
| throws SQLException { | ||
| return this; | ||
| } | ||
|
|
||
| } |
54 changes: 54 additions & 0 deletions
54
src/main/java/org/mariadb/jdbc/credential/CredentialPluginLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * MariaDB Client for Java | ||
| * | ||
| * Copyright (c) 2012-2014 Monty Program Ab. | ||
| * Copyright (c) 2015-2017 MariaDB Ab. | ||
| * | ||
| * This library is free software; you can redistribute it and/or modify it under | ||
| * the terms of the GNU Lesser General Public License as published by the Free | ||
| * Software Foundation; either version 2.1 of the License, or (at your option) | ||
| * any later version. | ||
| * | ||
| * This library is distributed in the hope that it will be useful, but | ||
| * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
| * for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public License along | ||
| * with this library; if not, write to Monty Program Ab info@montyprogram.com. | ||
| * | ||
| */ | ||
|
|
||
| package org.mariadb.jdbc.credential; | ||
|
|
||
| import java.sql.SQLException; | ||
| import java.util.ServiceLoader; | ||
|
|
||
| /** | ||
| * Provider to handle plugin authentication. This can allow library users to override our default | ||
| * Authentication provider. | ||
| */ | ||
| public class CredentialPluginLoader { | ||
|
|
||
| /** | ||
| * Get current Identity plugin according to option `identityType`. | ||
| * | ||
| * @param type identity plugin type | ||
| * @return identity plugin | ||
| * @throws SQLException if no identity plugin found with this type is in classpath | ||
| */ | ||
| public static CredentialPlugin get(String type) throws SQLException { | ||
| if (type == null || type.isEmpty()) { | ||
| return null; | ||
| } | ||
| ServiceLoader<CredentialPlugin> loader = ServiceLoader.load(CredentialPlugin.class); | ||
| for (CredentialPlugin implClass : loader) { | ||
| if (type.equals(implClass.type())) { | ||
| return implClass; | ||
| } | ||
| } | ||
| throw new SQLException( | ||
| "No identity plugin registered with the type \"" + type + "\".", "08004", 1251); | ||
| } | ||
|
|
||
| } |
81 changes: 81 additions & 0 deletions
81
src/main/java/org/mariadb/jdbc/credential/aws/AwsCredentialGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * MariaDB Client for Java | ||
| * | ||
| * Copyright (c) 2012-2014 Monty Program Ab. | ||
| * Copyright (c) 2015-2019 MariaDB Ab. | ||
| * | ||
| * This library is free software; you can redistribute it and/or modify it under | ||
| * the terms of the GNU Lesser General Public License as published by the Free | ||
| * Software Foundation; either version 2.1 of the License, or (at your option) | ||
| * any later version. | ||
| * | ||
| * This library is distributed in the hope that it will be useful, but | ||
| * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
| * for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public License along | ||
| * with this library; if not, write to Monty Program Ab info@montyprogram.com. | ||
| * | ||
| */ | ||
|
|
||
| package org.mariadb.jdbc.credential.aws; | ||
|
|
||
| import com.amazonaws.auth.AWSCredentialsProvider; | ||
| import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
| import com.amazonaws.auth.BasicAWSCredentials; | ||
| import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; | ||
| import com.amazonaws.regions.DefaultAwsRegionProviderChain; | ||
| import com.amazonaws.services.rds.auth.GetIamAuthTokenRequest; | ||
| import com.amazonaws.services.rds.auth.RdsIamAuthTokenGenerator; | ||
| import java.util.Properties; | ||
| import org.mariadb.jdbc.HostAddress; | ||
| import org.mariadb.jdbc.credential.Credential; | ||
|
|
||
| public class AwsCredentialGenerator { | ||
|
|
||
| private RdsIamAuthTokenGenerator generator; | ||
| private GetIamAuthTokenRequest request; | ||
| private String userName; | ||
|
|
||
| /** | ||
| * AWS Generator. | ||
| * | ||
| * @param nonMappedOptions non standard options | ||
| * @param userName user | ||
| * @param hostAddress current server information | ||
| */ | ||
| public AwsCredentialGenerator(Properties nonMappedOptions, String userName, | ||
| HostAddress hostAddress) { | ||
| // Build RDS IAM-auth token generator | ||
| this.userName = userName; | ||
| AWSCredentialsProvider awsCredentialsProvider; | ||
| String accessKeyId = nonMappedOptions.getProperty("accessKeyId"); | ||
| String secretKey = nonMappedOptions.getProperty("secretKey"); | ||
| String region = nonMappedOptions.getProperty("region"); | ||
|
|
||
| if (accessKeyId != null && secretKey != null) { | ||
| awsCredentialsProvider = | ||
| new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKeyId, secretKey)); | ||
| } else { | ||
| awsCredentialsProvider = new DefaultAWSCredentialsProviderChain(); | ||
| } | ||
|
|
||
| this.generator = | ||
| RdsIamAuthTokenGenerator.builder() | ||
| .credentials(awsCredentialsProvider) | ||
| .region(region != null ? region : new DefaultAwsRegionProviderChain().getRegion()) | ||
| .build(); | ||
| this.request = | ||
| GetIamAuthTokenRequest.builder() | ||
| .hostname(hostAddress.host) | ||
| .port(hostAddress.port) | ||
| .userName(userName) | ||
| .build(); | ||
|
|
||
| } | ||
|
|
||
| public Credential getToken() { | ||
| return new Credential(userName, generator.getAuthToken(this.request)); | ||
| } | ||
| } |
Oops, something went wrong.