|
| 1 | +/* |
| 2 | + * Copyright 2017-2026 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 com.google.cloud.spring.autoconfigure.secretmanager; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.springframework.boot.actuate.endpoint.SanitizableData; |
| 25 | +import org.springframework.core.env.PropertySource; |
| 26 | + |
| 27 | +/** |
| 28 | + * Unit tests for {@link SecretManagerSanitizingFunction}. |
| 29 | + */ |
| 30 | +class SecretManagerSanitizingFunctionTest { |
| 31 | + |
| 32 | + private final SecretManagerSanitizingFunction sanitizingFunction = |
| 33 | + new SecretManagerSanitizingFunction(); |
| 34 | + |
| 35 | + @Test |
| 36 | + void sanitizesSmAtPrefixProperty() { |
| 37 | + String key = "my.secret"; |
| 38 | + String unresolvedValue = "${sm@my-password}"; |
| 39 | + String resolvedValue = "super-secret-123"; |
| 40 | + |
| 41 | + PropertySource<?> mockSource = mock(PropertySource.class); |
| 42 | + when(mockSource.getProperty(key)).thenReturn(unresolvedValue); |
| 43 | + |
| 44 | + SanitizableData data = new SanitizableData(mockSource, key, resolvedValue); |
| 45 | + SanitizableData result = sanitizingFunction.apply(data); |
| 46 | + |
| 47 | + assertThat(result.getValue()).isEqualTo(unresolvedValue); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void sanitizesSmSlashPrefixProperty() { |
| 52 | + String key = "my.secret"; |
| 53 | + String unresolvedValue = "${sm://my-password}"; |
| 54 | + String resolvedValue = "super-secret-123"; |
| 55 | + |
| 56 | + PropertySource<?> mockSource = mock(PropertySource.class); |
| 57 | + when(mockSource.getProperty(key)).thenReturn(unresolvedValue); |
| 58 | + |
| 59 | + SanitizableData data = new SanitizableData(mockSource, key, resolvedValue); |
| 60 | + SanitizableData result = sanitizingFunction.apply(data); |
| 61 | + |
| 62 | + assertThat(result.getValue()).isEqualTo(unresolvedValue); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void sanitizesCompositeValueContainingSmReference() { |
| 67 | + String key = "my.database.url"; |
| 68 | + String unresolvedValue = "https://user:${sm@my-pass}@host/db"; |
| 69 | + String resolvedValue = "https://user:secret123@host/db"; |
| 70 | + |
| 71 | + PropertySource<?> mockSource = mock(PropertySource.class); |
| 72 | + when(mockSource.getProperty(key)).thenReturn(unresolvedValue); |
| 73 | + |
| 74 | + SanitizableData data = new SanitizableData(mockSource, key, resolvedValue); |
| 75 | + SanitizableData result = sanitizingFunction.apply(data); |
| 76 | + |
| 77 | + // The resolved URL (with embedded secret) should be replaced by the unresolved expression. |
| 78 | + assertThat(result.getValue()).isEqualTo(unresolvedValue); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void doesNotSanitizeRegularProperty() { |
| 83 | + String key = "normal.prop"; |
| 84 | + String value = "normal-value"; |
| 85 | + |
| 86 | + PropertySource<?> mockSource = mock(PropertySource.class); |
| 87 | + when(mockSource.getProperty(key)).thenReturn(value); |
| 88 | + |
| 89 | + SanitizableData data = new SanitizableData(mockSource, key, value); |
| 90 | + SanitizableData result = sanitizingFunction.apply(data); |
| 91 | + |
| 92 | + assertThat(result.getValue()).isEqualTo(value); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void returnsDataUnchangedWhenPropertySourceIsNull() { |
| 97 | + SanitizableData data = new SanitizableData(null, "my.key", "some-value"); |
| 98 | + SanitizableData result = sanitizingFunction.apply(data); |
| 99 | + |
| 100 | + assertThat(result.getValue()).isEqualTo("some-value"); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void returnsDataUnchangedWhenValueIsNull() { |
| 105 | + PropertySource<?> mockSource = mock(PropertySource.class); |
| 106 | + when(mockSource.getProperty("my.key")).thenReturn("${sm@some-secret}"); |
| 107 | + |
| 108 | + SanitizableData data = new SanitizableData(mockSource, "my.key", null); |
| 109 | + SanitizableData result = sanitizingFunction.apply(data); |
| 110 | + |
| 111 | + assertThat(result.getValue()).isNull(); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void returnsDataUnchangedWhenUnresolvedValueIsNull() { |
| 116 | + PropertySource<?> mockSource = mock(PropertySource.class); |
| 117 | + when(mockSource.getProperty("my.key")).thenReturn(null); |
| 118 | + |
| 119 | + SanitizableData data = new SanitizableData(mockSource, "my.key", "resolved-value"); |
| 120 | + SanitizableData result = sanitizingFunction.apply(data); |
| 121 | + |
| 122 | + // No SM prefix detected — pass through unchanged. |
| 123 | + assertThat(result.getValue()).isEqualTo("resolved-value"); |
| 124 | + } |
| 125 | +} |
0 commit comments