Skip to content

Commit

Permalink
Add Default Cleanup Action to fetch_uri and examples TTPs (#525)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #525

Used `remove_path` action to implement default cleanup action for `fetch_uri` that removes the downloaded file.

Also added `basic.yaml` and `proxy.yaml` examples under `example-ttps/actions/fetchuri`.

Reviewed By: RoboticPrism

Differential Revision: D69863376

fbshipit-source-id: 26721c688d0aba4550ac5f856f144c8445c14c64
  • Loading branch information
d0n601 authored and facebook-github-bot committed Feb 19, 2025
1 parent 427338f commit a8cd351
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
11 changes: 11 additions & 0 deletions example-ttps/actions/fetchuri/basic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
api_version: 2.0
uuid: fe7f257f-19d2-4324-8590-bfddeb1156e2
name: fetch_uri_basic_example
description: |
This TTP shows you how to use the fetch action type fetch files with an http request.
steps:
- name: Fetch Something
fetch_uri: https://raw.githubusercontent.com/facebookincubator/TTPForge/refs/heads/main/README.md
location: /tmp/ttpforge_fetch_uri_file_{{randAlphaNum 10}}
cleanup: default
12 changes: 12 additions & 0 deletions example-ttps/actions/fetchuri/proxy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
api_version: 2.0
uuid: 81af6d8f-0da9-49cd-8fff-8edd4adc7bfe
name: fetch_uri_proxy_example
description: |
This TTP shows you how to use the fetch action type fetch files with an http request through an http proxy.
steps:
- name: Fetch Something Through Proxy
fetch_uri: https://raw.githubusercontent.com/facebookincubator/TTPForge/refs/heads/main/README.md
location: /tmp/ttpforge_fetch_uri_file_{{randAlphaNum 10}}
proxy: http://localhost:8080
cleanup: default
19 changes: 10 additions & 9 deletions pkg/blocks/fetchuri.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,17 @@ func (f *FetchURIStep) Validate(execCtx TTPExecutionContext) error {
// Execute runs the step and returns an error if one occurs.
func (f *FetchURIStep) Execute(execCtx TTPExecutionContext) (*ActResult, error) {
logging.L().Info("========= Executing ==========")

logging.L().Infof("FetchURI: %s", f.FetchURI)
if err := f.fetchURI(execCtx); err != nil {
logging.L().Error(zap.Error(err))
return nil, err
}

logging.L().Info("========= Result ==========")

logging.L().Infof("Fetched URI to location: %s", f.Location)
return &ActResult{}, nil
}

// Cleanup is a method to establish a link with the Cleanup interface.
// Assumes that the type is the cleanup step and is invoked by
// f.CleanupStep.Cleanup.
func (f *FetchURIStep) Cleanup(execCtx TTPExecutionContext) (*ActResult, error) {
return f.Execute(execCtx)
}

// fetchURI executes the FetchURIStep with the specified Location, Uri, and additional arguments,
// and an error if any errors occur.
func (f *FetchURIStep) fetchURI(execCtx TTPExecutionContext) error {
Expand Down Expand Up @@ -181,3 +174,11 @@ func (f *FetchURIStep) fetchURI(execCtx TTPExecutionContext) error {

return nil
}

// GetDefaultCleanupAction will instruct the calling code
// to remove the file fetched by this action.
func (s *FetchURIStep) GetDefaultCleanupAction() Action {
return &RemovePathAction{
Path: s.Location,
}
}

0 comments on commit a8cd351

Please sign in to comment.