Skip to content

Commit

Permalink
RANGER-5028: credentialbuilder module: update for code readability im…
Browse files Browse the repository at this point in the history
…provement
  • Loading branch information
pradeepagrawal8184 committed Dec 10, 2024
1 parent f5521c9 commit 3a4b050
Show file tree
Hide file tree
Showing 6 changed files with 620 additions and 633 deletions.
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,82 @@
* 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() {
}

String crendentialProviderPrefixBcfks = "bcfks" + "://file";
String crendentialProviderPrefixLocalBcfks= "localbcfks" + "://file";
crendentialProviderPrefixBcfks=crendentialProviderPrefixBcfks.toLowerCase();
crendentialProviderPrefixLocalBcfks=crendentialProviderPrefixLocalBcfks.toLowerCase();
public static String getDecryptedString(String credentialProviderPath, String alias, String storeType) {
String credential = null;
try {
if (credentialProviderPath == 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;
}
credentialProviderPath = credentialProviderPath.trim();
alias = alias.trim();
if (credentialProviderPath.toLowerCase().startsWith(crendentialProviderPrefixJceks) ||
credentialProviderPath.toLowerCase().startsWith(crendentialProviderPrefixLocalJceks) ||
credentialProviderPath.toLowerCase().startsWith(crendentialProviderPrefixBcfks) ||
credentialProviderPath.toLowerCase().startsWith(crendentialProviderPrefixLocalBcfks)) {
conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH,
//UserProvider.SCHEME_NAME + ":///," +
credentialProviderPath);
} else {
if (credentialProviderPath.startsWith("/")) {
if (StringUtils.equalsIgnoreCase(storeType, "bcfks")) {
conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, credentialProviderPath);
} else {
conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH,
//UserProvider.SCHEME_NAME + ":///," +
JavaKeyStoreProvider.SCHEME_NAME + "://file" + credentialProviderPath);
}
} else {
conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH,
//UserProvider.SCHEME_NAME + ":///," +
JavaKeyStoreProvider.SCHEME_NAME + "://file/" + credentialProviderPath);
}
}
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

0 comments on commit 3a4b050

Please sign in to comment.