Refactor DNS mocking implementation.
This commit is contained in:
parent
e784137480
commit
7ac1bead49
3 changed files with 27 additions and 34 deletions
|
@ -360,14 +360,14 @@
|
|||
}
|
||||
}
|
||||
|
||||
// Handle Mock Servers environment variables (SB_INSTANCE_MOCK_SERVER_*)
|
||||
// Handle Mock Servers environment variables (SB_INSTANCE_DNS_MOCK_*)
|
||||
$mockServers = [];
|
||||
foreach(self::getMockServerValues() as $mockServer)
|
||||
{
|
||||
$mockServer = explode(' ', $mockServer);
|
||||
if(count($mockServer) !== 2)
|
||||
{
|
||||
Logger::getLogger()->warning(sprintf('Invalid Mock Server format: %s', implode(' ', $mockServer)));
|
||||
Logger::getLogger()->warning(sprintf('Invalid DNS Mock Server format: %s', implode(' ', $mockServer)));
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -399,7 +399,7 @@
|
|||
}
|
||||
|
||||
/**
|
||||
* Retrieves all environment variable values that start with the prefix 'SB_INSTANCE_MOCK_SERVER_'.
|
||||
* Retrieves all environment variable values that start with the prefix 'SB_INSTANCE_DNS_MOCK_'.
|
||||
*
|
||||
* @return array An array of environment variable values filtered by the specified prefix.
|
||||
*/
|
||||
|
@ -411,7 +411,7 @@
|
|||
// Filter variables that start with the specified prefix
|
||||
$filtered = array_filter($envVars, function ($key)
|
||||
{
|
||||
return str_starts_with($key, 'SB_INSTANCE_MOCK_SERVER_');
|
||||
return str_starts_with($key, 'SB_INSTANCE_DNS_MOCK_');
|
||||
}, ARRAY_FILTER_USE_KEY);
|
||||
|
||||
// Return only the values as an array
|
||||
|
@ -453,7 +453,7 @@
|
|||
" SB_CACHE_USERNAME - The cache username (default: null)\n" .
|
||||
" SB_CACHE_PASSWORD - The cache password (default: null)\n" .
|
||||
" SB_CACHE_DATABASE - The cache database (default: 0)\n" .
|
||||
" SB_INSTANCE_MOCK_SERVER_* - Mock server environment variables, format: (<domain> <txt>), eg; SB_INSTANCE_MOCK_SERVER_N64: teapot.com <txt>\n";
|
||||
" SB_INSTANCE_DNS_MOCK_* - Mock server environment variables, format: (<domain> <txt>), eg; SB_INSTANCE_DNS_MOCK_N64: teapot.com <txt>\n";
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
private string $name;
|
||||
private ?string $domain;
|
||||
private ?string $rpcEndpoint;
|
||||
private array $dnsMocks;
|
||||
|
||||
/**
|
||||
* Constructor that initializes object properties with the provided data.
|
||||
|
@ -21,6 +22,7 @@
|
|||
$this->name = $data['name'];
|
||||
$this->domain = $data['domain'];
|
||||
$this->rpcEndpoint = $data['rpc_endpoint'];
|
||||
$this->dnsMocks = $data['dns_mocks'];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -55,4 +57,12 @@
|
|||
{
|
||||
return $this->rpcEndpoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getDnsMocks(): array
|
||||
{
|
||||
return $this->dnsMocks;
|
||||
}
|
||||
}
|
|
@ -10,7 +10,6 @@
|
|||
|
||||
class ServerResolver
|
||||
{
|
||||
private static bool $mockingEnabled = false;
|
||||
private static array $mockedRecords = [];
|
||||
|
||||
/**
|
||||
|
@ -25,18 +24,23 @@
|
|||
*/
|
||||
public static function resolveDomain(string $domain, bool $useDatabase=true): DnsRecord
|
||||
{
|
||||
// Return the mocked record if mocking is enabled
|
||||
if(self::$mockingEnabled)
|
||||
// Return the mocked record if the mocking record is set
|
||||
if(isset(self::$mockedRecords[$domain]))
|
||||
{
|
||||
if(isset(self::$mockedRecords[$domain]))
|
||||
{
|
||||
return self::$mockedRecords[$domain];
|
||||
}
|
||||
return self::$mockedRecords[$domain];
|
||||
}
|
||||
|
||||
// Return the mocked record from the configuration if one is set
|
||||
if(isset(Configuration::getInstanceConfiguration()->getDnsMocks()[$domain]))
|
||||
{
|
||||
return DnsHelper::parseTxt(Configuration::getInstanceConfiguration()->getDnsMocks()[$domain]);
|
||||
}
|
||||
|
||||
// Check the database if enabled
|
||||
if ($useDatabase)
|
||||
{
|
||||
// Return from the database cache if one exists
|
||||
// TODO: Implement renewal here
|
||||
$resolvedServer = ResolvedDnsRecordsManager::getDnsRecord($domain);
|
||||
if ($resolvedServer !== null)
|
||||
{
|
||||
|
@ -44,12 +48,12 @@
|
|||
}
|
||||
}
|
||||
|
||||
// Resolve DNS & Records
|
||||
$txtRecords = self::dnsGetTxtRecords($domain);
|
||||
if ($txtRecords === false)
|
||||
{
|
||||
throw new ResolutionException(sprintf("Failed to resolve DNS TXT records for %s", $domain));
|
||||
}
|
||||
|
||||
$fullRecord = self::concatenateTxtRecords($txtRecords);
|
||||
|
||||
try
|
||||
|
@ -106,27 +110,6 @@
|
|||
return $fullRecordBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if mocking is enabled.
|
||||
*
|
||||
* @return bool True if mocking is enabled, false otherwise.
|
||||
*/
|
||||
public static function isMockingEnabled(): bool
|
||||
{
|
||||
return self::$mockingEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables or disables mocking functionality.
|
||||
*
|
||||
* @param bool $enabled Indicates whether mocking should be enabled (true) or disabled (false).
|
||||
* @return void
|
||||
*/
|
||||
public static function setMockingEnabled(bool $enabled): void
|
||||
{
|
||||
self::$mockingEnabled = $enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the mocked records.
|
||||
*
|
||||
|
|
Loading…
Add table
Reference in a new issue