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

Php/http_api #678

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
95b8f17
WIP Use VaaS HTTP API
lennartdohmann Dec 12, 2024
18a2799
WIP Use VaaS HTTP API
lennartdohmann Dec 12, 2024
1e87846
WIP Tests for VaaS HTTP API
lennartdohmann Dec 12, 2024
3137361
WIP Basic tests done
lennartdohmann Dec 12, 2024
536ae08
WIP Fix missing phpspec/prophecy-phpunit
lennartdohmann Dec 12, 2024
8cb1351
WIP Fix tests
lennartdohmann Dec 12, 2024
429d66c
WIP Use hash lookup for tests
lennartdohmann Dec 12, 2024
c428334
WIP Fix examples
lennartdohmann Dec 12, 2024
016e2bf
WIP Fix examples for real
lennartdohmann Dec 12, 2024
d11804b
WIP Fix examples for real for real
lennartdohmann Dec 12, 2024
213a3ed
WIP Fix streaming by adding content-length and add transfer timeout f…
lennartdohmann Dec 13, 2024
8884ceb
WIP Fix tests
lennartdohmann Dec 13, 2024
0bae25c
Apply review requests
lennartdohmann Dec 16, 2024
4c035f5
Add Sha256Test.php to test suite
lennartdohmann Dec 16, 2024
3d009ef
Add logging
lennartdohmann Dec 16, 2024
c6af743
Fix missing dependency for monolog
lennartdohmann Dec 16, 2024
7433afc
Fix monolog version
lennartdohmann Dec 16, 2024
c207c1d
Fix warnings
lennartdohmann Dec 16, 2024
ada45f9
Add Readme.md content
lennartdohmann Dec 16, 2024
f174527
Some little more adjustments
lennartdohmann Dec 16, 2024
f486dd2
Use a VaaS builder
lennartdohmann Dec 16, 2024
aea157e
Update Readme.md
lennartdohmann Dec 16, 2024
73f397b
Update php/examples/Readme.md
lennartdohmann Dec 17, 2024
498815f
Fix location of phpunit for vscode
lennartdohmann Dec 18, 2024
78762ce
Update .gitignore
lennartdohmann Jan 6, 2025
d27d375
refactors the forXXX-options
ata-no-one Dec 18, 2024
4c01528
Refators the Authenntication stuff
ata-no-one Dec 17, 2024
4df943e
Add missing cancellation token
lennartdohmann Dec 18, 2024
3c5158b
update dependencies
ata-no-one Jan 10, 2025
002d743
refactors the options
ata-no-one Dec 18, 2024
a77ab99
Merge pull request #681 from GDATASoftwareAG/php/http_api_authenticat…
lennartdohmann Dec 18, 2024
364a2cc
add php 8.4 to the pipeline
Jan 10, 2025
4da7869
Merge pull request #682 from GDATASoftwareAG/php/http_api_options_pro…
lennartdohmann Dec 19, 2024
fc71e9d
Merge pull request #686 from GDATASoftwareAG/update-dependencies
lennartdohmann Jan 10, 2025
2927e1a
Refactor reviewed stuff
lennartdohmann Jan 14, 2025
46b56be
Authenticators should extend TokenReceiver.php directly
lennartdohmann Jan 20, 2025
ead1756
Add S3 scanner for testing
lennartdohmann Jan 21, 2025
91dbfcc
Rename
lennartdohmann Jan 28, 2025
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
14 changes: 7 additions & 7 deletions .github/workflows/ci-php.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ on:
env:
CLIENT_ID: ${{ secrets.CLIENT_ID }}
CLIENT_SECRET: ${{secrets.CLIENT_SECRET}}
VAAS_URL: "wss://gateway.production.vaas.gdatasecurity.de"
VAAS_URL: "https://gateway.production.vaas.gdatasecurity.de"
TOKEN_URL: "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"
VAAS_CLIENT_ID: ${{ secrets.VAAS_CLIENT_ID }}
VAAS_USER_NAME: ${{ secrets.VAAS_USER_NAME }}
Expand All @@ -41,7 +41,7 @@ jobs:
runs-on: ubuntu-22.04
strategy:
matrix:
version: ["8.1", "8.2", "8.3"]
version: ["8.1", "8.2", "8.3", "8.4"]
steps:
- name: checkout
uses: actions/checkout@v4
Expand All @@ -57,7 +57,7 @@ jobs:
run: |
echo "CLIENT_ID=${{ secrets.STAGING_CLIENT_ID }}" >> $GITHUB_ENV
echo "CLIENT_SECRET=${{ secrets.STAGING_CLIENT_SECRET }}" >> $GITHUB_ENV
echo "VAAS_URL=wss://gateway.staging.vaas.gdatasecurity.de" >> $GITHUB_ENV
echo "VAAS_URL=https://gateway.staging.vaas.gdatasecurity.de" >> $GITHUB_ENV
echo "TOKEN_URL=https://account-staging.gdata.de/realms/vaas-staging/protocol/openid-connect/token" >> $GITHUB_ENV
echo "VAAS_CLIENT_ID=${{ secrets.STAGING_VAAS_CLIENT_ID }}" >> $GITHUB_ENV
echo "VAAS_USER_NAME=${{ secrets.STAGING_VAAS_USER_NAME }}" >> $GITHUB_ENV
Expand All @@ -68,7 +68,7 @@ jobs:
run: |
echo "CLIENT_ID=${{ secrets.DEVELOP_CLIENT_ID }}" >> $GITHUB_ENV
echo "CLIENT_SECRET=${{ secrets.DEVELOP_CLIENT_SECRET }}" >> $GITHUB_ENV
echo "VAAS_URL=wss://gateway.develop.vaas.gdatasecurity.de" >> $GITHUB_ENV
echo "VAAS_URL=https://gateway.develop.vaas.gdatasecurity.de" >> $GITHUB_ENV
echo "TOKEN_URL=https://account-staging.gdata.de/realms/vaas-develop/protocol/openid-connect/token" >> $GITHUB_ENV
echo "VAAS_CLIENT_ID=${{ secrets.DEVELOP_VAAS_CLIENT_ID }}" >> $GITHUB_ENV
echo "VAAS_USER_NAME=${{ secrets.DEVELOP_VAAS_USER_NAME }}" >> $GITHUB_ENV
Expand All @@ -82,12 +82,12 @@ jobs:
- name: install php dependencies
uses: php-actions/composer@v6
with:
working_dir: php/tests/vaas
working_dir: php/tests/VaasTesting
php_version: ${{ matrix.version }}

- name: run tests
run: ./vendor/bin/phpunit --colors --testdox
working-directory: php/tests/vaas
run: ./vendor/bin/phpunit --colors --testdox --exclude-group exclude
working-directory: php/tests/VaasTesting

- name: install example requirements
run: composer install
Expand Down
2 changes: 1 addition & 1 deletion php/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
*.lock
package.xml
package.xml
2 changes: 1 addition & 1 deletion php/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"phpunit.phpunit": "/workspaces/vaas/php/tests/vaas/vendor/bin/phpunit",
"phpunit.phpunit": "/workspaces/vaas/php/tests/VaasTesting/vendor/bin/phpunit",
"phpunit.php": "/usr/local/bin/php",
"php.debug.ideKey": "vsc",
"php.debug.executablePath": "/usr/local/bin/php",
Expand Down
2 changes: 1 addition & 1 deletion php/examples/Readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Examples

You find some examples for *VaaS* interagtions in this folder. All examples or in a research or MVP state. None of them is production ready or intended to ever be.
You find some examples for *VaaS* integrations in this folder. All examples or in a research or MVP state. None of them is production ready or intended to ever be.
65 changes: 22 additions & 43 deletions php/examples/VaasExample/AuthenticationExamples.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,33 @@
namespace VaasExamples;

use VaasSdk\Authentication\ClientCredentialsGrantAuthenticator;
use VaasSdk\Exceptions\InvalidSha256Exception;
use VaasSdk\Exceptions\TimeoutException;
use VaasSdk\Exceptions\VaasAuthenticationException;
use VaasSdk\ResourceOwnerPasswordGrantAuthenticator;
use VaasSdk\Sha256;
use VaasSdk\Vaas;

$USE_RESOURCE_OWNER_PASSWORD_GRANT_AUTHENTICATOR = false;

// If you got a username and password from us, you can use the ResourceOwnerPasswordAuthenticator like this
if ($USE_RESOURCE_OWNER_PASSWORD_GRANT_AUTHENTICATOR){
$authenticator = new ResourceOwnerPasswordGrantAuthenticator(
"vaas-customer",
getenv("VAAS_USER_NAME"),
getenv("VAAS_PASSWORD"),
getenv("TOKEN_URL")
);
}

// $authenticator = new ResourceOwnerPasswordGrantAuthenticator(
// clientId: getenv("CLIENT_ID"),
// username: getenv("USERNAME"),
// password: getenv("PASSWORD"),
// tokenUrl: getenv("TOKEN_URL")
// );
// You may use self registration and create a new username and password for the
// ResourceOwnerPasswordAuthenticator by yourself like the example above on https://vaas.gdata.de/login

// If you got a client id and client secret from us, you can use the ClientCredentialsGrantAuthenticator like this
else{
$authenticator = new ClientCredentialsGrantAuthenticator(
getenv("CLIENT_ID"),
getenv("CLIENT_SECRET"),
getenv("TOKEN_URL")
);
}

$vaas = new Vaas(
getenv("VAAS_URL")
// `Password` authentication method by yourself like the example above on https://vaas.gdata.de/login

// If you got a client id and client secret from us, you can use the `Client Credentials` authentication method like this

$authenticator = new ClientCredentialsGrantAuthenticator(
clientId: getenv("CLIENT_ID"),
clientSecret: getenv("CLIENT_SECRET"),
tokenUrl: getenv("TOKEN_URL")
);

try {
$vaas->Connect($authenticator->getToken());
} catch (VaasAuthenticationException $e) {
fwrite(STDERR, "Authentication failed: " . $e->getMessage() . "\n");
exit(1);
}
$vaas = Vaas::builder()
->withAuthenticator($authenticator)
->build();

// Get verdict for an eicar hash
try {
$vaasVerdict = $vaas->ForSha256("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8");
} catch (InvalidSha256Exception $e) {
fwrite(STDERR, "Invalid sha256: " . $e->getMessage() . "\n");
exit(1);
} catch (TimeoutException $e) {
fwrite(STDERR, "Timeout: " . $e->getMessage() . "\n");
exit(1);
}
fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is $vaasVerdict->Verdict \n");
$vaasVerdict = $vaas->forSha256Async(Sha256::TryFromString("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8"))->await();
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is $vaasVerdict->verdict->value \n");
15 changes: 8 additions & 7 deletions php/examples/VaasExample/GetVerdictByFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@

include_once("./vendor/autoload.php");


$authenticator = new ClientCredentialsGrantAuthenticator(
getenv("CLIENT_ID"),
getenv("CLIENT_SECRET"),
getenv("TOKEN_URL") ?: "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"
clientId: getenv("CLIENT_ID"),
clientSecret: getenv("CLIENT_SECRET"),
tokenUrl: getenv("TOKEN_URL")
);

$vaas = (new Vaas())
$vaas = Vaas::builder()
lennartdohmann marked this conversation as resolved.
Show resolved Hide resolved
->withAuthenticator($authenticator)
->withUrl(getenv("VAAS_URL") ?? "wss://gateway.production.vaas.gdatasecurity.de")
lennartdohmann marked this conversation as resolved.
Show resolved Hide resolved
->build();


$scanPath = getenv("SCAN_PATH");
$vaasVerdict = $vaas->ForFile($scanPath);
$vaasVerdict = $vaas->forFileAsync($scanPath)->await();

fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is " . $vaasVerdict->Verdict->value . " \n");
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is " . $vaasVerdict->verdict->value . " \n");
27 changes: 16 additions & 11 deletions php/examples/VaasExample/GetVerdictByHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@
namespace VaasExamples;

use VaasSdk\Authentication\ClientCredentialsGrantAuthenticator;
use VaasSdk\Sha256;
use VaasSdk\Vaas;

include_once("./vendor/autoload.php");


$authenticator = new ClientCredentialsGrantAuthenticator(
getenv("CLIENT_ID"),
getenv("CLIENT_SECRET"),
getenv("TOKEN_URL") ?: "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"
clientId: getenv("CLIENT_ID"),
clientSecret: getenv("CLIENT_SECRET"),
tokenUrl: getenv("TOKEN_URL")
);
$vaas = (new Vaas())

$vaas = Vaas::builder()
->withAuthenticator($authenticator)
->withUrl(getenv("VAAS_URL") ?? "wss://gateway.production.vaas.gdatasecurity.de")
lennartdohmann marked this conversation as resolved.
Show resolved Hide resolved
->build();

// EICAR
$vaasVerdict = $vaas->ForSha256("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8");
fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is " . $vaasVerdict->Verdict->value . " \n");
// SOMEFILE
$vaasVerdict = $vaas->ForSha256("70caea443deb0d0a890468f9ac0a9b1187676ba3e66eb60a722b187107eb1ea8");
fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is " . $vaasVerdict->Verdict->value . " \n");

// Malicious hash
$vaasVerdict = $vaas->forSha256Async(Sha256::TryFromString("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8"))->await();
lennartdohmann marked this conversation as resolved.
Show resolved Hide resolved
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is " . $vaasVerdict->verdict->value . " \n");


// Some file
$vaasVerdict = $vaas->forSha256Async(Sha256::TryFromString("70caea443deb0d0a890468f9ac0a9b1187676ba3e66eb60a722b187107eb1ea8"))->await();
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is " . $vaasVerdict->verdict->value . " \n");
23 changes: 13 additions & 10 deletions php/examples/VaasExample/GetVerdictByUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@

include_once("./vendor/autoload.php");


$authenticator = new ClientCredentialsGrantAuthenticator(
getenv("CLIENT_ID"),
getenv("CLIENT_SECRET"),
getenv("TOKEN_URL") ?: "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"
clientId: getenv("CLIENT_ID"),
clientSecret: getenv("CLIENT_SECRET"),
tokenUrl: getenv("TOKEN_URL")
);
$vaas = (new Vaas())

$vaas = Vaas::builder()
->withAuthenticator($authenticator)
->withUrl(getenv("VAAS_URL") ?? "wss://gateway.production.vaas.gdatasecurity.de")
lennartdohmann marked this conversation as resolved.
Show resolved Hide resolved
->build();


// EICAR
$vaasVerdict = $vaas->ForUrl("https://secure.eicar.org/eicar.com");
fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is " . $vaasVerdict->Verdict->value . " \n");
// SOMEFILE
$vaasVerdict = $vaas->ForUrl("https://www.gdatasoftware.com/oem/verdict-as-a-service");
fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is " . $vaasVerdict->Verdict->value . " \n");
$vaasVerdict = $vaas->forUrlAsync("https://secure.eicar.org/eicar.com")->await();
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is " . $vaasVerdict->verdict->value . " \n");

// Some file
$vaasVerdict = $vaas->forUrlAsync("https://www.gdatasoftware.com/oem/verdict-as-a-service")->await();
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is " . $vaasVerdict->verdict->value . " \n");
141 changes: 141 additions & 0 deletions php/examples/VaasExample/ScanS3Bucket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace VaasExamples;

use Aws\Credentials\Credentials;
use Aws\Signature\SignatureV4;
use Dotenv\Dotenv;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use SimpleXMLElement;
use VaasSdk\Authentication\ClientCredentialsGrantAuthenticator;
use VaasSdk\Exceptions\VaasClientException;
use VaasSdk\Options\VaasOptions;
use VaasSdk\Vaas;

include_once("./vendor/autoload.php");

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$CLIENT_ID = getenv("CLIENT_ID");
$CLIENT_SECRET = getenv("CLIENT_SECRET");
$VAAS_URL = getenv("VAAS_URL");
$TOKEN_URL = getenv("TOKEN_URL");
$S3_ACCESS_KEY = getenv("S3_ACCESS_KEY");
$S3_SECRET_KEY = getenv("S3_SECRET_KEY");
$S3_URL = getenv("S3_URL");
$S3_BUCKET = getenv("S3_BUCKET");
$S3_REGION = getenv("S3_REGION");

// Build VaaS
$authenticator = new ClientCredentialsGrantAuthenticator(
clientId: $CLIENT_ID,
clientSecret: $CLIENT_SECRET,
tokenUrl: $TOKEN_URL
);
$vaasOptions = new VaasOptions(
useHashLookup: true,
useCache: false,
vaasUrl: $VAAS_URL,
timeout: 300
);
try {
$vaas = Vaas::builder()
->withOptions($vaasOptions)
->withAuthenticator($authenticator)
->build();
} catch (VaasClientException $e) {
fwrite(STDERR, "Error: " . $e->getMessage() . "\n");
exit(1);
}

// List S3 bucket
$client = new Client();
$request = new Request("GET", "$S3_URL/$S3_BUCKET?list-type=2");
$credentials = new Credentials($S3_ACCESS_KEY, $S3_SECRET_KEY);
$signer = new SignatureV4("s3", $S3_REGION);
$signedRequest = $signer->signRequest($request, $credentials);
$keys = [];
try {
$response = $client->send($signedRequest);
$xml = new SimpleXMLElement($response->getBody()->getContents());
foreach ($xml->Contents as $content) {
$keys[] = (string)$content->Key;
}
} catch (GuzzleException $e) {
fwrite(STDERR, "Error: " . $e->getMessage() . "\n");
exit(1);
} catch (Exception $e) {
fwrite(STDERR, "Error: " . $e->getMessage() . "\n");
exit(1);
}

// Iterate over everything in S3 bucket and scan with VaaS
$results = [];
$progress = 0;
$count = count($keys);
$startTimeTotal = microtime(true);
foreach ($keys as $key){
// Pretty print progress
$progress++;
$percentageDone = number_format($progress / $count * 100, 1) . "%";
echo chr(27).chr(91).'H'.chr(27).chr(91).'J';
echo "\nProgress: $percentageDone [";
$done = $progress / $count * 30;
for ($i = 0; $i < 30; $i++) {
echo $i < $done ? "=" : " ";
}
echo "]\n";
echo "Execution time: " . number_format(microtime(true) - $startTimeTotal, 3) . "s\n";
echo "Current key: $key\n\n";

// Download file from S3 to temp file
$request = new Request("GET", "$S3_URL/$S3_BUCKET/$key");
$request->withHeader("Accept", "application/octet-stream");
$credentials = new Credentials($S3_ACCESS_KEY, $S3_SECRET_KEY);
$signer = new SignatureV4("s3", $S3_REGION);
$signedRequest = $signer->signRequest($request, $credentials);
try {
$response = $client->send($signedRequest);
} catch (GuzzleException $e) {
fwrite(STDERR, "Error: " . $e->getMessage() . "\n");
exit(1);
}
$sample = tempnam(sys_get_temp_dir(), "vaas-sample-");
$handle = fopen($sample, "w");
fwrite($handle, $response->getBody());
fclose($handle);

// Scan file with VaaS and track time
$startTime = microtime(true);
$vaasVerdict = $vaas->forFileAsync($sample)->await();
$endTime = microtime(true);
$executionTime = ($endTime - $startTime) * 1000;

// Save VaaS verdict and execution time
$results[] = [
"key" => $key,
"executionTimeInMs" => number_format($executionTime, 3),
"verdict" => [
"sha256" => $vaasVerdict->sha256,
"verdict" => $vaasVerdict->verdict->value,
"detection" => $vaasVerdict->detection,
"fileType" => $vaasVerdict->fileType,
"mimeType" => $vaasVerdict->mimeType
]
];

// Delete temp file
unlink($sample);
}

$endTimeTotal = microtime(true);
$executionTime = number_format($endTimeTotal - $startTimeTotal, 3);

file_put_contents("results-$S3_BUCKET.json", json_encode($results, JSON_PRETTY_PRINT));

echo "Results written to results.json\n";
echo "Total execution time: " . $executionTime . "s\n";
Loading