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

https://github.com/sbandaru/guvnor · Java · 137 lines · 99 code · 22 blank · 16 comment · 10 complexity · 74538c5bea7ae5dbcd505e36c57d8f57 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.annotation.PostConstruct;
  21. import javax.crypto.Cipher;
  22. import javax.crypto.spec.SecretKeySpec;
  23. import javax.enterprise.context.ApplicationScoped;
  24. import org.drools.repository.RulesRepositoryConfigurator;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. @ApplicationScoped
  28. public class GuvnorBootstrapConfiguration {
  29. private static final String ADMIN_USERNAME_DEFAULT = "admin";
  30. private static final String ADMIN_USERNAME_PROPERTY = "org.drools.repository.admin.username";
  31. private static final String ADMIN_PASSWORD_DEFAULT = "password";
  32. private static final String ADMIN_PASSWORD_PROPERTY = "org.drools.repository.admin.password";
  33. private static final String MAILMAN_USERNAME_DEFAULT = "mailman";
  34. private static final String MAILMAN_USERNAME_PROPERTY = "org.drools.repository.mailman.username";
  35. private static final String MAILMAN_PASSWORD_DEFAULT = "password";
  36. private static final String MAILMAN_PASSWORD_PROPERTY = "org.drools.repository.mailman.password";
  37. private static final String SECURE_PASSWORDS_PROPERTY = "org.drools.repository.secure.passwords";
  38. private transient final Logger log = LoggerFactory.getLogger(getClass());
  39. private Map<String, String> properties = new HashMap<String, String>();
  40. public Map<String, String> getProperties() {
  41. return properties;
  42. }
  43. public void setProperties(Map<String, String> properties) {
  44. this.properties = properties;
  45. }
  46. @PostConstruct
  47. public void validate() {
  48. if (!properties.containsKey(RulesRepositoryConfigurator.CONFIGURATOR_CLASS)) {
  49. throw new IllegalStateException("The beans.xml file does not have a GuvnorBootstrapConfiguration " +
  50. "with a property for the configurator class (" + RulesRepositoryConfigurator.CONFIGURATOR_CLASS
  51. + ") configured.");
  52. }
  53. }
  54. public String extractAdminUsername() {
  55. if (!properties.containsKey(ADMIN_USERNAME_PROPERTY)) {
  56. return ADMIN_USERNAME_DEFAULT;
  57. }
  58. return properties.get(ADMIN_USERNAME_PROPERTY);
  59. }
  60. public String extractAdminPassword() {
  61. if (!properties.containsKey(ADMIN_PASSWORD_PROPERTY)) {
  62. log.debug("Could not find property " + ADMIN_PASSWORD_PROPERTY + " for user " + ADMIN_USERNAME_DEFAULT);
  63. return ADMIN_PASSWORD_DEFAULT;
  64. }
  65. String password = properties.get(ADMIN_PASSWORD_PROPERTY);
  66. if ("true".equalsIgnoreCase(properties.get(SECURE_PASSWORDS_PROPERTY))) {
  67. password = decode(password);
  68. }
  69. return password;
  70. }
  71. public String extractMailmanUsername() {
  72. if (!properties.containsKey(MAILMAN_USERNAME_PROPERTY)) {
  73. return MAILMAN_USERNAME_DEFAULT;
  74. }
  75. return properties.get(MAILMAN_USERNAME_PROPERTY);
  76. }
  77. public String extractMailmanPassword() {
  78. if (!properties.containsKey(MAILMAN_PASSWORD_PROPERTY)) {
  79. log.debug("Could not find property " + MAILMAN_PASSWORD_PROPERTY + " for user " + MAILMAN_USERNAME_DEFAULT);
  80. return MAILMAN_PASSWORD_DEFAULT;
  81. }
  82. String password = properties.get(MAILMAN_PASSWORD_PROPERTY);
  83. if ("true".equalsIgnoreCase(properties.get(SECURE_PASSWORDS_PROPERTY))) {
  84. password = decode(password);
  85. }
  86. return password;
  87. }
  88. private String decode(String secret) {
  89. String decodedPassword = secret;
  90. try {
  91. byte[] kbytes = "jaas is the way".getBytes();
  92. SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");
  93. BigInteger n = new BigInteger(secret, 16);
  94. byte[] encoding = n.toByteArray();
  95. //SECURITY-344: fix leading zeros
  96. if (encoding.length % 8 != 0) {
  97. int length = encoding.length;
  98. int newLength = ((length / 8) + 1) * 8;
  99. int pad = newLength - length; //number of leading zeros
  100. byte[] old = encoding;
  101. encoding = new byte[newLength];
  102. for (int i = old.length - 1; i >= 0; i--) {
  103. encoding[i + pad] = old[i];
  104. }
  105. }
  106. Cipher cipher = Cipher.getInstance("Blowfish");
  107. cipher.init(Cipher.DECRYPT_MODE, key);
  108. byte[] decode = cipher.doFinal(encoding);
  109. decodedPassword = new String(decode);
  110. } catch (Exception e) {
  111. log.error(e.getMessage(), e);
  112. }
  113. return decodedPassword;
  114. }
  115. }