Skip to content

Commit

Permalink
Updated SC Files method as per #762
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveMcGrath committed May 14, 2024
1 parent 64f0f5a commit 1af134f
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 19 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ python-box>=4.0
defusedxml>=0.5.0
urllib3==1.26.18
typing-extensions>=3.10.0.2
requests-toolbelt==1.0.0
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@
'urllib3>=1.26.18',
'typing-extensions>=4.0.1',
'dataclasses>=0.8;python_version=="3.6"',
'requests-toolbelt>=1.0.0'
],
)
17 changes: 12 additions & 5 deletions tenable/sc/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
.. autoclass:: FileAPI
:members:
'''
from requests_toolbelt.multipart.encoder import MultipartEncoder
from .base import SCEndpoint

class FileAPI(SCEndpoint):
_path = 'file'
_box = True

def upload(self, fobj):
'''
Uploads a file into SecurityCenter and returns the file identifier
Expand All @@ -29,8 +33,11 @@ def upload(self, fobj):
The filename identifier to use for subsequent calls in
Tenable Security Center.
'''
return self._api.post('file/upload', files={
'Filedata': fobj}).json()['response']['filename']
encoder = MultipartEncoder({'Filedata': ('filedata', fobj)})
return self._post('upload',
data=encoder,
headers={'Content-Type': encoder.content_type}
).response.filename

def clear(self, filename):
'''
Expand All @@ -45,6 +52,6 @@ def clear(self, filename):
:obj:`str`:
The file location on disk that was removed.
'''
return self._api.post('file/clear', json={
'filename': self._check('filename', filename, str)
}).json()['response']['filename']
return self._post('clear',
json={'filename': filename}
).response.filename
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ docker>=3.7.2
certifi>=2023.7.22 # not directly required, pinned by Snyk to avoid a vulnerability
requests>=2.31.0 # not directly required, pinned by Snyk to avoid a vulnerability
black>=24.3.0 # not directly required, pinned by Snyk to avoid a vulnerability
requests-toolbelt==1.0.0
2 changes: 1 addition & 1 deletion tests/sc/cassettes/sc_login.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interactions:
"enabled":"true","attributeSets":[]},{"name":"CSV","type":"csv","enabled":"true","attributeSets":[]},
{"name":"RTF","type":"rtf","enabled":"true","attributeSets":[]},{"name":"DISA ARF","type":"arf","enabled":"false",
"attributeSets":["arf"]},{"name":"DISA ASR","type":"asr","enabled":"false","attributeSets":[]},
{"name":"CyberScope","type":"lasr","enabled":"true","attributeSets":["lasr"]}],"version":"5.8.0",
{"name":"CyberScope","type":"lasr","enabled":"true","attributeSets":["lasr"]}],"version":"6.4.0",
"buildID":"201810303290","banner":"","releaseID":"201810303290","uuid":"3ae050e2-486c-5c1a-a6a5-45305557cec3",
"logo":"assets\/mariana\/images\/sc4icon.png","serverAuth":"any","serverClassification":"None",
"sessionTimeout":"3600","licenseStatus":"Valid","ACAS":"false","freshInstall":"no","headerText":"",
Expand Down
17 changes: 16 additions & 1 deletion tests/sc/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
configuration which would be used for testing sc.
'''
import os

import responses
import pytest

from tenable.errors import APIError, NotFoundError
Expand Down Expand Up @@ -112,3 +112,18 @@ def teardown():

request.addfinalizer(teardown)
return grp


@pytest.fixture
def tsc():
with responses.RequestsMock() as rsps:
rsps.get('https://nourl/rest/system',
json={
'error_code': None,
'response': {'version': '6.4.0'}
}
)
return TenableSC(url='https://nourl',
access_key='SOMETHING',
secret_key='SECRET'
)
31 changes: 19 additions & 12 deletions tests/sc/test_files.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
'''
test file for testing various scenarios in file functionality
'''
import os

from io import BytesIO
import responses
import pytest


@pytest.mark.vcr()
def test_files_upload_clear_success(security_center):
'''
test files upload and clear for success
'''
with open('1000003.xml', 'w+') as file:
response = security_center.files.upload(fobj=file)
filename = security_center.files.clear(response)
assert filename == 'filename'
os.remove('1000003.xml')
@responses.activate
def test_files_upload(tsc):
fake = BytesIO(b'Test File')
responses.post('https://nourl/rest/file/upload',
json={
'error_code': '',
'response': {'filename': 'something'}
},
)

assert 'something' == tsc.files.upload(fake)

#with open('1000003.xml', 'w+') as file:
# response = security_center.files.upload(fobj=file)
# filename = security_center.files.clear(response)
# assert filename == 'filename'
#os.remove('1000003.xml')

0 comments on commit 1af134f

Please sign in to comment.