Skip to content

Commit 5f6b0c8

Browse files
Sector95Justinleandrodamascena
authored
feat(event_handler): mutualTLS Security Scheme for OpenAPI (#5484)
* mutualtls security scheme implementation * security scheme documentation updates * Adding mTLS test * Adding mTLS test --------- Co-authored-by: Justin <[email protected]> Co-authored-by: Leandro Damascena <[email protected]>
1 parent 3988469 commit 5f6b0c8

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

aws_lambda_powertools/event_handler/openapi/models.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ class SecuritySchemeType(Enum):
363363
http = "http"
364364
oauth2 = "oauth2"
365365
openIdConnect = "openIdConnect"
366+
mutualTLS = "mutualTLS"
366367

367368

368369
class SecurityBase(OpenAPIExtensions):
@@ -440,7 +441,11 @@ class OpenIdConnect(SecurityBase):
440441
openIdConnectUrl: str
441442

442443

443-
SecurityScheme = Union[APIKey, HTTPBase, OAuth2, OpenIdConnect, HTTPBearer]
444+
class MutualTLS(SecurityBase):
445+
type_: SecuritySchemeType = Field(default=SecuritySchemeType.mutualTLS, alias="type")
446+
447+
448+
SecurityScheme = Union[APIKey, HTTPBase, OAuth2, OpenIdConnect, HTTPBearer, MutualTLS]
444449

445450

446451
# https://swagger.io/specification/#components-object

docs/core/event_handler/api_gateway.md

+1
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,7 @@ OpenAPI 3 lets you describe APIs protected using the following security schemes:
11111111
| [API keys](https://swagger.io/docs/specification/authentication/api-keys/https://swagger.io/docs/specification/authentication/api-keys/){target="_blank"} (e.g: query strings, cookies) | `APIKey` | API keys in headers, query strings or [cookies](https://swagger.io/docs/specification/authentication/cookie-authentication/){target="_blank"}. |
11121112
| [OAuth 2](https://swagger.io/docs/specification/authentication/oauth2/){target="_blank"} | `OAuth2` | Authorization protocol that gives an API client limited access to user data on a web server. |
11131113
| [OpenID Connect Discovery](https://swagger.io/docs/specification/authentication/openid-connect-discovery/){target="_blank"} | `OpenIdConnect` | Identity layer built [on top of the OAuth 2.0 protocol](https://openid.net/developers/how-connect-works/){target="_blank"} and supported by some OAuth 2.0. |
1114+
| [Mutual TLS](https://swagger.io/specification/#security-scheme-object){target="_blank"}. | `MutualTLS` | Client/server certificate mutual authentication scheme. |
11141115

11151116
???-note "Using OAuth2 with the Swagger UI?"
11161117
You can use the `OAuth2Config` option to configure a default OAuth2 app on the generated Swagger UI.

tests/functional/event_handler/_pydantic/test_openapi_security_schemes.py

+22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
APIKey,
44
APIKeyIn,
55
HTTPBearer,
6+
MutualTLS,
67
OAuth2,
78
OAuthFlowImplicit,
89
OAuthFlows,
@@ -110,3 +111,24 @@ def handler():
110111
open_id_connect_scheme = security_schemes["openIdConnect"]
111112
assert open_id_connect_scheme.type_.value == "openIdConnect"
112113
assert open_id_connect_scheme.openIdConnectUrl == "https://example.com/oauth2/authorize"
114+
115+
116+
def test_openapi_security_scheme_mtls():
117+
app = APIGatewayRestResolver()
118+
119+
@app.get("/")
120+
def handler():
121+
raise NotImplementedError()
122+
123+
schema = app.get_openapi_schema(
124+
security_schemes={
125+
"mutualTLS": MutualTLS(description="mTLS Authentication"),
126+
},
127+
)
128+
129+
security_schemes = schema.components.securitySchemes
130+
assert security_schemes is not None
131+
132+
assert "mutualTLS" in security_schemes
133+
mtls_scheme = security_schemes["mutualTLS"]
134+
assert mtls_scheme.description == "mTLS Authentication"

0 commit comments

Comments
 (0)