From 388e9743f63f5a02b9f571cab88508acf7c6a86d Mon Sep 17 00:00:00 2001 From: Bennet Gallein Date: Sat, 11 May 2024 21:46:30 +0200 Subject: [PATCH 1/7] feat: add tsig api --- src/Powerdns.php | 78 ++++++------ src/Resources/TSIGKey.php | 163 ++++++++++++++++++++++++ src/Resources/TSIGKeySet.php | 135 ++++++++++++++++++++ src/TSIGKey.php | 67 ++++++++++ src/TSIGKeyAlgorithms.php | 19 +++ src/Transformers/TSIGKeyTransformer.php | 18 +++ 6 files changed, 439 insertions(+), 41 deletions(-) create mode 100644 src/Resources/TSIGKey.php create mode 100644 src/Resources/TSIGKeySet.php create mode 100644 src/TSIGKey.php create mode 100644 src/TSIGKeyAlgorithms.php create mode 100644 src/Transformers/TSIGKeyTransformer.php diff --git a/src/Powerdns.php b/src/Powerdns.php index 94813bb..049ce34 100644 --- a/src/Powerdns.php +++ b/src/Powerdns.php @@ -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. */ @@ -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; @@ -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; @@ -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; @@ -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) { @@ -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()); @@ -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); } @@ -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; } @@ -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')) ); } @@ -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. @@ -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); @@ -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".'); } @@ -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); } @@ -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']; } @@ -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(); @@ -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; @@ -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, ]; diff --git a/src/Resources/TSIGKey.php b/src/Resources/TSIGKey.php new file mode 100644 index 0000000..01a0dfd --- /dev/null +++ b/src/Resources/TSIGKey.php @@ -0,0 +1,163 @@ +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; + } +} diff --git a/src/Resources/TSIGKeySet.php b/src/Resources/TSIGKeySet.php new file mode 100644 index 0000000..a82ad57 --- /dev/null +++ b/src/Resources/TSIGKeySet.php @@ -0,0 +1,135 @@ +tsigResources = $resourceRecords; + } + } + + /** + * Add a single tsigkey resource to the existing collection. + * + * @param TSIGKey $metaResource The tsigkey resource to add. + * + * @return TSIGKeySet The current TSIGKeySet instance. + */ + public function addResource(TSIGKey $metaResource): self { + $this->tsigResources[] = $metaResource; + + return $this; + } + + /** + * Get the number of tsigkey resources in this collection. + * + * @return int The number of tsigkey resources. + */ + public function count(): int { + return count($this->tsigResources); + } + + /** + * Check if the current collection is not empty. + * + * @return bool True when there are tsigkey resources in this collection. + */ + public function isNotEmpty(): bool { + return !$this->isEmpty(); + } + + /** + * Check if the current collection is empty. + * + * @return bool True when there are no tsigkey resources in this collection. + */ + public function isEmpty(): bool { + return empty($this->tsigResources); + } + + /** + * Loop through the collection and call the given closure for each tsigkey resource. + * + * @param Closure $closure The closure to execute for each tsigkey resource. + * + * @return TSIGKeySet The current TSIGKeySet instance. + */ + public function map(Closure $closure): self { + foreach ($this->tsigResources as $index => $resource) { + $this->tsigResources[$index] = $closure($resource, $index); + } + + return $this; + } + + /** + * Delete all tsigkey resources that are set in the current collection. + * + * @return bool True when the tsigkey resources are deleted. + */ + public function delete(): bool { + foreach ($this->tsigResources as $resource) { + $resource->delete(); + } + + return true; + } + + /** + * {@inheritdoc} + */ + public function getIterator(): ArrayIterator { + return new ArrayIterator($this->tsigResources); + } + + /** + * {@inheritdoc} + */ + public function offsetExists($offset): bool { + return isset($this->tsigResources[$offset]); + } + + /** + * {@inheritdoc} + * + * @return TSIGKey + */ + #[ReturnTypeWillChange] + public function offsetGet($offset) { + return $this->tsigResources[$offset]; + } + + /** + * {@inheritdoc} + */ + public function offsetSet($offset, $value): void { + $this->tsigResources[$offset] = $value; + } + + /** + * {@inheritdoc} + */ + public function offsetUnset($offset): void { + unset($this->tsigResources[$offset]); + } +} diff --git a/src/TSIGKey.php b/src/TSIGKey.php new file mode 100644 index 0000000..318fe11 --- /dev/null +++ b/src/TSIGKey.php @@ -0,0 +1,67 @@ +connector->get('tsigkeys'); + + $resultSet = new TSIGKeySet(); + foreach ($items as $item) { + $resultSet->addResource(new TSIGKeyResource($item)); + } + + return $resultSet; + } + + /** + * Creat a new TSIG Key + * + * @param array|string $data The data. + * + * @return TSIGKeySet The created key data set. + */ + public function create(TSIGKeyResource $data): TSIGKeySet { + $response = $this->connector->post('tsigkeys', new TSIGKeyTransformer($data)); + + return new TSIGKeySet([new TSIGKeyResource($response)]); + } + + /** + * Update an existing tsig key. + * + * @param TSIGKeyResource $key The key data item to update. + * + * @return bool True if the update was successful. + */ + public function update(TSIGKeyResource $key): bool { + $response = $this->connector->put('tsigkeys/' . $key->getId(), new TSIGKeyTransformer($key)); + + // If the response is empty, everything is fine. + return empty($response); + } + + /** + * Delete an existing tsigkey item. + * + * @param TSIGKeyResource $key The tsigkey data item to delete. + * + * @return bool True if the delete was successful. + */ + public function delete(TSIGKeyResource $key): bool { + $response = $this->connector->delete('tsigkeys/' . $key->getId()); + + // If the response is empty, everything is fine. + return empty($response); + } +} diff --git a/src/TSIGKeyAlgorithms.php b/src/TSIGKeyAlgorithms.php new file mode 100644 index 0000000..86da30f --- /dev/null +++ b/src/TSIGKeyAlgorithms.php @@ -0,0 +1,19 @@ + $this->data->getName(), + 'id' => $this->data->getId(), + 'algorithm' => $this->data->getAlgorithm(), + 'key' => $this->data->getKey(), + 'type' => $this->data->getType() + ]; + } +} From 19dd12cd88cb4f8ee93e008751f57751fc9b8299 Mon Sep 17 00:00:00 2001 From: Bennet Gallein Date: Sat, 11 May 2024 21:46:38 +0200 Subject: [PATCH 2/7] chore: add tsig tests --- tests/functional/TSIGKeysTest.php | 54 +++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tests/functional/TSIGKeysTest.php diff --git a/tests/functional/TSIGKeysTest.php b/tests/functional/TSIGKeysTest.php new file mode 100644 index 0000000..083b235 --- /dev/null +++ b/tests/functional/TSIGKeysTest.php @@ -0,0 +1,54 @@ +powerdns->tsigkeys(); + $resource = new TSIGKeyResource(); + + $resource->setName($name); + $resource->setAlgorithm(TSIGKeyAlgorithms::HMAC_SHA512); + + $key = $manager->create($resource); + + $this->assertSame(1, $key->count()); + + $created = $key->offsetGet(0); + $this->assertNotEquals("", $created->getKey()); + + // cleanup + $manager->delete($created); + + } + + public function testCreateWithNonUrlFriendlyName(): void { + $name = "this/is/not/aa-_412'aur\\asd-url-friendly"; + + $manager = $this->powerdns->tsigkeys(); + $resource = new TSIGKeyResource(); + + $resource->setName($name); + $resource->setAlgorithm(TSIGKeyAlgorithms::HMAC_SHA512); + + $key = $manager->create($resource); + + $this->assertSame(1, $key->count()); + + $created = $key->offsetGet(0); + $this->assertNotEquals("", $created->getKey()); + + // cleanup + $manager->delete($created); + } + +} From 23eb2ef742ca4cd7b215decfe081d96d58ca3d5b Mon Sep 17 00:00:00 2001 From: Bennet Gallein Date: Sat, 11 May 2024 22:46:24 +0200 Subject: [PATCH 3/7] chore: code formatting --- src/Powerdns.php | 70 ++++++++++------ src/Resources/TSIGKey.php | 106 ++++++++++++++---------- src/Resources/TSIGKeySet.php | 39 ++++++--- src/TSIGKey.php | 24 +++--- src/TSIGKeyAlgorithms.php | 21 +++-- src/Transformers/TSIGKeyTransformer.php | 14 ++-- tests/functional/TSIGKeysTest.php | 24 +++--- 7 files changed, 174 insertions(+), 124 deletions(-) diff --git a/src/Powerdns.php b/src/Powerdns.php index 049ce34..2724c73 100644 --- a/src/Powerdns.php +++ b/src/Powerdns.php @@ -10,7 +10,8 @@ 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. */ @@ -97,7 +98,8 @@ 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; @@ -112,9 +114,10 @@ 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; @@ -127,7 +130,8 @@ 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; @@ -142,7 +146,8 @@ 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) { @@ -167,7 +172,8 @@ 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()); @@ -180,7 +186,8 @@ 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); } @@ -191,8 +198,9 @@ 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; } @@ -206,12 +214,13 @@ 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')) ); } @@ -222,17 +231,18 @@ 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 + * Get a TSIGKey instance to work with. * * @return TSIGKey The TSIGKey instance */ - public function tsigkeys(): TSIGKey { + public function tsigkeys(): TSIGKey + { return new TSIGKey($this->connector); } @@ -248,15 +258,16 @@ public function tsigkeys(): TSIGKey { * * @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); @@ -272,7 +283,8 @@ 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".'); } @@ -302,7 +314,8 @@ 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']; } @@ -311,7 +324,8 @@ 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(); @@ -327,7 +341,8 @@ 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; @@ -338,10 +353,11 @@ 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, ]; diff --git a/src/Resources/TSIGKey.php b/src/Resources/TSIGKey.php index 01a0dfd..c1bb38b 100644 --- a/src/Resources/TSIGKey.php +++ b/src/Resources/TSIGKey.php @@ -4,114 +4,126 @@ namespace Exonet\Powerdns\Resources; -class TSIGKey { +class TSIGKey +{ /** - * The name of the key + * 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 + * 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 + * 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" + * Set to "TSIGKey". * * @var string */ - private $type = "TSIGKey"; + private $type = 'TSIGKey'; /** * Record constructor. * * @param string $content Optional content to set. */ - public function __construct(?array $content = null) { + 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'] ?? ""); + $this->setName($content['name'] ?? ''); + $this->setId($content['id'] ?? ''); + $this->setAlgorithm($content['algorithm'] ?? ''); + $this->setKey($content['key'] ?? ''); + $this->setType($content['type'] ?? ''); } } /** - * Get set to "TSIGKey" + * Get set to "TSIGKey". * - * @return string + * @return string */ - public function getType() { + public function getType() + { return $this->type; } /** - * Set set to "TSIGKey" + * Set set to "TSIGKey". * - * @param string $type Set to "TSIGKey" + * @param string $type Set to "TSIGKey" * - * @return self + * @return self */ - public function setType(string $type) { + 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 + * 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 + * @return string */ - public function getKey() { + 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 + * 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 + * @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 + * @return self */ - public function setKey(string $key) { + public function setKey(string $key) + { $this->key = $key; return $this; } /** - * Get the algorithm of the TSIG key + * Get the algorithm of the TSIG key. * - * @return string + * @return string */ - public function getAlgorithm() { + public function getAlgorithm() + { return $this->algorithm; } /** - * Set the algorithm of the TSIG key + * Set the algorithm of the TSIG key. * - * @param string $algorithm The algorithm of the TSIG key + * @param string $algorithm The algorithm of the TSIG key * - * @return self + * @return self */ - public function setAlgorithm(string $algorithm) { + public function setAlgorithm(string $algorithm) + { $this->algorithm = $algorithm; return $this; @@ -120,42 +132,46 @@ public function setAlgorithm(string $algorithm) { /** * Get the ID for this key, used in the TSIGkey URL endpoint. * - * @return string + * @return string */ - public function getId() { + 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. + * @param string $id The ID for this key, used in the TSIGkey URL endpoint. * - * @return self + * @return self */ - public function setId(string $id) { + public function setId(string $id) + { $this->id = $id; return $this; } /** - * Get the name of the key + * Get the name of the key. * - * @return string + * @return string */ - public function getName() { + public function getName() + { return $this->name; } /** - * Set the name of the key + * Set the name of the key. * - * @param string $name The name of the key + * @param string $name The name of the key * - * @return self + * @return self */ - public function setName(string $name) { + public function setName(string $name) + { $this->name = $name; return $this; diff --git a/src/Resources/TSIGKeySet.php b/src/Resources/TSIGKeySet.php index a82ad57..058a1d6 100644 --- a/src/Resources/TSIGKeySet.php +++ b/src/Resources/TSIGKeySet.php @@ -10,7 +10,8 @@ use IteratorAggregate; use ReturnTypeWillChange; -class TSIGKeySet implements IteratorAggregate, ArrayAccess { +class TSIGKeySet implements IteratorAggregate, ArrayAccess +{ /** * @var TSIGKey[] Array containing resources. */ @@ -21,7 +22,8 @@ class TSIGKeySet implements IteratorAggregate, ArrayAccess { * * @param array|null $resourceRecords (Optional) The tsigkey resources to add. */ - public function __construct(?array $resourceRecords = null) { + public function __construct(?array $resourceRecords = null) + { if ($resourceRecords) { $this->tsigResources = $resourceRecords; } @@ -34,7 +36,8 @@ public function __construct(?array $resourceRecords = null) { * * @return TSIGKeySet The current TSIGKeySet instance. */ - public function addResource(TSIGKey $metaResource): self { + public function addResource(TSIGKey $metaResource): self + { $this->tsigResources[] = $metaResource; return $this; @@ -45,7 +48,8 @@ public function addResource(TSIGKey $metaResource): self { * * @return int The number of tsigkey resources. */ - public function count(): int { + public function count(): int + { return count($this->tsigResources); } @@ -54,7 +58,8 @@ public function count(): int { * * @return bool True when there are tsigkey resources in this collection. */ - public function isNotEmpty(): bool { + public function isNotEmpty(): bool + { return !$this->isEmpty(); } @@ -63,7 +68,8 @@ public function isNotEmpty(): bool { * * @return bool True when there are no tsigkey resources in this collection. */ - public function isEmpty(): bool { + public function isEmpty(): bool + { return empty($this->tsigResources); } @@ -74,7 +80,8 @@ public function isEmpty(): bool { * * @return TSIGKeySet The current TSIGKeySet instance. */ - public function map(Closure $closure): self { + public function map(Closure $closure): self + { foreach ($this->tsigResources as $index => $resource) { $this->tsigResources[$index] = $closure($resource, $index); } @@ -87,7 +94,8 @@ public function map(Closure $closure): self { * * @return bool True when the tsigkey resources are deleted. */ - public function delete(): bool { + public function delete(): bool + { foreach ($this->tsigResources as $resource) { $resource->delete(); } @@ -98,14 +106,16 @@ public function delete(): bool { /** * {@inheritdoc} */ - public function getIterator(): ArrayIterator { + public function getIterator(): ArrayIterator + { return new ArrayIterator($this->tsigResources); } /** * {@inheritdoc} */ - public function offsetExists($offset): bool { + public function offsetExists($offset): bool + { return isset($this->tsigResources[$offset]); } @@ -115,21 +125,24 @@ public function offsetExists($offset): bool { * @return TSIGKey */ #[ReturnTypeWillChange] - public function offsetGet($offset) { + public function offsetGet($offset) + { return $this->tsigResources[$offset]; } /** * {@inheritdoc} */ - public function offsetSet($offset, $value): void { + public function offsetSet($offset, $value): void + { $this->tsigResources[$offset] = $value; } /** * {@inheritdoc} */ - public function offsetUnset($offset): void { + public function offsetUnset($offset): void + { unset($this->tsigResources[$offset]); } } diff --git a/src/TSIGKey.php b/src/TSIGKey.php index 318fe11..121a0db 100644 --- a/src/TSIGKey.php +++ b/src/TSIGKey.php @@ -6,14 +6,15 @@ use Exonet\Powerdns\Resources\TSIGKeySet; use Exonet\Powerdns\Transformers\TSIGKeyTransformer; -class TSIGKey extends AbstractZone { +class TSIGKey extends AbstractZone +{ /** - * Get all tsigkeys on the server - * + * Get all tsigkeys on the server. * * @return TSIGKeySet The meta data set. */ - public function get(): TSIGKeySet { + public function get(): TSIGKeySet + { $items = $this->connector->get('tsigkeys'); $resultSet = new TSIGKeySet(); @@ -25,13 +26,14 @@ public function get(): TSIGKeySet { } /** - * Creat a new TSIG Key + * Creat a new TSIG Key. * * @param array|string $data The data. * * @return TSIGKeySet The created key data set. */ - public function create(TSIGKeyResource $data): TSIGKeySet { + public function create(TSIGKeyResource $data): TSIGKeySet + { $response = $this->connector->post('tsigkeys', new TSIGKeyTransformer($data)); return new TSIGKeySet([new TSIGKeyResource($response)]); @@ -44,8 +46,9 @@ public function create(TSIGKeyResource $data): TSIGKeySet { * * @return bool True if the update was successful. */ - public function update(TSIGKeyResource $key): bool { - $response = $this->connector->put('tsigkeys/' . $key->getId(), new TSIGKeyTransformer($key)); + public function update(TSIGKeyResource $key): bool + { + $response = $this->connector->put('tsigkeys/'.$key->getId(), new TSIGKeyTransformer($key)); // If the response is empty, everything is fine. return empty($response); @@ -58,8 +61,9 @@ public function update(TSIGKeyResource $key): bool { * * @return bool True if the delete was successful. */ - public function delete(TSIGKeyResource $key): bool { - $response = $this->connector->delete('tsigkeys/' . $key->getId()); + public function delete(TSIGKeyResource $key): bool + { + $response = $this->connector->delete('tsigkeys/'.$key->getId()); // If the response is empty, everything is fine. return empty($response); diff --git a/src/TSIGKeyAlgorithms.php b/src/TSIGKeyAlgorithms.php index 86da30f..f8a6998 100644 --- a/src/TSIGKeyAlgorithms.php +++ b/src/TSIGKeyAlgorithms.php @@ -1,19 +1,18 @@ $this->data->getName(), - 'id' => $this->data->getId(), + 'name' => $this->data->getName(), + 'id' => $this->data->getId(), 'algorithm' => $this->data->getAlgorithm(), - 'key' => $this->data->getKey(), - 'type' => $this->data->getType() + 'key' => $this->data->getKey(), + 'type' => $this->data->getType(), ]; } } diff --git a/tests/functional/TSIGKeysTest.php b/tests/functional/TSIGKeysTest.php index 083b235..2d857bb 100644 --- a/tests/functional/TSIGKeysTest.php +++ b/tests/functional/TSIGKeysTest.php @@ -1,4 +1,5 @@ powerdns->tsigkeys(); + $manager = $this->powerdns->tsigkeys(); $resource = new TSIGKeyResource(); $resource->setName($name); @@ -24,17 +25,17 @@ public function testCreateTSIGKey(): void { $this->assertSame(1, $key->count()); $created = $key->offsetGet(0); - $this->assertNotEquals("", $created->getKey()); + $this->assertNotEquals('', $created->getKey()); // cleanup $manager->delete($created); - } - public function testCreateWithNonUrlFriendlyName(): void { + public function testCreateWithNonUrlFriendlyName(): void + { $name = "this/is/not/aa-_412'aur\\asd-url-friendly"; - $manager = $this->powerdns->tsigkeys(); + $manager = $this->powerdns->tsigkeys(); $resource = new TSIGKeyResource(); $resource->setName($name); @@ -45,10 +46,9 @@ public function testCreateWithNonUrlFriendlyName(): void { $this->assertSame(1, $key->count()); $created = $key->offsetGet(0); - $this->assertNotEquals("", $created->getKey()); + $this->assertNotEquals('', $created->getKey()); // cleanup $manager->delete($created); } - } From 6c3775ec8d83bb2d02b97e27c0353863464405fe Mon Sep 17 00:00:00 2001 From: Bennet Gallein Date: Sat, 11 May 2024 22:48:49 +0200 Subject: [PATCH 4/7] feat: add delete function --- src/TSIGKey.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/TSIGKey.php b/src/TSIGKey.php index 121a0db..cb46140 100644 --- a/src/TSIGKey.php +++ b/src/TSIGKey.php @@ -13,7 +13,7 @@ class TSIGKey extends AbstractZone * * @return TSIGKeySet The meta data set. */ - public function get(): TSIGKeySet + public function getAll(): TSIGKeySet { $items = $this->connector->get('tsigkeys'); @@ -25,6 +25,20 @@ public function get(): TSIGKeySet return $resultSet; } + /** + * Get a single tsigkey. + * + * @param string $id the id + * + * @return TSIGKeyResource The meta data set. + */ + public function get(string $id): TSIGKeyResource + { + $item = $this->connector->get('tsigkeys/'.$id); + + return new TSIGKeyResource($item); + } + /** * Creat a new TSIG Key. * From aecac9b043b611fd1922283cbbe4c6a4d98e9b0e Mon Sep 17 00:00:00 2001 From: Bennet Gallein Date: Mon, 13 May 2024 19:38:22 +0200 Subject: [PATCH 5/7] feat: merge testing-branch * feat: working on getting this working * feat: add seperate functions to change the fields * chore: add tests to cover all functions --- src/Resources/TSIGKey.php | 38 ++-- src/TSIGKey.php | 81 ++++++--- src/Transformers/TSIGKeyCreateTransformer.php | 18 ++ src/Transformers/TSIGKeyTransformer.php | 20 --- .../TSIGKeyUpdateAlgorithmTransformer.php | 14 ++ .../TSIGKeyUpdateKeyTransformer.php | 14 ++ .../TSIGKeyUpdateNameTransformer.php | 14 ++ src/Transformers/Transformer.php | 9 +- tests/functional/TSIGKeysTest.php | 162 ++++++++++++++++-- 9 files changed, 276 insertions(+), 94 deletions(-) create mode 100644 src/Transformers/TSIGKeyCreateTransformer.php delete mode 100644 src/Transformers/TSIGKeyTransformer.php create mode 100644 src/Transformers/TSIGKeyUpdateAlgorithmTransformer.php create mode 100644 src/Transformers/TSIGKeyUpdateKeyTransformer.php create mode 100644 src/Transformers/TSIGKeyUpdateNameTransformer.php diff --git a/src/Resources/TSIGKey.php b/src/Resources/TSIGKey.php index c1bb38b..6de7ed7 100644 --- a/src/Resources/TSIGKey.php +++ b/src/Resources/TSIGKey.php @@ -4,8 +4,7 @@ namespace Exonet\Powerdns\Resources; -class TSIGKey -{ +class TSIGKey { /** * The name of the key. * @@ -46,14 +45,13 @@ class TSIGKey * * @param string $content Optional content to set. */ - public function __construct(?array $content = null) - { + 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'] ?? ''); + $this->setType('TSIGKey'); } } @@ -62,8 +60,7 @@ public function __construct(?array $content = null) * * @return string */ - public function getType() - { + public function getType() { return $this->type; } @@ -74,8 +71,7 @@ public function getType() * * @return self */ - public function setType(string $type) - { + public function setType(string $type) { $this->type = $type; return $this; @@ -86,8 +82,7 @@ public function setType(string $type) * * @return string */ - public function getKey() - { + public function getKey() { return $this->key; } @@ -98,8 +93,7 @@ public function getKey() * * @return self */ - public function setKey(string $key) - { + public function setKey(string $key) { $this->key = $key; return $this; @@ -110,8 +104,7 @@ public function setKey(string $key) * * @return string */ - public function getAlgorithm() - { + public function getAlgorithm() { return $this->algorithm; } @@ -122,8 +115,7 @@ public function getAlgorithm() * * @return self */ - public function setAlgorithm(string $algorithm) - { + public function setAlgorithm(string $algorithm) { $this->algorithm = $algorithm; return $this; @@ -134,8 +126,7 @@ public function setAlgorithm(string $algorithm) * * @return string */ - public function getId() - { + public function getId() { return $this->id; } @@ -146,8 +137,7 @@ public function getId() * * @return self */ - public function setId(string $id) - { + public function setId(string $id) { $this->id = $id; return $this; @@ -158,8 +148,7 @@ public function setId(string $id) * * @return string */ - public function getName() - { + public function getName() { return $this->name; } @@ -170,8 +159,7 @@ public function getName() * * @return self */ - public function setName(string $name) - { + public function setName(string $name) { $this->name = $name; return $this; diff --git a/src/TSIGKey.php b/src/TSIGKey.php index cb46140..5075d61 100644 --- a/src/TSIGKey.php +++ b/src/TSIGKey.php @@ -4,17 +4,34 @@ use Exonet\Powerdns\Resources\TSIGKey as TSIGKeyResource; use Exonet\Powerdns\Resources\TSIGKeySet; -use Exonet\Powerdns\Transformers\TSIGKeyTransformer; +use Exonet\Powerdns\Transformers\TSIGKeyCreateTransformer; +use Exonet\Powerdns\Transformers\TSIGKeyUpdateAlgorithmTransformer; +use Exonet\Powerdns\Transformers\TSIGKeyUpdateKeyTransformer; +use Exonet\Powerdns\Transformers\TSIGKeyUpdateNameTransformer; + +class TSIGKey { + + /** + * + * @var ConnectorInterface $connector + */ + private $connector = null; + /** + * get a new instance of the tsig interface + * + * @param ConnectorInterface $connector + */ + public function __construct( + ConnectorInterface $connector) { + $this->connector = $connector; + } -class TSIGKey extends AbstractZone -{ /** * Get all tsigkeys on the server. * * @return TSIGKeySet The meta data set. */ - public function getAll(): TSIGKeySet - { + public function list(): TSIGKeySet { $items = $this->connector->get('tsigkeys'); $resultSet = new TSIGKeySet(); @@ -32,9 +49,8 @@ public function getAll(): TSIGKeySet * * @return TSIGKeyResource The meta data set. */ - public function get(string $id): TSIGKeyResource - { - $item = $this->connector->get('tsigkeys/'.$id); + public function get(string $id): TSIGKeyResource { + $item = $this->connector->get('tsigkeys/' . $id); return new TSIGKeyResource($item); } @@ -44,28 +60,46 @@ public function get(string $id): TSIGKeyResource * * @param array|string $data The data. * - * @return TSIGKeySet The created key data set. + * @return TSIGKeyResource The created key data. */ - public function create(TSIGKeyResource $data): TSIGKeySet - { - $response = $this->connector->post('tsigkeys', new TSIGKeyTransformer($data)); + public function create(TSIGKeyResource $data): TSIGKeyResource { + $response = $this->connector->post('tsigkeys', new TSIGKeyCreateTransformer($data)); - return new TSIGKeySet([new TSIGKeyResource($response)]); + return new TSIGKeyResource($response); } /** - * Update an existing tsig key. + * Update an existing TSIGKey and reset the algorithm + * + * @param TSIGKeyResource $resource The key data item to update. * - * @param TSIGKeyResource $key The key data item to update. + * @return TSIGKeyResource the updated key resource. + */ + public function updateAlgorithm(TSIGKeyResource $resource): TSIGKeyResource { + $response = $this->connector->put('tsigkeys/' . $resource->getId(), new TSIGKeyUpdateAlgorithmTransformer($resource)); + return new TSIGKeyResource($response); + } + + /** + * update the key of a tsigkey * - * @return bool True if the update was successful. + * @param TSIGKeyResource $resource + * @return TSIGKeyResource */ - public function update(TSIGKeyResource $key): bool - { - $response = $this->connector->put('tsigkeys/'.$key->getId(), new TSIGKeyTransformer($key)); + public function updateKey(TSIGKeyResource $resource): TSIGKeyResource { + $response = $this->connector->put('tsigkeys/' . $resource->getId(), new TSIGKeyUpdateKeyTransformer($resource)); + return new TSIGKeyResource($response); + } - // If the response is empty, everything is fine. - return empty($response); + /** + * change the name of a tsigkey, this will change remove the old key and add a new one + * + * @param TSIGKeyResource $resource + * @return TSIGKeyResource + */ + public function updateName(TSIGKeyResource $resource): TSIGKeyResource { + $response = $this->connector->put('tsigkeys/' . $resource->getId(), new TSIGKeyUpdateNameTransformer($resource)); + return new TSIGKeyResource($response); } /** @@ -75,9 +109,8 @@ public function update(TSIGKeyResource $key): bool * * @return bool True if the delete was successful. */ - public function delete(TSIGKeyResource $key): bool - { - $response = $this->connector->delete('tsigkeys/'.$key->getId()); + public function delete(TSIGKeyResource $key): bool { + $response = $this->connector->delete('tsigkeys/' . $key->getId()); // If the response is empty, everything is fine. return empty($response); diff --git a/src/Transformers/TSIGKeyCreateTransformer.php b/src/Transformers/TSIGKeyCreateTransformer.php new file mode 100644 index 0000000..5ac9bcd --- /dev/null +++ b/src/Transformers/TSIGKeyCreateTransformer.php @@ -0,0 +1,18 @@ + $this->data->getName(), + 'id' => $this->data->getId(), + 'algorithm' => $this->data->getAlgorithm(), + 'key' => $this->data->getKey(), + 'type' => $this->data->getType(), + ]; + } +} diff --git a/src/Transformers/TSIGKeyTransformer.php b/src/Transformers/TSIGKeyTransformer.php deleted file mode 100644 index 5dcd1c6..0000000 --- a/src/Transformers/TSIGKeyTransformer.php +++ /dev/null @@ -1,20 +0,0 @@ - $this->data->getName(), - 'id' => $this->data->getId(), - 'algorithm' => $this->data->getAlgorithm(), - 'key' => $this->data->getKey(), - 'type' => $this->data->getType(), - ]; - } -} diff --git a/src/Transformers/TSIGKeyUpdateAlgorithmTransformer.php b/src/Transformers/TSIGKeyUpdateAlgorithmTransformer.php new file mode 100644 index 0000000..90e1c50 --- /dev/null +++ b/src/Transformers/TSIGKeyUpdateAlgorithmTransformer.php @@ -0,0 +1,14 @@ + $this->data->getAlgorithm(), + ]; + } +} diff --git a/src/Transformers/TSIGKeyUpdateKeyTransformer.php b/src/Transformers/TSIGKeyUpdateKeyTransformer.php new file mode 100644 index 0000000..4031059 --- /dev/null +++ b/src/Transformers/TSIGKeyUpdateKeyTransformer.php @@ -0,0 +1,14 @@ + $this->data->getKey(), + ]; + } +} diff --git a/src/Transformers/TSIGKeyUpdateNameTransformer.php b/src/Transformers/TSIGKeyUpdateNameTransformer.php new file mode 100644 index 0000000..bf38eac --- /dev/null +++ b/src/Transformers/TSIGKeyUpdateNameTransformer.php @@ -0,0 +1,14 @@ + $this->data->getName(), + ]; + } +} diff --git a/src/Transformers/Transformer.php b/src/Transformers/Transformer.php index ba5e7bb..fc0e146 100644 --- a/src/Transformers/Transformer.php +++ b/src/Transformers/Transformer.php @@ -2,8 +2,7 @@ namespace Exonet\Powerdns\Transformers; -abstract class Transformer -{ +abstract class Transformer { /** * @var mixed[] Array holding the data to transform. */ @@ -14,8 +13,7 @@ abstract class Transformer * * @param null $data (optional) The data to transform. */ - public function __construct($data = null) - { + public function __construct($data = null) { if ($data) { $this->setData($data); } @@ -28,8 +26,7 @@ public function __construct($data = null) * * @return $this The current transformer instance. */ - public function setData($data): self - { + public function setData($data): self { $this->data = $data; return $this; diff --git a/tests/functional/TSIGKeysTest.php b/tests/functional/TSIGKeysTest.php index 2d857bb..3ca0198 100644 --- a/tests/functional/TSIGKeysTest.php +++ b/tests/functional/TSIGKeysTest.php @@ -8,13 +8,39 @@ /** * @internal */ -class TSIGKeysTest extends FunctionalTestCase -{ - public function testCreateTSIGKey(): void - { - $name = 'test-key'; +class TSIGKeysTest extends FunctionalTestCase { + /** + * test that normal creation of a default key works, asserting that the key is not empty + * + * @return void + */ + public function testCreateTSIGKey(): void { + $name = 'tsigkey-' . mt_rand(100, 10000); + + $manager = $this->powerdns->tsigkeys(); + $resource = new TSIGKeyResource(); + + $resource->setName($name); + $resource->setAlgorithm(TSIGKeyAlgorithms::HMAC_SHA512); + + $key = $manager->create($resource); + + $this->assertNotEquals('', $key->getKey()); + $this->assertEquals($key->getName(), $name); - $manager = $this->powerdns->tsigkeys(); + // cleanup + $manager->delete($key); + } + + /** + * testing the the single endpoint works + * + * @return void + */ + public function testGetSingle(): void { + $name = 'test-key2-' . mt_rand(100, 10000) . microtime(false); + + $manager = $this->powerdns->tsigkeys(); $resource = new TSIGKeyResource(); $resource->setName($name); @@ -22,20 +48,27 @@ public function testCreateTSIGKey(): void $key = $manager->create($resource); - $this->assertSame(1, $key->count()); + // get single key + $fromApi = $manager->get($key->getId()); - $created = $key->offsetGet(0); - $this->assertNotEquals('', $created->getKey()); + $this->assertEquals($key->getId(), $fromApi->getId()); + $this->assertEquals($key->getName(), $fromApi->getName()); + $this->assertEquals($key->getAlgorithm(), $fromApi->getAlgorithm()); + $this->assertEquals($key->getKey(), $fromApi->getKey()); // cleanup - $manager->delete($created); + $res = $manager->delete($key); } - public function testCreateWithNonUrlFriendlyName(): void - { - $name = "this/is/not/aa-_412'aur\\asd-url-friendly"; + /** + * testing that creating a key with a very weird name works + * + * @return void + */ + public function testCreateWithNonUrlFriendlyName(): void { + $name = "this/is/not/aa-_412'aur\\asd-url-friendly-" . mt_rand(100, 10000); - $manager = $this->powerdns->tsigkeys(); + $manager = $this->powerdns->tsigkeys(); $resource = new TSIGKeyResource(); $resource->setName($name); @@ -43,12 +76,103 @@ public function testCreateWithNonUrlFriendlyName(): void $key = $manager->create($resource); - $this->assertSame(1, $key->count()); - - $created = $key->offsetGet(0); - $this->assertNotEquals('', $created->getKey()); + $this->assertNotEquals('', $key->getKey()); // cleanup - $manager->delete($created); + $manager->delete($key); + } + + /** + * testing that the delete function works + * + * @return void + */ + public function testDelete(): void { + $name = 'tsigkey-' . mt_rand(100, 10000); + + $manager = $this->powerdns->tsigkeys(); + $resource = new TSIGKeyResource(); + + $resource->setName($name); + $resource->setAlgorithm(TSIGKeyAlgorithms::HMAC_SHA512); + + $key = $manager->create($resource); + + // delete + $res = $manager->delete($key); + + $this->assertTrue($res); + } + + /** + * testing that changing the algorithm works. + * + * changing the algo does not regenerate the key. + * + * @return void + */ + public function testChangeAlgorithm(): void { + $name = 'tsigkey-' . time(); + + $manager = $this->powerdns->tsigkeys(); + $resource = new TSIGKeyResource(); + + $resource->setName($name); + $resource->setAlgorithm(TSIGKeyAlgorithms::HMAC_SHA512); + + $key = $manager->create($resource); + + // update + $upd = new TSIGKeyResource([ + 'id' => $key->getId(), + 'algorithm' => TSIGKeyAlgorithms::HMAC_SHA256 + ]); + + $updatedKey = $manager->updateAlgorithm($upd); + + + $this->assertNotEquals($updatedKey->getAlgorithm(), $key->getAlgorithm()); + $this->assertEquals($updatedKey->getKey(), $key->getKey()); + + // // delete + $res = $manager->delete($key); + } + + /** + * testign that changing the name works. + * + * As per the note in the powerdns documentation, updating the + * name does create a new key with the same values as the old key, removing the odl one after copy. + * + * @return void + */ + public function testChangeName(): void { + $name = 'tsigkey-' . time(); + $nameToUpdate = 'tsigkey2-' . time(); + + $manager = $this->powerdns->tsigkeys(); + $resource = new TSIGKeyResource(); + + $resource->setName($name); + $resource->setAlgorithm(TSIGKeyAlgorithms::HMAC_SHA512); + + $key = $manager->create($resource); + + // update + $upd = new TSIGKeyResource([ + 'id' => $key->getId(), + 'name' => $nameToUpdate + ]); + + $updatedKey = $manager->updateName($upd); + + + $this->assertEquals($updatedKey->getAlgorithm(), $key->getAlgorithm()); + // the key does not change when updating the name + $this->assertEquals($updatedKey->getKey(), $key->getKey()); + $this->assertEquals($updatedKey->getName(), $nameToUpdate); + + // // delete + $res = $manager->delete($updatedKey); } } From 9fada1398d3779d081bba307227b00e2fffda2b8 Mon Sep 17 00:00:00 2001 From: Bennet Gallein Date: Mon, 13 May 2024 19:38:52 +0200 Subject: [PATCH 6/7] chore: formatting --- src/Resources/TSIGKey.php | 36 ++++++--- src/TSIGKey.php | 57 ++++++++------ src/Transformers/TSIGKeyCreateTransformer.php | 14 ++-- .../TSIGKeyUpdateAlgorithmTransformer.php | 6 +- .../TSIGKeyUpdateKeyTransformer.php | 6 +- .../TSIGKeyUpdateNameTransformer.php | 6 +- src/Transformers/Transformer.php | 9 ++- tests/functional/TSIGKeysTest.php | 77 +++++++++---------- 8 files changed, 120 insertions(+), 91 deletions(-) diff --git a/src/Resources/TSIGKey.php b/src/Resources/TSIGKey.php index 6de7ed7..dd92d2d 100644 --- a/src/Resources/TSIGKey.php +++ b/src/Resources/TSIGKey.php @@ -4,7 +4,8 @@ namespace Exonet\Powerdns\Resources; -class TSIGKey { +class TSIGKey +{ /** * The name of the key. * @@ -45,7 +46,8 @@ class TSIGKey { * * @param string $content Optional content to set. */ - public function __construct(?array $content = null) { + public function __construct(?array $content = null) + { if ($content) { $this->setName($content['name'] ?? ''); $this->setId($content['id'] ?? ''); @@ -60,7 +62,8 @@ public function __construct(?array $content = null) { * * @return string */ - public function getType() { + public function getType() + { return $this->type; } @@ -71,7 +74,8 @@ public function getType() { * * @return self */ - public function setType(string $type) { + public function setType(string $type) + { $this->type = $type; return $this; @@ -82,7 +86,8 @@ public function setType(string $type) { * * @return string */ - public function getKey() { + public function getKey() + { return $this->key; } @@ -93,7 +98,8 @@ public function getKey() { * * @return self */ - public function setKey(string $key) { + public function setKey(string $key) + { $this->key = $key; return $this; @@ -104,7 +110,8 @@ public function setKey(string $key) { * * @return string */ - public function getAlgorithm() { + public function getAlgorithm() + { return $this->algorithm; } @@ -115,7 +122,8 @@ public function getAlgorithm() { * * @return self */ - public function setAlgorithm(string $algorithm) { + public function setAlgorithm(string $algorithm) + { $this->algorithm = $algorithm; return $this; @@ -126,7 +134,8 @@ public function setAlgorithm(string $algorithm) { * * @return string */ - public function getId() { + public function getId() + { return $this->id; } @@ -137,7 +146,8 @@ public function getId() { * * @return self */ - public function setId(string $id) { + public function setId(string $id) + { $this->id = $id; return $this; @@ -148,7 +158,8 @@ public function setId(string $id) { * * @return string */ - public function getName() { + public function getName() + { return $this->name; } @@ -159,7 +170,8 @@ public function getName() { * * @return self */ - public function setName(string $name) { + public function setName(string $name) + { $this->name = $name; return $this; diff --git a/src/TSIGKey.php b/src/TSIGKey.php index 5075d61..67e6d7d 100644 --- a/src/TSIGKey.php +++ b/src/TSIGKey.php @@ -9,20 +9,21 @@ use Exonet\Powerdns\Transformers\TSIGKeyUpdateKeyTransformer; use Exonet\Powerdns\Transformers\TSIGKeyUpdateNameTransformer; -class TSIGKey { - +class TSIGKey +{ /** - * - * @var ConnectorInterface $connector + * @var ConnectorInterface */ - private $connector = null; + private $connector; + /** - * get a new instance of the tsig interface + * get a new instance of the tsig interface. * * @param ConnectorInterface $connector */ public function __construct( - ConnectorInterface $connector) { + ConnectorInterface $connector + ) { $this->connector = $connector; } @@ -31,7 +32,8 @@ public function __construct( * * @return TSIGKeySet The meta data set. */ - public function list(): TSIGKeySet { + public function list(): TSIGKeySet + { $items = $this->connector->get('tsigkeys'); $resultSet = new TSIGKeySet(); @@ -49,8 +51,9 @@ public function list(): TSIGKeySet { * * @return TSIGKeyResource The meta data set. */ - public function get(string $id): TSIGKeyResource { - $item = $this->connector->get('tsigkeys/' . $id); + public function get(string $id): TSIGKeyResource + { + $item = $this->connector->get('tsigkeys/'.$id); return new TSIGKeyResource($item); } @@ -62,43 +65,52 @@ public function get(string $id): TSIGKeyResource { * * @return TSIGKeyResource The created key data. */ - public function create(TSIGKeyResource $data): TSIGKeyResource { + public function create(TSIGKeyResource $data): TSIGKeyResource + { $response = $this->connector->post('tsigkeys', new TSIGKeyCreateTransformer($data)); return new TSIGKeyResource($response); } /** - * Update an existing TSIGKey and reset the algorithm + * Update an existing TSIGKey and reset the algorithm. * * @param TSIGKeyResource $resource The key data item to update. * * @return TSIGKeyResource the updated key resource. */ - public function updateAlgorithm(TSIGKeyResource $resource): TSIGKeyResource { - $response = $this->connector->put('tsigkeys/' . $resource->getId(), new TSIGKeyUpdateAlgorithmTransformer($resource)); + public function updateAlgorithm(TSIGKeyResource $resource): TSIGKeyResource + { + $response = $this->connector->put('tsigkeys/'.$resource->getId(), new TSIGKeyUpdateAlgorithmTransformer($resource)); + return new TSIGKeyResource($response); } /** - * update the key of a tsigkey + * update the key of a tsigkey. * * @param TSIGKeyResource $resource + * * @return TSIGKeyResource */ - public function updateKey(TSIGKeyResource $resource): TSIGKeyResource { - $response = $this->connector->put('tsigkeys/' . $resource->getId(), new TSIGKeyUpdateKeyTransformer($resource)); + public function updateKey(TSIGKeyResource $resource): TSIGKeyResource + { + $response = $this->connector->put('tsigkeys/'.$resource->getId(), new TSIGKeyUpdateKeyTransformer($resource)); + return new TSIGKeyResource($response); } /** - * change the name of a tsigkey, this will change remove the old key and add a new one + * change the name of a tsigkey, this will change remove the old key and add a new one. * * @param TSIGKeyResource $resource + * * @return TSIGKeyResource */ - public function updateName(TSIGKeyResource $resource): TSIGKeyResource { - $response = $this->connector->put('tsigkeys/' . $resource->getId(), new TSIGKeyUpdateNameTransformer($resource)); + public function updateName(TSIGKeyResource $resource): TSIGKeyResource + { + $response = $this->connector->put('tsigkeys/'.$resource->getId(), new TSIGKeyUpdateNameTransformer($resource)); + return new TSIGKeyResource($response); } @@ -109,8 +121,9 @@ public function updateName(TSIGKeyResource $resource): TSIGKeyResource { * * @return bool True if the delete was successful. */ - public function delete(TSIGKeyResource $key): bool { - $response = $this->connector->delete('tsigkeys/' . $key->getId()); + public function delete(TSIGKeyResource $key): bool + { + $response = $this->connector->delete('tsigkeys/'.$key->getId()); // If the response is empty, everything is fine. return empty($response); diff --git a/src/Transformers/TSIGKeyCreateTransformer.php b/src/Transformers/TSIGKeyCreateTransformer.php index 5ac9bcd..1d11fae 100644 --- a/src/Transformers/TSIGKeyCreateTransformer.php +++ b/src/Transformers/TSIGKeyCreateTransformer.php @@ -2,17 +2,19 @@ namespace Exonet\Powerdns\Transformers; -class TSIGKeyCreateTransformer extends Transformer { +class TSIGKeyCreateTransformer extends Transformer +{ /** * {@inheritdoc} */ - public function transform() { + public function transform() + { return (object) [ - 'name' => $this->data->getName(), - 'id' => $this->data->getId(), + 'name' => $this->data->getName(), + 'id' => $this->data->getId(), 'algorithm' => $this->data->getAlgorithm(), - 'key' => $this->data->getKey(), - 'type' => $this->data->getType(), + 'key' => $this->data->getKey(), + 'type' => $this->data->getType(), ]; } } diff --git a/src/Transformers/TSIGKeyUpdateAlgorithmTransformer.php b/src/Transformers/TSIGKeyUpdateAlgorithmTransformer.php index 90e1c50..2c434d4 100644 --- a/src/Transformers/TSIGKeyUpdateAlgorithmTransformer.php +++ b/src/Transformers/TSIGKeyUpdateAlgorithmTransformer.php @@ -2,11 +2,13 @@ namespace Exonet\Powerdns\Transformers; -class TSIGKeyUpdateAlgorithmTransformer extends Transformer { +class TSIGKeyUpdateAlgorithmTransformer extends Transformer +{ /** * {@inheritdoc} */ - public function transform() { + public function transform() + { return (object) [ 'algorithm' => $this->data->getAlgorithm(), ]; diff --git a/src/Transformers/TSIGKeyUpdateKeyTransformer.php b/src/Transformers/TSIGKeyUpdateKeyTransformer.php index 4031059..3d3d4c8 100644 --- a/src/Transformers/TSIGKeyUpdateKeyTransformer.php +++ b/src/Transformers/TSIGKeyUpdateKeyTransformer.php @@ -2,11 +2,13 @@ namespace Exonet\Powerdns\Transformers; -class TSIGKeyUpdateKeyTransformer extends Transformer { +class TSIGKeyUpdateKeyTransformer extends Transformer +{ /** * {@inheritdoc} */ - public function transform() { + public function transform() + { return (object) [ 'key' => $this->data->getKey(), ]; diff --git a/src/Transformers/TSIGKeyUpdateNameTransformer.php b/src/Transformers/TSIGKeyUpdateNameTransformer.php index bf38eac..ddae7e9 100644 --- a/src/Transformers/TSIGKeyUpdateNameTransformer.php +++ b/src/Transformers/TSIGKeyUpdateNameTransformer.php @@ -2,11 +2,13 @@ namespace Exonet\Powerdns\Transformers; -class TSIGKeyUpdateNameTransformer extends Transformer { +class TSIGKeyUpdateNameTransformer extends Transformer +{ /** * {@inheritdoc} */ - public function transform() { + public function transform() + { return (object) [ 'name' => $this->data->getName(), ]; diff --git a/src/Transformers/Transformer.php b/src/Transformers/Transformer.php index fc0e146..ba5e7bb 100644 --- a/src/Transformers/Transformer.php +++ b/src/Transformers/Transformer.php @@ -2,7 +2,8 @@ namespace Exonet\Powerdns\Transformers; -abstract class Transformer { +abstract class Transformer +{ /** * @var mixed[] Array holding the data to transform. */ @@ -13,7 +14,8 @@ abstract class Transformer { * * @param null $data (optional) The data to transform. */ - public function __construct($data = null) { + public function __construct($data = null) + { if ($data) { $this->setData($data); } @@ -26,7 +28,8 @@ public function __construct($data = null) { * * @return $this The current transformer instance. */ - public function setData($data): self { + public function setData($data): self + { $this->data = $data; return $this; diff --git a/tests/functional/TSIGKeysTest.php b/tests/functional/TSIGKeysTest.php index 3ca0198..a0e87ea 100644 --- a/tests/functional/TSIGKeysTest.php +++ b/tests/functional/TSIGKeysTest.php @@ -8,16 +8,16 @@ /** * @internal */ -class TSIGKeysTest extends FunctionalTestCase { +class TSIGKeysTest extends FunctionalTestCase +{ /** - * test that normal creation of a default key works, asserting that the key is not empty - * - * @return void + * test that normal creation of a default key works, asserting that the key is not empty. */ - public function testCreateTSIGKey(): void { - $name = 'tsigkey-' . mt_rand(100, 10000); + public function testCreateTSIGKey(): void + { + $name = 'tsigkey-'.mt_rand(100, 10000); - $manager = $this->powerdns->tsigkeys(); + $manager = $this->powerdns->tsigkeys(); $resource = new TSIGKeyResource(); $resource->setName($name); @@ -33,14 +33,13 @@ public function testCreateTSIGKey(): void { } /** - * testing the the single endpoint works - * - * @return void + * testing the the single endpoint works. */ - public function testGetSingle(): void { - $name = 'test-key2-' . mt_rand(100, 10000) . microtime(false); + public function testGetSingle(): void + { + $name = 'test-key2-'.mt_rand(100, 10000).microtime(false); - $manager = $this->powerdns->tsigkeys(); + $manager = $this->powerdns->tsigkeys(); $resource = new TSIGKeyResource(); $resource->setName($name); @@ -61,14 +60,13 @@ public function testGetSingle(): void { } /** - * testing that creating a key with a very weird name works - * - * @return void + * testing that creating a key with a very weird name works. */ - public function testCreateWithNonUrlFriendlyName(): void { - $name = "this/is/not/aa-_412'aur\\asd-url-friendly-" . mt_rand(100, 10000); + public function testCreateWithNonUrlFriendlyName(): void + { + $name = "this/is/not/aa-_412'aur\\asd-url-friendly-".mt_rand(100, 10000); - $manager = $this->powerdns->tsigkeys(); + $manager = $this->powerdns->tsigkeys(); $resource = new TSIGKeyResource(); $resource->setName($name); @@ -83,14 +81,13 @@ public function testCreateWithNonUrlFriendlyName(): void { } /** - * testing that the delete function works - * - * @return void + * testing that the delete function works. */ - public function testDelete(): void { - $name = 'tsigkey-' . mt_rand(100, 10000); + public function testDelete(): void + { + $name = 'tsigkey-'.mt_rand(100, 10000); - $manager = $this->powerdns->tsigkeys(); + $manager = $this->powerdns->tsigkeys(); $resource = new TSIGKeyResource(); $resource->setName($name); @@ -108,13 +105,12 @@ public function testDelete(): void { * testing that changing the algorithm works. * * changing the algo does not regenerate the key. - * - * @return void */ - public function testChangeAlgorithm(): void { - $name = 'tsigkey-' . time(); + public function testChangeAlgorithm(): void + { + $name = 'tsigkey-'.time(); - $manager = $this->powerdns->tsigkeys(); + $manager = $this->powerdns->tsigkeys(); $resource = new TSIGKeyResource(); $resource->setName($name); @@ -124,13 +120,12 @@ public function testChangeAlgorithm(): void { // update $upd = new TSIGKeyResource([ - 'id' => $key->getId(), - 'algorithm' => TSIGKeyAlgorithms::HMAC_SHA256 + 'id' => $key->getId(), + 'algorithm' => TSIGKeyAlgorithms::HMAC_SHA256, ]); $updatedKey = $manager->updateAlgorithm($upd); - $this->assertNotEquals($updatedKey->getAlgorithm(), $key->getAlgorithm()); $this->assertEquals($updatedKey->getKey(), $key->getKey()); @@ -143,14 +138,13 @@ public function testChangeAlgorithm(): void { * * As per the note in the powerdns documentation, updating the * name does create a new key with the same values as the old key, removing the odl one after copy. - * - * @return void */ - public function testChangeName(): void { - $name = 'tsigkey-' . time(); - $nameToUpdate = 'tsigkey2-' . time(); + public function testChangeName(): void + { + $name = 'tsigkey-'.time(); + $nameToUpdate = 'tsigkey2-'.time(); - $manager = $this->powerdns->tsigkeys(); + $manager = $this->powerdns->tsigkeys(); $resource = new TSIGKeyResource(); $resource->setName($name); @@ -160,13 +154,12 @@ public function testChangeName(): void { // update $upd = new TSIGKeyResource([ - 'id' => $key->getId(), - 'name' => $nameToUpdate + 'id' => $key->getId(), + 'name' => $nameToUpdate, ]); $updatedKey = $manager->updateName($upd); - $this->assertEquals($updatedKey->getAlgorithm(), $key->getAlgorithm()); // the key does not change when updating the name $this->assertEquals($updatedKey->getKey(), $key->getKey()); From 818ffeb1a97981c95db7c339a4b1eb8f72258a98 Mon Sep 17 00:00:00 2001 From: Bennet Gallein Date: Mon, 3 Jun 2024 18:25:31 +0200 Subject: [PATCH 7/7] fix: remove type setter --- src/Resources/TSIGKey.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/Resources/TSIGKey.php b/src/Resources/TSIGKey.php index dd92d2d..7d132b5 100644 --- a/src/Resources/TSIGKey.php +++ b/src/Resources/TSIGKey.php @@ -67,20 +67,6 @@ 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. *