Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit d2bfede

Browse files
authored
Functions to make application public/private and share application (#8)
* Functions to make application public/private and share application
1 parent 6cf5257 commit d2bfede

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

serverlessrepo/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
"""
44

55
from .publish import publish_application
6+
from .permission_helper import make_application_public, make_application_private, share_application_with_accounts

serverlessrepo/permission_helper.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import boto3
2+
3+
from .application_policy import ApplicationPolicy
4+
5+
6+
def make_application_public(application_id):
7+
"""
8+
This function sets the application to be public
9+
:raises ValueError
10+
"""
11+
if not application_id:
12+
raise ValueError('Require application id to make the app public')
13+
14+
application_policy = ApplicationPolicy(['*'], [ApplicationPolicy.DEPLOY])
15+
application_policy.validate()
16+
boto3.client('serverlessrepo').put_application_policy(
17+
ApplicationId=application_id,
18+
Statements=[application_policy.to_statement()]
19+
)
20+
21+
22+
def make_application_private(application_id):
23+
"""
24+
This function sets the application to be private
25+
:raises ValueError
26+
"""
27+
if not application_id:
28+
raise ValueError('Require application id to make the app private')
29+
30+
boto3.client('serverlessrepo').put_application_policy(
31+
ApplicationId=application_id,
32+
Statements=[]
33+
)
34+
35+
36+
def share_application_with_accounts(application_id, account_ids):
37+
"""
38+
This function shares the application privately with given AWS account IDs
39+
:param account_ids: List of AWS account IDs, or *
40+
:type account_ids: list of str
41+
:raises ValueError
42+
"""
43+
if not application_id or not account_ids:
44+
raise ValueError('Require application id and list of AWS account IDs to share the app')
45+
46+
application_policy = ApplicationPolicy(account_ids, [ApplicationPolicy.DEPLOY])
47+
application_policy.validate()
48+
boto3.client('serverlessrepo').put_application_policy(
49+
ApplicationId=application_id,
50+
Statements=[application_policy.to_statement()]
51+
)

tests/unit/test_permission_helper.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from unittest import TestCase
2+
from mock import Mock, patch
3+
4+
import serverlessrepo.permission_helper as permission_helper
5+
from serverlessrepo.application_policy import ApplicationPolicy
6+
from serverlessrepo.exceptions import InvalidApplicationPolicyError
7+
8+
9+
class TestPermissionHelper(TestCase):
10+
11+
def setUp(self):
12+
self.patcher = patch('serverlessrepo.permission_helper.boto3')
13+
self.boto3_mock = self.patcher.start()
14+
self.serverlessrepo_mock = Mock()
15+
self.boto3_mock.client.return_value = self.serverlessrepo_mock
16+
self.application_id = 'arn:aws:serverlessrepo:us-east-1:123456789012:applications/test-app'
17+
self.account_ids = ['123456789012']
18+
19+
def test_make_application_public_succeeded(self):
20+
permission_helper.make_application_public(self.application_id)
21+
self.serverlessrepo_mock.put_application_policy.assert_called_with(
22+
ApplicationId=self.application_id,
23+
Statements=[{
24+
'Principals': ['*'],
25+
'Actions': [ApplicationPolicy.DEPLOY]
26+
}]
27+
)
28+
29+
def test_make_application_public_exception_with_empty_application_id(self):
30+
with self.assertRaises(ValueError) as context:
31+
permission_helper.make_application_public('')
32+
33+
message = str(context.exception)
34+
expected = 'Require application id to make the app public'
35+
self.assertEqual(expected, message)
36+
37+
def test_make_application_private_succeeded(self):
38+
permission_helper.make_application_private(self.application_id)
39+
self.serverlessrepo_mock.put_application_policy.assert_called_with(
40+
ApplicationId=self.application_id,
41+
Statements=[]
42+
)
43+
44+
def test_make_application_private_exception_with_empty_application_id(self):
45+
with self.assertRaises(ValueError) as context:
46+
permission_helper.make_application_private('')
47+
48+
message = str(context.exception)
49+
expected = 'Require application id to make the app private'
50+
self.assertEqual(expected, message)
51+
52+
def test_share_application_with_accounts_succeeded(self):
53+
permission_helper.share_application_with_accounts(self.application_id, self.account_ids)
54+
self.serverlessrepo_mock.put_application_policy.assert_called_with(
55+
ApplicationId=self.application_id,
56+
Statements=[{
57+
'Principals': self.account_ids,
58+
'Actions': [ApplicationPolicy.DEPLOY]
59+
}]
60+
)
61+
62+
def test_share_application_with_accounts_exception_with_empty_application_id(self):
63+
with self.assertRaises(ValueError) as context:
64+
permission_helper.share_application_with_accounts('', self.account_ids)
65+
66+
message = str(context.exception)
67+
expected = 'Require application id and list of AWS account IDs to share the app'
68+
self.assertEqual(expected, message)
69+
70+
def test_share_application_with_accounts_exception_with_empty_account_ids(self):
71+
with self.assertRaises(ValueError) as context:
72+
permission_helper.share_application_with_accounts(self.application_id, [])
73+
74+
message = str(context.exception)
75+
expected = 'Require application id and list of AWS account IDs to share the app'
76+
self.assertEqual(expected, message)
77+
78+
def test_share_application_with_accounts_exception_with_invalid_account_ids(self):
79+
with self.assertRaises(InvalidApplicationPolicyError) as context:
80+
permission_helper.share_application_with_accounts(self.application_id, ['123', '456'])
81+
82+
message = str(context.exception)
83+
expected = 'principal should be 12-digit AWS account ID or "*"'
84+
self.assertTrue(expected in message)
85+
86+
def tearDown(self):
87+
self.patcher.stop()

0 commit comments

Comments
 (0)