/* * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package jdk.internal.event; import java.time.Duration; import java.time.Instant; import java.util.Date; /** * A helper class to have events committed to both the JFR and Logger * frameworks, when configured. JFR functionality may be enhanced * at a later time to allow for configuration of logging directly from * JFR Event classes. */ public final class EventHelper { private static final System.Logger.Level LOG_LEVEL = System.Logger.Level.DEBUG; // helper class used for logging security related events for now private static final String SECURITY_LOGGER_NAME = "jdk.event.security"; private static final System.Logger SECURITY_LOGGER = System.getLogger(SECURITY_LOGGER_NAME); private static final boolean LOGGING_SECURITY = SECURITY_LOGGER.isLoggable(LOG_LEVEL); /** * Helper method to commit a TLSHandshakeEvent to both the JFR * and Logging frameworks. */ public static void commitTLSHandshakeEvent(TLSHandshakeEvent event, Instant start, String remoteHost, int remotePort, String cipherSuite, String protocolVersion, String certChain) { if (event != null && event.shouldCommit()) { event.remoteHost = remoteHost; event.remotePort = remotePort; event.cipherSuite = cipherSuite; event.protocolVersion = protocolVersion; event.certificateChain = certChain; event.commit(); } if (isLoggingSecurity()) { String prepend = getDurationString(start); SECURITY_LOGGER.log(LOG_LEVEL, prepend + " TLSHandshake: {0}:{1,number,#}, {2}, {3}, {4}", remoteHost, remotePort, protocolVersion, cipherSuite, certChain); } } /** * Helper method to commit a SecurityPropertyEvent to both the JFR * and Logging frameworks. */ public static void commitSecurityPropertyEvent(SecurityPropertyEvent event, String key, String value) { if (event.shouldCommit()) { event.name = key; event.value = value; event.commit(); } if (isLoggingSecurity()) { SECURITY_LOGGER.log(LOG_LEVEL, "SecurityProperty: key:{0}, value:{1}", key, value); } } /** * Helper method to commit a CertChainEvent to both the JFR * and Logging frameworks. */ public static void commitCertChainEvent(CertificateChainEvent event, String certs) { if (event.shouldCommit()) { event.serialNumbers = certs; event.commit(); } if (isLoggingSecurity()) { SECURITY_LOGGER.log(LOG_LEVEL, "CertificateChain: {0}", certs); } } /** * Helper method to commit an X509CertEvent to both the JFR * and Logging frameworks. */ public static void commitX509CertEvent(X509CertEvent event, String algId, String serialNum, String subject, String issuer, String keyType, int length, long beginDate, long endDate) { if (event.shouldCommit()) { event.algorithm = algId; event.serialNumber = serialNum; event.subject = subject; event.issuer = issuer; event.keyType = keyType; event.length = length; event.validFrom = beginDate; event.validUntil = endDate; event.commit(); } if (isLoggingSecurity()) { SECURITY_LOGGER.log(LOG_LEVEL, "X509Certificate: Alg:{0}, Serial:{1}" + ", Subject:{2}, Issuer:{3}, Key type:{4}, Length:{5,number,#}" + ", Valid from:{6}, Valid until:{7}", algId, serialNum, subject, issuer, keyType, length, new Date(beginDate), new Date(endDate)); } } /** * Method to calculate a duration timestamp for events which measure * the start and end times of certain operations. * @param start Instant indicating when event started recording * @return A string representing duraction from start time to * time of this method call. Empty string is start is null. */ private static String getDurationString(Instant start) { if (start != null) { Duration duration = Duration.between(start, Instant.now()); long micros = duration.toNanos() / 1_000; if (micros < 1_000_000) { return "duration = " + (micros / 1_000.0) + " ms:"; } else { return "duration = " + ((micros / 1_000) / 1_000.0) + " s:"; } } else { return ""; } } /** * Helper to determine if security events are being logged * at a preconfigured logging level. The configuration value * is read once at class initialization. * * @return boolean indicating whether an event should be logged */ public static boolean isLoggingSecurity() { return LOGGING_SECURITY; } }