Class SecurityRunner
java.lang.Object
neqsim.mcp.runners.SecurityRunner
Security and multi-tenancy layer for the NeqSim MCP server.
Provides:
- API key-based authentication for production deployments
- Per-user session isolation to prevent cross-contamination
- Comprehensive audit logging of all simulation and data access operations
- Rate limiting to protect compute resources from abuse
- User/project context for multi-tenant usage
This is an application-level security layer. In production, combine with transport-level security (TLS, OAuth2) provided by the deployment platform.
- Version:
- 1.0
- Author:
- Even Solbraa
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static classAn audit log entry.(package private) static classRate limiting state for a user.(package private) static classUser context associated with an API key. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final ConcurrentHashMap<String, SecurityRunner.UserContext> Registered API keys (in production, these would come from a database or vault).private static final List<SecurityRunner.AuditEntry> Audit log (in production, this would write to a persistent store).private static final intDefault rate limit: requests per minute.private static booleanWhether security enforcement is enabled.private static final com.google.gson.Gsonprivate static final intMax audit log entries kept in memory.private static final ConcurrentHashMap<String, SecurityRunner.RateState> Rate limiting: requests per key in the current time window.private static final longRate window in milliseconds.private static final AtomicLongGlobal request counter. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor — all methods are static. -
Method Summary
Modifier and TypeMethodDescriptionprivate static Stringauthenticate(com.google.gson.JsonObject input) Authenticates with an API key and returns user context.static StringcheckAccess(String apiKey, String tool) Checks authentication and rate limiting for an incoming request.private static booleancheckRateLimit(String key, int limit) Checks rate limiting for a key.private static StringcreateApiKey(com.google.gson.JsonObject input) Creates a new API key for a user/project.private static StringCreates a standard error JSON response.private static StringgetAuditLog(com.google.gson.JsonObject input) Returns recent audit log entries.private static StringReturns current rate limit status for all authenticated users.private static StringReturns current security status.private static voidLogs an audit entry.private static StringrevokeApiKey(com.google.gson.JsonObject input) Revokes an API key.static StringMain entry point for security operations.private static StringsetConfig(com.google.gson.JsonObject input) Configures security settings.
-
Field Details
-
GSON
private static final com.google.gson.Gson GSON -
API_KEYS
Registered API keys (in production, these would come from a database or vault). -
AUDIT_LOG
Audit log (in production, this would write to a persistent store). -
RATE_LIMITS
Rate limiting: requests per key in the current time window. -
DEFAULT_RATE_LIMIT
private static final int DEFAULT_RATE_LIMITDefault rate limit: requests per minute.- See Also:
-
RATE_WINDOW_MS
private static final long RATE_WINDOW_MSRate window in milliseconds.- See Also:
-
MAX_AUDIT_LOG_SIZE
private static final int MAX_AUDIT_LOG_SIZEMax audit log entries kept in memory.- See Also:
-
enabled
private static volatile boolean enabledWhether security enforcement is enabled. -
REQUEST_COUNTER
Global request counter.
-
-
Constructor Details
-
SecurityRunner
private SecurityRunner()Private constructor — all methods are static.
-
-
Method Details
-
run
-
checkAccess
Checks authentication and rate limiting for an incoming request. Call this at the beginning of any protected tool invocation.- Parameters:
apiKey- the API key (optional if security is disabled)tool- the tool being invoked- Returns:
- null if allowed, or an error JSON string if denied
-
createApiKey
Creates a new API key for a user/project.- Parameters:
input- JSON with user details- Returns:
- JSON with the new API key
-
revokeApiKey
Revokes an API key.- Parameters:
input- JSON with apiKey- Returns:
- JSON confirmation
-
authenticate
Authenticates with an API key and returns user context.- Parameters:
input- JSON with apiKey- Returns:
- JSON with authentication result
-
getAuditLog
Returns recent audit log entries.- Parameters:
input- JSON with optional filters (userId, tool, limit)- Returns:
- JSON with audit entries
-
getRateLimits
Returns current rate limit status for all authenticated users.- Returns:
- JSON with rate limit details
-
setConfig
Configures security settings.- Parameters:
input- JSON with configuration- Returns:
- JSON confirmation
-
getStatus
Returns current security status.- Returns:
- JSON with security status
-
checkRateLimit
Checks rate limiting for a key.- Parameters:
key- the API keylimit- the max requests per window- Returns:
- true if within limits
-
logAudit
-
errorJson
-