From ab8aa793cfde9999d886474de8f3c995dee9f590 Mon Sep 17 00:00:00 2001 From: erabii Date: Tue, 18 Oct 2022 21:37:54 +0300 Subject: [PATCH] move Input class to a record (#1111) --- .../discovery/ServicePortSecureResolver.java | 50 +++++++++---------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/ServicePortSecureResolver.java b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/ServicePortSecureResolver.java index 07a71333..787946bb 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/ServicePortSecureResolver.java +++ b/spring-cloud-kubernetes-fabric8-discovery/src/main/java/org/springframework/cloud/kubernetes/fabric8/discovery/ServicePortSecureResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2019 the original author or authors. + * Copyright 2013-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,22 +16,19 @@ package org.springframework.cloud.kubernetes.fabric8.discovery; -import java.util.Collections; import java.util.Map; import java.util.Set; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties; +import org.springframework.core.log.LogAccessor; class ServicePortSecureResolver { - private static final Log LOG = LogFactory.getLog(ServicePortSecureResolver.class); + private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(ServicePortSecureResolver.class)); - private static final Set TRUTHY_STRINGS = Stream.of("true", "on", "yes", "1").collect(Collectors.toSet()); + private static final Set TRUTHY_STRINGS = Set.of("true", "on", "yes", "1"); private final KubernetesDiscoveryProperties properties; @@ -52,38 +49,39 @@ class ServicePortSecureResolver { */ boolean resolve(Input input) { - String securedLabelValue = input.serviceLabels.getOrDefault("secured", "false"); + String securedLabelValue = input.serviceLabels().getOrDefault("secured", "false"); + String serviceName = input.serviceName(); + Integer port = input.port(); + if (TRUTHY_STRINGS.contains(securedLabelValue)) { - LOG.debug("Considering service with name: " + input.serviceName + " and port " + input.port - + " is secure since the service contains a true value for the 'secured' label"); + logEntry(serviceName, port, "the service contains a true value for the 'secured' label"); return true; } - String securedAnnotationValue = input.serviceAnnotations.getOrDefault("secured", "false"); + String securedAnnotationValue = input.serviceAnnotations().getOrDefault("secured", "false"); if (TRUTHY_STRINGS.contains(securedAnnotationValue)) { - LOG.debug("Considering service with name: " + input.serviceName + " and port " + input.port - + " is secure since the service contains a true value for the 'secured' annotation"); + logEntry(serviceName, port, "the service contains a true value for the 'secured' annotation"); return true; } - if (input.port != null && this.properties.getKnownSecurePorts().contains(input.port)) { - LOG.debug("Considering service with name: " + input.serviceName + " and port " + input.port - + " is secure due to the port being a known https port"); + if (port != null && properties.getKnownSecurePorts().contains(port)) { + logEntry(serviceName, port, "port is known to be a https port"); return true; } return false; } - static final class Input { + private static void logEntry(String serviceName, Integer port, String part) { + LOG.debug(() -> "Considering service with name: " + serviceName + " and port " + port + + " to be secure since " + part); + } - private final Integer port; - - private final String serviceName; - - private final Map serviceLabels; - - private final Map serviceAnnotations; + /** + * @author wind57 + */ + record Input(Integer port, String serviceName, Map serviceLabels, + Map serviceAnnotations) { // used only for testing Input(Integer port, String serviceName) { @@ -94,8 +92,8 @@ class ServicePortSecureResolver { Map serviceAnnotations) { this.port = port; this.serviceName = serviceName; - this.serviceLabels = serviceLabels == null ? Collections.emptyMap() : serviceLabels; - this.serviceAnnotations = serviceAnnotations == null ? Collections.emptyMap() : serviceAnnotations; + this.serviceLabels = serviceLabels == null ? Map.of() : serviceLabels; + this.serviceAnnotations = serviceAnnotations == null ? Map.of() : serviceAnnotations; } }