|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 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 java.io.IOException; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Set; |
| 23 | + |
| 24 | +import com.fasterxml.jackson.core.JsonFactory; |
| 25 | +import com.fasterxml.jackson.core.JsonParser; |
| 26 | +import com.fasterxml.jackson.core.JsonToken; |
| 27 | + |
| 28 | +import org.springframework.cloud.config.environment.Environment; |
| 29 | +import org.springframework.util.StringUtils; |
| 30 | + |
| 31 | +/** |
| 32 | + * Abstract base class for any @{link |
| 33 | + * org.springframework.cloud.config.server.encryption.ResourceEncryptor} implementations. |
| 34 | + * Meant to house shared configuration and logic. |
| 35 | + * |
| 36 | + * @author Sean Stiglitz |
| 37 | + */ |
| 38 | +abstract class AbstractCipherResourceEncryptor implements ResourceEncryptor { |
| 39 | + |
| 40 | + protected final String CIPHER_MARKER = "{cipher}"; |
| 41 | + |
| 42 | + private final TextEncryptorLocator encryptor; |
| 43 | + |
| 44 | + private EnvironmentPrefixHelper helper = new EnvironmentPrefixHelper(); |
| 45 | + |
| 46 | + AbstractCipherResourceEncryptor(TextEncryptorLocator encryptor) { |
| 47 | + this.encryptor = encryptor; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public abstract List<String> getSupportedExtensions(); |
| 52 | + |
| 53 | + @Override |
| 54 | + public abstract String decrypt(String text, Environment environment) |
| 55 | + throws IOException; |
| 56 | + |
| 57 | + protected String decryptWithJacksonParser(String text, String name, String[] profiles, |
| 58 | + JsonFactory factory) throws IOException { |
| 59 | + Set<String> valsToDecrpyt = new HashSet<String>(); |
| 60 | + JsonParser parser = factory.createParser(text); |
| 61 | + JsonToken token; |
| 62 | + |
| 63 | + while ((token = parser.nextToken()) != null) { |
| 64 | + if (token.equals(JsonToken.VALUE_STRING) |
| 65 | + && parser.getValueAsString().startsWith(CIPHER_MARKER)) { |
| 66 | + valsToDecrpyt.add(parser.getValueAsString().trim()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + for (String value : valsToDecrpyt) { |
| 71 | + String decryptedValue = decryptValue(value.replace(CIPHER_MARKER, ""), name, |
| 72 | + profiles); |
| 73 | + text = text.replace(value, decryptedValue); |
| 74 | + } |
| 75 | + |
| 76 | + return text; |
| 77 | + } |
| 78 | + |
| 79 | + protected String decryptValue(String value, String name, String[] profiles) { |
| 80 | + return encryptor |
| 81 | + .locate(this.helper.getEncryptorKeys(name, |
| 82 | + StringUtils.arrayToCommaDelimitedString(profiles), value)) |
| 83 | + .decrypt(this.helper.stripPrefix(value)); |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments