Skip to content

Commit

Permalink
Add runtime exception + Javadoc exceptions + add testcase for missing…
Browse files Browse the repository at this point in the history
… security dependencies
  • Loading branch information
chilimannen committed Mar 12, 2017
1 parent adc0cfb commit bb88659
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 19 deletions.
1 change: 1 addition & 0 deletions core/main/java/com/codingchili/core/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.codingchili.core.configuration.CoreStrings;
import com.codingchili.core.context.*;
import com.codingchili.core.context.CoreException;
import com.codingchili.core.files.Configurations;
import com.codingchili.core.logging.ConsoleLogger;
import com.codingchili.core.logging.Level;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.codingchili.core.context;

import com.codingchili.core.context.exception.CoreExceptionFormat;
import com.codingchili.core.protocol.ResponseStatus;

/**
Expand All @@ -8,7 +9,7 @@
* Exceptions should extend this class to allow for catching all core-type exceptions.
* Additionally, all classes extending CoreException must be safe to be forwarded to clients.
*/
public class CoreException extends Exception {
public class CoreException extends Exception implements CoreExceptionFormat {
private ResponseStatus status = ResponseStatus.ERROR;

protected CoreException(String error) {
Expand All @@ -23,5 +24,4 @@ protected CoreException(String error, ResponseStatus status) {
public ResponseStatus status() {
return status;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.codingchili.core.context;

import com.codingchili.core.context.exception.CoreExceptionFormat;
import com.codingchili.core.protocol.ResponseStatus;

/**
* @author Robin Duda
*
* Runtime exception.
*/
public class CoreRuntimeException extends RuntimeException implements CoreExceptionFormat {
private ResponseStatus status = ResponseStatus.ERROR;

/**
* @param description of the generated error and the cause.
*/
protected CoreRuntimeException(String description) {
super(description);
}

/**
* @param error description of the error and the cause.
* @param status the error level.
*/
protected CoreRuntimeException(String error, ResponseStatus status) {
super(error);
this.status = status;
}

@Override
public ResponseStatus status() {
return status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.codingchili.core.context.exception;

import com.codingchili.core.protocol.ResponseStatus;

/**
* @author Robin Duda
*
* Base format for all core based exceptions.
*/
public interface CoreExceptionFormat {
/**
* @return the error status, most likely ERROR.
*/
ResponseStatus status();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
* Throw when a command was given on the terminal that does not exist.
*/
public class NoSuchCommandException extends CoreException {

/**
* @param command the requested command that was missing.
*/
public NoSuchCommandException(String command) {
super(CoreStrings.getNoSuchCommand(command));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

/**
* @author Robin Duda
*
* Throw when a subsystem was used before it was initialized.
*/
public class SystemNotInitializedException extends RuntimeException {

/**
* @param clazz the system that was not initialized.
*/
public SystemNotInitializedException(Class clazz) {
super(CoreStrings.getSystemNotInitialized(clazz.getSimpleName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ private TokenFactory getFactory(TokenIdentifier identifier) {
return new TokenFactory(secret);
} else {
logger.onSecurityDependencyMissing(identifier.getService(), identifier.getSecret());
throw new RuntimeException(
new SecurityMissingDependencyException(identifier.getService(), identifier.getSecret()));
throw new SecurityMissingDependencyException(identifier.getService(), identifier.getSecret());
}
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.codingchili.core.security.exception;

import com.codingchili.core.configuration.CoreStrings;
import com.codingchili.core.context.CoreException;
import com.codingchili.core.context.CoreRuntimeException;

/**
* Created by robdu on 2017-03-10.
* Throw when a security configuration is pointing out a secret as a token dependency
* that does not exist in the targeted service.
*/
public class SecurityMissingDependencyException extends CoreException {
public class SecurityMissingDependencyException extends CoreRuntimeException {

/**
* @param target the name of the service that a token-secret was requested for.
* @param identifier the name of the secret that was requested.
*/
public SecurityMissingDependencyException(String target, String identifier) {
super(CoreStrings.getSecurityDependencyMissing(target, identifier));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import com.codingchili.core.files.JsonFileStore;
import com.codingchili.core.logging.ConsoleLogger;
import com.codingchili.core.protocol.Serializer;
import com.codingchili.core.security.exception.SecurityMissingDependencyException;
import com.codingchili.core.testing.ContextMock;

import static com.codingchili.core.configuration.CoreStrings.DIR_ROOT;
import static com.codingchili.core.configuration.CoreStrings.testFile;
import static com.codingchili.core.configuration.CoreStrings.*;

/**
* @author Robin Duda
Expand All @@ -41,7 +41,6 @@ public class AuthenticationGeneratorIT {
private static final String SERVICE_2_TOKEN = "service2token";
private static final String SERVICE1_JSON = "service1.json";
private static final String SERVICE2_JSON = "service2.json";
private static final String SECURITY_JSON = "security.json";
private AuthenticationGenerator generator;
private SystemContext context;

Expand All @@ -66,7 +65,7 @@ private SecuritySettings createSecuritySettings() {
SecuritySettings security = new SecuritySettings();
HashMap<String, AuthenticationDependency> dependencies = new HashMap<>();

security.setPath(CoreStrings.PATH_SECURITY);
security.setPath(PATH_SECURITY);
security.setSecretBytes(64);

dependencies.put(SERVICE_REGEX, new AuthenticationDependency()
Expand All @@ -89,8 +88,8 @@ private SecuritySettings createSecuritySettings() {

@After
public void tearDown() {
JsonFileStore.writeObject(new JsonObject(), testFile(AUTHENTICATION_GENERATOR, "service1.json"));
JsonFileStore.writeObject(new JsonObject(), testFile(AUTHENTICATION_GENERATOR, "service2.json"));
JsonFileStore.writeObject(new JsonObject(), testFile(AUTHENTICATION_GENERATOR, SERVICE1_JSON));
JsonFileStore.writeObject(new JsonObject(), testFile(AUTHENTICATION_GENERATOR, SERVICE2_JSON));
}

@Test
Expand Down Expand Up @@ -130,7 +129,15 @@ public void testGenerateTokens(TestContext test) throws IOException {

@Test
public void testGenerateWithBrokenReferenceFails(TestContext test) {

String tokenName = "token";
String missingName = "mising-target-secret";
SecuritySettings settings = Configurations.get(PATH_SECURITY, SecuritySettings.class);
settings.getDependencies().get(SERVICE_1).addToken(tokenName, SERVICE_2, missingName);
try {
generator.all();
test.fail("Generation did not fail when missing a secret dependency.");
} catch (SecurityMissingDependencyException ignored) {
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.codingchili.authentication.configuration.AuthenticationContext;
import com.codingchili.authentication.model.AsyncAccountStore;
import com.codingchili.authentication.model.ClientAuthentication;
import io.vertx.core.Future;

import com.codingchili.core.context.CoreException;
import com.codingchili.core.protocol.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

import com.codingchili.realmregistry.configuration.RegistryContext;
import com.codingchili.realmregistry.model.*;
import io.vertx.core.Future;

import java.util.List;

import com.codingchili.core.context.CoreException;
import com.codingchili.core.protocol.*;
import com.codingchili.core.security.Token;

import static com.codingchili.common.Strings.*;
import static com.codingchili.core.protocol.Access.PUBLIC;
Expand Down

0 comments on commit bb88659

Please sign in to comment.