mcp-security-framework 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. mcp_security_framework/__init__.py +96 -0
  2. mcp_security_framework/cli/__init__.py +18 -0
  3. mcp_security_framework/cli/cert_cli.py +511 -0
  4. mcp_security_framework/cli/security_cli.py +791 -0
  5. mcp_security_framework/constants.py +209 -0
  6. mcp_security_framework/core/__init__.py +61 -0
  7. mcp_security_framework/core/auth_manager.py +1011 -0
  8. mcp_security_framework/core/cert_manager.py +1663 -0
  9. mcp_security_framework/core/permission_manager.py +735 -0
  10. mcp_security_framework/core/rate_limiter.py +602 -0
  11. mcp_security_framework/core/security_manager.py +943 -0
  12. mcp_security_framework/core/ssl_manager.py +735 -0
  13. mcp_security_framework/examples/__init__.py +75 -0
  14. mcp_security_framework/examples/django_example.py +615 -0
  15. mcp_security_framework/examples/fastapi_example.py +472 -0
  16. mcp_security_framework/examples/flask_example.py +506 -0
  17. mcp_security_framework/examples/gateway_example.py +803 -0
  18. mcp_security_framework/examples/microservice_example.py +690 -0
  19. mcp_security_framework/examples/standalone_example.py +576 -0
  20. mcp_security_framework/middleware/__init__.py +250 -0
  21. mcp_security_framework/middleware/auth_middleware.py +292 -0
  22. mcp_security_framework/middleware/fastapi_auth_middleware.py +447 -0
  23. mcp_security_framework/middleware/fastapi_middleware.py +757 -0
  24. mcp_security_framework/middleware/flask_auth_middleware.py +465 -0
  25. mcp_security_framework/middleware/flask_middleware.py +591 -0
  26. mcp_security_framework/middleware/mtls_middleware.py +439 -0
  27. mcp_security_framework/middleware/rate_limit_middleware.py +403 -0
  28. mcp_security_framework/middleware/security_middleware.py +507 -0
  29. mcp_security_framework/schemas/__init__.py +109 -0
  30. mcp_security_framework/schemas/config.py +694 -0
  31. mcp_security_framework/schemas/models.py +709 -0
  32. mcp_security_framework/schemas/responses.py +686 -0
  33. mcp_security_framework/tests/__init__.py +0 -0
  34. mcp_security_framework/utils/__init__.py +121 -0
  35. mcp_security_framework/utils/cert_utils.py +525 -0
  36. mcp_security_framework/utils/crypto_utils.py +475 -0
  37. mcp_security_framework/utils/validation_utils.py +571 -0
  38. mcp_security_framework-0.1.0.dist-info/METADATA +411 -0
  39. mcp_security_framework-0.1.0.dist-info/RECORD +76 -0
  40. mcp_security_framework-0.1.0.dist-info/WHEEL +5 -0
  41. mcp_security_framework-0.1.0.dist-info/entry_points.txt +3 -0
  42. mcp_security_framework-0.1.0.dist-info/top_level.txt +2 -0
  43. tests/__init__.py +0 -0
  44. tests/test_cli/__init__.py +0 -0
  45. tests/test_cli/test_cert_cli.py +379 -0
  46. tests/test_cli/test_security_cli.py +657 -0
  47. tests/test_core/__init__.py +0 -0
  48. tests/test_core/test_auth_manager.py +582 -0
  49. tests/test_core/test_cert_manager.py +795 -0
  50. tests/test_core/test_permission_manager.py +395 -0
  51. tests/test_core/test_rate_limiter.py +626 -0
  52. tests/test_core/test_security_manager.py +841 -0
  53. tests/test_core/test_ssl_manager.py +532 -0
  54. tests/test_examples/__init__.py +8 -0
  55. tests/test_examples/test_fastapi_example.py +264 -0
  56. tests/test_examples/test_flask_example.py +238 -0
  57. tests/test_examples/test_standalone_example.py +292 -0
  58. tests/test_integration/__init__.py +0 -0
  59. tests/test_integration/test_auth_flow.py +502 -0
  60. tests/test_integration/test_certificate_flow.py +527 -0
  61. tests/test_integration/test_fastapi_integration.py +341 -0
  62. tests/test_integration/test_flask_integration.py +398 -0
  63. tests/test_integration/test_standalone_integration.py +493 -0
  64. tests/test_middleware/__init__.py +0 -0
  65. tests/test_middleware/test_fastapi_middleware.py +523 -0
  66. tests/test_middleware/test_flask_middleware.py +582 -0
  67. tests/test_middleware/test_security_middleware.py +493 -0
  68. tests/test_schemas/__init__.py +0 -0
  69. tests/test_schemas/test_config.py +811 -0
  70. tests/test_schemas/test_models.py +879 -0
  71. tests/test_schemas/test_responses.py +1054 -0
  72. tests/test_schemas/test_serialization.py +493 -0
  73. tests/test_utils/__init__.py +0 -0
  74. tests/test_utils/test_cert_utils.py +510 -0
  75. tests/test_utils/test_crypto_utils.py +603 -0
  76. tests/test_utils/test_validation_utils.py +477 -0
@@ -0,0 +1,209 @@
1
+ """
2
+ Constants Module
3
+
4
+ This module contains all constants used throughout the MCP Security Framework.
5
+ It centralizes configuration values, error codes, and default settings to
6
+ eliminate hardcoded values from the codebase.
7
+
8
+ Author: MCP Security Team
9
+ Version: 1.0.0
10
+ License: MIT
11
+ """
12
+
13
+ # Network and IP Constants
14
+ DEFAULT_CLIENT_IP = "127.0.0.1"
15
+ DEFAULT_SERVER_HOST = "localhost"
16
+ DEFAULT_SERVER_PORT = 8000
17
+
18
+ # Rate Limiting Constants
19
+ DEFAULT_REQUESTS_PER_MINUTE = 60
20
+ DEFAULT_REQUESTS_PER_HOUR = 1000
21
+ MAX_REQUESTS_PER_MINUTE = 10000
22
+ MAX_REQUESTS_PER_HOUR = 100000
23
+ DEFAULT_BURST_LIMIT = 2
24
+ MAX_BURST_LIMIT = 10
25
+ DEFAULT_WINDOW_SIZE_SECONDS = 60
26
+ MAX_WINDOW_SIZE_SECONDS = 3600
27
+ DEFAULT_CLEANUP_INTERVAL = 300
28
+ MAX_CLEANUP_INTERVAL = 3600
29
+
30
+ # Security Constants
31
+ DEFAULT_CACHE_TTL = 300 # 5 minutes
32
+ DEFAULT_FAILED_AUTH_CACHE_TTL = 60 # 1 minute
33
+ DEFAULT_RATE_LIMIT_CACHE_TTL = 60 # 1 minute
34
+
35
+ # Cryptographic Constants
36
+ PBKDF2_ITERATIONS = 100000
37
+ DEFAULT_SALT_LENGTH = 32
38
+ DEFAULT_API_KEY_LENGTH = 32
39
+ DEFAULT_RSA_KEY_SIZE = 2048
40
+ MAX_RSA_KEY_SIZE = 4096
41
+ MIN_RSA_KEY_SIZE = 2048
42
+
43
+ # Certificate Constants
44
+ DEFAULT_CERTIFICATE_VALIDITY_DAYS = 365
45
+ MAX_CERTIFICATE_VALIDITY_DAYS = 3650 # 10 years
46
+ DEFAULT_KEY_SIZE = 2048
47
+
48
+ # Logging Constants
49
+ DEFAULT_LOG_LEVEL = "INFO"
50
+ DEFAULT_MAX_FILE_SIZE_MB = 10
51
+ MAX_MAX_FILE_SIZE_MB = 1000
52
+ DEFAULT_BACKUP_COUNT = 5
53
+ MAX_BACKUP_COUNT = 100
54
+
55
+ # HTTP Status Codes
56
+ HTTP_OK = 200
57
+ HTTP_CREATED = 201
58
+ HTTP_BAD_REQUEST = 400
59
+ HTTP_UNAUTHORIZED = 401
60
+ HTTP_FORBIDDEN = 403
61
+ HTTP_NOT_FOUND = 404
62
+ HTTP_TOO_MANY_REQUESTS = 429
63
+ HTTP_INTERNAL_SERVER_ERROR = 500
64
+
65
+ # Error Codes
66
+ class ErrorCodes:
67
+ """Error codes for the MCP Security Framework."""
68
+
69
+ # SSL/TLS Errors (-32001 to -32009)
70
+ SSL_CONFIGURATION_ERROR = -32001
71
+ CERTIFICATE_VALIDATION_ERROR = -32002
72
+ SSL_CONTEXT_CREATION_ERROR = -32003
73
+ SSL_HANDSHAKE_ERROR = -32004
74
+
75
+ # Authentication Errors (-32010 to -32019)
76
+ AUTHENTICATION_ERROR = -32010
77
+ AUTHENTICATION_CONFIGURATION_ERROR = -32011
78
+ API_KEY_NOT_FOUND = -32012
79
+ JWT_VALIDATION_ERROR = -32013
80
+ CERTIFICATE_AUTH_ERROR = -32014
81
+ BASIC_AUTH_ERROR = -32015
82
+ AUTH_METHOD_NOT_SUPPORTED = -32016
83
+
84
+ # Authorization Errors (-32020 to -32029)
85
+ PERMISSION_DENIED_ERROR = -32020
86
+ INSUFFICIENT_PERMISSIONS = -32021
87
+ ROLE_NOT_FOUND = -32022
88
+ PERMISSION_NOT_FOUND = -32023
89
+
90
+ # Rate Limiting Errors (-32030 to -32039)
91
+ RATE_LIMIT_EXCEEDED_ERROR = -32030
92
+ RATE_LIMIT_CONFIGURATION_ERROR = -32031
93
+ RATE_LIMIT_STORAGE_ERROR = -32032
94
+
95
+ # Middleware Errors (-32040 to -32049)
96
+ SECURITY_MIDDLEWARE_ERROR = -32040
97
+ AUTH_MIDDLEWARE_ERROR = -32041
98
+ MTLS_MIDDLEWARE_ERROR = -32042
99
+ RATE_LIMIT_MIDDLEWARE_ERROR = -32043
100
+
101
+ # Certificate Management Errors (-32050 to -32059)
102
+ CERTIFICATE_GENERATION_ERROR = -32050
103
+ CERTIFICATE_REVOCATION_ERROR = -32051
104
+ CERTIFICATE_STORAGE_ERROR = -32052
105
+ CA_CONFIGURATION_ERROR = -32053
106
+
107
+ # Crypto Errors (-32060 to -32069)
108
+ CRYPTO_ERROR = -32060
109
+ KEY_GENERATION_ERROR = -32061
110
+ HASHING_ERROR = -32062
111
+ ENCRYPTION_ERROR = -32063
112
+
113
+ # Configuration Errors (-32070 to -32079)
114
+ CONFIGURATION_ERROR = -32070
115
+ VALIDATION_ERROR = -32071
116
+ SERIALIZATION_ERROR = -32072
117
+
118
+ # General Errors (-32080 to -32099)
119
+ GENERAL_ERROR = -32080
120
+ NOT_IMPLEMENTED_ERROR = -32081
121
+ UNSUPPORTED_OPERATION_ERROR = -32082
122
+
123
+ # Security Headers
124
+ DEFAULT_SECURITY_HEADERS = {
125
+ "X-Content-Type-Options": "nosniff",
126
+ "X-Frame-Options": "DENY",
127
+ "X-XSS-Protection": "1; mode=block",
128
+ "Strict-Transport-Security": "max-age=31536000; includeSubDomains",
129
+ "Content-Security-Policy": "default-src 'self'",
130
+ "Referrer-Policy": "strict-origin-when-cross-origin"
131
+ }
132
+
133
+ # Authentication Methods
134
+ AUTH_METHODS = {
135
+ "API_KEY": "api_key",
136
+ "JWT": "jwt",
137
+ "CERTIFICATE": "certificate",
138
+ "BASIC": "basic",
139
+ "OAUTH2": "oauth2"
140
+ }
141
+
142
+ # Storage Backends
143
+ STORAGE_BACKENDS = {
144
+ "MEMORY": "memory",
145
+ "REDIS": "redis",
146
+ "DATABASE": "database"
147
+ }
148
+
149
+ # Hash Algorithms
150
+ HASH_ALGORITHMS = {
151
+ "SHA256": "sha256",
152
+ "SHA512": "sha512",
153
+ "MD5": "md5"
154
+ }
155
+
156
+ # TLS Versions
157
+ TLS_VERSIONS = {
158
+ "TLSv1.0": "TLSv1.0",
159
+ "TLSv1.1": "TLSv1.1",
160
+ "TLSv1.2": "TLSv1.2",
161
+ "TLSv1.3": "TLSv1.3"
162
+ }
163
+
164
+ # Certificate Revocation Reasons
165
+ CERTIFICATE_REVOCATION_REASONS = {
166
+ "UNSPECIFIED": "unspecified",
167
+ "KEY_COMPROMISE": "key_compromise",
168
+ "CA_COMPROMISE": "ca_compromise",
169
+ "AFFILIATION_CHANGED": "affiliation_changed",
170
+ "SUPERSEDED": "superseded",
171
+ "CESSATION_OF_OPERATION": "cessation_of_operation",
172
+ "CERTIFICATE_HOLD": "certificate_hold"
173
+ }
174
+
175
+ # Log Levels
176
+ LOG_LEVELS = {
177
+ "DEBUG": "DEBUG",
178
+ "INFO": "INFO",
179
+ "WARNING": "WARNING",
180
+ "ERROR": "ERROR",
181
+ "CRITICAL": "CRITICAL"
182
+ }
183
+
184
+ # Time Constants (in seconds)
185
+ TIME_CONSTANTS = {
186
+ "MINUTE": 60,
187
+ "HOUR": 3600,
188
+ "DAY": 86400,
189
+ "WEEK": 604800,
190
+ "MONTH": 2592000, # 30 days
191
+ "YEAR": 31536000 # 365 days
192
+ }
193
+
194
+ # Cache Keys
195
+ CACHE_KEY_PREFIXES = {
196
+ "AUTH": "auth",
197
+ "RATE_LIMIT": "rate_limit",
198
+ "PERMISSION": "permission",
199
+ "CERTIFICATE": "certificate"
200
+ }
201
+
202
+ # Environment Variables
203
+ ENV_VARS = {
204
+ "DEFAULT_CLIENT_IP": "DEFAULT_CLIENT_IP",
205
+ "DEFAULT_SERVER_HOST": "DEFAULT_SERVER_HOST",
206
+ "DEFAULT_SERVER_PORT": "DEFAULT_SERVER_PORT",
207
+ "LOG_LEVEL": "LOG_LEVEL",
208
+ "ENVIRONMENT": "ENVIRONMENT"
209
+ }
@@ -0,0 +1,61 @@
1
+ """
2
+ Core Components Module
3
+
4
+ This module provides the core security components for the MCP Security Framework.
5
+ It includes the main business logic classes for authentication, authorization,
6
+ SSL/TLS management, certificate management, and rate limiting.
7
+
8
+ Key Components:
9
+ - RateLimiter: Rate limiting functionality
10
+ - PermissionManager: Role and permission management
11
+ - SSLManager: SSL/TLS management
12
+ - AuthManager: Authentication management
13
+ - CertificateManager: Certificate management
14
+ - SecurityManager: Main security manager
15
+
16
+ Author: MCP Security Team
17
+ Version: 1.0.0
18
+ License: MIT
19
+ """
20
+
21
+ from .auth_manager import AuthenticationError, AuthManager, JWTValidationError
22
+ from .cert_manager import (
23
+ CertificateConfigurationError,
24
+ CertificateGenerationError,
25
+ CertificateManager,
26
+ CertificateValidationError,
27
+ )
28
+ from .permission_manager import (
29
+ PermissionConfigurationError,
30
+ PermissionManager,
31
+ PermissionValidationError,
32
+ RoleNotFoundError,
33
+ )
34
+ from .rate_limiter import RateLimitEntry, RateLimiter
35
+ from .security_manager import (
36
+ SecurityConfigurationError,
37
+ SecurityManager,
38
+ SecurityValidationError,
39
+ )
40
+ from .ssl_manager import SSLConfigurationError, SSLManager
41
+
42
+ __all__ = [
43
+ "RateLimiter",
44
+ "RateLimitEntry",
45
+ "PermissionManager",
46
+ "PermissionConfigurationError",
47
+ "RoleNotFoundError",
48
+ "PermissionValidationError",
49
+ "SSLManager",
50
+ "SSLConfigurationError",
51
+ "CertificateValidationError",
52
+ "AuthManager",
53
+ "AuthenticationError",
54
+ "JWTValidationError",
55
+ "CertificateManager",
56
+ "CertificateGenerationError",
57
+ "CertificateConfigurationError",
58
+ "SecurityManager",
59
+ "SecurityConfigurationError",
60
+ "SecurityValidationError",
61
+ ]