Skip to content

Commit

Permalink
Merge pull request #122 from pagreene/json-passing
Browse files Browse the repository at this point in the history
Allow the passing of query JSON
  • Loading branch information
pagreene committed Oct 7, 2020
2 parents d47114c + 43ecd0a commit 32be7d6
Show file tree
Hide file tree
Showing 21 changed files with 2,167 additions and 868 deletions.
70 changes: 70 additions & 0 deletions after_zappa
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python

import os
import json
import boto3

from indra_db.config import CONFIG
from indra_db.util.aws import get_role_kwargs


# Lambda CONFIG parameters
aws_role = CONFIG['lambda']['role']
aws_primary_function = CONFIG['lambda']['function']

# Load the Zappa config file.
ZAPPA_CONFIG = 'zappa_settings.json'
if not os.path.exists(ZAPPA_CONFIG):
raise Exception(f"No valid zappa config file present. "
f"Expecting: {ZAPPA_CONFIG}")
with open('zappa_settings.json', 'r') as f:
zappa_settings = json.load(f)


def fix_permissions(deployment):
"""Add permissions to the lambda function to allow access from API Gateway.
When Zappa runs, it removes permission for the primary endpoint to call
the lambda functions it creates. This function goes in and fixes those
permissions, and is intended to be run after a zappa update.
"""
# Get relevant settings from the zappa config.
project_name = zappa_settings[deployment]['project_name']
region = zappa_settings[deployment]['aws_region']
if zappa_settings[deployment]['profile_name'].lower() != aws_role.lower():
raise Exception("Required roles do not match!")

# Get the ID for the API on API Gateway
kwargs, identity = get_role_kwargs(aws_role)
api_gateway = boto3.client('apigateway', **kwargs)
api_data = api_gateway.get_rest_apis()
for item in api_data['items']:
if item['name'] == aws_primary_function:
break
else:
raise Exception(f"Could not find api matching name: "
f"{aws_primary_function}")

# Give the API Gateway access to the lambda functions.
account_id = identity['Account']
lambda_client = boto3.client('lambda', **kwargs)
for label, endpoint in [('root', ''), ('leafs', '/*')]:
source_arn = (f"arn:aws:execute-api:{region}:{account_id}:{item['id']}"
f"/*/*/{deployment}{endpoint}")
statement_id = f'{aws_primary_function}-access-to-{deployment}-{label}'
lambda_client.add_permission(FunctionName=f'{project_name}-{deployment}',
Action='lambda:InvokeFunction',
Principal='apigateway.amazonaws.com',
SourceArn=source_arn,
StatementId=statement_id)
return


def main():
import sys
deployment = sys.argv[1]
fix_permissions(deployment)


if __name__ == '__main__':
main()
5 changes: 2 additions & 3 deletions indra_db/client/readonly/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
from .interactions import *
from .pa_statements import *
from .query import *
from .util import *
from .query import *
30 changes: 0 additions & 30 deletions indra_db/client/readonly/pa_statements.py

This file was deleted.

0 comments on commit 32be7d6

Please sign in to comment.