Package com.tnsai.quality.security
Class SecurityEnforcer
java.lang.Object
com.tnsai.quality.security.SecurityEnforcer
- All Implemented Interfaces:
com.tnsai.security.SecurityEnforcerHandle,AutoCloseable
public final class SecurityEnforcer
extends Object
implements com.tnsai.security.SecurityEnforcerHandle, AutoCloseable
Enforces security policies defined via @Security annotation.
This class is a facade that delegates to focused services:
AccessControlManager— caller ID checks, role-based checks, permission queriesEncryptionService— AES-256-GCM encrypt/decryptAuditLogger— field masking and audit logging at 4 levels
Timeout execution remains in this class since it requires the ExecutorService.
Usage
// With auto-generated encryption key
SecurityEnforcer enforcer = new SecurityEnforcer();
// With custom encryption config
EncryptionConfig config = EncryptionConfig.fromBase64Key(myKey);
SecurityEnforcer enforcer = new SecurityEnforcer(config);
// Before action execution; ActionExecutor calls enforceAccess(AccessRequest) as its access gate
Map<String, Object> secureParams = enforcer.processParameters(action, role, params);
// Execute with timeout
Object result = enforcer.executeWithTimeout(action, role, () -> doExecute());
// Process result
Object secureResult = enforcer.processResult(action, role, result);
- Author:
- TnsAI Team
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.tnsai.security.SecurityEnforcerHandle
com.tnsai.security.SecurityEnforcerHandle.Factory -
Field Summary
Fields inherited from interface com.tnsai.security.SecurityEnforcerHandle
NOOP -
Constructor Summary
ConstructorsConstructorDescriptionCreates a SecurityEnforcer with an auto-generated encryption key.SecurityEnforcer(EncryptionConfig encryptionConfig) Creates a SecurityEnforcer with the specified encryption configuration.SecurityEnforcer(EncryptionConfig encryptionConfig, com.tnsai.security.PermissionProvider permissionProvider) Creates a SecurityEnforcer with the specified encryption configuration and permission provider. -
Method Summary
Modifier and TypeMethodDescriptionvoidaudit(com.tnsai.metadata.ActionMetadata action, com.tnsai.roles.Role role, String callerAgentId, Map<String, Object> parameters, Object result, long durationMs) Logs audit information based on audit level.voidaudit(com.tnsai.metadata.ActionMetadata action, Object target, String callerAgentId, Map<String, Object> parameters, Object result, long durationMs) Logs audit information for an action whose target is not aRole, asaudit(ActionMetadata, Role, String, Map, Object, long)does; its@Securityfeatures are read as on aRole.voidclose()Shuts down the timeout executor.Decrypts a Base64-encoded AES-256-GCM encrypted value.Encrypts a string value using AES-256-GCM.voidenforceAccess(com.tnsai.security.AccessRequest request) voidenforceAccessControl(com.tnsai.metadata.ActionMetadata action, com.tnsai.roles.Role role, String callerAgentId, Set<String> callerRoles) Enforces access control based on the action method's@Security, else the role class's own.<T> TexecuteWithTimeout(com.tnsai.metadata.ActionMetadata action, com.tnsai.roles.Role role, Callable<T> task) Executes an action with security timeout enforcement.<T> TexecuteWithTimeout(com.tnsai.metadata.ActionMetadata action, Object target, Callable<T> task) Executes an action whose target is not aRolewith security timeout enforcement; its@Securityfeatures are read as on aRole.Gets the encryption configuration.com.tnsai.security.PermissionProviderGets the permission provider, if any.maskForLogging(com.tnsai.metadata.ActionMetadata action, com.tnsai.roles.Role role, Map<String, Object> parameters) Masks sensitive fields in parameters for logging.processParameters(com.tnsai.metadata.ActionMetadata action, com.tnsai.roles.Role role, Map<String, Object> parameters) Processes parameters based on security settings.processParameters(com.tnsai.metadata.ActionMetadata action, Object target, Map<String, Object> parameters) Processes parameters for an action whose target is not aRole, asprocessParameters(ActionMetadata, Role, Map)does; its@Securityfeatures are read as on aRole.processResult(com.tnsai.metadata.ActionMetadata action, com.tnsai.roles.Role role, Object result) Processes the result based on security settings.processResult(com.tnsai.metadata.ActionMetadata action, Object target, Object result) Processes the result for an action whose target is not aRole, asprocessResult(ActionMetadata, Role, Object)does; its@Securityfeatures are read as on aRole.voidrequireHonored(com.tnsai.metadata.ActionMetadata action, Object target) Honors every target-typed feature, so it refuses nothing.voidshutdown()Shuts down the timeout executor.
-
Constructor Details
-
SecurityEnforcer
public SecurityEnforcer()Creates a SecurityEnforcer with an auto-generated encryption key. Attempts to discover aPermissionProvidervia ServiceLoader. -
SecurityEnforcer
Creates a SecurityEnforcer with the specified encryption configuration. Attempts to discover aPermissionProvidervia ServiceLoader.- Parameters:
encryptionConfig- the encryption configuration to use
-
SecurityEnforcer
public SecurityEnforcer(EncryptionConfig encryptionConfig, com.tnsai.security.PermissionProvider permissionProvider) Creates a SecurityEnforcer with the specified encryption configuration and permission provider.- Parameters:
encryptionConfig- the encryption configuration to usepermissionProvider- the permission provider (null for no permission checks)
-
-
Method Details
-
enforceAccess
public void enforceAccess(com.tnsai.security.AccessRequest request) - Specified by:
enforceAccessin interfacecom.tnsai.security.SecurityEnforcerHandle
-
getEncryptionConfig
Gets the encryption configuration.- Returns:
- the encryption configuration
-
getPermissionProvider
public com.tnsai.security.PermissionProvider getPermissionProvider()Gets the permission provider, if any.- Returns:
- the permission provider, or null if none registered
-
enforceAccessControl
public void enforceAccessControl(com.tnsai.metadata.ActionMetadata action, com.tnsai.roles.Role role, String callerAgentId, Set<String> callerRoles) Enforces access control based on the action method's@Security, else the role class's own.This is not the executor's access gate and does not see inherited declarations: a
@Securityon a superclass, an interface or an overridden method is not checked here. The executor checks every@SecuritythroughenforceAccess(com.tnsai.security.AccessRequest).- Specified by:
enforceAccessControlin interfacecom.tnsai.security.SecurityEnforcerHandle- Parameters:
action- the action metadatarole- the role containing the actioncallerAgentId- the ID of the calling agent (null if unknown)callerRoles- the roles of the calling agent- Throws:
com.tnsai.security.SecurityException- if access is denied
-
processParameters
public Map<String,Object> processParameters(com.tnsai.metadata.ActionMetadata action, com.tnsai.roles.Role role, Map<String, Object> parameters) Processes parameters based on security settings. Handles encryption if encryptParams is true.- Specified by:
processParametersin interfacecom.tnsai.security.SecurityEnforcerHandle- Parameters:
action- the action metadatarole- the role containing the actionparameters- the original parameters- Returns:
- processed parameters (encrypted if required)
-
requireHonored
Honors every target-typed feature, so it refuses nothing.- Specified by:
requireHonoredin interfacecom.tnsai.security.SecurityEnforcerHandle- Parameters:
action- the action metadatatarget- the object containing the action
-
processParameters
public Map<String,Object> processParameters(com.tnsai.metadata.ActionMetadata action, Object target, Map<String, Object> parameters) Processes parameters for an action whose target is not aRole, asprocessParameters(ActionMetadata, Role, Map)does; its@Securityfeatures are read as on aRole.- Specified by:
processParametersin interfacecom.tnsai.security.SecurityEnforcerHandle- Parameters:
action- the action metadatatarget- the object containing the actionparameters- the original parameters- Returns:
- processed parameters (encrypted if required)
-
processResult
public Object processResult(com.tnsai.metadata.ActionMetadata action, com.tnsai.roles.Role role, Object result) Processes the result based on security settings. Handles encryption if encryptResult is true.- Specified by:
processResultin interfacecom.tnsai.security.SecurityEnforcerHandle- Parameters:
action- the action metadatarole- the role containing the actionresult- the original result- Returns:
- processed result (encrypted if required)
-
processResult
Processes the result for an action whose target is not aRole, asprocessResult(ActionMetadata, Role, Object)does; its@Securityfeatures are read as on aRole.- Specified by:
processResultin interfacecom.tnsai.security.SecurityEnforcerHandle- Parameters:
action- the action metadatatarget- the object containing the actionresult- the original result- Returns:
- processed result (encrypted if required)
-
executeWithTimeout
public <T> T executeWithTimeout(com.tnsai.metadata.ActionMetadata action, com.tnsai.roles.Role role, Callable<T> task) Executes an action with security timeout enforcement.- Specified by:
executeWithTimeoutin interfacecom.tnsai.security.SecurityEnforcerHandle- Type Parameters:
T- the result type- Parameters:
action- the action metadatarole- the role containing the actiontask- the task to execute- Returns:
- the execution result
- Throws:
com.tnsai.security.SecurityException- if timeout is exceeded
-
executeWithTimeout
public <T> T executeWithTimeout(com.tnsai.metadata.ActionMetadata action, Object target, Callable<T> task) throws Exception Executes an action whose target is not aRolewith security timeout enforcement; its@Securityfeatures are read as on aRole. UnlikeexecuteWithTimeout(ActionMetadata, Role, Callable), a failure oftaskpropagates unchanged: without a declaredsecurityTimeoutthe task runs on the calling thread, and with one only the timeout and an interrupt of the waiting thread become aSecurityException.- Specified by:
executeWithTimeoutin interfacecom.tnsai.security.SecurityEnforcerHandle- Type Parameters:
T- the result type- Parameters:
action- the action metadatatarget- the object containing the actiontask- the task to execute- Returns:
- the execution result
- Throws:
Exception- whatevertaskthrowscom.tnsai.security.SecurityException- if the timeout is exceeded
-
maskForLogging
public Map<String,Object> maskForLogging(com.tnsai.metadata.ActionMetadata action, com.tnsai.roles.Role role, Map<String, Object> parameters) Masks sensitive fields in parameters for logging.- Parameters:
action- the action metadatarole- the role containing the actionparameters- the parameters to mask- Returns:
- masked parameters safe for logging
-
audit
public void audit(com.tnsai.metadata.ActionMetadata action, com.tnsai.roles.Role role, String callerAgentId, Map<String, Object> parameters, Object result, long durationMs) Logs audit information based on audit level.- Specified by:
auditin interfacecom.tnsai.security.SecurityEnforcerHandle- Parameters:
action- the action metadatarole- the role containing the actioncallerAgentId- the caller agent IDparameters- the action parametersresult- the action resultdurationMs- execution duration in milliseconds
-
audit
public void audit(com.tnsai.metadata.ActionMetadata action, Object target, String callerAgentId, Map<String, Object> parameters, Object result, long durationMs) Logs audit information for an action whose target is not aRole, asaudit(ActionMetadata, Role, String, Map, Object, long)does; its@Securityfeatures are read as on aRole.- Specified by:
auditin interfacecom.tnsai.security.SecurityEnforcerHandle- Parameters:
action- the action metadatatarget- the object containing the actioncallerAgentId- the caller agent IDparameters- the action parametersresult- the action resultdurationMs- execution duration in milliseconds
-
encrypt
Encrypts a string value using AES-256-GCM.The encrypted output format is: Base64(IV + ciphertext + authTag) where IV is prepended to allow decryption.
- Parameters:
value- the plaintext value to encrypt- Returns:
- Base64-encoded encrypted value
- Throws:
com.tnsai.security.SecurityException- if encryption fails
-
decrypt
Decrypts a Base64-encoded AES-256-GCM encrypted value.- Parameters:
encrypted- the Base64-encoded encrypted value- Returns:
- the decrypted plaintext value
- Throws:
com.tnsai.security.SecurityException- if decryption fails or authentication fails
-
close
public void close()Shuts down the timeout executor.- Specified by:
closein interfaceAutoCloseable
-
shutdown
public void shutdown()Shuts down the timeout executor.
-