Spaces:
Sleeping
Sleeping
Started adding lambdas support
Browse files- cbr_athena/_extra_methods/for_osbot_aws/Lambda.py +11 -0
- cbr_athena/lambdas/__init__.py +0 -0
- cbr_athena/lambdas/dev/__init__.py +0 -0
- cbr_athena/lambdas/dev/hello_world.py +3 -0
- cbr_athena/lambdas/dev/permissions_tests.py +8 -0
- requirements.txt +6 -1
- tests/aws/test_AWS_Setup.py +13 -0
- tests/lambdas/dev/test_hello_world.py +35 -0
- tests/lambdas/dev/test_permissions_tests.py +55 -0
cbr_athena/_extra_methods/for_osbot_aws/Lambda.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from osbot_utils.utils.Misc import wait_for
|
2 |
+
|
3 |
+
# this could be done with a waiter, but it takes much more time (1.5 secs vs 5 seconds)
|
4 |
+
# self.lambda_function.client().get_waiter('function_updated').wait(FunctionName=self.lambda_function.name)
|
5 |
+
def wait_for_function_update_to_complete(lambda_function, max_attempts=20, wait_time=0.1):
|
6 |
+
for i in range(max_attempts):
|
7 |
+
status = lambda_function.configuration().get('LastUpdateStatus')
|
8 |
+
if status == 'Successful':
|
9 |
+
break
|
10 |
+
wait_for(wait_time)
|
11 |
+
return status
|
cbr_athena/lambdas/__init__.py
ADDED
File without changes
|
cbr_athena/lambdas/dev/__init__.py
ADDED
File without changes
|
cbr_athena/lambdas/dev/hello_world.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
def run(event, context):
|
2 |
+
name = event.get('name') or 'world'
|
3 |
+
return f'hello {name}!'
|
cbr_athena/lambdas/dev/permissions_tests.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from osbot_aws.apis.S3 import S3
|
2 |
+
|
3 |
+
|
4 |
+
def run(event, context):
|
5 |
+
name = event.get('name') or 'world'
|
6 |
+
s3 = S3()
|
7 |
+
return s3.buckets()
|
8 |
+
return f'testing permissions {name}!'
|
requirements.txt
CHANGED
@@ -3,4 +3,9 @@
|
|
3 |
git+https://github.com/owasp-sbot/OSBot-Utils.git
|
4 |
gradio
|
5 |
openai
|
6 |
-
python-dotenv
|
|
|
|
|
|
|
|
|
|
|
|
3 |
git+https://github.com/owasp-sbot/OSBot-Utils.git
|
4 |
gradio
|
5 |
openai
|
6 |
+
python-dotenv
|
7 |
+
|
8 |
+
# for AWS support
|
9 |
+
git+https://github.com/owasp-sbot/OSBot-AWS.git
|
10 |
+
boto3
|
11 |
+
#botocore
|
tests/aws/test_AWS_Setup.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from unittest import TestCase
|
2 |
+
|
3 |
+
from osbot_aws.apis.STS import STS
|
4 |
+
|
5 |
+
|
6 |
+
class test_AWS_Setup(TestCase):
|
7 |
+
|
8 |
+
def setUp(self) -> None:
|
9 |
+
pass
|
10 |
+
|
11 |
+
def test_setup(self):
|
12 |
+
STS().check_current_session_credentials()
|
13 |
+
assert True is True
|
tests/lambdas/dev/test_hello_world.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#@pytest.mark.skip('needs AWS credentials to run')
|
2 |
+
from unittest import TestCase
|
3 |
+
|
4 |
+
from osbot_aws.apis.Session import Session
|
5 |
+
from osbot_aws.apis.test_helpers.Temp_Aws_Roles import Temp_Aws_Roles
|
6 |
+
from osbot_aws.deploy.Deploy_Lambda import Deploy_Lambda
|
7 |
+
from osbot_utils.utils.Dev import pprint
|
8 |
+
|
9 |
+
from cbr_athena._extra_methods.for_osbot_aws.Lambda import wait_for_function_update_to_complete
|
10 |
+
from cbr_athena.lambdas.dev.hello_world import run
|
11 |
+
|
12 |
+
|
13 |
+
class test_hello_world(TestCase):
|
14 |
+
|
15 |
+
def setUp(self) -> None:
|
16 |
+
self.handler = run
|
17 |
+
self.deploy_lambda = Deploy_Lambda(self.handler)
|
18 |
+
self.lambda_function = self.deploy_lambda.lambda_function()
|
19 |
+
|
20 |
+
def test_invoke_directly(self):
|
21 |
+
result = self.handler({'name': 'me'}, None)
|
22 |
+
assert result == 'hello me!'
|
23 |
+
|
24 |
+
def test_deploying_lambda_function(self):
|
25 |
+
assert self.deploy_lambda.update().get('status') == 'ok'
|
26 |
+
wait_for_function_update_to_complete(self.lambda_function)
|
27 |
+
|
28 |
+
assert self.lambda_function.invoke({'name': 'me'}) == 'hello me!'
|
29 |
+
|
30 |
+
def test_lambda_setup(self):
|
31 |
+
temp_aws_roles = Temp_Aws_Roles()
|
32 |
+
if temp_aws_roles.for_lambda_invocation__not_exists():
|
33 |
+
result = temp_aws_roles.for_lambda_invocation__create()
|
34 |
+
pprint(result)
|
35 |
+
assert temp_aws_roles.for_lambda_invocation_exists()
|
tests/lambdas/dev/test_permissions_tests.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#@pytest.mark.skip('needs AWS credentials to run')
|
2 |
+
from unittest import TestCase
|
3 |
+
|
4 |
+
from osbot_aws.apis.STS import STS
|
5 |
+
from osbot_aws.apis.Session import Session
|
6 |
+
from osbot_aws.apis.test_helpers.Temp_Aws_Roles import Temp_Aws_Roles
|
7 |
+
from osbot_aws.deploy.Deploy_Lambda import Deploy_Lambda
|
8 |
+
from osbot_aws.helpers.IAM_Role import IAM_Role
|
9 |
+
from osbot_utils.utils.Dev import pprint
|
10 |
+
|
11 |
+
from cbr_athena._extra_methods.for_osbot_aws.Lambda import wait_for_function_update_to_complete
|
12 |
+
from cbr_athena.lambdas.dev.permissions_tests import run
|
13 |
+
|
14 |
+
|
15 |
+
class test_hello_world(TestCase):
|
16 |
+
|
17 |
+
def setUp(self) -> None:
|
18 |
+
self.handler = run
|
19 |
+
self.deploy_lambda = Deploy_Lambda(self.handler)
|
20 |
+
self.lambda_function = self.deploy_lambda.lambda_function()
|
21 |
+
|
22 |
+
def test_invoke_directly(self):
|
23 |
+
self.test_check_credentials()
|
24 |
+
result = self.handler({'name': 'me'}, None)
|
25 |
+
pprint(result)
|
26 |
+
#assert result == 'hello me!'
|
27 |
+
|
28 |
+
def test_check_credentials(self):
|
29 |
+
|
30 |
+
role_arn = self.deploy_lambda.role_arn
|
31 |
+
role_name = self.deploy_lambda.role_arn.split('/')[-1]
|
32 |
+
#role_arn = "temp_role_for_lambda_invocation"
|
33 |
+
pprint(role_arn)
|
34 |
+
iam_role = IAM_Role(role_name)
|
35 |
+
|
36 |
+
pprint(iam_role.policies_statements())
|
37 |
+
#
|
38 |
+
#STS()
|
39 |
+
|
40 |
+
def test_deploying_lambda_function(self):
|
41 |
+
self.test_check_credentials()
|
42 |
+
self.deploy_lambda.add_osbot_aws()
|
43 |
+
assert self.deploy_lambda.update().get('status') == 'ok'
|
44 |
+
wait_for_function_update_to_complete(self.lambda_function)
|
45 |
+
|
46 |
+
result = self.lambda_function.invoke({'name': 'me'})
|
47 |
+
pprint(result)
|
48 |
+
#assert self.lambda_function.invoke({'name': 'me'}) == 'testing permissions me!'
|
49 |
+
|
50 |
+
def test_lambda_setup(self):
|
51 |
+
temp_aws_roles = Temp_Aws_Roles()
|
52 |
+
if temp_aws_roles.for_lambda_invocation__not_exists():
|
53 |
+
result = temp_aws_roles.for_lambda_invocation__create()
|
54 |
+
pprint(result)
|
55 |
+
assert temp_aws_roles.for_lambda_invocation_exists()
|