|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 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.config.server.encryption; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; |
| 22 | +import org.springframework.boot.autoconfigure.web.ServerProperties; |
| 23 | +import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; |
| 24 | +import org.springframework.boot.builder.SpringApplicationBuilder; |
| 25 | +import org.springframework.cloud.bootstrap.encrypt.TextEncryptorUtils; |
| 26 | +import org.springframework.cloud.config.server.config.DefaultTextEncryptionAutoConfiguration; |
| 27 | +import org.springframework.cloud.config.server.config.RsaEncryptionAutoConfiguration; |
| 28 | +import org.springframework.context.ConfigurableApplicationContext; |
| 29 | +import org.springframework.security.crypto.encrypt.Encryptors; |
| 30 | +import org.springframework.security.crypto.encrypt.TextEncryptor; |
| 31 | +import org.springframework.security.rsa.crypto.RsaSecretEncryptor; |
| 32 | + |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author Ryan Baxter |
| 37 | + */ |
| 38 | +public class EncryptionAutoConfigurationTests { |
| 39 | + |
| 40 | + @Test |
| 41 | + public void defaultNoKeyAutoConfigurationTest() { |
| 42 | + ConfigurableApplicationContext context = new SpringApplicationBuilder( |
| 43 | + DefaultTextEncryptionAutoConfiguration.class, RsaEncryptionAutoConfiguration.class, |
| 44 | + ServletWebServerFactoryAutoConfiguration.class, ServerProperties.class, |
| 45 | + PropertyPlaceholderAutoConfiguration.class).properties("server.port=0").run(); |
| 46 | + TextEncryptor textEncryptor = context.getBean(TextEncryptor.class); |
| 47 | + String[] textEncryptorLocatorNames = context.getBeanNamesForType(TextEncryptorLocator.class); |
| 48 | + assertThat(textEncryptor).isInstanceOf(TextEncryptorUtils.FailsafeTextEncryptor.class); |
| 49 | + assertThat(textEncryptorLocatorNames).isEmpty(); |
| 50 | + context.close(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void defaultKeyStoreAutoConfigurationTest() { |
| 55 | + ConfigurableApplicationContext context = new SpringApplicationBuilder( |
| 56 | + DefaultTextEncryptionAutoConfiguration.class, RsaEncryptionAutoConfiguration.class, |
| 57 | + ServletWebServerFactoryAutoConfiguration.class, ServerProperties.class, |
| 58 | + PropertyPlaceholderAutoConfiguration.class) |
| 59 | + .properties("server.port=0", "encrypt.key-store.location=classpath:server.jks", |
| 60 | + "encrypt.key-store.password=letmein", "encrypt.key-store.alias=myKey") |
| 61 | + .run(); |
| 62 | + TextEncryptor textEncryptor = context.getBean(TextEncryptor.class); |
| 63 | + TextEncryptorLocator textEncryptorLocator = context.getBean(TextEncryptorLocator.class); |
| 64 | + assertThat(textEncryptor).isInstanceOf(RsaSecretEncryptor.class); |
| 65 | + assertThat(textEncryptorLocator).isInstanceOf(KeyStoreTextEncryptorLocator.class); |
| 66 | + context.close(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void defaultKeyAutoConfigurationTest() { |
| 71 | + ConfigurableApplicationContext context = new SpringApplicationBuilder( |
| 72 | + DefaultTextEncryptionAutoConfiguration.class, RsaEncryptionAutoConfiguration.class, |
| 73 | + ServletWebServerFactoryAutoConfiguration.class, ServerProperties.class, |
| 74 | + PropertyPlaceholderAutoConfiguration.class).properties("server.port=0", "encrypt.key=mykey").run(); |
| 75 | + TextEncryptor textEncryptor = context.getBean(TextEncryptor.class); |
| 76 | + String[] textEncryptorLocatorNames = context.getBeanNamesForType(TextEncryptorLocator.class); |
| 77 | + assertThat(textEncryptor.getClass().getName()) |
| 78 | + .isEqualTo("org.springframework.security.crypto.encrypt.HexEncodingTextEncryptor"); |
| 79 | + assertThat(textEncryptorLocatorNames).isEmpty(); |
| 80 | + context.close(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void bootstrapNoKeyAutoConfigurationTest() { |
| 85 | + ConfigurableApplicationContext context = new SpringApplicationBuilder( |
| 86 | + DefaultTextEncryptionAutoConfiguration.class, RsaEncryptionAutoConfiguration.class, |
| 87 | + ServletWebServerFactoryAutoConfiguration.class, ServerProperties.class, |
| 88 | + PropertyPlaceholderAutoConfiguration.class) |
| 89 | + .properties("server.port=0", "spring.cloud.bootstrap.enabled=true").run(); |
| 90 | + TextEncryptor textEncryptor = context.getBean(TextEncryptor.class); |
| 91 | + String[] textEncryptorLocatorNames = context.getBeanNamesForType(TextEncryptorLocator.class); |
| 92 | + assertThat(textEncryptor.getClass().isInstance(Encryptors.noOpText().getClass())); |
| 93 | + assertThat(textEncryptorLocatorNames).isEmpty(); |
| 94 | + context.close(); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void bootstrapKeyAutoConfigurationTests() { |
| 99 | + ConfigurableApplicationContext context = new SpringApplicationBuilder( |
| 100 | + DefaultTextEncryptionAutoConfiguration.class, RsaEncryptionAutoConfiguration.class, |
| 101 | + ServletWebServerFactoryAutoConfiguration.class, ServerProperties.class, |
| 102 | + PropertyPlaceholderAutoConfiguration.class) |
| 103 | + .properties("server.port=0", "spring.cloud.bootstrap.enabled=true", "encrypt.key=mykey").run(); |
| 104 | + TextEncryptor textEncryptor = context.getBean(TextEncryptor.class); |
| 105 | + assertThat(textEncryptor.getClass().getName()) |
| 106 | + .isEqualTo("org.springframework.security.crypto.encrypt.HexEncodingTextEncryptor"); |
| 107 | + context.close(); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void bootstrapKeyStoreAutoConfigurationTest() { |
| 112 | + ConfigurableApplicationContext context = new SpringApplicationBuilder( |
| 113 | + DefaultTextEncryptionAutoConfiguration.class, RsaEncryptionAutoConfiguration.class, |
| 114 | + ServletWebServerFactoryAutoConfiguration.class, ServerProperties.class, |
| 115 | + PropertyPlaceholderAutoConfiguration.class) |
| 116 | + .properties("server.port=0", "spring.cloud.bootstrap.enabled=true", |
| 117 | + "encrypt.key-store.location=classpath:server.jks", "encrypt.key-store.password=letmein", |
| 118 | + "encrypt.key-store.alias=myKey") |
| 119 | + .run(); |
| 120 | + TextEncryptor textEncryptor = context.getBean(TextEncryptor.class); |
| 121 | + TextEncryptorLocator textEncryptorLocator = context.getBean(TextEncryptorLocator.class); |
| 122 | + assertThat(textEncryptor).isInstanceOf(LocatorTextEncryptor.class); |
| 123 | + assertThat(textEncryptorLocator).isInstanceOf(KeyStoreTextEncryptorLocator.class); |
| 124 | + context.close(); |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments