Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RANGER-5028: credentialbuilder module: update for code readability improvements #444

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions credentialbuilder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<packaging>jar</packaging>
<name>Credential Builder</name>
<description>Credential Builder for non-hadoop java codebase</description>
<properties>
<checkstyle.failOnViolation>true</checkstyle.failOnViolation>
<checkstyle.skip>false</checkstyle.skip>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.woodstox</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,80 +17,74 @@
* under the License.
*/

package org.apache.ranger.credentialapi;
import java.util.ArrayList;
import java.util.List;
package org.apache.ranger.credentialapi;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.alias.CredentialProvider;
import org.apache.hadoop.security.alias.CredentialProviderFactory;
import org.apache.hadoop.security.alias.JavaKeyStoreProvider;

public class CredentialReader {
import java.util.ArrayList;
import java.util.List;

public static String getDecryptedString(String CrendentialProviderPath,String alias, String storeType) {
String credential=null;
try{
if(CrendentialProviderPath==null || alias==null){
return null;
}
char[] pass = null;
Configuration conf = new Configuration();
String crendentialProviderPrefixJceks=JavaKeyStoreProvider.SCHEME_NAME + "://file";
String crendentialProviderPrefixLocalJceks="localjceks://file";
crendentialProviderPrefixJceks=crendentialProviderPrefixJceks.toLowerCase();
public class CredentialReader {
private CredentialReader() {
// to block instantiation
}
pradeepagrawal8184 marked this conversation as resolved.
Show resolved Hide resolved

String crendentialProviderPrefixBcfks = "bcfks" + "://file";
String crendentialProviderPrefixLocalBcfks= "localbcfks" + "://file";
crendentialProviderPrefixBcfks=crendentialProviderPrefixBcfks.toLowerCase();
crendentialProviderPrefixLocalBcfks=crendentialProviderPrefixLocalBcfks.toLowerCase();
public static String getDecryptedString(String crendentialProviderPath, String alias, String storeType) {
String credential = null;
try {
if (crendentialProviderPath == null || alias == null) {
return null;
}
char[] pass = null;
Configuration conf = new Configuration();
String crendentialProviderPrefixJceks = JavaKeyStoreProvider.SCHEME_NAME + "://file";
String crendentialProviderPrefixLocalJceks = "localjceks://file";
crendentialProviderPrefixJceks = crendentialProviderPrefixJceks.toLowerCase();

CrendentialProviderPath=CrendentialProviderPath.trim();
alias=alias.trim();
if(CrendentialProviderPath.toLowerCase().startsWith(crendentialProviderPrefixJceks) ||
CrendentialProviderPath.toLowerCase().startsWith(crendentialProviderPrefixLocalJceks) ||
CrendentialProviderPath.toLowerCase().startsWith(crendentialProviderPrefixBcfks) ||
CrendentialProviderPath.toLowerCase().startsWith(crendentialProviderPrefixLocalBcfks)){
conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH,
//UserProvider.SCHEME_NAME + ":///," +
CrendentialProviderPath);
}else{
if(CrendentialProviderPath.startsWith("/")){
if(StringUtils.equalsIgnoreCase(storeType, "bcfks")) {
conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, CrendentialProviderPath);
} else {
conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH,
//UserProvider.SCHEME_NAME + ":///," +
JavaKeyStoreProvider.SCHEME_NAME + "://file" + CrendentialProviderPath);
}
String crendentialProviderPrefixBcfks = "bcfks" + "://file";
String crendentialProviderPrefixLocalBcfks = "localbcfks" + "://file";
crendentialProviderPrefixBcfks = crendentialProviderPrefixBcfks.toLowerCase();
crendentialProviderPrefixLocalBcfks = crendentialProviderPrefixLocalBcfks.toLowerCase();

}else{
conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH,
//UserProvider.SCHEME_NAME + ":///," +
JavaKeyStoreProvider.SCHEME_NAME + "://file/" + CrendentialProviderPath);
}
}
List<CredentialProvider> providers = CredentialProviderFactory.getProviders(conf);
List<String> aliasesList=new ArrayList<String>();
CredentialProvider.CredentialEntry credEntry=null;
for(CredentialProvider provider: providers) {
//System.out.println("Credential Provider :" + provider);
aliasesList=provider.getAliases();
if(aliasesList!=null && aliasesList.contains(alias.toLowerCase())){
credEntry=null;
credEntry= provider.getCredentialEntry(alias.toLowerCase());
pass = credEntry.getCredential();
if(pass!=null && pass.length>0){
credential=String.valueOf(pass);
break;
}
}
}
}catch(Exception ex){
ex.printStackTrace();
credential=null;
}
return credential;
}
crendentialProviderPath = crendentialProviderPath.trim();
alias = alias.trim();
if (crendentialProviderPath.toLowerCase().startsWith(crendentialProviderPrefixJceks) || crendentialProviderPath.toLowerCase().startsWith(crendentialProviderPrefixLocalJceks) || crendentialProviderPath.toLowerCase().startsWith(crendentialProviderPrefixBcfks) || crendentialProviderPath.toLowerCase().startsWith(crendentialProviderPrefixLocalBcfks)) {
conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, crendentialProviderPath);
} else {
if (crendentialProviderPath.startsWith("/")) {
if (StringUtils.equalsIgnoreCase(storeType, "bcfks")) {
conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, crendentialProviderPath);
} else {
conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, JavaKeyStoreProvider.SCHEME_NAME + "://file" + crendentialProviderPath);
}
} else {
conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, JavaKeyStoreProvider.SCHEME_NAME + "://file/" + crendentialProviderPath);
}
}
List<CredentialProvider> providers = CredentialProviderFactory.getProviders(conf);
List<String> aliasesList = new ArrayList<String>();
CredentialProvider.CredentialEntry credEntry = null;
for (CredentialProvider provider : providers) {
//System.out.println("Credential Provider :" + provider);
aliasesList = provider.getAliases();
if (aliasesList != null && aliasesList.contains(alias.toLowerCase())) {
credEntry = null;
credEntry = provider.getCredentialEntry(alias.toLowerCase());
pass = credEntry.getCredential();
if (pass != null && pass.length > 0) {
credential = String.valueOf(pass);
break;
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
credential = null;
}
return credential;
}
}
Loading
Loading