Skip to content

Commit

Permalink
Added documentation and logs
Browse files Browse the repository at this point in the history
  • Loading branch information
armstrongmsg committed Dec 18, 2020
1 parent 7d9add4 commit 33ca75d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 30 deletions.
23 changes: 13 additions & 10 deletions src/main/java/cloud/fogbow/ms/api/http/request/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@
import cloud.fogbow.common.exceptions.FogbowException;
import cloud.fogbow.ms.api.http.CommonKeys;
import cloud.fogbow.ms.api.parameters.Provider;
import cloud.fogbow.ms.constants.ApiDocumentation;
import cloud.fogbow.ms.constants.SystemConstants;
import cloud.fogbow.ms.core.ApplicationFacade;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@CrossOrigin
@RestController
@RequestMapping(value = Admin.ADMIN_ENDPOINT)
// TODO documentation
@Api(description = ApiDocumentation.Admin.API)
public class Admin {
public static final String ADMIN_ENDPOINT = SystemConstants.SERVICE_BASE_ENDPOINT + "admin";
// TODO use these endpoint values
public static final String RELOAD_ENDPOINT = ADMIN_ENDPOINT + "/reload";
public static final String ADD_PROVIDER_ENDPOINT = ADMIN_ENDPOINT + "/addprovider";
public static final String ADD_PROVIDER_ENDPOINT = ADMIN_ENDPOINT + "/provider";

// TODO documentation
@ApiOperation(value = ApiDocumentation.Admin.RELOAD)
@RequestMapping(value = "/reload", method = RequestMethod.POST)
public ResponseEntity<Boolean> reload(
@ApiParam(value = cloud.fogbow.common.constants.ApiDocumentation.Token.SYSTEM_USER_TOKEN)
Expand All @@ -34,7 +38,7 @@ public ResponseEntity<Boolean> reload(
return new ResponseEntity<>(HttpStatus.OK);
}

// TODO documentation
@ApiOperation(value = ApiDocumentation.Admin.ADD_PROVIDER)
@RequestMapping(value = "/provider", method = RequestMethod.POST)
public ResponseEntity<Boolean> addProvider(
@ApiParam(value = cloud.fogbow.common.constants.ApiDocumentation.Token.SYSTEM_USER_TOKEN)
Expand All @@ -44,7 +48,7 @@ public ResponseEntity<Boolean> addProvider(
return new ResponseEntity<>(HttpStatus.OK);
}

// TODO documentation
@ApiOperation(value = ApiDocumentation.Admin.REMOVE_PROVIDER)
@RequestMapping(value = "/provider", method = RequestMethod.DELETE)
public ResponseEntity<Boolean> removeProvider(
@ApiParam(value = cloud.fogbow.common.constants.ApiDocumentation.Token.SYSTEM_USER_TOKEN)
Expand All @@ -54,7 +58,7 @@ public ResponseEntity<Boolean> removeProvider(
return new ResponseEntity<>(HttpStatus.OK);
}

// TODO documentation
@ApiOperation(value = ApiDocumentation.Admin.ADD_TARGET)
@RequestMapping(value = "/target", method = RequestMethod.POST)
public ResponseEntity<Boolean> addTarget(
@ApiParam(value = cloud.fogbow.common.constants.ApiDocumentation.Token.SYSTEM_USER_TOKEN)
Expand All @@ -64,7 +68,7 @@ public ResponseEntity<Boolean> addTarget(
return new ResponseEntity<>(HttpStatus.OK);
}

// TODO documentation
@ApiOperation(value = ApiDocumentation.Admin.ADD_REQUESTER)
@RequestMapping(value = "/requester", method = RequestMethod.POST)
public ResponseEntity<Boolean> addRequester(
@ApiParam(value = cloud.fogbow.common.constants.ApiDocumentation.Token.SYSTEM_USER_TOKEN)
Expand All @@ -74,18 +78,17 @@ public ResponseEntity<Boolean> addRequester(
return new ResponseEntity<>(HttpStatus.OK);
}

// TODO documentation
@ApiOperation(value = ApiDocumentation.Admin.REMOVE_TARGET)
@RequestMapping(value = "/target", method = RequestMethod.DELETE)
public ResponseEntity<Boolean> removeTarget(
@ApiParam(value = cloud.fogbow.common.constants.ApiDocumentation.Token.SYSTEM_USER_TOKEN)
@RequestHeader(required = false, value = CommonKeys.SYSTEM_USER_TOKEN_HEADER_KEY) String systemUserToken,
@RequestBody Provider provider) throws FogbowException {
ApplicationFacade.getInstance().removeTargetProvider(systemUserToken, provider.getProvider());
// ApplicationFacade.getInstance().addTargetProvider(systemUserToken, provider.getProvider());
return new ResponseEntity<>(HttpStatus.OK);
}

// TODO documentation
@ApiOperation(value = ApiDocumentation.Admin.REMOVE_REQUESTER)
@RequestMapping(value = "/requester", method = RequestMethod.DELETE)
public ResponseEntity<Boolean> removeRequester(
@ApiParam(value = cloud.fogbow.common.constants.ApiDocumentation.Token.SYSTEM_USER_TOKEN)
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/cloud/fogbow/ms/constants/ApiDocumentation.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,19 @@ public static class Authorization {
public static final String TARGET_OPERATION = "States whether the local provider is allowed to perform operations in the given provider.";
}

public static class Admin {
public static final String API = "Manages admin-only operations";
public static final String RELOAD = "Reloads service configuration.";
public static final String ADD_PROVIDER = "Adds given provider to the list of known providers.";
public static final String REMOVE_PROVIDER = "Removes given provider from all the lists of providers kept by the service.";
public static final String ADD_TARGET = "Adds given provider to the list of targets, used by the MembershipService "
+ "to authorize remote operations.";
public static final String REMOVE_TARGET = "Removes given provider from the list of targets, used by the MembershipService "
+ "to authorize remote operations.";
public static final String ADD_REQUESTER = "Adds given provider to the list of requesters, used by the MembershipService "
+ "to authorized operations from other providers";
public static final String REMOVE_REQUESTER = "Removes given provider from the list of requesters, used by the MembershipService "
+ "to authorized operations from other providers";

}
}
16 changes: 15 additions & 1 deletion src/main/java/cloud/fogbow/ms/constants/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,28 @@ public static class Exception {
public static final String INVALID_MEMBER_NAME = "Invalid member name in configuration.";
public static final String MEMBER_IS_NOT_REQUESTER = "Member is not requester.";
public static final String MEMBER_IS_NOT_TARGET = "Member is not target.";
public static final String NO_ADMIN_SPECIFIED = "No admin specified in the configuration file.";
public static final String PROVIDER_IS_ALREADY_A_MEMBER = "Provider is already a member.";
public static final String PROVIDER_IS_ALREADY_A_REQUESTER = "Provider is already a requester.";
public static final String PROVIDER_IS_ALREADY_A_TARGET = "Provider is already a target.";
public static final String UNABLE_TO_FIND_CLASS_S = "Unable to find class %s.";
public static final String USER_IS_NOT_ADMIN = "Not-admin user trying to perform admin-only operation.";
}

public static class Log {
public static final String ADDING_NEW_PROVIDER = "Adding provider: %s";
public static final String ADDING_REQUESTER_PROVIDER = "Adding requester: %s";
public static final String ADDING_TARGET_PROVIDER = "Adding target: %s";
public static final String GET_PUBLIC_KEY = "Get public key received.";
public static final String INTERNAL_SERVER_ERROR = "Internal server error.";
public static final String GET_PUBLIC_KEY = "Get public key received.";
public static final String RELOADING_AUTHORIZATION_PLUGIN = "Reloading authorization plugin.";
public static final String RELOADING_CONFIGURATION = "Reloading service configuration.";
public static final Object RELOADING_MEMBERSHIP_PLUGIN = "Reloading membership plugin.";
public static final String RELOADING_MS_KEYS_HOLDER = "Reloading service keys.";
public static final String RELOADING_PROPERTIES_HOLDER = "Reloading properties holder.";
public static final String RELOADING_PUBLIC_KEYS_HOLDER = "Reloading public keys holder.";
public static final String REMOVING_PROVIDER = "Removing provider: %s";
public static final String REMOVING_REQUESTER_PROVIDER = "Removing requester: %s";
public static final String REMOVING_TARGET_PROVIDER = "Removing target: %s";
}
}
39 changes: 26 additions & 13 deletions src/main/java/cloud/fogbow/ms/core/ApplicationFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import cloud.fogbow.common.plugins.authorization.AuthorizationPlugin;
import cloud.fogbow.common.util.CryptoUtil;
import cloud.fogbow.common.util.ServiceAsymmetricKeysHolder;
import cloud.fogbow.ms.constants.Messages;
import cloud.fogbow.ms.core.authorization.AdminOperation;

// TODO add tests
Expand Down Expand Up @@ -89,11 +90,12 @@ public void setAuthorizationPlugin(AuthorizationPlugin<AdminOperation> authoriza
}

public void addProvider(String userToken, String provider) throws FogbowException {
LOGGER.info(String.format(Messages.Log.ADDING_NEW_PROVIDER, provider));

RSAPublicKey asPublicKey = MSPublicKeysHolder.getInstance().getAsPublicKey();
SystemUser systemUser = AuthenticationUtil.authenticate(asPublicKey, userToken);
this.authorizationPlugin.isAuthorized(systemUser, new AdminOperation());

// TODO add logging
setAsReloading();

try {
Expand All @@ -104,11 +106,12 @@ public void addProvider(String userToken, String provider) throws FogbowExceptio
}

public void removeProvider(String userToken, String provider) throws FogbowException {
LOGGER.info(String.format(Messages.Log.REMOVING_PROVIDER, provider));

RSAPublicKey asPublicKey = MSPublicKeysHolder.getInstance().getAsPublicKey();
SystemUser systemUser = AuthenticationUtil.authenticate(asPublicKey, userToken);
this.authorizationPlugin.isAuthorized(systemUser, new AdminOperation());

// TODO add logging

setAsReloading();

try {
Expand All @@ -119,11 +122,12 @@ public void removeProvider(String userToken, String provider) throws FogbowExcep
}

public void addTargetProvider(String userToken, String provider) throws FogbowException {
LOGGER.info(String.format(Messages.Log.ADDING_TARGET_PROVIDER, provider));

RSAPublicKey asPublicKey = MSPublicKeysHolder.getInstance().getAsPublicKey();
SystemUser systemUser = AuthenticationUtil.authenticate(asPublicKey, userToken);
this.authorizationPlugin.isAuthorized(systemUser, new AdminOperation());

// TODO add logging

setAsReloading();

try {
Expand All @@ -135,11 +139,12 @@ public void addTargetProvider(String userToken, String provider) throws FogbowEx
}

public void addRequesterProvider(String userToken, String provider) throws FogbowException {
LOGGER.info(String.format(Messages.Log.ADDING_REQUESTER_PROVIDER, provider));

RSAPublicKey asPublicKey = MSPublicKeysHolder.getInstance().getAsPublicKey();
SystemUser systemUser = AuthenticationUtil.authenticate(asPublicKey, userToken);
this.authorizationPlugin.isAuthorized(systemUser, new AdminOperation());

// TODO add logging

setAsReloading();

try {
Expand All @@ -151,11 +156,12 @@ public void addRequesterProvider(String userToken, String provider) throws Fogbo
}

public void removeTargetProvider(String userToken, String provider) throws FogbowException {
LOGGER.info(String.format(Messages.Log.REMOVING_TARGET_PROVIDER, provider));

RSAPublicKey asPublicKey = MSPublicKeysHolder.getInstance().getAsPublicKey();
SystemUser systemUser = AuthenticationUtil.authenticate(asPublicKey, userToken);
this.authorizationPlugin.isAuthorized(systemUser, new AdminOperation());

// TODO add logging
setAsReloading();

try {
Expand All @@ -166,11 +172,12 @@ public void removeTargetProvider(String userToken, String provider) throws Fogbo
}

public void removeRequesterProvider(String userToken, String provider) throws FogbowException {
RSAPublicKey asPublicKey = MSPublicKeysHolder.getInstance().getAsPublicKey();
LOGGER.info(String.format(Messages.Log.REMOVING_REQUESTER_PROVIDER, provider));

RSAPublicKey asPublicKey = MSPublicKeysHolder.getInstance().getAsPublicKey();
SystemUser systemUser = AuthenticationUtil.authenticate(asPublicKey, userToken);
this.authorizationPlugin.isAuthorized(systemUser, new AdminOperation());

// TODO add logging
setAsReloading();

try {
Expand All @@ -182,6 +189,8 @@ public void removeRequesterProvider(String userToken, String provider) throws Fo
}

public void reload(String userToken) throws FogbowException {
LOGGER.info(Messages.Log.RELOADING_CONFIGURATION);

RSAPublicKey asPublicKey = MSPublicKeysHolder.getInstance().getAsPublicKey();
SystemUser systemUser = AuthenticationUtil.authenticate(asPublicKey, userToken);
this.authorizationPlugin.isAuthorized(systemUser, new AdminOperation());
Expand All @@ -201,17 +210,21 @@ private void doReload() throws ConfigurationErrorException {
}
}

// TODO add logging
LOGGER.info(Messages.Log.RELOADING_PROPERTIES_HOLDER);
PropertiesHolder.reset();

LOGGER.info(Messages.Log.RELOADING_PUBLIC_KEYS_HOLDER);
MSPublicKeysHolder.reset();

// TODO add logging
LOGGER.info(Messages.Log.RELOADING_MS_KEYS_HOLDER);
String publicKeyFilePath = PropertiesHolder.getInstance().getProperty(FogbowConstants.PUBLIC_KEY_FILE_PATH);
String privateKeyFilePath = PropertiesHolder.getInstance().getProperty(FogbowConstants.PRIVATE_KEY_FILE_PATH);
ServiceAsymmetricKeysHolder.reset(publicKeyFilePath, privateKeyFilePath);

// TODO add logging
LOGGER.info(Messages.Log.RELOADING_AUTHORIZATION_PLUGIN);
this.authorizationPlugin = PluginInstantiator.getAuthorizationPlugin();

LOGGER.info(Messages.Log.RELOADING_MEMBERSHIP_PLUGIN);
this.membershipService = PluginInstantiator.getMembershipService();
} finally {
finishReloading();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
import cloud.fogbow.common.models.SystemUser;
import cloud.fogbow.common.plugins.authorization.AuthorizationPlugin;
import cloud.fogbow.ms.constants.ConfigurationPropertyKeys;
import cloud.fogbow.ms.constants.Messages;
import cloud.fogbow.ms.core.PropertiesHolder;

public class AdminAuthorizationPlugin implements AuthorizationPlugin<AdminOperation> {

private static final String SEPARATOR = ",";
private Set<String> adminsIds;

public AdminAuthorizationPlugin() throws ConfigurationErrorException {
adminsIds = new HashSet<String>();
String adminsIdsString = PropertiesHolder.getInstance().getProperty(ConfigurationPropertyKeys.ADMINS_IDS);

if (adminsIdsString.isEmpty()) {
// TODO add message
throw new ConfigurationErrorException();
throw new ConfigurationErrorException(Messages.Exception.NO_ADMIN_SPECIFIED);
}

// TODO constant
for (String adminId : adminsIdsString.split(",")) {
for (String adminId : adminsIdsString.split(SEPARATOR)) {
adminsIds.add(adminId);
}
}
Expand All @@ -35,8 +35,7 @@ public boolean isAuthorized(SystemUser systemUser, AdminOperation operation) thr
String userId = systemUser.getId();

if (!adminsIds.contains(userId)) {
// TODO add message
throw new UnauthorizedRequestException();
throw new UnauthorizedRequestException(Messages.Exception.USER_IS_NOT_ADMIN);
}

return true;
Expand Down

0 comments on commit 33ca75d

Please sign in to comment.