Skip to content

Commit f26a362

Browse files
Merge pull request #678 from GDATASoftwareAG/php/http_api
Php/http_api
2 parents e0718ea + 1dbab31 commit f26a362

File tree

70 files changed

+1675
-2062
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1675
-2062
lines changed

.github/workflows/ci-php.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ on:
2929
env:
3030
CLIENT_ID: ${{ secrets.CLIENT_ID }}
3131
CLIENT_SECRET: ${{secrets.CLIENT_SECRET}}
32-
VAAS_URL: "wss://gateway.production.vaas.gdatasecurity.de"
32+
VAAS_URL: "https://gateway.production.vaas.gdatasecurity.de"
3333
TOKEN_URL: "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"
3434
VAAS_CLIENT_ID: ${{ secrets.VAAS_CLIENT_ID }}
3535
VAAS_USER_NAME: ${{ secrets.VAAS_USER_NAME }}
@@ -41,7 +41,7 @@ jobs:
4141
runs-on: ubuntu-22.04
4242
strategy:
4343
matrix:
44-
version: ["8.1", "8.2", "8.3"]
44+
version: ["8.1", "8.2", "8.3", "8.4"]
4545
steps:
4646
- name: checkout
4747
uses: actions/checkout@v4
@@ -57,7 +57,7 @@ jobs:
5757
run: |
5858
echo "CLIENT_ID=${{ secrets.STAGING_CLIENT_ID }}" >> $GITHUB_ENV
5959
echo "CLIENT_SECRET=${{ secrets.STAGING_CLIENT_SECRET }}" >> $GITHUB_ENV
60-
echo "VAAS_URL=wss://gateway.staging.vaas.gdatasecurity.de" >> $GITHUB_ENV
60+
echo "VAAS_URL=https://gateway.staging.vaas.gdatasecurity.de" >> $GITHUB_ENV
6161
echo "TOKEN_URL=https://account-staging.gdata.de/realms/vaas-staging/protocol/openid-connect/token" >> $GITHUB_ENV
6262
echo "VAAS_CLIENT_ID=${{ secrets.STAGING_VAAS_CLIENT_ID }}" >> $GITHUB_ENV
6363
echo "VAAS_USER_NAME=${{ secrets.STAGING_VAAS_USER_NAME }}" >> $GITHUB_ENV
@@ -68,7 +68,7 @@ jobs:
6868
run: |
6969
echo "CLIENT_ID=${{ secrets.DEVELOP_CLIENT_ID }}" >> $GITHUB_ENV
7070
echo "CLIENT_SECRET=${{ secrets.DEVELOP_CLIENT_SECRET }}" >> $GITHUB_ENV
71-
echo "VAAS_URL=wss://gateway.develop.vaas.gdatasecurity.de" >> $GITHUB_ENV
71+
echo "VAAS_URL=https://gateway.develop.vaas.gdatasecurity.de" >> $GITHUB_ENV
7272
echo "TOKEN_URL=https://account-staging.gdata.de/realms/vaas-develop/protocol/openid-connect/token" >> $GITHUB_ENV
7373
echo "VAAS_CLIENT_ID=${{ secrets.DEVELOP_VAAS_CLIENT_ID }}" >> $GITHUB_ENV
7474
echo "VAAS_USER_NAME=${{ secrets.DEVELOP_VAAS_USER_NAME }}" >> $GITHUB_ENV
@@ -82,12 +82,12 @@ jobs:
8282
- name: install php dependencies
8383
uses: php-actions/composer@v6
8484
with:
85-
working_dir: php/tests/vaas
85+
working_dir: php/tests/VaasTesting
8686
php_version: ${{ matrix.version }}
8787

8888
- name: run tests
89-
run: ./vendor/bin/phpunit --colors --testdox
90-
working-directory: php/tests/vaas
89+
run: ./vendor/bin/phpunit --colors --testdox --exclude-group exclude
90+
working-directory: php/tests/VaasTesting
9191

9292
- name: install example requirements
9393
run: composer install

php/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
*.lock
2-
package.xml
2+
package.xml

php/.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"phpunit.phpunit": "/workspaces/vaas/php/tests/vaas/vendor/bin/phpunit",
2+
"phpunit.phpunit": "/workspaces/vaas/php/tests/VaasTesting/vendor/bin/phpunit",
33
"phpunit.php": "/usr/local/bin/php",
44
"php.debug.ideKey": "vsc",
55
"php.debug.executablePath": "/usr/local/bin/php",

php/examples/Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Examples
22

3-
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.
3+
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.

php/examples/VaasExample/AuthenticationExamples.php

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,33 @@
33
namespace VaasExamples;
44

55
use VaasSdk\Authentication\ClientCredentialsGrantAuthenticator;
6-
use VaasSdk\Exceptions\InvalidSha256Exception;
7-
use VaasSdk\Exceptions\TimeoutException;
8-
use VaasSdk\Exceptions\VaasAuthenticationException;
9-
use VaasSdk\ResourceOwnerPasswordGrantAuthenticator;
6+
use VaasSdk\Sha256;
107
use VaasSdk\Vaas;
118

12-
$USE_RESOURCE_OWNER_PASSWORD_GRANT_AUTHENTICATOR = false;
13-
149
// If you got a username and password from us, you can use the ResourceOwnerPasswordAuthenticator like this
15-
if ($USE_RESOURCE_OWNER_PASSWORD_GRANT_AUTHENTICATOR){
16-
$authenticator = new ResourceOwnerPasswordGrantAuthenticator(
17-
"vaas-customer",
18-
getenv("VAAS_USER_NAME"),
19-
getenv("VAAS_PASSWORD"),
20-
getenv("TOKEN_URL")
21-
);
22-
}
10+
11+
// $authenticator = new ResourceOwnerPasswordGrantAuthenticator(
12+
// clientId: getenv("CLIENT_ID"),
13+
// username: getenv("USERNAME"),
14+
// password: getenv("PASSWORD"),
15+
// tokenUrl: getenv("TOKEN_URL")
16+
// );
17+
2318
// You may use self registration and create a new username and password for the
24-
// ResourceOwnerPasswordAuthenticator by yourself like the example above on https://vaas.gdata.de/login
25-
26-
// If you got a client id and client secret from us, you can use the ClientCredentialsGrantAuthenticator like this
27-
else{
28-
$authenticator = new ClientCredentialsGrantAuthenticator(
29-
getenv("CLIENT_ID"),
30-
getenv("CLIENT_SECRET"),
31-
getenv("TOKEN_URL")
32-
);
33-
}
34-
35-
$vaas = new Vaas(
36-
getenv("VAAS_URL")
19+
// `Password` authentication method by yourself like the example above on https://vaas.gdata.de/login
20+
21+
// If you got a client id and client secret from us, you can use the `Client Credentials` authentication method like this
22+
23+
$authenticator = new ClientCredentialsGrantAuthenticator(
24+
clientId: getenv("CLIENT_ID"),
25+
clientSecret: getenv("CLIENT_SECRET"),
26+
tokenUrl: getenv("TOKEN_URL")
3727
);
3828

39-
try {
40-
$vaas->Connect($authenticator->getToken());
41-
} catch (VaasAuthenticationException $e) {
42-
fwrite(STDERR, "Authentication failed: " . $e->getMessage() . "\n");
43-
exit(1);
44-
}
29+
$vaas = Vaas::builder()
30+
->withAuthenticator($authenticator)
31+
->build();
4532

4633
// Get verdict for an eicar hash
47-
try {
48-
$vaasVerdict = $vaas->ForSha256("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8");
49-
} catch (InvalidSha256Exception $e) {
50-
fwrite(STDERR, "Invalid sha256: " . $e->getMessage() . "\n");
51-
exit(1);
52-
} catch (TimeoutException $e) {
53-
fwrite(STDERR, "Timeout: " . $e->getMessage() . "\n");
54-
exit(1);
55-
}
56-
fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is $vaasVerdict->Verdict \n");
34+
$vaasVerdict = $vaas->forSha256Async(Sha256::TryFromString("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8")->await())->await();
35+
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is $vaasVerdict->verdict->value \n");

php/examples/VaasExample/GetVerdictByFile.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77

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

10+
1011
$authenticator = new ClientCredentialsGrantAuthenticator(
11-
getenv("CLIENT_ID"),
12-
getenv("CLIENT_SECRET"),
13-
getenv("TOKEN_URL") ?: "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"
12+
clientId: getenv("CLIENT_ID"),
13+
clientSecret: getenv("CLIENT_SECRET"),
14+
tokenUrl: getenv("TOKEN_URL")
1415
);
1516

16-
$vaas = (new Vaas())
17+
$vaas = Vaas::builder()
1718
->withAuthenticator($authenticator)
18-
->withUrl(getenv("VAAS_URL") ?? "wss://gateway.production.vaas.gdatasecurity.de")
1919
->build();
2020

21+
2122
$scanPath = getenv("SCAN_PATH");
22-
$vaasVerdict = $vaas->ForFile($scanPath);
23+
$vaasVerdict = $vaas->forFileAsync($scanPath)->await();
2324

24-
fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is " . $vaasVerdict->Verdict->value . " \n");
25+
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is " . $vaasVerdict->verdict->value . " \n");

php/examples/VaasExample/GetVerdictByHash.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,28 @@
33
namespace VaasExamples;
44

55
use VaasSdk\Authentication\ClientCredentialsGrantAuthenticator;
6+
use VaasSdk\Sha256;
67
use VaasSdk\Vaas;
78

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

11+
1012
$authenticator = new ClientCredentialsGrantAuthenticator(
11-
getenv("CLIENT_ID"),
12-
getenv("CLIENT_SECRET"),
13-
getenv("TOKEN_URL") ?: "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"
13+
clientId: getenv("CLIENT_ID"),
14+
clientSecret: getenv("CLIENT_SECRET"),
15+
tokenUrl: getenv("TOKEN_URL")
1416
);
15-
$vaas = (new Vaas())
17+
18+
$vaas = Vaas::builder()
1619
->withAuthenticator($authenticator)
17-
->withUrl(getenv("VAAS_URL") ?? "wss://gateway.production.vaas.gdatasecurity.de")
1820
->build();
1921

20-
// EICAR
21-
$vaasVerdict = $vaas->ForSha256("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8");
22-
fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is " . $vaasVerdict->Verdict->value . " \n");
23-
// SOMEFILE
24-
$vaasVerdict = $vaas->ForSha256("70caea443deb0d0a890468f9ac0a9b1187676ba3e66eb60a722b187107eb1ea8");
25-
fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is " . $vaasVerdict->Verdict->value . " \n");
22+
23+
// Malicious hash
24+
$vaasVerdict = $vaas->forSha256Async(Sha256::TryFromString("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8")->await())->await();
25+
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is " . $vaasVerdict->verdict->value . " \n");
26+
27+
28+
// Some file
29+
$vaasVerdict = $vaas->forSha256Async(Sha256::TryFromString("70caea443deb0d0a890468f9ac0a9b1187676ba3e66eb60a722b187107eb1ea8")->await())->await();
30+
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is " . $vaasVerdict->verdict->value . " \n");

php/examples/VaasExample/GetVerdictByUrl.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@
77

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

10+
1011
$authenticator = new ClientCredentialsGrantAuthenticator(
11-
getenv("CLIENT_ID"),
12-
getenv("CLIENT_SECRET"),
13-
getenv("TOKEN_URL") ?: "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"
12+
clientId: getenv("CLIENT_ID"),
13+
clientSecret: getenv("CLIENT_SECRET"),
14+
tokenUrl: getenv("TOKEN_URL")
1415
);
15-
$vaas = (new Vaas())
16+
17+
$vaas = Vaas::builder()
1618
->withAuthenticator($authenticator)
17-
->withUrl(getenv("VAAS_URL") ?? "wss://gateway.production.vaas.gdatasecurity.de")
1819
->build();
1920

21+
2022
// EICAR
21-
$vaasVerdict = $vaas->ForUrl("https://secure.eicar.org/eicar.com");
22-
fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is " . $vaasVerdict->Verdict->value . " \n");
23-
// SOMEFILE
24-
$vaasVerdict = $vaas->ForUrl("https://www.gdatasoftware.com/oem/verdict-as-a-service");
25-
fwrite(STDOUT, "Verdict for $vaasVerdict->Sha256 is " . $vaasVerdict->Verdict->value . " \n");
23+
$vaasVerdict = $vaas->forUrlAsync("https://secure.eicar.org/eicar.com")->await();
24+
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is " . $vaasVerdict->verdict->value . " \n");
25+
26+
// Some file
27+
$vaasVerdict = $vaas->forUrlAsync("https://www.gdatasoftware.com/oem/verdict-as-a-service")->await();
28+
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is " . $vaasVerdict->verdict->value . " \n");
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
namespace VaasExamples;
4+
5+
use Aws\Credentials\Credentials;
6+
use Aws\Signature\SignatureV4;
7+
use Dotenv\Dotenv;
8+
use Exception;
9+
use GuzzleHttp\Client;
10+
use GuzzleHttp\Exception\GuzzleException;
11+
use GuzzleHttp\Psr7\Request;
12+
use SimpleXMLElement;
13+
use VaasSdk\Authentication\ClientCredentialsGrantAuthenticator;
14+
use VaasSdk\Exceptions\VaasClientException;
15+
use VaasSdk\Options\VaasOptions;
16+
use VaasSdk\Vaas;
17+
18+
include_once("./vendor/autoload.php");
19+
20+
$dotenv = Dotenv::createImmutable(__DIR__);
21+
$dotenv->load();
22+
23+
$CLIENT_ID = getenv("CLIENT_ID");
24+
$CLIENT_SECRET = getenv("CLIENT_SECRET");
25+
$VAAS_URL = getenv("VAAS_URL");
26+
$TOKEN_URL = getenv("TOKEN_URL");
27+
$S3_ACCESS_KEY = getenv("S3_ACCESS_KEY");
28+
$S3_SECRET_KEY = getenv("S3_SECRET_KEY");
29+
$S3_URL = getenv("S3_URL");
30+
$S3_BUCKET = getenv("S3_BUCKET");
31+
$S3_REGION = getenv("S3_REGION");
32+
33+
// Build VaaS
34+
$authenticator = new ClientCredentialsGrantAuthenticator(
35+
clientId: $CLIENT_ID,
36+
clientSecret: $CLIENT_SECRET,
37+
tokenUrl: $TOKEN_URL
38+
);
39+
$vaasOptions = new VaasOptions(
40+
useHashLookup: true,
41+
useCache: false,
42+
vaasUrl: $VAAS_URL,
43+
timeout: 300
44+
);
45+
try {
46+
$vaas = Vaas::builder()
47+
->withOptions($vaasOptions)
48+
->withAuthenticator($authenticator)
49+
->build();
50+
} catch (VaasClientException $e) {
51+
fwrite(STDERR, "Error: " . $e->getMessage() . "\n");
52+
exit(1);
53+
}
54+
55+
// List S3 bucket
56+
$client = new Client();
57+
$request = new Request("GET", "$S3_URL/$S3_BUCKET?list-type=2");
58+
$credentials = new Credentials($S3_ACCESS_KEY, $S3_SECRET_KEY);
59+
$signer = new SignatureV4("s3", $S3_REGION);
60+
$signedRequest = $signer->signRequest($request, $credentials);
61+
$keys = [];
62+
try {
63+
$response = $client->send($signedRequest);
64+
$xml = new SimpleXMLElement($response->getBody()->getContents());
65+
foreach ($xml->Contents as $content) {
66+
$keys[] = (string)$content->Key;
67+
}
68+
} catch (GuzzleException $e) {
69+
fwrite(STDERR, "Error: " . $e->getMessage() . "\n");
70+
exit(1);
71+
} catch (Exception $e) {
72+
fwrite(STDERR, "Error: " . $e->getMessage() . "\n");
73+
exit(1);
74+
}
75+
76+
// Iterate over everything in S3 bucket and scan with VaaS
77+
$results = [];
78+
$progress = 0;
79+
$count = count($keys);
80+
$startTimeTotal = microtime(true);
81+
foreach ($keys as $key){
82+
// Pretty print progress
83+
$progress++;
84+
$percentageDone = number_format($progress / $count * 100, 1) . "%";
85+
echo chr(27).chr(91).'H'.chr(27).chr(91).'J';
86+
echo "\nProgress: $percentageDone [";
87+
$done = $progress / $count * 30;
88+
for ($i = 0; $i < 30; $i++) {
89+
echo $i < $done ? "=" : " ";
90+
}
91+
echo "]\n";
92+
echo "Execution time: " . number_format(microtime(true) - $startTimeTotal, 3) . "s\n";
93+
echo "Current key: $key\n\n";
94+
95+
// Download file from S3 to temp file
96+
$request = new Request("GET", "$S3_URL/$S3_BUCKET/$key");
97+
$request->withHeader("Accept", "application/octet-stream");
98+
$credentials = new Credentials($S3_ACCESS_KEY, $S3_SECRET_KEY);
99+
$signer = new SignatureV4("s3", $S3_REGION);
100+
$signedRequest = $signer->signRequest($request, $credentials);
101+
try {
102+
$response = $client->send($signedRequest);
103+
} catch (GuzzleException $e) {
104+
fwrite(STDERR, "Error: " . $e->getMessage() . "\n");
105+
exit(1);
106+
}
107+
$sample = tempnam(sys_get_temp_dir(), "vaas-sample-");
108+
$handle = fopen($sample, "w");
109+
fwrite($handle, $response->getBody());
110+
fclose($handle);
111+
112+
// Scan file with VaaS and track time
113+
$startTime = microtime(true);
114+
$vaasVerdict = $vaas->forFileAsync($sample)->await();
115+
$endTime = microtime(true);
116+
$executionTime = ($endTime - $startTime) * 1000;
117+
118+
// Save VaaS verdict and execution time
119+
$results[] = [
120+
"key" => $key,
121+
"executionTimeInMs" => number_format($executionTime, 3),
122+
"verdict" => [
123+
"sha256" => $vaasVerdict->sha256,
124+
"verdict" => $vaasVerdict->verdict->value,
125+
"detection" => $vaasVerdict->detection,
126+
"fileType" => $vaasVerdict->fileType,
127+
"mimeType" => $vaasVerdict->mimeType
128+
]
129+
];
130+
131+
// Delete temp file
132+
unlink($sample);
133+
}
134+
135+
$endTimeTotal = microtime(true);
136+
$executionTime = number_format($endTimeTotal - $startTimeTotal, 3);
137+
138+
file_put_contents("results-$S3_BUCKET.json", json_encode($results, JSON_PRETTY_PRINT));
139+
140+
echo "Results written to results.json\n";
141+
echo "Total execution time: " . $executionTime . "s\n";

0 commit comments

Comments
 (0)