-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[proposal] Add support for built in custom authorizers #356
Comments
|
I'm not sure how it would be possible that some lambda functions would respect the config values and some wouldn't. Presumably if there's a stage-level config value that applies to all lambda functions, they should all take that configuration value. Good point about the TTL, that would likely be part of the authorizer definition, so what about: @app.authorizer('MyAuthName', ttl=300)
def my_auth(auth_request):
pass |
@jamesls the configuration spec looks good. I am assuming env vars and tags having the merging behavior when provided for a specific lambda function? |
@kyleknap Yep, that's the plan. |
Implemented via 4d1751d |
This proposes adding support for writing custom authorizers
using chalice and seamlessly integrating them into a chalice app.
User API
I think we should continue on the decorator based approach
and add an authorizer decorator:
From the code above:
@app.authorizer()
Input: Auth Request
The auth request is based on the event dictionary lambda passes to
the authorizer:
Output: Authorized/Unauthorized
The required response for API gateway is a policy indicating
what resources they are allowed to access. This is typically
arn:aws:execute-api:...:/some-path
. You also providea principal id as well as optional context. This is all
handled in the
Authorized
class:To simplify the interface, the user doesn't have to provide
the full ARNs and instead can just provide the resource paths:
Connecting authorizers to view functions
To keep things simple, we can reuse the existing
authorizer
option in the
.route()
call to indicate you want to usea custom authorizer. Here's a full example:
Possible Implementation
The API gateway API for configuring a custom authorizer is that you
must provide a lambda function arn to use. I could route everything
through the existing chalice lambda handler (which only handles routes)
right now, but there's several downsides that I didn't like. The biggest
one is that you infer which type of request is being handled by inspecting the
set of keys in the event dict. If there's
authType, token, methodArn
, thenit's an auth request. Otherwise it's an API request. While that sounds fine
in theory, I'm weary of mixing auth requests with normal view requests.
So the proposal here is to create a separate lambda function for each
@app.authorizer
. There's a few caveats though:and the custom authorizer. The only difference is in the
value of the
handler
passed to thecreate_function
call.In the API handler case, it will be
app.app
. In the caseof an authorizer, it will be
app.<auth_function>
. In theworking example above this would be
app.my_auth
.with the API handler.
app.py
.Your implementation could be elsewhere (separate package, somewhere in
chalicelib, etc), but the registration and entry point of the authorizer
needs to be in app.py.
As for the code itself, there's several places that need to change:
lambda functions, but now it can possibly deploy multiple lambda
functions.
LambdaDeployer
. The returntype is still the same, but there will be a new key returned that's
a mapping of all the lambda functions deployed.
snippet for a built in auth. The signature to the SwaggerGenerator
needs to change. Right now you pass in a single lambda ARN, which
we assume is the arn of the API handler. Now it should just accept
the dict of deployed values, which will contain all the lambda ARNs
we've deployed.
gateway authorizer to be able to call this new lambda function. We
had to add similar logic for the API lambda handler. We need the
same thing for the API gateway authorizer.
Tracking Deployment
To track what we've deployed, and to support more functions in the future,
I propose updating the
deployed.json
to add a newlambda_functions
key. The value is a dict of function name to function properties. The
function properties include the lambda arn, and the purpose of the function.
With this change the only value for purpose will be
authorizer
.Example:
With this model in place, we should be able to add more types of lambda functions
quickly (assuming the retrictions above still hold for these new function
types).
Configuring the Authorizers
The config is already hierarchical for most options. You can specify a config
value at either the stage level or the "global" level which applies to all
stages.
The proposal here is to introduce another level of granularity below stage that
is "per-lambda-function". With the exception of
api_gateway_stage
, all theexisting config options can now also be specified per-lambda-function:
manage_iam_role
iam_role_arn
autogen_policy
iam_policy_file
environment_variables
lambda_timeout
lambda_memory_size
tags
Now when deploying a lambda function, we'll check per-lambda-function,
per-stage, and the globally for config settings.
Example
The
lambda_functions
key specifies the per-lambda-functiongranularity, and the keys are the function or resource names. The
api_handler
name is a special built-in name for the mainlambda handler in chalice. Otherwise the names are either the
function name or the explicit name of the authorizer if one is given:
So for example, the
my_auth
function in thebeta
stage would havethese values from the per-lambda-function config:
iam_policy_file
- "beta-app-policy-auth.json"lambda_timeout
- 10lambda_memory_size
- 256And this value from the per-stage config:
autogen_policy
- falseAnd nothing from the global config.
The text was updated successfully, but these errors were encountered: