Skip to content

Commit 9858649

Browse files
committed
Polish "Enable masking ACL token in the logs"
Adds test, also mask ConsulDiscoveryProperties.aclToken in toString() Fixes gh-889
1 parent 91528c5 commit 9858649

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2013-present the original author or authors.
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+
* https://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+
17+
package org.springframework.cloud.consul.config;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
public class ConsulConfigPropertiesTests {
24+
25+
@Test
26+
public void aclTokenToStringMasked() {
27+
ConsulConfigProperties properties = new ConsulConfigProperties();
28+
properties.setAclToken("myAclToken");
29+
assertThat(properties.toString()).doesNotContain("myAclToken").contains("******");
30+
}
31+
32+
}

spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ public void setManagementEnableTagOverride(Boolean managementEnableTagOverride)
619619

620620
@Override
621621
public String toString() {
622-
return new ToStringCreator(this).append("aclToken", this.aclToken)
622+
return new ToStringCreator(this).append("aclToken", this.aclToken != null ? "******" : null)
623623
.append("catalogServicesWatchDelay", this.catalogServicesWatchDelay)
624624
.append("catalogServicesWatchTimeout", this.catalogServicesWatchTimeout)
625625
.append("consistencyMode", this.consistencyMode)

spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/reactive/ConsulReactiveDiscoveryClientTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import reactor.test.StepVerifier;
3434

3535
import org.springframework.cloud.client.ServiceInstance;
36+
import org.springframework.cloud.commons.util.InetUtils;
3637
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
3738

3839
import static java.util.Collections.emptyList;
@@ -88,9 +89,18 @@ public void shouldReturnFluxOfServices() {
8889
verify(consulClient).getCatalogServices(any(CatalogServicesRequest.class));
8990
}
9091

92+
@Test
93+
public void aclTokenToStringMasked() {
94+
InetUtils inetUtils = mock(InetUtils.class);
95+
when(inetUtils.findFirstNonLoopbackHostInfo()).thenReturn(mock(InetUtils.HostInfo.class));
96+
ConsulDiscoveryProperties consulDiscoveryProperties = new ConsulDiscoveryProperties(inetUtils);
97+
consulDiscoveryProperties.setAclToken("myAclToken");
98+
assertThat(consulDiscoveryProperties.toString()).doesNotContain("myAclToken").contains("******");
99+
}
100+
91101
@Test
92102
public void shouldReturnFluxOfServicesWithAclToken() {
93-
when(properties.getAclToken()).thenReturn("aclToken");
103+
when(properties.getAclToken()).thenReturn("myAclToken");
94104
when(consulClient.getCatalogServices(any(CatalogServicesRequest.class))).thenReturn(consulServicesResponse());
95105
Flux<String> services = client.getServices();
96106
StepVerifier.create(services).expectNext("my-service").expectComplete().verify();

0 commit comments

Comments
 (0)