Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional folder argument for file uploads to S3 Buckets #208

Merged
merged 4 commits into from
Jan 15, 2025

Conversation

raverburg
Copy link
Contributor

A new feature to add an optional folder argument, allowing users to specify the target location within the S3 bucket for file uploads. This creates flexability for users wanting to specify where acquire uploads the files.

Example config:
{ "arguments": [ ], "public_key": "", "upload": { "mode": "cloud", "endpoint": "s3.amazonaws.com", "access_id": "", "access_key": "", "bucket": "example", "folder":"Evidence/Uploads" } }

This update introduces an optional folder argument, allowing users to specify the target location within the S3 bucket for file uploads.
@DissectBot
Copy link

@raverburg thank you for your contribution! As this is your first code contribution, please read the following Contributor License Agreement (CLA). If you agree with the CLA, please reply with the following information:

@DissectBot agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
Contributor License Agreement

Contribution License Agreement

This Contribution License Agreement ("Agreement") governs your Contribution(s) (as defined below) and conveys certain license rights to Fox-IT B.V. ("Fox-IT") for your Contribution(s) to Fox-IT"s open source Dissect project. This Agreement covers any and all Contributions that you ("You" or "Your"), now or in the future, Submit (as defined below) to this project. This Agreement is between Fox-IT B.V. and You and takes effect when you click an “I Accept” button, check box presented with these terms, otherwise accept these terms or, if earlier, when You Submit a Contribution.

  1. Definitions.
    "Contribution" means any original work of authorship, including any modifications or additions to an existing work, that is intentionally submitted by You to Fox-IT for inclusion in, or documentation of, any of the software products owned or managed by, or on behalf of, Fox-IT as part of the Project (the "Work").
    "Project" means any of the projects owned or managed by Fox-IT and offered under a license approved by the Open Source Initiative (www.opensource.org).
    "Submit" means any form of electronic, verbal, or written communication sent to Fox-IT or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, Fox-IT for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by You as "Not a Contribution."

  2. Grant of Copyright License. Subject to the terms and conditions of this Agreement, You hereby grant to Fox-IT and to recipients of software distributed by Fox-IT a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute Your Contributions and such derivative works.

  3. Grant of Patent License. Subject to the terms and conditions of this Agreement, You hereby grant to Fox-IT and to recipients of software distributed by Fox-IT a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, maintain, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by You that are necessarily infringed by Your Contribution(s) alone or by combination of Your Contribution(s) with the Work to which such Contribution(s) was submitted. If any entity institutes patent litigation against You or any other entity (including a cross-claim or counterclaim in a lawsuit) alleging that your Contribution, or the Work to which you have contributed, constitutes direct or contributory patent infringement, then any patent licenses granted to that entity under this Agreement for that Contribution or Work shall terminate as of the date such litigation is filed.

  4. Representations. You represent that:

    • You are legally entitled to grant the above license.
    • each of Your Contributions is Your original creation (see section 8 for submissions on behalf of others).
    • Your Contribution submissions include complete details of any third-party license or other restriction (including, but not limited to, related patents and trademarks) of which you are personally aware and which are associated with any part of Your Contributions.
  5. Employer. If Your Contribution is made in the course of Your work for an employer or Your employer has intellectual property rights in Your Submission by contract or applicable law, You must secure permission from Your employer to make the Contribution before signing this Agreement. In that case, the term "You" in this Agreement will refer to You and the employer collectively. If You change employers in the future and desire to Submit additional Contribution for the new employer, then You agree to sign a new Agreement and secure permission from the new employer before Submitting those Contributions.

  6. Support. You are not expected to provide support for Your Contribution, unless You choose to do so. Any such support provided to the Project is provided free of charge.

  7. Warranty. Unless required by applicable law or agreed to in writing, You provide Your Contributions on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE.

  8. Third party material. Should You wish to submit work that is not Your original creation, You may only submit it to Fox-IT separately from any Contribution, identifying the complete details of its source and of any license or other restriction (including, but not limited to, related patents, trademarks, and license agreements) of which You are personally aware, and conspicuously marking the work as "Submitted on behalf of a third-party: [named here]".

  9. Notify. You agree to notify Fox-IT of any facts or circumstances of which You become aware that would make the above representations inaccurate in any respect.

  10. Governing law / competent court. This Agreement is governed by the laws of the Netherlands. Any disputes that may arise are resolved by arbitration in accordance with the Arbitration Regulations of the Foundation for the Settlement of Automation Disputes (Stichting Geschillenoplossing Automatisering – SGOA – (www.sgoa.eu), this without prejudice to either party"s right to request preliminary relief in preliminary relief proceedings or arbitral preliminary relief proceedings. Arbitration proceedings take place in Amsterdam, or in any other place designated in the Arbitration Regulations. Arbitration shall take place in English.

@raverburg
Copy link
Contributor Author

@DissectBot agree

Copy link
Contributor

@Miauwkeru Miauwkeru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, seems like a sane feature to add. Could you add some tests that showcase this functionality to tests/test_minio_uploader.py?

@@ -20,6 +20,7 @@ def __init__(self, upload: dict[str, str], **kwargs: dict[str, Any]) -> None:
self.access_id = upload.get("access_id")
self.access_key = upload.get("access_key")
self.bucket_name = upload.get("bucket")
self.folder = upload.get("folder", "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.folder = upload.get("folder", "")
self.folder = upload.get("folder", "").rstrip("/")

Comment on lines 50 to 53
if self.folder:
object_path = f"{self.folder.rstrip('/')}/{os.path.basename(path)}"
else:
object_path = os.path.basename(path)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if self.folder:
object_path = f"{self.folder.rstrip('/')}/{os.path.basename(path)}"
else:
object_path = os.path.basename(path)
object_path = path.name
if self.folder:
object_path = f"{self.folder}/{object_path}"

@raverburg
Copy link
Contributor Author

Thanks for the PR, seems like a sane feature to add. Could you add some tests that showcase this functionality to tests/test_minio_uploader.py?

Done! It's been a while since I last wrote tests, so please feel free to correct me if this is not what you meant. :)

Copy link
Contributor

@Miauwkeru Miauwkeru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, all the return types of the test functions should return a -> None but this file has not changed in a bit, so I won't ask you to do that ;)

After you made the changes, it might be a good idea to run tox run -e fix to fix any formatting issues. It will prevent the scenario that the linting part of our pipeline fails :)

Comment on lines 94 to 95
minio.folder = "Uploads/test_folder"
assert minio.folder == "Uploads/test_folder"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part only tests the assignment of the folder attribute, not what happens during the __init__ of the object. I think this would test the thing you where after

Suggested change
minio.folder = "Uploads/test_folder"
assert minio.folder == "Uploads/test_folder"
arguments["folder"] = "Uploads/test_folder"
minio_no_backslash = minio_plugin(upload=arguments)
assert minio_no_backslash.folder == "Uploads/test_folder"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're totally right, this is what I meant to do ;)

@@ -79,3 +79,46 @@ def test_upload_file_multiple_failures(minio_instance: MinIO):
with patch("acquire.uploaders.plugin.log") as mocked_logger:
upload_files_using_uploader(minio_instance, [Path("hello")])
mocked_logger.error.assert_called_with("Upload %s FAILED after too many attempts. Stopping.", Path("hello"))

def test_minio_folder_initialization(minio_plugin):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_minio_folder_initialization(minio_plugin):
def test_minio_folder_initialization(minio_plugin: Callable) -> None:

Callable should probably be imported at the top first.

assert minio_no_folder.folder == ""


def test_upload_file_with_folder(minio_instance: MinIO):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_upload_file_with_folder(minio_instance: MinIO):
def test_upload_file_with_folder(minio_instance: MinIO) -> None:

Comment on lines 114 to 117
mock_client = Mock()
minio_instance.folder = ""
mock_client = Mock()
test_path = Path("example.txt")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mock_client = Mock()
minio_instance.folder = ""
mock_client = Mock()
test_path = Path("example.txt")
mock_client = Mock()
test_path = Path("example.txt")
minio_instance.folder = ""

for consistency with the other test :)

@Miauwkeru
Copy link
Contributor

Thanks for the PR, seems like a sane feature to add. Could you add some tests that showcase this functionality to tests/test_minio_uploader.py?

Done! It's been a while since I last wrote tests, so please feel free to correct me if this is not what you meant. :)

This is what I meant, thanks for the tests!

Miauwkeru
Miauwkeru previously approved these changes Jan 14, 2025
@Miauwkeru Miauwkeru dismissed their stale review January 14, 2025 15:57

Linting issue

Copy link

codecov bot commented Jan 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 47.42%. Comparing base (c284e87) to head (06eb6c9).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #208      +/-   ##
==========================================
+ Coverage   47.38%   47.42%   +0.04%     
==========================================
  Files          26       26              
  Lines        3438     3441       +3     
==========================================
+ Hits         1629     1632       +3     
  Misses       1809     1809              
Flag Coverage Δ
unittests 47.42% <100.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Miauwkeru
Copy link
Contributor

there is only one more issue, with an unused import :)

@raverburg
Copy link
Contributor Author

there is only one more issue, with an unused import :)

Oops.. should be good now :D

@Miauwkeru Miauwkeru merged commit c134ca5 into fox-it:main Jan 15, 2025
16 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants