1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * The contents of this file are subject to the terms of either the Universal Permissive License 7 * v 1.0 as shown at http://oss.oracle.com/licenses/upl 8 * 9 * or the following license: 10 * 11 * Redistribution and use in source and binary forms, with or without modification, are permitted 12 * provided that the following conditions are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions 15 * and the following disclaimer. 16 * 17 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of 18 * conditions and the following disclaimer in the documentation and/or other materials provided with 19 * the distribution. 20 * 21 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to 22 * endorse or promote products derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 26 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 31 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 package org.openjdk.jmc.flightrecorder.rules.jdk.general; 34 35 import java.util.Collection; 36 import java.util.Collections; 37 import java.util.Set; 38 import java.util.concurrent.Callable; 39 import java.util.concurrent.FutureTask; 40 import java.util.concurrent.RunnableFuture; 41 42 import org.openjdk.jmc.common.item.Aggregators; 43 import org.openjdk.jmc.common.item.IItemCollection; 44 import org.openjdk.jmc.common.item.ItemFilters; 45 import org.openjdk.jmc.common.util.IPreferenceValueProvider; 46 import org.openjdk.jmc.common.util.TypedPreference; 47 import org.openjdk.jmc.flightrecorder.jdk.JdkAttributes; 48 import org.openjdk.jmc.flightrecorder.jdk.JdkFilters; 49 import org.openjdk.jmc.flightrecorder.jdk.JdkTypeIDs; 50 import org.openjdk.jmc.flightrecorder.rules.IRule; 51 import org.openjdk.jmc.flightrecorder.rules.Result; 52 import org.openjdk.jmc.flightrecorder.rules.jdk.messages.internal.Messages; 53 import org.openjdk.jmc.flightrecorder.rules.util.JfrRuleTopics; 54 import org.openjdk.jmc.flightrecorder.rules.util.RulesToolkit; 55 import org.openjdk.jmc.flightrecorder.rules.util.RulesToolkit.EventAvailability; 56 57 public class ManagementAgentRule implements IRule { 58 59 private static final String RESULT_ID = "ManagementAgent"; //$NON-NLS-1$ 60 61 private Result getResult(IItemCollection items, IPreferenceValueProvider valueProvider) { 62 EventAvailability eventAvailability = RulesToolkit.getEventAvailability(items, JdkTypeIDs.SYSTEM_PROPERTIES); 63 if (eventAvailability == EventAvailability.UNAVAILABLE || eventAvailability == EventAvailability.DISABLED) { 64 return RulesToolkit.getEventAvailabilityResult(this, items, eventAvailability, 65 JdkTypeIDs.SYSTEM_PROPERTIES); 66 } 67 68 // FIXME: Move the filter inside the aggregate. 69 IItemCollection properties = items.apply(JdkFilters.SYSTEM_PROPERTIES); 70 71 Set<String> portStr = properties 72 .apply(ItemFilters.equals(JdkAttributes.ENVIRONMENT_KEY, "com.sun.management.jmxremote.port")) //$NON-NLS-1$ 73 .getAggregate(Aggregators.distinct(JdkAttributes.ENVIRONMENT_VALUE)); 74 Set<String> authStr = properties 75 .apply(ItemFilters.equals(JdkAttributes.ENVIRONMENT_KEY, "com.sun.management.jmxremote.authenticate")) //$NON-NLS-1$ 76 .getAggregate(Aggregators.distinct(JdkAttributes.ENVIRONMENT_VALUE)); 77 Set<String> sslStr = properties 78 .apply(ItemFilters.equals(JdkAttributes.ENVIRONMENT_KEY, "com.sun.management.jmxremote.ssl")) //$NON-NLS-1$ 79 .getAggregate(Aggregators.distinct(JdkAttributes.ENVIRONMENT_VALUE)); 80 81 if (size(portStr) > 1 || size(authStr) > 1 || size(sslStr) > 1) { 82 return new Result(this, 50, Messages.getString(Messages.ManagementAgentRule_TEXT_INFO), 83 Messages.getString(Messages.ManagementAgentRule_TEXT_INFO_LONG)); 84 } 85 if (size(portStr) > 0) { 86 // Default is true. 87 boolean auth = size(authStr) > 0 ? Boolean.parseBoolean(authStr.iterator().next()) : true; 88 boolean ssl = size(sslStr) > 0 ? Boolean.parseBoolean(sslStr.iterator().next()) : true; 89 if (!auth && !ssl) { 90 String shortMessage = Messages.getString(Messages.ManagmentAgentRuleFactory_TEXT_WARN_BOTH_DISABLED); 91 String longMessage = Messages.getString(Messages.ManagmentAgentRuleFactory_TEXT_WARN_BOTH_DISABLED_LONG) 92 + "<p>" + Messages.getString(Messages.ManagmentAgentRuleFactory_TEXT_WARN_CONFIGURE_GUIDE); //$NON-NLS-1$ 93 return new Result(this, 100, shortMessage, longMessage); 94 } else if (!auth) { 95 String shortMessage = Messages.getString(Messages.ManagmentAgentRuleFactory_TEXT_WARN_AUTH_DISABLED); 96 String longMessage = Messages.getString(Messages.ManagmentAgentRuleFactory_TEXT_WARN_AUTH_DISABLED_LONG) 97 + "<p>" + Messages.getString(Messages.ManagmentAgentRuleFactory_TEXT_WARN_CONFIGURE_GUIDE); //$NON-NLS-1$ 98 return new Result(this, 100, shortMessage, longMessage); 99 } else if (!ssl) { 100 String shortMessage = Messages.getString(Messages.ManagmentAgentRuleFactory_TEXT_INFO_SSL_DISABLED); 101 String longMessage = Messages.getString(Messages.ManagmentAgentRuleFactory_TEXT_INFO_SSL_DISABLED_LONG) 102 + "<p>" + Messages.getString(Messages.ManagmentAgentRuleFactory_TEXT_WARN_CONFIGURE_GUIDE); //$NON-NLS-1$ 103 return new Result(this, 50, shortMessage, longMessage); 104 } 105 } 106 return new Result(this, 0, Messages.getString(Messages.ManagmentAgentRuleFactory_TEXT_OK)); 107 } 108 109 private static int size(Collection<?> set) { 110 return set == null ? 0 : set.size(); 111 } 112 113 @Override 114 public RunnableFuture<Result> evaluate(final IItemCollection items, final IPreferenceValueProvider valueProvider) { 115 FutureTask<Result> evaluationTask = new FutureTask<>(new Callable<Result>() { 116 @Override 117 public Result call() throws Exception { 118 return getResult(items, valueProvider); 119 } 120 }); 121 return evaluationTask; 122 } 123 124 @Override 125 public Collection<TypedPreference<?>> getConfigurationAttributes() { 126 return Collections.emptyList(); 127 } 128 129 @Override 130 public String getId() { 131 return RESULT_ID; 132 } 133 134 @Override 135 public String getName() { 136 return Messages.getString(Messages.ManagmentAgentRuleFactory_RULE_NAME); 137 } 138 139 @Override 140 public String getTopic() { 141 return JfrRuleTopics.JVM_INFORMATION_TOPIC; 142 } 143 }