Skip to content

Commit

Permalink
Add Sanitize examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pangea-andrest authored and kenany committed Oct 20, 2024
1 parent 2ad55bc commit edbc0e3
Show file tree
Hide file tree
Showing 17 changed files with 679 additions and 9 deletions.
6 changes: 4 additions & 2 deletions examples/.examples-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ go-sdk-examples:
- file_scan
- intel
- redact
- vault
- sanitize
- share
- vault
image: golang:${GO_VERSION}
before_script:
- export PANGEA_AUDIT_CONFIG_ID="${PANGEA_AUDIT_CONFIG_ID_1_LVE_AWS}"
Expand All @@ -32,10 +33,11 @@ go-sdk-examples:
- export PANGEA_REDACT_CONFIG_ID="${PANGEA_REDACT_CONFIG_ID_1_LVE_AWS}"
- export PANGEA_REDACT_MULTICONFIG_TOKEN="${PANGEA_INTEGRATION_MULTI_CONFIG_TOKEN_LVE_AWS}"
- export PANGEA_REDACT_TOKEN="${PANGEA_INTEGRATION_TOKEN_LVE_AWS}"
- export PANGEA_SANITIZE_TOKEN="${PANGEA_INTEGRATION_TOKEN_LVE_AWS}"
- export PANGEA_SHARE_TOKEN="${PANGEA_INTEGRATION_TOKEN_LVE_AWS}"
- export PANGEA_URL_INTEL_TOKEN="${PANGEA_INTEGRATION_TOKEN_LVE_AWS}"
- export PANGEA_USER_INTEL_TOKEN="${PANGEA_INTEGRATION_TOKEN_LVE_AWS}"
- export PANGEA_VAULT_TOKEN="${PANGEA_INTEGRATION_TOKEN_LVE_AWS}"
- export PANGEA_SHARE_TOKEN="${PANGEA_INTEGRATION_TOKEN_LVE_AWS}"
script:
- cd examples/${EXAMPLE_FOLDER}
- bash ../../dev/run_examples.sh
Expand Down
14 changes: 14 additions & 0 deletions examples/sanitize/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module examples/sanitize

go 1.22

require github.com/pangeacyber/pangea-go/pangea-sdk/v4 v4.1.0

require (
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/rs/zerolog v1.33.0 // indirect
golang.org/x/sys v0.26.0 // indirect
)
35 changes: 35 additions & 0 deletions examples/sanitize/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k=
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU=
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pangeacyber/pangea-go/pangea-sdk/v4 v4.1.0 h1:GbwAZyhNqUph/n/tvxaCyna+sVOojJsiXFe7PkbR7t8=
github.com/pangeacyber/pangea-go/pangea-sdk/v4 v4.1.0/go.mod h1:5XIW52PITcGiggsoSVoFstTlF/5cU51sywvBD5gB6pk=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
106 changes: 106 additions & 0 deletions examples/sanitize/sanitize_and_share.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package main

import (
"context"
"fmt"
"log"
"os"
"time"

"github.com/pangeacyber/pangea-go/pangea-sdk/v4/pangea"
"github.com/pangeacyber/pangea-go/pangea-sdk/v4/service/sanitize"
)

func main() {
// Set filePath to your own file
const filePath = "./testdata/ds11.pdf"

// Load Pangea token from environment variables
token := os.Getenv("PANGEA_SANITIZE_TOKEN")
if token == "" {
log.Fatal("Unauthorized: No token present.")
}

ctx, cancelFn := context.WithTimeout(context.Background(), 120*time.Second)
defer cancelFn()

// Create a new Sanitize client with Pangea token and domain
client := sanitize.New(&pangea.Config{
Token: token,
Domain: os.Getenv("PANGEA_DOMAIN"),
QueuedRetryEnabled: true,
PollResultTimeout: 120 * time.Second,
Retry: true,
RetryConfig: &pangea.RetryConfig{
RetryMax: 4,
},
})

file, err := os.Open(filePath)
if err != nil {
log.Fatalf("unexpected error: %v", err)
}

// Get file upload params
params, err := pangea.GetUploadFileParams(file)
if err != nil {
log.Fatalf("unexpected error: %v", err)
}

// Create a SanitizeRequest.
// In this case TransferMethod is set to TMpostURL
// Set CRC32C, SHA256 and Size
input := &sanitize.SanitizeRequest{
Content: &sanitize.SanitizeContent{
URLIntel: pangea.Bool(true),
URLIntelProvider: "crowdstrike",
DomainIntel: pangea.Bool(true),
DomainIntelProvider: "crowdstrike",
Defang: pangea.Bool(true),
DefangThreshold: pangea.Int(20),
RemoveInteractive: pangea.Bool(true),
RemoveAttachments: pangea.Bool(true),
Redact: pangea.Bool(true),
},
// Enable Secure Share output and set output folder
ShareOutput: &sanitize.SanitizeShareOutput{
Enabled: pangea.Bool(true),
OutputFolder: "sdk_test/sanitize/",
},
File: &sanitize.SanitizeFile{
ScanProvider: "crowdstrike",
},
UploadedFileName: "uploaded_file",
TransferRequest: pangea.TransferRequest{
TransferMethod: pangea.TMpostURL,
},
SHA256: params.SHA256,
CRC32C: params.CRC32C,
Size: pangea.Int(params.Size),
}

fmt.Println("Sending Sanitize request...")
resp, err := client.Sanitize(ctx, input, file)
if err != nil {
log.Fatalf("Failed to process Sanitize request. Unexpected error: %v", err.Error())
}

fmt.Println("File Sanitized:")
if resp.Result.DestShareID != nil {
fmt.Printf("\tShare ID: %s\n", *resp.Result.DestShareID)
}
if resp.Result.DestURL != nil {
fmt.Printf("\tDest URL: %s\n", *resp.Result.DestURL)
}

fmt.Printf("\tRedact data: %s\n", pangea.Stringify(resp.Result.Data.Redact))
fmt.Printf("\tDefang data: %s\n", pangea.Stringify(resp.Result.Data.Defang))
fmt.Printf("\tCDR data: %s\n", pangea.Stringify(resp.Result.Data.CDR))

if resp.Result.Data.MaliciousFile {
fmt.Println("File IS malicious")
} else {
fmt.Println("File is NOT malicious")
}

}
96 changes: 96 additions & 0 deletions examples/sanitize/sanitize_multipart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"context"
"fmt"
"log"
"os"
"time"

"github.com/pangeacyber/pangea-go/pangea-sdk/v4/pangea"
"github.com/pangeacyber/pangea-go/pangea-sdk/v4/service/sanitize"
)

func main() {
// Set filePath to your own file
const filePath = "./testdata/ds11.pdf"

// Load Pangea token from environment variables
token := os.Getenv("PANGEA_SANITIZE_TOKEN")
if token == "" {
log.Fatal("Unauthorized: No token present.")
}

ctx, cancelFn := context.WithTimeout(context.Background(), 120*time.Second)
defer cancelFn()

// Create a new Sanitize client with Pangea token and domain
client := sanitize.New(&pangea.Config{
Token: token,
Domain: os.Getenv("PANGEA_DOMAIN"),
QueuedRetryEnabled: true,
PollResultTimeout: 120 * time.Second,
Retry: true,
RetryConfig: &pangea.RetryConfig{
RetryMax: 4,
},
})

file, err := os.Open(filePath)
if err != nil {
log.Fatalf("unexpected error: %v", err)
}

// Create a SanitizeRequest.
// In this case TransferMethod is set to TMmultipartURL
input := &sanitize.SanitizeRequest{
Content: &sanitize.SanitizeContent{
URLIntel: pangea.Bool(true),
URLIntelProvider: "crowdstrike",
DomainIntel: pangea.Bool(true),
DomainIntelProvider: "crowdstrike",
Defang: pangea.Bool(true),
DefangThreshold: pangea.Int(20),
RemoveInteractive: pangea.Bool(true),
RemoveAttachments: pangea.Bool(true),
Redact: pangea.Bool(true),
},
// Enable Secure Share output and set output folder
ShareOutput: &sanitize.SanitizeShareOutput{
Enabled: pangea.Bool(true),
OutputFolder: "sdk_test/sanitize/",
},
File: &sanitize.SanitizeFile{
ScanProvider: "crowdstrike",
},
UploadedFileName: "uploaded_file",
TransferRequest: pangea.TransferRequest{
TransferMethod: pangea.TMmultipart,
},
}

fmt.Println("Sending Sanitize request as multipart...")
resp, err := client.Sanitize(ctx, input, file)
if err != nil {
log.Fatalf("Failed to process Sanitize request. Unexpected error: %v", err.Error())
}

fmt.Println("File Sanitized:")
if resp.Result.DestShareID != nil {
fmt.Printf("\tShare ID: %s\n", *resp.Result.DestShareID)
}
if resp.Result.DestURL != nil {
fmt.Printf("\tDest URL: %s\n", *resp.Result.DestURL)
}

fmt.Printf("\tRedact data: %s\n", pangea.Stringify(resp.Result.Data.Redact))
fmt.Printf("\tDefang data: %s\n", pangea.Stringify(resp.Result.Data.Defang))
fmt.Printf("\tCDR data: %s\n", pangea.Stringify(resp.Result.Data.CDR))

if resp.Result.Data.MaliciousFile {
fmt.Println("File IS malicious")
} else {
fmt.Println("File is NOT malicious")
}

}
105 changes: 105 additions & 0 deletions examples/sanitize/sanitize_no_share.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package main

import (
"context"
"fmt"
"log"
"os"
"time"

"github.com/pangeacyber/pangea-go/pangea-sdk/v4/pangea"
"github.com/pangeacyber/pangea-go/pangea-sdk/v4/service/sanitize"
)

func main() {
// Set filePath to your own file
const filePath = "./testdata/ds11.pdf"

// Load Pangea token from environment variables
token := os.Getenv("PANGEA_SANITIZE_TOKEN")
if token == "" {
log.Fatal("Unauthorized: No token present.")
}

ctx, cancelFn := context.WithTimeout(context.Background(), 120*time.Second)
defer cancelFn()

// create a new sanitize client with pangea token and domain
client := sanitize.New(&pangea.Config{
Token: token,
Domain: os.Getenv("PANGEA_DOMAIN"),
QueuedRetryEnabled: true,
PollResultTimeout: 120 * time.Second,
Retry: true,
RetryConfig: &pangea.RetryConfig{
RetryMax: 4,
},
})

file, err := os.Open(filePath)
if err != nil {
log.Fatalf("unexpected error: %v", err)
}

// Get file upload params
params, err := pangea.GetUploadFileParams(file)
if err != nil {
log.Fatalf("unexpected error: %v", err)
}

// Create a SanitizeRequest.
// In this case TransferMethod is set to TMpostURL
// Set CRC32C, SHA256 and Size
input := &sanitize.SanitizeRequest{
Content: &sanitize.SanitizeContent{
URLIntel: pangea.Bool(true),
URLIntelProvider: "crowdstrike",
DomainIntel: pangea.Bool(true),
DomainIntelProvider: "crowdstrike",
Defang: pangea.Bool(true),
DefangThreshold: pangea.Int(20),
RemoveInteractive: pangea.Bool(true),
RemoveAttachments: pangea.Bool(true),
Redact: pangea.Bool(true),
},
// Disable Secure Share output
ShareOutput: &sanitize.SanitizeShareOutput{
Enabled: pangea.Bool(false),
},
File: &sanitize.SanitizeFile{
ScanProvider: "crowdstrike",
},
UploadedFileName: "uploaded_file",
TransferRequest: pangea.TransferRequest{
TransferMethod: pangea.TMpostURL,
},
SHA256: params.SHA256,
CRC32C: params.CRC32C,
Size: pangea.Int(params.Size),
}

fmt.Println("Sending Sanitize request...")
resp, err := client.Sanitize(ctx, input, file)
if err != nil {
log.Fatalf("Failed to process Sanitize request. Unexpected error: %v", err.Error())
}

fmt.Println("File Sanitized:")
if resp.Result.DestShareID != nil {
fmt.Printf("\tShare ID: %s\n", *resp.Result.DestShareID)
}
if resp.Result.DestURL != nil {
fmt.Printf("\tDest URL: %s\n", *resp.Result.DestURL)
}

fmt.Printf("\tRedact data: %s\n", pangea.Stringify(resp.Result.Data.Redact))
fmt.Printf("\tDefang data: %s\n", pangea.Stringify(resp.Result.Data.Defang))
fmt.Printf("\tCDR data: %s\n", pangea.Stringify(resp.Result.Data.CDR))

if resp.Result.Data.MaliciousFile {
fmt.Println("File IS malicious")
} else {
fmt.Println("File is NOT malicious")
}

}
Loading

0 comments on commit edbc0e3

Please sign in to comment.