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

Rust example 1 0 0 #488

Merged
merged 13 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions .github/workflows/ci-rust-gscan-cli.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: vaas-rust-ci
on:
push:
branches:
- main
- rust_example_1_0_0
paths:
- "rust/example/gdscan/**"
- ".github/workflows/ci-rust-gscan-cli.yaml"
pull_request:
branches:
- main
- rust_example_1_0_0
paths:
- "rust/example/gdscan/**"
- ".github/workflows/ci-rust-gscan-cli.yaml"
release:
types: [created]


jobs:
release:
name: release ${{ matrix.target }}
runs-on: ubuntu-22.04
steps:
- name: checkout
uses: actions/checkout@v4

- name: Scan for Viruses
uses: ./.github/actions/vaas-scan-action
with:
VAAS_CLIENT_ID: ${{ secrets.VAAS_SCAN_CLIENT_ID }}
VAAS_CLIENT_SECRET: ${{ secrets.VAAS_SCAN_CLIENT_SECRET }}

- uses: actions/checkout@master
- name: build
id: build
uses: rust-build/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
SRC_DIR: rust/examples/gscan
RUSTTARGET: x86_64-pc-windows-gnu
EXTRA_FILES: "Readme.md"
UPLOAD_MODE: none
ARCHIVE_NAME: "gscan.zip"

- name: Github Release
if: startsWith(github.ref, 'refs/tags/gscan')
uses: softprops/action-gh-release@v2
with:
files: ${{ steps.build.outputs.BUILT_ARCHIVE }}

- name: Attach file to release
if: startsWith(github.ref, 'refs/tags/gscan')
uses: svenstaro/upload-release-action@v2
id: attach_to_release
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: ${{ steps.build.outputs.BUILT_ARCHIVE }}
asset_name: gscan.zip
tag: ${{ github.ref }}
overwrite: true

- name: Microsoft Teams Notification
uses: skitionek/notify-microsoft-teams@master
if: failure()
with:
webhook_url: ${{ secrets.MSTEAMS_WEBHOOK }}
overwrite: "{title: `Failed workflow on for VaaS-SDK ${workflow}`, sections: [{activityTitle: 'build failed', activitySubtitle: `Failed workflow on for VaaS-SDK ${workflow}`, activityImage: 'https://adaptivecards.io/content/cats/3.png'}], themeColor: '#ff0000'}"
10 changes: 5 additions & 5 deletions rust/examples/gscan/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[package]
name = "gscan"
version = "0.1.0"
version = "1.0.0"
edition = "2021"
authors = ["GDATA CyberDefense AG <[email protected]>"]
license = "MIT"
description = "GDATA Verdict-as-a-Service CLI Scanner"
publish = false

[dependencies]
vaas = { version = "5.0.0" }
tokio = { version = "1.37", features = [ "rt-multi-thread", "macros"] }
clap = { version = "4.5.4", features = ["env"]}
vaas = { version = "6.0.0" }
tokio = { version = "1.37", features = ["rt-multi-thread", "macros"] }
clap = { version = "4.5.4", features = ["env", "cargo"] }
reqwest = "0.12.4"
futures = "0.3.30"
dotenv = "0.15"
dotenv = "0.15"
82 changes: 41 additions & 41 deletions rust/examples/gscan/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
use clap::{Arg, ArgAction, Command};
use clap::{crate_authors, crate_description, crate_name, crate_version, Arg, ArgAction, Command};
use reqwest::Url;
use std::{collections::HashMap, path::PathBuf, str::FromStr};
use vaas::{error::VResult, CancellationToken, Vaas, VaasVerdict};
use vaas::{
auth::authenticators::ClientCredentials, error::VResult, CancellationToken, Connection, Vaas,
VaasVerdict,
};

#[tokio::main]
async fn main() -> VResult<()> {
let matches = Command::new("GDATA command line scanner")
.version("0.1.0")
.author("GDATA CyberDefense AG")
.about("Scan files for malicious content")
let matches = Command::new(crate_name!())
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!())
.arg(
Arg::new("files")
.short('f')
.long("files")
.required_unless_present("urls")
.action(ArgAction::Append)
.help("List of files to scan spearated by whitepace"),
.help("List of files to scan separated by whitepace"),
)
.arg(
Arg::new("urls")
.short('u')
.long("urls")
.action(ArgAction::Append)
.required_unless_present("files")
.help("List of urls to scan spearated by whitepace"),
.help("List of urls to scan separated by whitepace"),
)
.arg(
Arg::new("client_id")
Expand Down Expand Up @@ -55,62 +58,59 @@ async fn main() -> VResult<()> {
.map(|f| Url::parse(f).unwrap_or_else(|_| panic!("Not a valid url: {}", f)))
.collect::<Vec<Url>>();

let client_id = matches.get_one::<String>("client_id").unwrap();
let client_secret = matches.get_one::<String>("client_secret").unwrap();
let client_id = matches
.get_one::<String>("client_id")
.expect("--client_id or the enviroment variable CLIENT_ID must be set");
let client_secret = matches
.get_one::<String>("client_secret")
.expect("--client_secret or the enviroment variable CLIENT_SECRET must be set");

let token = Vaas::get_token(client_id, client_secret).await?;
let authenticator = ClientCredentials::new(client_id.to_owned(), client_secret.to_owned());
let vaas_connection = Vaas::builder(authenticator).build()?.connect().await?;

let file_verdicts = scan_files(&files, &token).await?;
let url_verdicts = scan_urls(&urls, &token).await?;
let file_verdicts = scan_files(&files, &vaas_connection).await?;
let url_verdicts = scan_urls(&urls, &vaas_connection).await?;

file_verdicts.iter().for_each(|(f, v)| {
println!(
"File: {:?} -> {}",
f,
match v {
Ok(v) => v.verdict.to_string(),
Err(e) => e.to_string(),
}
)
});
file_verdicts
.iter()
.for_each(|(f, v)| print_verdicts(f.display().to_string(), v));

url_verdicts.iter().for_each(|(u, v)| {
println!(
"Url: {:?} -> {}",
u.to_string(),
match v {
Ok(v) => v.verdict.to_string(),
Err(e) => e.to_string(),
}
)
});
url_verdicts.iter().for_each(|(u, v)| print_verdicts(u, v));

Ok(())
}

fn print_verdicts<I: AsRef<str>>(i: I, v: &VResult<VaasVerdict>) {
print!("{} -> ", i.as_ref());
match v {
Ok(v) => {
println!("{}", v.verdict);
}
Err(e) => {
println!("{}", e.to_string());
pstadermann marked this conversation as resolved.
Show resolved Hide resolved
}
};
}

async fn scan_files<'a>(
files: &'a [PathBuf],
token: &str,
vaas_connection: &Connection,
) -> VResult<Vec<(&'a PathBuf, VResult<VaasVerdict>)>> {
let vaas = Vaas::builder(token.into()).build()?.connect().await?;

let ct = CancellationToken::from_minutes(1);
let verdicts = vaas.for_file_list(files, &ct).await;
let verdicts = vaas_connection.for_file_list(files, &ct).await;
let results = files.iter().zip(verdicts).collect();

Ok(results)
}

async fn scan_urls(
urls: &[Url],
token: &str,
vaas_connection: &Connection,
) -> VResult<HashMap<Url, Result<VaasVerdict, vaas::error::Error>>> {
let vaas = Vaas::builder(token.into()).build()?.connect().await?;

let ct = CancellationToken::from_minutes(1);
let mut verdicts = HashMap::new();
for url in urls {
let verdict = vaas.for_url(url, &ct).await;
let verdict = vaas_connection.for_url(url, &ct).await;
verdicts.insert(url.to_owned(), verdict);
}

Expand Down