forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed all reminants of the IdentityService. Created the KeysManager…
… to move the management of keys out of management server
- Loading branch information
Alex Huang
committed
Jan 14, 2014
1 parent
2893120
commit 68b8891
Showing
36 changed files
with
338 additions
and
758 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 was deleted.
Oops, something went wrong.
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
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
127 changes: 127 additions & 0 deletions
127
framework/security/src/org/apache/cloudstack/framework/security/keys/KeysManagerImpl.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,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 | ||
// | ||
// http: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.apache.cloudstack.framework.security.keys; | ||
|
||
import java.security.NoSuchAlgorithmException; | ||
import java.security.SecureRandom; | ||
|
||
import javax.inject.Inject; | ||
import javax.net.ssl.KeyManager; | ||
|
||
import org.apache.commons.codec.binary.Base64; | ||
import org.apache.log4j.Logger; | ||
|
||
import org.apache.cloudstack.framework.config.ConfigDepot; | ||
import org.apache.cloudstack.framework.config.ConfigKey; | ||
import org.apache.cloudstack.framework.config.Configurable; | ||
import org.apache.cloudstack.framework.config.dao.ConfigurationDao; | ||
import org.apache.cloudstack.framework.config.impl.ConfigurationVO; | ||
|
||
import com.cloud.utils.db.DB; | ||
import com.cloud.utils.db.SearchBuilder; | ||
import com.cloud.utils.db.SearchCriteria; | ||
|
||
/** | ||
* To be perfectly honest, I'm not sure why we need this class. This used | ||
* to be in ManagementServerImpl. I moved the functionality because it seems | ||
* many features will need this. However, the right thing will be for setup | ||
* and upgrade to take care of key generation. Here, the methods appear to | ||
* mainly be used for dynamic generation. I added this class because after | ||
* talking to Kelven, we think there will be other functionalities we need | ||
* to centralize to this class. We'll see how that works out. | ||
* | ||
* There's multiple problems here that we need to fix. | ||
* - Multiple servers can be generating keys. This is not atomic. | ||
* - The functionality of generating the keys should be moved over to setup/upgrade. | ||
* | ||
*/ | ||
public class KeysManagerImpl implements KeysManager, Configurable { | ||
private static final Logger s_logger = Logger.getLogger(KeysManagerImpl.class); | ||
|
||
@Inject | ||
ConfigurationDao _configDao; | ||
@Inject | ||
ConfigDepot _configDepot; | ||
|
||
@Override | ||
public String getHashKey() { | ||
String value = HashKey.value(); | ||
if (value == null) { | ||
_configDepot.set(HashKey, getBase64EncodedRandomKey(128)); | ||
} | ||
|
||
return HashKey.value(); | ||
} | ||
|
||
@Override | ||
public String getEncryptionKey() { | ||
String value = EncryptionKey.value(); | ||
if (value == null) { | ||
_configDepot.set(EncryptionKey, getBase64EncodedRandomKey(128)); | ||
} | ||
return EncryptionKey.value(); | ||
} | ||
|
||
@Override | ||
public String getEncryptionIV() { | ||
String value = EncryptionIV.value(); | ||
if (value == null) { | ||
_configDepot.set(EncryptionIV, getBase64EncodedRandomKey(128)); | ||
} | ||
return EncryptionIV.value(); | ||
} | ||
|
||
private static String getBase64EncodedRandomKey(int nBits) { | ||
SecureRandom random; | ||
try { | ||
random = SecureRandom.getInstance("SHA1PRNG"); | ||
byte[] keyBytes = new byte[nBits / 8]; | ||
random.nextBytes(keyBytes); | ||
return Base64.encodeBase64URLSafeString(keyBytes); | ||
} catch (NoSuchAlgorithmException e) { | ||
s_logger.error("Unhandled exception: ", e); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
@DB | ||
public void resetEncryptionKeyIV() { | ||
|
||
SearchBuilder<ConfigurationVO> sb = _configDao.createSearchBuilder(); | ||
sb.and("name1", sb.entity().getName(), SearchCriteria.Op.EQ); | ||
sb.or("name2", sb.entity().getName(), SearchCriteria.Op.EQ); | ||
sb.done(); | ||
|
||
SearchCriteria<ConfigurationVO> sc = sb.create(); | ||
sc.setParameters("name1", EncryptionKey.key()); | ||
sc.setParameters("name2", EncryptionIV.key()); | ||
|
||
_configDao.expunge(sc); | ||
} | ||
|
||
@Override | ||
public String getConfigComponentName() { | ||
return KeyManager.class.getSimpleName(); | ||
} | ||
|
||
@Override | ||
public ConfigKey<?>[] getConfigKeys() { | ||
return new ConfigKey<?>[] {EncryptionKey, EncryptionIV, HashKey}; | ||
} | ||
|
||
} |
Oops, something went wrong.