settings store code cleanup

This commit is contained in:
nathan 2015-07-17 02:48:29 +02:00
parent 8d90e57933
commit bb46a98d0e
2 changed files with 102 additions and 54 deletions

View File

@ -1,37 +1,53 @@
package dorkbox.network.util.store;
import java.security.SecureRandom;
import dorkbox.network.connection.EndPoint;
import dorkbox.network.util.exceptions.SecurityException;
import dorkbox.util.SerializationManager;
import dorkbox.util.storage.Storage;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import dorkbox.network.util.exceptions.SecurityException;
import java.io.IOException;
import java.security.SecureRandom;
public class NullSettingsStore extends SettingsStore {
public
class NullSettingsStore extends SettingsStore {
private byte[] serverSalt;
@Override
public ECPrivateKeyParameters getPrivateKey() throws SecurityException {
public
void init(final Class<? extends EndPoint> type, final SerializationManager serializationManager, final Storage storage)
throws IOException {
}
@Override
public
ECPrivateKeyParameters getPrivateKey() throws SecurityException {
return null;
}
@Override
public void savePrivateKey(ECPrivateKeyParameters serverPrivateKey) throws SecurityException {
public
void savePrivateKey(ECPrivateKeyParameters serverPrivateKey) throws SecurityException {
}
@Override
public ECPublicKeyParameters getPublicKey() throws SecurityException {
public
ECPublicKeyParameters getPublicKey() throws SecurityException {
return null;
}
@Override
public void savePublicKey(ECPublicKeyParameters serverPublicKey) throws SecurityException {
public
void savePublicKey(ECPublicKeyParameters serverPublicKey) throws SecurityException {
}
@Override
public byte[] getSalt() {
public
byte[] getSalt() {
if (this.serverSalt == null) {
SecureRandom secureRandom = new SecureRandom();
this.serverSalt = new byte[32];
@ -42,20 +58,24 @@ public class NullSettingsStore extends SettingsStore {
}
@Override
public ECPublicKeyParameters getRegisteredServerKey(byte[] hostAddress) throws SecurityException {
public
ECPublicKeyParameters getRegisteredServerKey(byte[] hostAddress) throws SecurityException {
return null;
}
@Override
public void addRegisteredServerKey(byte[] hostAddress, ECPublicKeyParameters publicKey) throws SecurityException {
public
void addRegisteredServerKey(byte[] hostAddress, ECPublicKeyParameters publicKey) throws SecurityException {
}
@Override
public boolean removeRegisteredServerKey(byte[] hostAddress) throws SecurityException {
public
boolean removeRegisteredServerKey(byte[] hostAddress) throws SecurityException {
return true;
}
@Override
public void shutdown() {
public
void shutdown() {
}
}

View File

@ -1,33 +1,47 @@
package dorkbox.network.util.store;
import dorkbox.network.connection.EndPoint;
import dorkbox.network.util.exceptions.SecurityException;
import dorkbox.util.SerializationManager;
import dorkbox.util.storage.Storage;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import dorkbox.network.util.exceptions.SecurityException;
import java.io.IOException;
/**
* This class provides a way for the network stack to use the server's database, instead of a property file (which it uses when stand-alone)
* <p>
* <p/>
* A static "create" method, with any number of parameters, is required to create this class (which is done via reflection)
*/
@SuppressWarnings("deprecation")
public abstract class SettingsStore {
public abstract
class SettingsStore {
/**
* Initialize the settingsStore with the provided serialization manager.
*/
public abstract
void init(Class<? extends EndPoint> type, SerializationManager serializationManager, Storage storage) throws IOException;
/**
* the specified class (or AdminActions directly) MUST be the one that is calling our admin action
*
* <p/>
* (ie, not just any class can call certain admin actions.
*
* <p/>
* OPTIMIZED METHOD
*/
protected static final void checkAccess(Class<?> callingClass) throws SecurityException {
protected static
void checkAccess(Class<?> callingClass) throws SecurityException {
Class<?> callerClass = sun.reflect.Reflection.getCallerClass(3);
// starts with will allow for anonymous inner classes.
if (callerClass == null || !callerClass.getName().startsWith(callingClass.getName())) {
if (callerClass == null || !callerClass.getName()
.startsWith(callingClass.getName())) {
String message = "Security violation by: " + (callerClass == null ? "???" : callerClass.getName());
Logger logger = LoggerFactory.getLogger(SettingsStore.class);
logger.error(message);
@ -39,12 +53,13 @@ public abstract class SettingsStore {
/**
* the specified class (or AdminActions directly) MUST be the one that is calling our admin action
*
* <p/>
* (ie, not just any class can call certain admin actions.
*
* <p/>
* OPTIMIZED METHOD
*/
protected static final void checkAccess(Class<?> callingClass1, Class<?> callingClass2) throws SecurityException {
protected static
void checkAccess(Class<?> callingClass1, Class<?> callingClass2) throws SecurityException {
Class<?> callerClass = sun.reflect.Reflection.getCallerClass(3);
boolean ok = false;
@ -65,12 +80,13 @@ public abstract class SettingsStore {
/**
* the specified class (or AdminActions directly) MUST be the one that is calling our admin action
*
* <p/>
* (ie, not just any class can call certain admin actions.
*
* <p/>
* OPTIMIZED METHOD
*/
protected static final void checkAccess(Class<?> callingClass1, Class<?> callingClass2, Class<?> callingClass3) throws SecurityException {
protected static
void checkAccess(Class<?> callingClass1, Class<?> callingClass2, Class<?> callingClass3) throws SecurityException {
Class<?> callerClass = sun.reflect.Reflection.getCallerClass(3);
boolean ok = false;
@ -92,14 +108,13 @@ public abstract class SettingsStore {
/**
* the specified class (or AdminActions directly) MUST be the one that is calling our admin action
*
* <p/>
* (ie, not just any class can call certain admin actions.
*/
protected static final void checkAccess(Class<?>... callingClasses) throws SecurityException {
protected static
void checkAccess(Class<?>... callingClasses) throws SecurityException {
Class<?> callerClass = sun.reflect.Reflection.getCallerClass(3);
boolean ok = false;
@ -125,21 +140,22 @@ public abstract class SettingsStore {
/**
* the specified class (or AdminActions directly) MUST be the one that is calling our admin action
*
* <p/>
* (ie, not just any class can call certain admin actions.
*
* <p/>
* OPTIMIZED METHOD
*
* @return true if allowed access.
*/
protected static final boolean checkAccessNoExit(Class<?> callingClass) {
protected static
boolean checkAccessNoExit(Class<?> callingClass) {
Class<?> callerClass = sun.reflect.Reflection.getCallerClass(3);
// starts with will allow for anonymous inner classes.
if (callerClass == null || !callerClass.getName().startsWith(callingClass.getName())) {
if (callerClass == null || !callerClass.getName()
.startsWith(callingClass.getName())) {
String message = "Security violation by: " + (callerClass == null ? "???" : callerClass.getName());
Logger logger = LoggerFactory.getLogger(SettingsStore.class);
logger.error(message);
@ -153,22 +169,22 @@ public abstract class SettingsStore {
/**
* the specified class (or AdminActions directly) MUST be the one that is calling our admin action
*
* <p/>
* (ie, not just any class can call certain admin actions.
*
* <p/>
* OPTIMIZED METHOD
*
* @return true if allowed access.
*/
protected static final boolean checkAccessNoExit(Class<?> callingClass1, Class<?> callingClass2) {
protected static
boolean checkAccessNoExit(Class<?> callingClass1, Class<?> callingClass2) {
Class<?> callerClass = sun.reflect.Reflection.getCallerClass(3);
boolean ok = false;
// starts with will allow for anonymous inner classes.
if (callerClass != null) {
String callerClassName = callerClass.getName();
ok = callerClassName.startsWith(callingClass1.getName()) ||
callerClassName.startsWith(callingClass2.getName());
ok = callerClassName.startsWith(callingClass1.getName()) || callerClassName.startsWith(callingClass2.getName());
}
if (!ok) {
@ -185,14 +201,15 @@ public abstract class SettingsStore {
/**
* the specified class (or AdminActions directly) MUST be the one that is calling our admin action
*
* <p/>
* (ie, not just any class can call certain admin actions.
*
* <p/>
* OPTIMIZED METHOD
*
* @return true if allowed access.
*/
protected static final boolean checkAccessNoExit(Class<?> callingClass1, Class<?> callingClass2, Class<?> callingClass3) {
protected static
boolean checkAccessNoExit(Class<?> callingClass1, Class<?> callingClass2, Class<?> callingClass3) {
Class<?> callerClass = sun.reflect.Reflection.getCallerClass(3);
boolean ok = false;
@ -216,12 +233,13 @@ public abstract class SettingsStore {
/**
* the specified class (or AdminActions directly) MUST be the one that is calling our admin action
*
* <p/>
* (ie, not just any class can call certain admin actions.
*
* @return true if allowed access.
*/
protected static final boolean checkAccessNoExit(Class<?>... callingClasses) {
protected static
boolean checkAccessNoExit(Class<?>... callingClasses) {
Class<?> callerClass = sun.reflect.Reflection.getCallerClass(3);
boolean ok = false;
@ -249,51 +267,61 @@ public abstract class SettingsStore {
/**
* Simple, property based method for saving the private key of the server
*/
public abstract ECPrivateKeyParameters getPrivateKey() throws SecurityException;
public abstract
ECPrivateKeyParameters getPrivateKey() throws SecurityException;
/**
* Simple, property based method for saving the private key of the server
*/
public abstract void savePrivateKey(ECPrivateKeyParameters serverPrivateKey) throws SecurityException ;
public abstract
void savePrivateKey(ECPrivateKeyParameters serverPrivateKey) throws SecurityException;
/**
* Simple, property based method to getting the public key of the server
*/
public abstract ECPublicKeyParameters getPublicKey() throws SecurityException ;
public abstract
ECPublicKeyParameters getPublicKey() throws SecurityException;
/**
* Simple, property based method for saving the public key of the server
*/
public abstract void savePublicKey(ECPublicKeyParameters serverPublicKey) throws SecurityException ;
public abstract
void savePublicKey(ECPublicKeyParameters serverPublicKey) throws SecurityException;
/**
* @return the server salt
*/
public abstract byte[] getSalt();
public abstract
byte[] getSalt();
/**
* Gets a previously registered computer by host IP address
*/
public abstract ECPublicKeyParameters getRegisteredServerKey(byte[] hostAddress) throws SecurityException;
public abstract
ECPublicKeyParameters getRegisteredServerKey(byte[] hostAddress) throws SecurityException;
/**
* Saves a registered computer by host IP address and public key
*/
public abstract void addRegisteredServerKey(byte[] hostAddress, ECPublicKeyParameters publicKey) throws SecurityException;
public abstract
void addRegisteredServerKey(byte[] hostAddress, ECPublicKeyParameters publicKey) throws SecurityException;
/**
* Deletes a registered computer by host IP address
*
* @return true if successful, false if there were problems (or it didn't exist)
*/
public abstract boolean removeRegisteredServerKey(byte[] hostAddress) throws SecurityException;
public abstract
boolean removeRegisteredServerKey(byte[] hostAddress) throws SecurityException;
/**
* Take the proper steps to shutdown the storage system.
*/
public abstract void shutdown();
public abstract
void shutdown();
}