|
| 1 | +package com.axway.yamles.utils.plugins.core; |
| 2 | + |
| 3 | +import java.security.SecureRandom; |
| 4 | +import java.util.Base64; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.Optional; |
| 7 | + |
| 8 | +import javax.crypto.SecretKeyFactory; |
| 9 | +import javax.crypto.spec.PBEKeySpec; |
| 10 | + |
| 11 | +import org.pf4j.Extension; |
| 12 | + |
| 13 | +import com.axway.yamles.utils.plugins.AbstractBuiltinLookupProvider; |
| 14 | +import com.axway.yamles.utils.plugins.FunctionArgument; |
| 15 | +import com.axway.yamles.utils.plugins.LookupFunction; |
| 16 | +import com.axway.yamles.utils.plugins.LookupFunctionException; |
| 17 | +import com.axway.yamles.utils.plugins.LookupProviderException; |
| 18 | + |
| 19 | +@Extension |
| 20 | +public class AnmPasswordHashLookupProvider extends AbstractBuiltinLookupProvider { |
| 21 | + public static class PasswordHashGenerator { |
| 22 | + public static final String VERSION = "AAGQAAAAAQAC"; |
| 23 | + |
| 24 | + private PasswordHashGenerator() { |
| 25 | + }; |
| 26 | + |
| 27 | + public static String generate(String password) throws Exception { |
| 28 | + byte[] salt = generateSalt(); |
| 29 | + byte[] hash = generateHashedPassord(password, salt); |
| 30 | + |
| 31 | + String value = new StringBuilder() // |
| 32 | + .append('$').append(VERSION) // |
| 33 | + .append('$').append(Base64.getEncoder().encodeToString(salt)) // |
| 34 | + .append('$').append(Base64.getEncoder().encodeToString(hash)) // |
| 35 | + .toString(); |
| 36 | + return value; |
| 37 | + } |
| 38 | + |
| 39 | + private static byte[] generateSalt() { |
| 40 | + SecureRandom random = new SecureRandom(); |
| 41 | + byte[] salt = new byte[16]; |
| 42 | + random.nextBytes(salt); |
| 43 | + |
| 44 | + return salt; |
| 45 | + } |
| 46 | + |
| 47 | + private static byte[] generateHashedPassord(String password, byte[] salt) throws Exception { |
| 48 | + PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 102400, 256); |
| 49 | + SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); |
| 50 | + return factory.generateSecret(spec).getEncoded(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + protected static class LF extends LookupFunction { |
| 55 | + |
| 56 | + public LF(String alias, AnmPasswordHashLookupProvider provider) { |
| 57 | + super(alias, provider, AbstractBuiltinLookupProvider.SOURCE); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Optional<String> lookup(Map<String, Object> args) throws LookupFunctionException { |
| 62 | + String password = getArg(ARG_PWD, args, ""); |
| 63 | + if (password == null || password.isEmpty()) { |
| 64 | + return Optional.empty(); |
| 65 | + } |
| 66 | + try { |
| 67 | + return Optional.of(PasswordHashGenerator.generate(password)); |
| 68 | + } catch (Exception e) { |
| 69 | + throw new LookupFunctionException(this, "ANM password hash generation failed", e); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + protected static FunctionArgument ARG_PWD = new FunctionArgument("pwd", true, "Password"); |
| 75 | + |
| 76 | + public AnmPasswordHashLookupProvider() { |
| 77 | + super(); |
| 78 | + add(ARG_PWD); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public String getName() { |
| 83 | + return "gen_anm_pwd_hash"; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public String getSummary() { |
| 88 | + return "Generate hashed password for Admin Node Manager user."; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public String getDescription() { |
| 93 | + return "Use a clear text password to generate a hashed password to be used for ANM users in 'adminUsers.json' file."; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + protected LookupFunction buildFunction() throws LookupProviderException { |
| 98 | + return new LF(getName(), this); |
| 99 | + } |
| 100 | +} |
0 commit comments