-
Notifications
You must be signed in to change notification settings - Fork 1.7k
AMBARI-24742. Encrypting/decrypting PASSWORD type properties when inserting them into the DB/using them #2458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3d0b7c6
AMBARI-24742. Encrypting/decrypting PASSWORD type properties when ins…
smolnar82 0e2dc79
AMBARI-24742. Handling the situation when stack is upgraded; document…
smolnar82 751189b
AMBARI-24472. Using a more effective way to populate password propert…
smolnar82 64812db
AMBARI-24742. Caching properties types by stacks
smolnar82 bd579c4
AMBARI-24742. To make encryptor really generic we implement a Config …
smolnar82 587d796
AMBARI-24742. Missing decryption when loading data from DB
smolnar82 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
127 changes: 127 additions & 0 deletions
127
...src/main/java/org/apache/ambari/server/security/encryption/ConfigPropertiesEncryptor.java
This file contains hidden or 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,127 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 | ||
| * <p/> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p/> | ||
| * 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.apache.ambari.server.security.encryption; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| import org.apache.ambari.server.AmbariException; | ||
| import org.apache.ambari.server.AmbariRuntimeException; | ||
| import org.apache.ambari.server.state.Cluster; | ||
| import org.apache.ambari.server.state.Config; | ||
| import org.apache.ambari.server.state.PropertyInfo.PropertyType; | ||
| import org.apache.ambari.server.state.StackId; | ||
| import org.apache.ambari.server.utils.TextEncoding; | ||
| import org.apache.commons.collections.CollectionUtils; | ||
|
|
||
| import com.google.inject.Inject; | ||
| import com.google.inject.Singleton; | ||
|
|
||
| /** | ||
| * {@link Encryptor} implementation for encrypting/decrypting PASSWORD type | ||
| * properties in {@link Config}'s properties | ||
| */ | ||
|
|
||
| @Singleton | ||
| public class ConfigPropertiesEncryptor implements Encryptor<Config> { | ||
|
|
||
| private static final String ENCRYPTED_PROPERTY_PREFIX = "${enc=aes256_hex, value="; | ||
| private static final String ENCRYPTED_PROPERTY_SCHEME = ENCRYPTED_PROPERTY_PREFIX + "%s}"; | ||
|
|
||
| private final EncryptionService encryptionService; | ||
| private final Map<Long, Map<StackId, Map<String, Set<String>>>> clusterPasswordProperties = new ConcurrentHashMap<>(); //Map<clusterId, <Map<stackId, Map<configType, Set<passwordPropertyKeys>>>>; | ||
|
|
||
| @Inject | ||
| public ConfigPropertiesEncryptor(EncryptionService encryptionService) { | ||
| this.encryptionService = encryptionService; | ||
| } | ||
|
|
||
| @Override | ||
| public void encryptSensitiveData(Config config) { | ||
| try { | ||
| final Map<String, String> configProperties = config.getProperties(); | ||
| if (configProperties != null) { | ||
| final Set<String> passwordProperties = getPasswordProperties(config.getCluster(), config.getType()); | ||
| if (CollectionUtils.isNotEmpty(passwordProperties)) { | ||
| final Map<String, String> encryptedProperties = new HashMap<>(configProperties); | ||
| for (Map.Entry<String, String> property : configProperties.entrySet()) { | ||
| if (passwordProperties.contains(property.getKey()) && !isEncryptedPassword(property.getValue())) { | ||
| encryptedProperties.put(property.getKey(), encryptAndDecoratePropertyValue(property.getValue())); | ||
| } | ||
| } | ||
| config.setProperties(encryptedProperties); | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| throw new AmbariRuntimeException("Error while encrypting sensitive data", e); | ||
| } | ||
| } | ||
|
|
||
| private boolean isEncryptedPassword(String password) { | ||
| return password != null && password.startsWith(ENCRYPTED_PROPERTY_PREFIX); // assuming previous encryption by this class | ||
| } | ||
|
|
||
| private Set<String> getPasswordProperties(Cluster cluster, String configType) throws AmbariException { | ||
| //in case of normal configuration change on the UI - or via the API - the current and desired stacks are equal | ||
| //in case of an upgrade they are different; in this case we want to get password properties from the desired stack | ||
| if (cluster.getCurrentStackVersion().equals(cluster.getDesiredStackVersion())) { | ||
| return getPasswordProperties(cluster, cluster.getCurrentStackVersion(), configType); | ||
| } else { | ||
| return getPasswordProperties(cluster, cluster.getDesiredStackVersion(), configType); | ||
| } | ||
| } | ||
|
|
||
| private Set<String> getPasswordProperties(Cluster cluster, StackId stackId, String configType) { | ||
| final long clusterId = cluster.getClusterId(); | ||
| clusterPasswordProperties.computeIfAbsent(clusterId, v -> new ConcurrentHashMap<>()).computeIfAbsent(stackId, v -> new ConcurrentHashMap<>()) | ||
| .computeIfAbsent(configType, v -> cluster.getConfigPropertiesTypes(configType, stackId).getOrDefault(PropertyType.PASSWORD, new HashSet<>())); | ||
| return clusterPasswordProperties.get(clusterId).get(stackId).getOrDefault(configType, new HashSet<>()); | ||
| } | ||
|
|
||
| private String encryptAndDecoratePropertyValue(String propertyValue) throws Exception { | ||
| final String encrypted = encryptionService.encrypt(propertyValue, TextEncoding.BIN_HEX); | ||
| return String.format(ENCRYPTED_PROPERTY_SCHEME, encrypted); | ||
| } | ||
|
|
||
| @Override | ||
| public void decryptSensitiveData(Config config) { | ||
| final Map<String, String> configProperties = config.getProperties(); | ||
| if (configProperties != null) { | ||
| final Map<String, String> decryptedProperties = new HashMap<>(configProperties); | ||
| for (Map.Entry<String, String> property : configProperties.entrySet()) { | ||
| if (isEncryptedPassword(property.getValue())) { | ||
| decryptedProperties.put(property.getKey(), decryptProperty(property.getValue())); | ||
| } | ||
| } | ||
| config.setProperties(decryptedProperties); | ||
| } | ||
| } | ||
|
|
||
| private String decryptProperty(String property) { | ||
| try { | ||
| // sample value: ${enc=aes256_hex, value=5248...303d} | ||
| final String encrypted = property.substring(ENCRYPTED_PROPERTY_PREFIX.length(), property.indexOf('}')); | ||
| return encryptionService.decrypt(encrypted, TextEncoding.BIN_HEX); | ||
| } catch (Exception e) { | ||
| throw new AmbariRuntimeException("Error while decrypting property", e); | ||
| } | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
ambari-server/src/main/java/org/apache/ambari/server/security/encryption/Encryptor.java
This file contains hidden or 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,43 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 | ||
| * <p/> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p/> | ||
| * 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.apache.ambari.server.security.encryption; | ||
|
|
||
| /** | ||
| * Defines a generic contract on encrypting/decrypting sensitive data | ||
| */ | ||
| public interface Encryptor<T> { | ||
|
|
||
| /** | ||
| * Encrypts the given encryptible object | ||
| * | ||
| * @param encryptible | ||
| * to be encrypted | ||
| * @return the encrypted value | ||
| */ | ||
| void encryptSensitiveData(T encryptible); | ||
|
|
||
| /** | ||
| * Decrypts the given decryptible object | ||
| * | ||
| * @param decryptible | ||
| * to be decrypted | ||
| * @return the decrypted value | ||
| */ | ||
| void decryptSensitiveData(T decryptible); | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why bother caching using the cluster id at all. Even if Ambari was managing multiple clusters, the stack definitions all would be the same for the same stack id.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm..that's true. I may remove that layer next week.