Skip to content

Commit

Permalink
MDTT-34: Fix - Basic auth issue in JSON data source (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
subhojit-axl authored Apr 29, 2022
1 parent 20bb9fb commit 3f75895
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 171 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ return [
'host' => "127.0.0.1",
'port' => "59002",
],
],
'http' => [
// JSON datasource basic authentication credetials
'source' => [
'username' => 'username',
'password' => 'fubar',
// Basic authentication protocol: basic, digest, or ntlm.
// Protocol is optional. If not mentioned, basic is considered.
'protocol' => 'ntlm',
],
'destination' => [
'username' => 'username',
'password' => 'puffball',
]
]
];
```
Expand Down Expand Up @@ -99,14 +113,14 @@ source:
data: https://dev.legacy-system.com/api/v1/users
# The pointer where all the list of items resides. Refer https://github.com/halaxa/json-machine#what-is-json-pointer-anyway for examples
selector: "/results/-/employees"
# Basic authentication credentials to access the endpoint. This is optional if the endpoint is publicly accessible.
auth_basic: "foo:bar"
# Datasource basic authentication credentials. Note that the key is same as mentioned in the test specification. This is optional if the endpoint is publicly accessible.
credential: source
# The endpoint that returns the destination dataset.
destination:
type: json
data: https://dev.new-system.com/api/v1/users
selector: "/results/-/employees"
auth_basic: "foo:bar"
credential: destination
tests:
-
sourceField: name
Expand Down
3 changes: 1 addition & 2 deletions src/Destination/Database.php → src/DataSource/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

declare(strict_types=1);

namespace Mdtt\Destination;
namespace Mdtt\DataSource;

use Iterator;
use Mdtt\DataSource\DataSource;
use Mdtt\Exception\SetupException;
use Mdtt\Utility\DataSource\Database as DbDatabase;
use mysqli_result;
Expand Down
46 changes: 35 additions & 11 deletions src/Destination/Json.php → src/DataSource/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,44 @@

declare(strict_types=1);

namespace Mdtt\Destination;
namespace Mdtt\DataSource;

use Iterator;
use JsonMachine\Items;
use Mdtt\DataSource\DataSource;
use Mdtt\Utility\DataSource\Json as JsonDataSourceUtility;

class Json extends DataSource
{
private string $selector;
private JsonDataSourceUtility $jsonDataSourceUtility;
private Items|null $items;
private ?string $username;
private ?string $password;
private ?string $protocol;

/**
* @param string|null $username
*/
public function setUsername(?string $username): void
{
$this->username = $username;
}

/**
* @param string|null $password
*/
public function setPassword(?string $password): void
{
$this->password = $password;
}

/**
* @param string|null $protocol
*/
public function setProtocol(?string $protocol): void
{
$this->protocol = $protocol;
}

public function __construct(
string $data,
Expand All @@ -25,21 +51,19 @@ public function __construct(
$this->jsonDataSourceUtility = $jsonDataSourceUtility;
}

/**
* @return string
*/
public function getSelector(): string
{
return $this->selector;
}

/**
* @inheritDoc
*/
public function getIterator(): Iterator
{
if (!isset($this->items)) {
$this->items = $this->jsonDataSourceUtility->getItems($this->data, $this->selector);
$this->items = $this->jsonDataSourceUtility->getItems(
$this->data,
$this->selector,
$this->username,
$this->password,
$this->protocol
);
}

foreach ($this->items as $item) {
Expand Down
66 changes: 36 additions & 30 deletions src/Definition/Validate/DataSource/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,43 +37,49 @@ public function validate(string $type, array $rawDataSourceDefinition): DataSour
if ($dataSourceType === "database") {
$this->doValidateDatabase($rawDataSourceDefinition);

if ($type === "source") {
return new \Mdtt\Source\Database(
$rawDataSourceDefinition['data'],
$rawDataSourceDefinition['database']
);
}

if ($type === "destination") {
return new \Mdtt\Destination\Database(
$rawDataSourceDefinition['data'],
$rawDataSourceDefinition['database']
);
}
return new \Mdtt\DataSource\Database(
$rawDataSourceDefinition['data'],
$rawDataSourceDefinition['database']
);
}

if ($dataSourceType === "json") {
$this->doValidateJson($rawDataSourceDefinition);

if (isset($rawDataSourceDefinition['auth_basic'])) {
$this->jsonDataSourceUtility->setAuthBasicCredential($rawDataSourceDefinition['auth_basic']);
$username = null;
$password = null;
$protocol = null;

if (isset($rawDataSourceDefinition['credential'])) {
$specification = require "tests/mdtt/spec.php";

$httpSpecification = $specification['http'];
/** @var string $credentialKey */
$credentialKey = $rawDataSourceDefinition['credential'];

if (!isset(
$httpSpecification[$credentialKey]['username'],
$httpSpecification[$credentialKey]['password']
)) {
throw new SetupException(
"Basic auth username and password are not provided, but credential is specified."
);
}

$username = $httpSpecification[$credentialKey]['username'];
$password = $httpSpecification[$credentialKey]['password'];
$protocol = $httpSpecification[$credentialKey]['protocol'] ?? null;
}

if ($type === "source") {
return new \Mdtt\Source\Json(
$rawDataSourceDefinition['data'],
$rawDataSourceDefinition['selector'],
$this->jsonDataSourceUtility,
);
}
$datasource = new \Mdtt\DataSource\Json(
$rawDataSourceDefinition['data'],
$rawDataSourceDefinition['selector'],
$this->jsonDataSourceUtility,
);
$datasource->setUsername($username);
$datasource->setPassword($password);
$datasource->setProtocol($protocol);

if ($type === "destination") {
return new \Mdtt\Destination\Json(
$rawDataSourceDefinition['data'],
$rawDataSourceDefinition['selector'],
$this->jsonDataSourceUtility
);
}
return $datasource;
}

throw new SetupException(sprintf("Unexpected data source type %s and data source definition passed.", $type));
Expand Down
58 changes: 0 additions & 58 deletions src/Source/Database.php

This file was deleted.

49 changes: 0 additions & 49 deletions src/Source/Json.php

This file was deleted.

28 changes: 11 additions & 17 deletions src/Utility/DataSource/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Mdtt\Utility\DataSource;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\StreamWrapper;
use JsonMachine\Exception\InvalidArgumentException;
use JsonMachine\Items;
Expand All @@ -16,33 +15,28 @@
class Json
{
private HttpClient $httpClient;
private ?string $authBasicCredential;

public function __construct(HttpClient $httpClient)
{
$this->httpClient = $httpClient;
}

/**
* @param string $authBasicCredential
*/
public function setAuthBasicCredential(string $authBasicCredential): void
{
$this->authBasicCredential = $authBasicCredential;
}

public function getItems(string $data, string $selector): Items
{
public function getItems(
string $data,
string $selector,
?string $username,
?string $password,
?string $protocol
): Items {
$httpClient = $this->httpClient->getClient();
$request = new Request('GET', $data);
$headers = [];

if (isset($this->authBasicCredential)) {
$credential = explode(':', $this->authBasicCredential);
$request->withHeader('auth', $credential);
if (isset($username, $password)) {
$headers['auth'] = [$username, $password, $protocol ?? 'basic'];
}

try {
$response = $httpClient->sendRequest($request);
$response = $httpClient->request('GET', $data, $headers);
} catch (ClientExceptionInterface $e) {
throw new ExecutionException($e->getMessage());
}
Expand Down
2 changes: 1 addition & 1 deletion src/Utility/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Mdtt\Utility;

use GuzzleHttp\Client;
use Psr\Http\Client\ClientInterface;
use GuzzleHttp\ClientInterface;

class HttpClient
{
Expand Down

0 comments on commit 3f75895

Please sign in to comment.