Skip to content

Commit

Permalink
Remove all usages of type and queue type and use sub-queue instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kilemonn committed Dec 5, 2023
1 parent de1dc9d commit 7ff52d6
Show file tree
Hide file tree
Showing 36 changed files with 1,137 additions and 1,135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import javax.persistence.*

/**
* An object that holds subqueue authentication information.
* If a specific sub queue is in restricted mode it will have a matching [AuthenticationMatrix] created which will
* be checked to verify if a specific sub queue can be operated on.
* If a specific sub-queue is in restricted mode it will have a matching [AuthenticationMatrix] created which will
* be checked to verify if a specific sub-queue can be operated on.
* This object is used for `In-memory`, `SQL` and `Redis`.
*
* @author github.com/Kilemonn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.mapping.Document

/**
* A holder object for the restricted sub queues. Refer to [AuthenticationMatrix].
* A holder object for the restricted sub-queues. Refer to [AuthenticationMatrix].
* This is used only for `Mongo`.
*
* @author github.com/Kilemonn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ package au.kilemon.messagequeue.authentication

/**
* An enum class used to represent the different types of `MultiQueueAuthentication`.
* This will drive whether authentication is available and for which sub queues.
* This will drive whether authentication is available and for which sub-queues.
*
* @author github.com/Kilemonn
*/
enum class RestrictionMode
{
/**
* This is the default, which enforces no authentication on any sub queue, messages can be enqueued and dequeued
* This is the default, which enforces no authentication on any sub-queue, messages can be enqueued and dequeued
* as required by any called without any form of authentication.
*/
NONE,

/**
* This is a hybrid mode where sub queues can be created without authentication, but other sub queues can
* This is a hybrid mode where sub-queues can be created without authentication, but other sub-queues can
* be created with it (if they do not already exist).
* Any sub queue created with authentication will not be accessible without a token, sub queues created without a
* Any sub-queue created with authentication will not be accessible without a token, sub-queues created without a
* token will continue to be accessible without one.
*/
HYBRID,

/**
* This is a restricted mode that forces any sub queue to be pre-created and a token will be given before messages
* can be stored or accessed in any sub queue.
* This is a restricted mode that forces any sub-queue to be pre-created and a token will be given before messages
* can be stored or accessed in any sub-queue.
*/
RESTRICTED;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.slf4j.Logger
import org.springframework.beans.factory.annotation.Autowired

/**
* The base Authenticator class. This is responsible to tracking which sub queues are marked as restricted and
* The base Authenticator class. This is responsible to tracking which sub-queues are marked as restricted and
* maintaining this underlying collection within the specified storage medium.
*
* @author github.com/Kilemonn
Expand All @@ -18,24 +18,24 @@ abstract class MultiQueueAuthenticator: HasLogger
abstract override val LOG: Logger

@Autowired
private lateinit var multiQueueAuthenticationType: RestrictionMode
private lateinit var restrictionMode: RestrictionMode

/**
* @return [multiQueueAuthenticationType]
* @return [restrictionMode]
*/
fun getAuthenticationType(): RestrictionMode
fun getRestrictionMode(): RestrictionMode
{
return multiQueueAuthenticationType
return restrictionMode
}

/**
* Used only for tests to update the set [RestrictionMode].
*
* @param authenticationType the new [RestrictionMode] to set
* @param restrictionMode the new [RestrictionMode] to set
*/
fun setAuthenticationType(authenticationType: RestrictionMode)
fun setRestrictionMode(restrictionMode: RestrictionMode)
{
multiQueueAuthenticationType = authenticationType
this.restrictionMode = restrictionMode
}

/**
Expand All @@ -50,8 +50,8 @@ abstract class MultiQueueAuthenticator: HasLogger
}

/**
* Determines whether based on the currently set [getAuthenticationType] and the provided [subQueue] and
* [JwtAuthenticationFilter.getSubQueue] to determine if the user is able to interact with the requested sub queue.
* Determines whether based on the currently set [getRestrictionMode] and the provided [subQueue] and
* [JwtAuthenticationFilter.getSubQueue] to determine if the user is able to interact with the requested sub-queue.
*
* @param subQueue the sub-queue identifier that is being requested access to
* @param throwException `true` to throw an exception if the [subQueue] cannot be accessed, otherwise the return
Expand All @@ -67,7 +67,7 @@ abstract class MultiQueueAuthenticator: HasLogger
{
if (throwException)
{
throw MultiQueueAuthorisationException(subQueue, getAuthenticationType())
throw MultiQueueAuthorisationException(subQueue, getRestrictionMode())
}
return false
}
Expand All @@ -87,7 +87,7 @@ abstract class MultiQueueAuthenticator: HasLogger
}
else
{
// If we are in hybrid mode and the sub queue is not restricted we should let it pass
// If we are in hybrid mode and the sub-queue is not restricted we should let it pass
return true
}
}
Expand All @@ -101,33 +101,33 @@ abstract class MultiQueueAuthenticator: HasLogger

if (throwException)
{
throw MultiQueueAuthorisationException(subQueue, getAuthenticationType())
throw MultiQueueAuthorisationException(subQueue, getRestrictionMode())
}
return false
}

/**
* Indicates whether [multiQueueAuthenticationType] is set to [RestrictionMode.NONE].
* Indicates whether [restrictionMode] is set to [RestrictionMode.NONE].
*/
fun isInNoneMode(): Boolean
{
return getAuthenticationType() == RestrictionMode.NONE
return getRestrictionMode() == RestrictionMode.NONE
}

/**
* Indicates whether [multiQueueAuthenticationType] is set to [RestrictionMode.HYBRID].
* Indicates whether [restrictionMode] is set to [RestrictionMode.HYBRID].
*/
fun isInHybridMode(): Boolean
{
return getAuthenticationType() == RestrictionMode.HYBRID
return getRestrictionMode() == RestrictionMode.HYBRID
}

/**
* Indicates whether [multiQueueAuthenticationType] is set to [RestrictionMode.RESTRICTED].
* Indicates whether [restrictionMode] is set to [RestrictionMode.RESTRICTED].
*/
fun isInRestrictedMode(): Boolean
{
return getAuthenticationType() == RestrictionMode.RESTRICTED
return getRestrictionMode() == RestrictionMode.RESTRICTED
}

/**
Expand Down Expand Up @@ -163,26 +163,26 @@ abstract class MultiQueueAuthenticator: HasLogger
* This will delegate to [addRestrictedEntryInternal].
*
* @param subQueue the sub-queue identifier to make restricted
* @return `true` if the sub queue identifier was added to the restriction set, otherwise `false` if there was
* @return `true` if the sub-queue identifier was added to the restriction set, otherwise `false` if there was
* no underlying change made. If [isInNoneMode] is set this will always return `false`.
*/
fun addRestrictedEntry(subQueue: String): Boolean
{
if (isInNoneMode())
{
LOG.trace("Skipping adding restricted entry for [{}] since the authentication type is set to [{}].", subQueue, getAuthenticationType())
LOG.trace("Skipping adding restricted entry for [{}] since the restriction mode is set to [{}].", subQueue, getRestrictionMode())
return false
}
else
{
return if (isRestricted(subQueue))
{
LOG.trace("Restriction for sub queue [{}] was not increased as it is already restricted.", subQueue)
LOG.trace("Restriction for sub-queue [{}] was not increased as it is already restricted.", subQueue)
false
}
else
{
LOG.info("Adding restriction to sub queue [{}].", subQueue)
LOG.info("Adding restriction to sub-queue [{}].", subQueue)
addRestrictedEntryInternal(subQueue)
true
}
Expand All @@ -208,19 +208,19 @@ abstract class MultiQueueAuthenticator: HasLogger
{
return if (isInNoneMode())
{
LOG.trace("Skipping removing restricted entry for [{}] since the authentication type is set to [{}].", subQueue, getAuthenticationType())
LOG.trace("Skipping removing restricted entry for [{}] since the restriction mode is set to [{}].", subQueue, getRestrictionMode())
false
}
else
{
return if (isRestricted(subQueue))
{
LOG.info("Removing restriction to sub queue [{}].", subQueue)
LOG.info("Removing restriction to sub-queue [{}].", subQueue)
removeRestrictionInternal(subQueue)
}
else
{
LOG.trace("Restriction for sub queue [{}] was not removed as it is currently unrestricted.", subQueue)
LOG.trace("Restriction for sub-queue [{}] was not removed as it is currently unrestricted.", subQueue)
false
}

Expand All @@ -243,7 +243,7 @@ abstract class MultiQueueAuthenticator: HasLogger
/**
* Clear the underlying restriction storage entries. (This is mainly used for testing).
*
* @return the amount of sub queue restrictions that were cleared
* @return the amount of sub-queue restrictions that were cleared
*/
abstract fun clearRestrictedSubQueues(): Long
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import au.kilemon.messagequeue.authentication.RestrictionMode
*
* @author github.com/Kilemonn
*/
class MultiQueueAuthorisationException(subQueue: String, authenticationType: RestrictionMode) : Exception(String.format(MESSAGE_FORMAT, subQueue, authenticationType))
class MultiQueueAuthorisationException(subQueue: String, restrictionMode: RestrictionMode) : Exception(String.format(MESSAGE_FORMAT, subQueue, restrictionMode))
{
companion object
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class QueueConfiguration : HasLogger
private lateinit var redisTemplate: RedisTemplate<String, QueueMessage>

/**
* Initialise the [MultiQueue] [Bean] based on the [MessageQueueSettings.multiQueueType].
* Initialise the [MultiQueue] [Bean] based on the [MessageQueueSettings.storageMedium].
*/
@Bean
open fun getMultiQueue(): MultiQueue
Expand All @@ -58,7 +58,7 @@ class QueueConfiguration : HasLogger

// Default to in-memory
var queue: MultiQueue = InMemoryMultiQueue()
when (messageQueueSettings.multiQueueType.uppercase()) {
when (messageQueueSettings.storageMedium.uppercase()) {
StorageMedium.REDIS.toString() -> {
queue = RedisMultiQueue(messageQueueSettings.redisPrefix, redisTemplate)
}
Expand All @@ -70,44 +70,44 @@ class QueueConfiguration : HasLogger
}
}

LOG.info("Initialising [{}] queue as the [{}] is set to [{}].", queue::class.java.name, MessageQueueSettings.STORAGE_MEDIUM, messageQueueSettings.multiQueueType)
LOG.info("Initialising [{}] queue as the [{}] is set to [{}].", queue::class.java.name, MessageQueueSettings.STORAGE_MEDIUM, messageQueueSettings.storageMedium)

return queue
}

/**
* Initialise the [RestrictionMode] which drives how sub queues are accessed and created.
* Initialise the [RestrictionMode] which drives how sub-queues are accessed and created.
*/
@Bean
open fun getMultiQueueAuthenticationType(): RestrictionMode
open fun getRestrictionMode(): RestrictionMode
{
var authenticationType = RestrictionMode.NONE
var restrictionMode = RestrictionMode.NONE

if (messageQueueSettings.multiQueueAuthentication.isNotBlank())
if (messageQueueSettings.restrictionMode.isNotBlank())
{
try
{
authenticationType = RestrictionMode.valueOf(messageQueueSettings.multiQueueAuthentication.uppercase())
restrictionMode = RestrictionMode.valueOf(messageQueueSettings.restrictionMode.uppercase())
}
catch (ex: Exception)
{
LOG.warn("Unable to initialise appropriate authentication type with provided value [{}], falling back to default [{}].", messageQueueSettings.multiQueueAuthentication, RestrictionMode.NONE, ex)
LOG.warn("Unable to initialise appropriate restriction mode with provided value [{}], falling back to default [{}].", messageQueueSettings.restrictionMode, RestrictionMode.NONE, ex)
}
}

LOG.info("Using [{}] authentication as the [{}] is set to [{}].", authenticationType, MessageQueueSettings.RESTRICTION_MODE, messageQueueSettings.multiQueueAuthentication)
LOG.info("Using [{}] restriction mode as the [{}] is set to [{}].", restrictionMode, MessageQueueSettings.RESTRICTION_MODE, messageQueueSettings.restrictionMode)

return authenticationType
return restrictionMode
}

/**
* Initialise the [MultiQueueAuthenticator] [Bean] based on the [MessageQueueSettings.multiQueueType].
* Initialise the [MultiQueueAuthenticator] [Bean] based on the [MessageQueueSettings.storageMedium].
*/
@Bean
open fun getMultiQueueAuthenticator(): MultiQueueAuthenticator
{
var authenticator: MultiQueueAuthenticator = InMemoryAuthenticator()
when (messageQueueSettings.multiQueueType.uppercase()) {
when (messageQueueSettings.storageMedium.uppercase()) {
StorageMedium.REDIS.toString() -> {
authenticator = RedisAuthenticator()
}
Expand All @@ -119,7 +119,7 @@ class QueueConfiguration : HasLogger
}
}

LOG.info("Initialising [{}] authenticator as the [{}] is set to [{}].", authenticator::class.java.name, MessageQueueSettings.STORAGE_MEDIUM, messageQueueSettings.multiQueueType)
LOG.info("Initialising [{}] authenticator as the [{}] is set to [{}].", authenticator::class.java.name, MessageQueueSettings.STORAGE_MEDIUM, messageQueueSettings.storageMedium)

return authenticator
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ class JwtAuthenticationFilter: OncePerRequestFilter(), HasLogger
{
if (tokenIsPresentAndQueueIsRestricted(subQueue, authenticator))
{
LOG.trace("Accepted request for sub queue [{}].", subQueue.get())
LOG.trace("Accepted request for sub-queue [{}].", subQueue.get())
filterChain.doFilter(request, response)
}
else
{
val token = if (subQueue.isPresent) subQueue.get() else "null"
LOG.error("Failed to manipulate sub queue [{}] with provided token as the authentication level is set to [{}].", token, authenticator.getAuthenticationType())
LOG.error("Failed to manipulate sub-queue [{}] with provided token as the authentication level is set to [{}].", token, authenticator.getRestrictionMode())
handlerExceptionResolver.resolveException(request, response, null, MultiQueueAuthenticationException())
return
}
Expand Down Expand Up @@ -156,13 +156,13 @@ class JwtAuthenticationFilter: OncePerRequestFilter(), HasLogger
/**
* Set the provided [Optional][String] into the [MDC] as [JwtAuthenticationFilter.SUB_QUEUE] if it is not [Optional.empty].
*
* @param subQueue an optional sub queue identifier, if it is not [Optional.empty] it will be placed into the [MDC]
* @param subQueue an optional sub-queue identifier, if it is not [Optional.empty] it will be placed into the [MDC]
*/
fun setSubQueue(subQueue: Optional<String>)
{
if (subQueue.isPresent)
{
LOG.trace("Setting resolved sub queue from token into request context [{}].", subQueue.get())
LOG.trace("Setting resolved sub-queue from token into request context [{}].", subQueue.get())
MDC.put(SUB_QUEUE, subQueue.get())
}
}
Expand Down
Loading

0 comments on commit 7ff52d6

Please sign in to comment.