PageRenderTime 70ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/guvnor-webapp/src/main/java/org/drools/guvnor/server/repository/GuvnorBootstrapConfiguration.java

https://github.com/bounty12281/guvnor
Java | 126 lines | 89 code | 21 blank | 16 comment | 9 complexity | 250a1537a054ccfd6cf820472fe147ec MD5 | raw file
  1. /*
  2. * Copyright 2011 JBoss Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.drools.guvnor.server.repository;
  17. import java.math.BigInteger;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import javax.crypto.Cipher;
  21. import javax.crypto.spec.SecretKeySpec;
  22. import javax.enterprise.context.ApplicationScoped;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. @ApplicationScoped
  26. public class GuvnorBootstrapConfiguration {
  27. private static final String ADMIN_USERNAME_DEFAULT = "admin";
  28. private static final String ADMIN_USERNAME_PROPERTY = "org.drools.repository.admin.username";
  29. private static final String ADMIN_PASSWORD_DEFAULT = "password";
  30. private static final String ADMIN_PASSWORD_PROPERTY = "org.drools.repository.admin.password";
  31. private static final String MAILMAN_USERNAME_DEFAULT = "mailman";
  32. private static final String MAILMAN_USERNAME_PROPERTY = "org.drools.repository.mailman.username";
  33. private static final String MAILMAN_PASSWORD_DEFAULT = "password";
  34. private static final String MAILMAN_PASSWORD_PROPERTY = "org.drools.repository.mailman.password";
  35. private static final String SECURE_PASSWORDS_PROPERTY = "org.drools.repository.secure.passwords";
  36. private transient final Logger log = LoggerFactory.getLogger(getClass());
  37. private Map<String, String> properties = new HashMap<String, String>();
  38. public Map<String, String> getProperties() {
  39. return properties;
  40. }
  41. public void setProperties(Map<String, String> properties) {
  42. this.properties = properties;
  43. }
  44. public String extractAdminUsername() {
  45. if (!properties.containsKey(ADMIN_USERNAME_PROPERTY)) {
  46. return ADMIN_USERNAME_DEFAULT;
  47. }
  48. return properties.get(ADMIN_USERNAME_PROPERTY);
  49. }
  50. public String extractAdminPassword() {
  51. if (!properties.containsKey(ADMIN_PASSWORD_PROPERTY)) {
  52. log.debug("Could not find property " + ADMIN_PASSWORD_PROPERTY + " for user " + ADMIN_USERNAME_DEFAULT);
  53. return ADMIN_PASSWORD_DEFAULT;
  54. }
  55. String password = properties.get(ADMIN_PASSWORD_PROPERTY);
  56. if ("true".equalsIgnoreCase(properties.get(SECURE_PASSWORDS_PROPERTY))) {
  57. password = decode(password);
  58. }
  59. return password;
  60. }
  61. public String extractMailmanUsername() {
  62. if (!properties.containsKey(MAILMAN_USERNAME_PROPERTY)) {
  63. return MAILMAN_USERNAME_DEFAULT;
  64. }
  65. return properties.get(MAILMAN_USERNAME_PROPERTY);
  66. }
  67. public String extractMailmanPassword() {
  68. if (!properties.containsKey(MAILMAN_PASSWORD_PROPERTY)) {
  69. log.debug("Could not find property " + MAILMAN_PASSWORD_PROPERTY + " for user " + MAILMAN_USERNAME_DEFAULT);
  70. return MAILMAN_PASSWORD_DEFAULT;
  71. }
  72. String password = properties.get(MAILMAN_PASSWORD_PROPERTY);
  73. if ("true".equalsIgnoreCase(properties.get(SECURE_PASSWORDS_PROPERTY))) {
  74. password = decode(password);
  75. }
  76. return password;
  77. }
  78. private String decode(String secret) {
  79. String decodedPassword = secret;
  80. try {
  81. byte[] kbytes = "jaas is the way".getBytes();
  82. SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");
  83. BigInteger n = new BigInteger(secret, 16);
  84. byte[] encoding = n.toByteArray();
  85. //SECURITY-344: fix leading zeros
  86. if (encoding.length % 8 != 0) {
  87. int length = encoding.length;
  88. int newLength = ((length / 8) + 1) * 8;
  89. int pad = newLength - length; //number of leading zeros
  90. byte[] old = encoding;
  91. encoding = new byte[newLength];
  92. for (int i = old.length - 1; i >= 0; i--) {
  93. encoding[i + pad] = old[i];
  94. }
  95. }
  96. Cipher cipher = Cipher.getInstance("Blowfish");
  97. cipher.init(Cipher.DECRYPT_MODE, key);
  98. byte[] decode = cipher.doFinal(encoding);
  99. decodedPassword = new String(decode);
  100. } catch (Exception e) {
  101. log.error(e.getMessage(), e);
  102. }
  103. return decodedPassword;
  104. }
  105. }