-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'sstiglitz-rebase-for-publish'
- Loading branch information
Showing
21 changed files
with
766 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...n/java/org/springframework/cloud/config/server/config/ResourceEncryptorConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2002-2019 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.config.server.config; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; | ||
import org.springframework.cloud.config.server.encryption.CipherResourceJsonEncryptor; | ||
import org.springframework.cloud.config.server.encryption.CipherResourcePropertiesEncryptor; | ||
import org.springframework.cloud.config.server.encryption.CipherResourceYamlEncryptor; | ||
import org.springframework.cloud.config.server.encryption.ResourceEncryptor; | ||
import org.springframework.cloud.config.server.encryption.TextEncryptorLocator; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Adds configuration to decrypt plain text files served through | ||
* {@link org.springframework.cloud.config.server.resource.ResourceController}. Each | ||
* supported extension is added as a key with its associated @{link | ||
* org.springframework.cloud.config.server.encryption.ResourceEncryptor} implementation as | ||
* a value. | ||
* | ||
* @author Sean Stiglitz | ||
*/ | ||
@Configuration | ||
@ConditionalOnExpression("${spring.cloud.config.server.encrypt.enabled:true} && ${spring.cloud.config.server.encrypt.plainTextEncrypt:false}") | ||
public class ResourceEncryptorConfiguration { | ||
|
||
@Autowired | ||
private TextEncryptorLocator encryptor; | ||
|
||
@Bean | ||
Map<String, ResourceEncryptor> resourceEncryptors() { | ||
Map<String, ResourceEncryptor> resourceEncryptorMap = new HashMap<>(); | ||
addSupportedExtensionsToMap(resourceEncryptorMap, | ||
new CipherResourceJsonEncryptor(encryptor)); | ||
addSupportedExtensionsToMap(resourceEncryptorMap, | ||
new CipherResourcePropertiesEncryptor(encryptor)); | ||
addSupportedExtensionsToMap(resourceEncryptorMap, | ||
new CipherResourceYamlEncryptor(encryptor)); | ||
return resourceEncryptorMap; | ||
} | ||
|
||
private void addSupportedExtensionsToMap( | ||
Map<String, ResourceEncryptor> resourceEncryptorMap, | ||
ResourceEncryptor resourceEncryptor) { | ||
for (String ext : resourceEncryptor.getSupportedExtensions()) { | ||
resourceEncryptorMap.put(ext, resourceEncryptor); | ||
} | ||
} | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
...a/org/springframework/cloud/config/server/encryption/AbstractCipherResourceEncryptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2002-2019 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.config.server.encryption; | ||
|
||
import java.io.IOException; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
|
||
import org.springframework.cloud.config.environment.Environment; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* Abstract base class for any @{link | ||
* org.springframework.cloud.config.server.encryption.ResourceEncryptor} implementations. | ||
* Meant to house shared configuration and logic. | ||
* | ||
* @author Sean Stiglitz | ||
*/ | ||
abstract class AbstractCipherResourceEncryptor implements ResourceEncryptor { | ||
|
||
protected final String CIPHER_MARKER = "{cipher}"; | ||
|
||
private final TextEncryptorLocator encryptor; | ||
|
||
private EnvironmentPrefixHelper helper = new EnvironmentPrefixHelper(); | ||
|
||
AbstractCipherResourceEncryptor(TextEncryptorLocator encryptor) { | ||
this.encryptor = encryptor; | ||
} | ||
|
||
@Override | ||
public abstract List<String> getSupportedExtensions(); | ||
|
||
@Override | ||
public abstract String decrypt(String text, Environment environment) | ||
throws IOException; | ||
|
||
protected String decryptWithJacksonParser(String text, String name, String[] profiles, | ||
JsonFactory factory) throws IOException { | ||
Set<String> valsToDecrpyt = new HashSet<String>(); | ||
JsonParser parser = factory.createParser(text); | ||
JsonToken token; | ||
|
||
while ((token = parser.nextToken()) != null) { | ||
if (token.equals(JsonToken.VALUE_STRING) | ||
&& parser.getValueAsString().startsWith(CIPHER_MARKER)) { | ||
valsToDecrpyt.add(parser.getValueAsString().trim()); | ||
} | ||
} | ||
|
||
for (String value : valsToDecrpyt) { | ||
String decryptedValue = decryptValue(value.replace(CIPHER_MARKER, ""), name, | ||
profiles); | ||
text = text.replace(value, decryptedValue); | ||
} | ||
|
||
return text; | ||
} | ||
|
||
protected String decryptValue(String value, String name, String[] profiles) { | ||
return encryptor | ||
.locate(this.helper.getEncryptorKeys(name, | ||
StringUtils.arrayToCommaDelimitedString(profiles), value)) | ||
.decrypt(this.helper.stripPrefix(value)); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
.../java/org/springframework/cloud/config/server/encryption/CipherResourceJsonEncryptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2013-2019 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.config.server.encryption; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
|
||
import org.springframework.cloud.config.environment.Environment; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @{link org.springframework.cloud.config.server.encryption.ResourceEncryptor} | ||
* implementation that can decrypt property values prefixed with {cipher} marker in a JSON | ||
* file. | ||
* @author Sean Stiglitz | ||
*/ | ||
@Component | ||
public class CipherResourceJsonEncryptor extends AbstractCipherResourceEncryptor | ||
implements ResourceEncryptor { | ||
|
||
private static final List<String> SUPPORTED_EXTENSIONS = Arrays.asList("json"); | ||
|
||
private final JsonFactory factory; | ||
|
||
public CipherResourceJsonEncryptor(TextEncryptorLocator encryptor) { | ||
super(encryptor); | ||
this.factory = new JsonFactory(); | ||
} | ||
|
||
@Override | ||
public List<String> getSupportedExtensions() { | ||
return SUPPORTED_EXTENSIONS; | ||
} | ||
|
||
@Override | ||
public String decrypt(String text, Environment environment) throws IOException { | ||
return decryptWithJacksonParser(text, environment.getName(), | ||
environment.getProfiles(), factory); | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
...org/springframework/cloud/config/server/encryption/CipherResourcePropertiesEncryptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2002-2019 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.config.server.encryption; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Properties; | ||
import java.util.Set; | ||
|
||
import org.springframework.cloud.config.environment.Environment; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @{link org.springframework.cloud.config.server.encryption.ResourceEncryptor} | ||
* implementation that can decrypt property values prefixed with {cipher} marker in a | ||
* Properties file. | ||
* @author Sean Stiglitz | ||
*/ | ||
@Component | ||
public class CipherResourcePropertiesEncryptor extends AbstractCipherResourceEncryptor | ||
implements ResourceEncryptor { | ||
|
||
private static final List<String> SUPPORTED_EXTENSIONS = Arrays.asList("properties"); | ||
|
||
public CipherResourcePropertiesEncryptor(TextEncryptorLocator encryptor) { | ||
super(encryptor); | ||
} | ||
|
||
@Override | ||
public List<String> getSupportedExtensions() { | ||
return SUPPORTED_EXTENSIONS; | ||
} | ||
|
||
@Override | ||
public String decrypt(String text, Environment environment) throws IOException { | ||
Set<String> valsToDecrpyt = new HashSet<String>(); | ||
Properties properties = new Properties(); | ||
StringBuffer sb = new StringBuffer(); | ||
properties.load(new ByteArrayInputStream(text.getBytes())); | ||
|
||
for (Object value : properties.values()) { | ||
String valueStr = value.toString(); | ||
if (valueStr.startsWith(CIPHER_MARKER)) { | ||
valsToDecrpyt.add(valueStr); | ||
} | ||
} | ||
|
||
for (String value : valsToDecrpyt) { | ||
String decryptedValue = decryptValue(value.replace(CIPHER_MARKER, ""), | ||
environment.getName(), environment.getProfiles()); | ||
text = text.replace(value, decryptedValue); | ||
} | ||
|
||
return text; | ||
} | ||
|
||
} |
Oops, something went wrong.