/modules/common/jboss-as/src/main/java/org/jboss/on/common/jbossas/JBossASDiscoveryUtils.java

https://github.com/ccrouch/rhq · Java · 117 lines · 79 code · 12 blank · 26 comment · 7 complexity · 96a2c1e0f559845613c440adb82b040a MD5 · raw file

  1. /*
  2. * Jopr Management Platform
  3. * Copyright (C) 2005-2009 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * published by the Free Software Foundation, and/or the GNU Lesser
  9. * General Public License, version 2.1, also as published by the Free
  10. * Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License and the GNU Lesser General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * and the GNU Lesser General Public License along with this program;
  20. * if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. */
  23. package org.jboss.on.common.jbossas;
  24. import java.io.File;
  25. import java.util.Set;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.jetbrains.annotations.Nullable;
  29. import org.rhq.core.util.exception.ThrowableUtil;
  30. /**
  31. * @author Ian Springer
  32. */
  33. public class JBossASDiscoveryUtils {
  34. private static final Log LOG = LogFactory.getLog(JBossASDiscoveryUtils.class);
  35. @Nullable
  36. public static UserInfo getJmxInvokerUserInfo(File configDir) {
  37. String securityDomain = getJmxInvokerSecurityDomain(configDir);
  38. if (securityDomain == null) {
  39. LOG.debug("The JMX invoker service is not configured to require authentication.");
  40. return null;
  41. }
  42. LOG.debug("The JMX invoker service is configured to use the '" + securityDomain
  43. + "' security domain for authentication.");
  44. File usersPropsFile = new File(configDir, "conf/props/" + securityDomain + "-users.properties");
  45. if (!usersPropsFile.exists()) {
  46. LOG.debug("Could not find users configuration for security domain '" + securityDomain
  47. + "' - " + usersPropsFile + " does not exist.");
  48. }
  49. File rolesPropsFile = new File(configDir, "conf/props/" + securityDomain + "-roles.properties");
  50. if (!rolesPropsFile.exists()) {
  51. LOG.debug("Could not find roles configuration for security domain '" + securityDomain
  52. + "' - " + rolesPropsFile + " does not exist.");
  53. }
  54. if (usersPropsFile.exists() && rolesPropsFile.exists()) {
  55. try {
  56. SecurityDomainInfo securityDomainInfo = new SecurityDomainInfo(usersPropsFile, rolesPropsFile);
  57. Set<String> adminUsers = securityDomainInfo.getUsers("JBossAdmin");
  58. if (!adminUsers.isEmpty()) {
  59. // Use the first one - it's as good as any.
  60. String adminUser = adminUsers.iterator().next();
  61. String adminPassword = securityDomainInfo.getPassword(adminUser);
  62. LOG.debug("Discovered principal (" + adminUser
  63. + ") and credentials for connecting to the JMX invoker service.");
  64. return new UserInfo(adminUser, adminPassword);
  65. }
  66. }
  67. catch (Exception e) {
  68. LOG.error("Could not determine username and password of admin user - failed to parse users and/or roles configuration file.");
  69. }
  70. }
  71. return null;
  72. }
  73. @Nullable
  74. private static String getJmxInvokerSecurityDomain(File configDir) {
  75. File deployDir = new File(configDir, "deploy");
  76. File jmxInvokerServiceXmlFile = new File(deployDir, "jmx-invoker-service.xml");
  77. String securityDomain = null;
  78. try {
  79. JmxInvokerServiceConfiguration jmxInvokerConfig = new JmxInvokerServiceConfiguration(jmxInvokerServiceXmlFile);
  80. securityDomain = jmxInvokerConfig.getSecurityDomain();
  81. }
  82. catch (Exception e) {
  83. LOG.debug("Failed to read " + jmxInvokerServiceXmlFile
  84. + " - unable to determine if authentication is enabled on the JMX invoker. Cause: "
  85. + ThrowableUtil.getAllMessages(e));
  86. }
  87. return securityDomain;
  88. }
  89. public static class UserInfo {
  90. private final String username;
  91. private final String password;
  92. public UserInfo(String username, String password) {
  93. this.username = username;
  94. this.password = password;
  95. }
  96. public String getUsername() {
  97. return username;
  98. }
  99. public String getPassword() {
  100. return password;
  101. }
  102. }
  103. private JBossASDiscoveryUtils() {
  104. }
  105. }