Skip to content

Commit

Permalink
fix(sensor_download): add file operation support to module
Browse files Browse the repository at this point in the history
Fixes #481

This PR introduces the built-in files AnsibleModule helper features to manage
file operations such as mode, owner, group, etc.
  • Loading branch information
carlosmmatos committed Apr 6, 2024
1 parent ba37c6f commit 642b462
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions plugins/modules/sensor_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
required: false
extends_documentation_fragment:
- files
- crowdstrike.falcon.credentials
- crowdstrike.falcon.credentials.auth
Expand All @@ -65,6 +66,13 @@
hash: "1234567890123456789012345678901234567890123456789012345678901234"
dest: "/tmp/windows"
name: falcon-sensor.exe
- name: Download the Falcon Sensor Installer to a temporary directory and set permissions
crowdstrike.falcon.sensor_download:
hash: "1234567890123456789012345678901234567890123456789012345678901234"
mode: "0755"
owner: "root"
group: "root"
"""

RETURN = r"""
Expand Down Expand Up @@ -112,10 +120,18 @@ def argspec():
return args


def update_permissions(module, changed, path):
"""Update the permissions on the file if needed."""
file_args = module.load_file_common_arguments(module.params, path=path)

return module.set_fs_attributes_if_different(file_args, changed=changed)


def main():
"""Entry point for module execution."""
module = AnsibleModule(
argument_spec=argspec(),
add_file_common_args=True,
supports_check_mode=True,
)

Expand Down Expand Up @@ -165,9 +181,15 @@ def main():
# Compare sha256 hashes to see if any changes have been made
dest_hash = module.sha256(path)
if dest_hash == sensor_hash:
# File already exists and is the same
# File already exists and content is the same. Update permissions if needed.
msg = "File already exists and content is the same."

if update_permissions(module, result["changed"], path):
msg += " Permissions were updated."
result.update(changed=True)

module.exit_json(
msg="File already exists and content is the same.",
msg=msg,
path=path,
**result,
)
Expand All @@ -193,6 +215,9 @@ def main():
with open(path, "wb") as save_file:
save_file.write(download)

# Set permissions on the file
update_permissions(module, result["changed"], path)

result.update(path=path)
module.exit_json(**result)
else:
Expand Down

0 comments on commit 642b462

Please sign in to comment.