Skip to content

Commit

Permalink
feat: add tsig api
Browse files Browse the repository at this point in the history
  • Loading branch information
bennetgallein committed May 11, 2024
1 parent 03ae449 commit 388e974
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 41 deletions.
78 changes: 37 additions & 41 deletions src/Powerdns.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class Powerdns implements PowerdnsInterface
{
class Powerdns implements PowerdnsInterface {
/**
* The version of this package. This is being used for the user-agent header.
*/
Expand Down Expand Up @@ -98,8 +97,7 @@ public function __construct(
*
* @return $this The current Powerdns class.
*/
public function setConnector(ConnectorInterface $connector): self
{
public function setConnector(ConnectorInterface $connector): self {
$this->connector = $connector;

return $this;
Expand All @@ -114,10 +112,9 @@ public function setConnector(ConnectorInterface $connector): self
*
* @return PowerdnsInterface The created PowerDNS client.
*/
public function connect(string $host, int $port = 8081, string $server = 'localhost'): PowerdnsInterface
{
$this->host = $host;
$this->port = $port;
public function connect(string $host, int $port = 8081, string $server = 'localhost'): PowerdnsInterface {
$this->host = $host;
$this->port = $port;
$this->server = $server;

return $this;
Expand All @@ -130,8 +127,7 @@ public function connect(string $host, int $port = 8081, string $server = 'localh
*
* @return PowerdnsInterface The current client.
*/
public function useKey(string $key): PowerdnsInterface
{
public function useKey(string $key): PowerdnsInterface {
$this->apiKey = $key;

return $this;
Expand All @@ -146,8 +142,7 @@ public function useKey(string $key): PowerdnsInterface
*
* @return Zone The created Zone.
*/
public function createZone(string $canonicalDomain, array $nameservers, bool $useDnssec = false): Zone
{
public function createZone(string $canonicalDomain, array $nameservers, bool $useDnssec = false): Zone {
$fixDot = substr($canonicalDomain, -1) !== '.';

if ($fixDot) {
Expand All @@ -172,8 +167,7 @@ public function createZone(string $canonicalDomain, array $nameservers, bool $us
*
* @return Zone The created zone.
*/
public function createZoneFromResource(ZoneResource $zoneResource): Zone
{
public function createZoneFromResource(ZoneResource $zoneResource): Zone {
$this->connector->post('zones', new CreateZoneTransformer($zoneResource));

return $this->zone($zoneResource->getName());
Expand All @@ -186,8 +180,7 @@ public function createZoneFromResource(ZoneResource $zoneResource): Zone
*
* @return Zone The zone.
*/
public function zone(string $canonicalDomain): Zone
{
public function zone(string $canonicalDomain): Zone {
return new Zone($this->connector, $canonicalDomain);
}

Expand All @@ -198,9 +191,8 @@ public function zone(string $canonicalDomain): Zone
*
* @return bool True that the zone was removed.
*/
public function deleteZone(string $canonicalDomain): bool
{
$this->connector->delete('zones/'.$canonicalDomain);
public function deleteZone(string $canonicalDomain): bool {
$this->connector->delete('zones/' . $canonicalDomain);

return true;
}
Expand All @@ -214,13 +206,12 @@ public function deleteZone(string $canonicalDomain): bool
*
* @see https://doc.powerdns.com/authoritative/http-api/zone.html#get--servers-server_id-zones
*/
public function listZones(bool $includeDnssecAndEditedSerialFields = false): array
{
public function listZones(bool $includeDnssecAndEditedSerialFields = false): array {
return array_map(
function (array $args) {
return new Zone($this->connector, $args['id']);
},
$this->connector->get('zones?dnssec='.($includeDnssecAndEditedSerialFields ? 'true' : 'false'))
$this->connector->get('zones?dnssec=' . ($includeDnssecAndEditedSerialFields ? 'true' : 'false'))
);
}

Expand All @@ -231,11 +222,20 @@ function (array $args) {
*
* @return Cryptokey The cryptokey instance.
*/
public function cryptokeys(string $canonicalDomain): Cryptokey
{
public function cryptokeys(string $canonicalDomain): Cryptokey {
return new Cryptokey($this->connector, $canonicalDomain);
}


/**
* Get a TSIGKey instance to work with
*
* @return TSIGKey The TSIGKey instance
*/
public function tsigkeys(): TSIGKey {
return new TSIGKey($this->connector);
}

/**
* Query PowerDNS internal statistics.
* The ring statistics are disabled by default to speedup the request and reduce the response size.
Expand All @@ -248,16 +248,15 @@ public function cryptokeys(string $canonicalDomain): Cryptokey
*
* @return array An array with statistics.
*/
public function statistics($statistic = null, $includeRings = false): array
{
public function statistics($statistic = null, $includeRings = false): array {
// Convert $includeRings param to string.
$includeRings = $includeRings ? 'true' : 'false';

$endpoint = 'statistics?includerings='.$includeRings;
$endpoint = 'statistics?includerings=' . $includeRings;

// Request a specific statistic.
if ($statistic) {
$endpoint .= '&statistic='.$statistic;
$endpoint .= '&statistic=' . $statistic;
}

return $this->connector->get($endpoint);
Expand All @@ -273,8 +272,7 @@ public function statistics($statistic = null, $includeRings = false): array
*
* @return SearchResultSet A collection with search results.
*/
public function search(string $query, int $size = 100, string $type = 'all'): SearchResultSet
{
public function search(string $query, int $size = 100, string $type = 'all'): SearchResultSet {
if (!in_array($type, ['all', 'zone', 'record', 'comment'])) {
throw new LogicException('Invalid search type given. Type must be one of "all", "zone", "record" or "comment".');
}
Expand All @@ -292,7 +290,9 @@ public function search(string $query, int $size = 100, string $type = 'all'): Se
)
);

$searchResults = array_map(static function ($item) { return new SearchResult($item); }, $response);
$searchResults = array_map(static function ($item) {
return new SearchResult($item);
}, $response);

return new SearchResultSet($searchResults);
}
Expand All @@ -302,8 +302,7 @@ public function search(string $query, int $size = 100, string $type = 'all'): Se
*
* @return string The server version.
*/
public function serverVersion(): string
{
public function serverVersion(): string {
return $this->connector->get('/')['version'];
}

Expand All @@ -312,8 +311,7 @@ public function serverVersion(): string
*
* @return LoggerInterface The log instance.
*/
public function log(): LoggerInterface
{
public function log(): LoggerInterface {
if ($this->logger === null) {
// If there's no logger set, use the NullLogger.
$this->logger = new NullLogger();
Expand All @@ -329,8 +327,7 @@ public function log(): LoggerInterface
*
* @return PowerdnsInterface The current client instance.
*/
public function setLogger(LoggerInterface $log): PowerdnsInterface
{
public function setLogger(LoggerInterface $log): PowerdnsInterface {
$this->logger = $log;

return $this;
Expand All @@ -341,11 +338,10 @@ public function setLogger(LoggerInterface $log): PowerdnsInterface
*
* @return mixed[] Array containing the client config items.
*/
public function getConfig(): array
{
public function getConfig(): array {
return [
'host' => $this->host,
'port' => $this->port,
'host' => $this->host,
'port' => $this->port,
'server' => $this->server,
'apiKey' => $this->apiKey,
];
Expand Down
163 changes: 163 additions & 0 deletions src/Resources/TSIGKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

declare(strict_types=1);

namespace Exonet\Powerdns\Resources;

class TSIGKey {
/**
* The name of the key
*
* @var string
*/
private $name;
/**
* The ID for this key, used in the TSIGkey URL endpoint.
*
* @var string
*/
private $id;
/**
* The algorithm of the TSIG key
*
* @var string
*/
private $algorithm;
/**
* The Base64 encoded secret key, empty when listing keys. MAY be empty when POSTing to have the server generate the key material
*
* @var string
*/
private $key;
/**
* Set to "TSIGKey"
*
* @var string
*/
private $type = "TSIGKey";

/**
* Record constructor.
*
* @param string $content Optional content to set.
*/
public function __construct(?array $content = null) {
if ($content) {
$this->setName($content['name'] ?? "");
$this->setId($content['id'] ?? "");
$this->setAlgorithm($content['algorithm'] ?? "");
$this->setKey($content['key'] ?? "");
$this->setType($content['type'] ?? "");
}
}

/**
* Get set to "TSIGKey"
*
* @return string
*/
public function getType() {
return $this->type;
}

/**
* Set set to "TSIGKey"
*
* @param string $type Set to "TSIGKey"
*
* @return self
*/
public function setType(string $type) {
$this->type = $type;

return $this;
}

/**
* Get the Base64 encoded secret key, empty when listing keys. MAY be empty when POSTing to have the server generate the key material
*
* @return string
*/
public function getKey() {
return $this->key;
}

/**
* Set the Base64 encoded secret key, empty when listing keys. MAY be empty when POSTing to have the server generate the key material
*
* @param string $key The Base64 encoded secret key, empty when listing keys. MAY be empty when POSTing to have the server generate the key material
*
* @return self
*/
public function setKey(string $key) {
$this->key = $key;

return $this;
}

/**
* Get the algorithm of the TSIG key
*
* @return string
*/
public function getAlgorithm() {
return $this->algorithm;
}

/**
* Set the algorithm of the TSIG key
*
* @param string $algorithm The algorithm of the TSIG key
*
* @return self
*/
public function setAlgorithm(string $algorithm) {
$this->algorithm = $algorithm;

return $this;
}

/**
* Get the ID for this key, used in the TSIGkey URL endpoint.
*
* @return string
*/
public function getId() {
return $this->id;
}

/**
* Set the ID for this key, used in the TSIGkey URL endpoint.
*
* @param string $id The ID for this key, used in the TSIGkey URL endpoint.
*
* @return self
*/
public function setId(string $id) {
$this->id = $id;

return $this;
}

/**
* Get the name of the key
*
* @return string
*/
public function getName() {
return $this->name;
}

/**
* Set the name of the key
*
* @param string $name The name of the key
*
* @return self
*/
public function setName(string $name) {
$this->name = $name;

return $this;
}
}
Loading

0 comments on commit 388e974

Please sign in to comment.