Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cant connect to PowerdDNS - php fatal error #11

Closed
tkald opened this issue Jul 11, 2019 · 8 comments · Fixed by #13
Closed

Cant connect to PowerdDNS - php fatal error #11

tkald opened this issue Jul 11, 2019 · 8 comments · Fixed by #13
Assignees

Comments

@tkald
Copy link

tkald commented Jul 11, 2019

Detailed description

Seems like I can't connect to PDNS api - even though everything seems correct.
Changed also api port to 8081 in /src/Powerdns.php file.

php add.php

PHP Fatal error:  Uncaught Error: Wrong parameters for Exonet\Powerdns\Exceptions\PowerdnsException([string $message [, long $code [, Throwable $previous = NULL]]]) in /root/www/vendor/exonet/powerdns-php/src/Connector.php:159
Stack trace:
#0 /root/www/vendor/exonet/powerdns-php/src/Connector.php(159): Exception->__construct(NULL)
#1 /root/www/vendor/exonet/powerdns-php/src/Connector.php(128): Exonet\Powerdns\Connector->parseResponse(Object(GuzzleHttp\Psr7\Response))
#2 /root/www/vendor/exonet/powerdns-php/src/Connector.php(63): Exonet\Powerdns\Connector->makeCall('POST', 'zones', '{"name":"exampl...')
#3 /root/www/vendor/exonet/powerdns-php/src/Powerdns.php(146): Exonet\Powerdns\Connector->post('zones', Object(Exonet\Powerdns\Transformers\CreateZoneTransformer))
#4 /root/www/vendor/exonet/powerdns-php/examples/add.php(13): Exonet\Powerdns\Powerdns->createZone('example.com.', Array)
#5 {main}
  thrown in /root/www/vendor/exonet/powerdns-php/src/Connector.php on line 159

cat add.php

<?php

require __DIR__.'/../../../../vendor/autoload.php';
require __DIR__.'/CliLogger.php';
use Exonet\Powerdns\Powerdns;
use Exonet\Powerdns\RecordType;

// Initialize the Powerdns client.
$powerdns = new Powerdns('192.168.40.175', 'redacted_key');
// Create a new zone.
$zone = $powerdns->createZone(
    'example.com',
    ['ns1.example.com.', 'ns2.example.']
);

// Add two DNS records to the zone.
$zone->create([
    ['type' => RecordType::A, 'content' => '127.0.0.1', 'ttl' => 60, 'name' => '@'],
    ['type' => RecordType::A, 'content' => '127.0.0.1', 'ttl' => 60, 'name' => 'www'],
]);

Your environment

  • Ubuntu 18.04

  • PHP 7.2.19-0ubuntu0.18.04.1 (cli)

  • powerdns-php v 1.0

  • pdns-server v 4.1.10-1pdns.xenial

@trizz
Copy link
Member

trizz commented Jul 12, 2019

Hi @tkald,

Can you add $powerdns->setLogger(new CliLogger()); to your script, just below $powerdns = new Powerdns('192.168.40.175', 'redacted_key');? Apparently an error is returned from PowerDNS, but without content (or at least not an error object). By 'activating' the CLI logger some more info is displayed for each step and it should at least show the HTTP code as returned by PowerDNS.

@tkald
Copy link
Author

tkald commented Jul 12, 2019

Thank you, @trizz !

Added logger line to php and got 400 error:

php add.php
[ DEBUG ]     Sending [POST] request
                url: 192.168.40.175:8081/api/v1/servers/localhost/zones
                headers: Array
                (
                    [X-API-Key] => redacted_key
                    [Accept] => application/json
                    [User-Agent] => exonet-powerdns-php/v1.0.0
                )

                payload: {"name":"example.com.","kind":"Native","dnssec":false,"api_rectify":true,"soa_edit_api":"INCEPTION-INCREMENT","masters":[],"nameservers":["ns1.example.com.","ns2.example."]}
[ DEBUG ]     Request completed
                statusCode: 400
PHP Fatal error:  Uncaught Error: Wrong parameters for Exonet\Powerdns\Exceptions\PowerdnsException([string $message [, long $code [, Throwable $previous = NULL]]]) in /root/www/vendor/exonet/powerdns-php/src/Connector.php:159
Stack trace:
#0 /root/www/vendor/exonet/powerdns-php/src/Connector.php(159): Exception->__construct(NULL)
#1 /root/www/vendor/exonet/powerdns-php/src/Connector.php(128): Exonet\Powerdns\Connector->parseResponse(Object(GuzzleHttp\Psr7\Response))
#2 /root/www/vendor/exonet/powerdns-php/src/Connector.php(63): Exonet\Powerdns\Connector->makeCall('POST', 'zones', '{"name":"exampl...')
#3 /root/www/vendor/exonet/powerdns-php/src/Powerdns.php(146): Exonet\Powerdns\Connector->post('zones', Object(Exonet\Powerdns\Transformers\CreateZoneTransformer))
#4 /root/www/vendor/exonet/powerdns-php/examples/add.php(14): Exonet\Powerdns\Powerdns->createZone('example.com.', Array)
#5 {main}
  thrown in /root/www/vendor/exonet/powerdns-php/src/Connector.php on line 159

Tried same payload/headers directly with curl - no problem creating content.

@trizz trizz self-assigned this Jul 15, 2019
@trizz
Copy link
Member

trizz commented Jul 16, 2019

Unfortunatly, I can not reproduce this specific error. I've used the exact same code as you've posted above, but all without errors. I've pushed a new branch with a possible fix for the 'Uncaught Error' and a fix for the wrong PTR field. Can you try that branch and see if a more clear error is returned?

According to the API documentation it is possible that you're code is not sending the data in UTF8, but I can't validate that.

Changed also api port to 8081 in /src/Powerdns.php file.

If you need to define a different port, it can be done when constructing the client, there is no need to change the package source for that:

$powerdns = new Powerdns('192.168.40.175', 'redacted_key', 8081);

@tkald
Copy link
Author

tkald commented Jul 16, 2019

thaks @trizz !

Switched to new branch.
I added var_dump($response) in parseRespone function and got "Bad Request" response from PDNS:

php add.php
[ DEBUG ]     Sending [POST] request
                url: 192.168.40.175:8081/api/v1/servers/localhost/zones
                headers: Array
                (
                    [X-API-Key] => redacted_key
                    [Accept] => application/json
                    [User-Agent] => exonet-powerdns-php/v1.0.0
                )

                payload: {"name":"example.com.","kind":"Native","dnssec":false,"api_rectify":true,"soa_edit_api":"INCEPTION-INCREMENT","masters":[],"nameservers":["ns1.example.com.","ns2.example."]}
[ DEBUG ]     Request completed
                statusCode: 400
object(GuzzleHttp\Psr7\Response)#26 (6) {
  ["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=>
  string(11) "Bad Request"
  ["statusCode":"GuzzleHttp\Psr7\Response":private]=>
  int(400)
  ["headers":"GuzzleHttp\Psr7\Response":private]=>
  array(4) {
    ["Connection"]=>
    array(1) {
      [0]=>
      string(5) "close"
    }
    ["Content-Length"]=>
    array(1) {
      [0]=>
      string(2) "11"
    }
    ["Content-Type"]=>
    array(1) {
      [0]=>
      string(25) "text/plain; charset=utf-8"
    }
    ["Server"]=>
    array(1) {
      [0]=>
      string(15) "PowerDNS/4.1.10"
    }
  }
  ["headerNames":"GuzzleHttp\Psr7\Response":private]=>
  array(4) {
    ["connection"]=>
    string(10) "Connection"
    ["content-length"]=>
    string(14) "Content-Length"
    ["content-type"]=>
    string(12) "Content-Type"
    ["server"]=>
    string(6) "Server"
  }
  ["protocol":"GuzzleHttp\Psr7\Response":private]=>
  string(3) "1.1"
  ["stream":"GuzzleHttp\Psr7\Response":private]=>
  object(GuzzleHttp\Psr7\Stream)#25 (7) {
    ["stream":"GuzzleHttp\Psr7\Stream":private]=>
    resource(45) of type (stream)
    ["size":"GuzzleHttp\Psr7\Stream":private]=>
    NULL
    ["seekable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["readable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["writable":"GuzzleHttp\Psr7\Stream":private]=>
    bool(true)
    ["uri":"GuzzleHttp\Psr7\Stream":private]=>
    string(10) "php://temp"
    ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=>
    array(0) {
    }
  }
}
PHP Fatal error:  Uncaught Error: Wrong parameters for Exonet\Powerdns\Exceptions\PowerdnsException([string $message [, long $code [, Throwable $previous = NULL]]]) in /root/www/vendor/exonet/powerdns-php/src/Connector.php:161
Stack trace:
#0 /root/www/vendor/exonet/powerdns-php/src/Connector.php(161): Exception->__construct(NULL)
#1 /root/www/vendor/exonet/powerdns-php/src/Connector.php(128): Exonet\Powerdns\Connector->parseResponse(Object(GuzzleHttp\Psr7\Response))
#2 /root/www/vendor/exonet/powerdns-php/src/Connector.php(63): Exonet\Powerdns\Connector->makeCall('POST', 'zones', '{"name":"exampl...')
#3 /root/www/vendor/exonet/powerdns-php/src/Powerdns.php(146): Exonet\Powerdns\Connector->post('zones', Object(Exonet\Powerdns\Transformers\CreateZoneTransformer))
#4 /root/www/vendor/exonet/powerdns-php/examples/add.php(14): Exonet\Powerdns\Powerdns->createZone('example.com.', Array)
#5 {main}
  thrown in /root/www/vendor/exonet/powerdns-php/src/Connector.php on line 161

@tkald
Copy link
Author

tkald commented Jul 16, 2019

Did some network packet capture comparisons with curl post request and noticed that with curl header Content-Type => application/x-www-form-urlencoded is added.
So I also added Content-Type to headers application sends in Connector.php:

private function getDefaultHeaders() : array
    {
        return [
            'X-API-Key' => $this->powerdns->getConfig()['apiKey'],
            'Accept' => 'application/json',
            'User-Agent' => 'exonet-powerdns-php/'.Powerdns::CLIENT_VERSION,
            'Content-Type' => 'application/json',
        ];
    }

And voila, example.com domain got created to my PDSN.
Works with application/x-www-form-urlencoded Content-Type also.

@trizz
Copy link
Member

trizz commented Jul 17, 2019

I've updated my branch. Can you validate that the client is working for you as expected?

@tkald
Copy link
Author

tkald commented Jul 17, 2019

Yes, all good. Thank you @trizz !

@trizz trizz mentioned this issue Jul 17, 2019
@trizz
Copy link
Member

trizz commented Jul 17, 2019

@tkald A new version is published!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants