Skip to content

Commit f613228

Browse files
committed
Added the ability to map to the account the asset is tied to #44 #43
1 parent 5e55961 commit f613228

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

tenable_aws_sechub/finding.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
AWS Security Hub finding.
44
"""
55

6-
from typing import Dict
6+
import logging
7+
from typing import Dict, List
78

89
import arrow
910
from restfly.utils import dict_clean, dict_flatten, trunc
1011

12+
log = logging.getLogger('sechub.finding')
1113
SEV_MAP = {0: 0, 1: 3, 2: 5, 3: 7, 4: 10}
1214
STATE_MAP = {
1315
'OPEN': 'ACTIVE',
@@ -24,16 +26,25 @@ class Finding:
2426

2527
region: str
2628
account_id: str
29+
allowed_accounts: List[str] | None = None
2730
start_date: str
2831
map_to_asset_account: bool
2932

3033
def __init__(
31-
self, region: str, account_id: str, map_to_asset_account: bool = False
34+
self,
35+
region: str,
36+
account_id: str,
37+
map_to_asset_account: bool = False,
38+
allowed_accounts: List[str] | None = None,
3239
):
3340
self.region = region
3441
self.account_id = account_id
3542
self.map_to_asset_account = map_to_asset_account
3643
self.start_date = arrow.now().isoformat()
44+
if allowed_accounts:
45+
self.allowed_accounts = allowed_accounts
46+
elif map_to_asset_account and not allowed_accounts:
47+
self.allowed_accounts = [account_id]
3748

3849
def check_required_params(self, vuln: Dict):
3950
"""
@@ -64,6 +75,15 @@ def check_required_params(self, vuln: Dict):
6475
f' were not set on asset {vuln["asset.uuid"]}'
6576
)
6677
)
78+
if (
79+
self.allowed_accounts
80+
and vuln.get('asset.aws_owner_id') not in self.allowed_accounts
81+
and self.map_to_asset_account
82+
):
83+
raise KeyError(
84+
f'asset {vuln["asset.aws_owner_id"]}:{vuln["asset.uuid"]} is not within'
85+
' one of the allowed accounts.'
86+
)
6787

6888
def generate(self, vuln: Dict) -> Dict:
6989
"""

tenable_aws_sechub/transform.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def __init__(self, config: Dict):
6060
region=config.get('aws_region', self.aws.meta.region_name),
6161
account_id=str(config['aws_account_id']),
6262
map_to_asset_account=config.get('map_to_asset_account', False),
63+
allowed_accounts=config.get('allowed_accounts'),
6364
)
6465
self._log = logging.getLogger('Tenb2SecHub')
6566

tenable_aws_sechub/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = '2.0.8'
1+
version = '2.1.0'

tests/test_finding.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_finding_required_typerror(finding_local):
2222
'01234567-1234-abcd-0987-01234567890a'
2323
),
2424
):
25-
resp = f.check_required_params(finding_local)
25+
f.check_required_params(finding_local)
2626

2727

2828
def test_finding_generate_finding_keyerror(finding_local, finding_aws):
@@ -33,11 +33,20 @@ def test_finding_generate_finding_keyerror(finding_local, finding_aws):
3333
"""
3434
f = Finding('', '')
3535
with pytest.raises(KeyError):
36-
resp = f.generate(finding_local)
36+
f.generate(finding_local)
3737

3838
del finding_aws['plugin.type']
3939
with pytest.raises(KeyError, match='plugin.type'):
40-
resp = f.generate(finding_aws)
40+
f.generate(finding_aws)
41+
42+
43+
def test_finding_account_restrictions(finding_aws):
44+
f = Finding('', '600832220000', True)
45+
f.generate(finding_aws)
46+
47+
f = Finding('', 'ACCOUNT-ID', True)
48+
with pytest.raises(KeyError, match='s not within one of the allowed accounts'):
49+
f.generate(finding_aws)
4150

4251

4352
def test_finding_generate_finding_success(finding_aws):
@@ -56,7 +65,7 @@ def test_finding_generate_finding_success(finding_aws):
5665
assert resp['LastObservedAt'] == '2018-12-14T12:07:38.155Z'
5766
assert (
5867
resp['ProductArn']
59-
== 'arn:aws:securityhub:AWS-REGION-1::product/tenable/tenable-io'
68+
== 'arn:aws:securityhub:AWS-REGION-1::product/tenable/vulnerability-management'
6069
)
6170
assert resp['AwsAccountId'] == 'ACCOUNT-ID'
6271
assert resp['GeneratorId'] == 'tenable-plugin-106875'
@@ -82,7 +91,7 @@ def test_finding_generate_finding_success(finding_aws):
8291
assert resp['Resources'][0]['Details']['AwsEc2Instance']['IpV4Addresses'] == [
8392
'192.168.101.249'
8493
]
85-
assert resp['Resources'][0]['Details']['AwsEc2Instance']['IpV6Addresses'] == []
94+
assert 'IpV6Addresses' not in resp['Resources'][0]['Details']['AwsEc2Instance']
8695
assert resp['ProductFields']['CVE'] == ''
8796
assert resp['ProductFields']['Plugin Family'] == 'Debian Local Security Checks'
8897
assert resp['ProductFields']['Type'] == 'local'

0 commit comments

Comments
 (0)