php-opencloud / openstack

PHP SDK for OpenStack clouds
Apache License 2.0
222 stars 148 forks source link

401 error while trying to consume identity api #256

Open benIT opened 6 years ago

benIT commented 6 years ago

I am trying to run the following script(adapted from this example):

<?php

/**@doc: https://github.com/php-opencloud/openstack/blob/master/samples/identity/v3/projects/list_projects.php
 * @see to install private cloud certificate https://superuser.com/a/719047/734645
 */
require 'vendor/autoload.php';
$env = [];
$env['username'] = getenv('OS_USERNAME');
$env['password'] = getenv('OS_PASSWORD');
$env['project_name'] = getenv('OS_PROJECT_NAME');//v2: OS_TENANT_NAME VS v3: OS_PROJECT_NAME
$env['project_id'] = getenv('OS_PROJECT_ID');
$env['auth_url'] = getenv('OS_AUTH_URL');
$env['region_name'] = getenv('OS_REGION_NAME');

echo sprintf('checking env var %s', PHP_EOL);
foreach ($env as $key => $value) {
    if (!$value) {
        die(sprintf("please set '%s' env var.", $key));
    } else {
        echo sprintf("%s : %s %s", $key, $value, PHP_EOL);
    }
}
echo sprintf('checking env var ends %s', PHP_EOL);
$openstack = new OpenStack\OpenStack([
    'authUrl' => $env['auth_url'],
    'region' => $env['region_name'],
    'user' => [
        'id' => $env['username'],
        'password' => $env['password']
    ],
    'scope' => ['project' => ['id' => $env['project_id']]]
]);

$identity = $openstack->identityV3(['region' => $env['region_name']]);
foreach ($identity->listProjects() as $project) {
    var_dump($project);
}

But I get a 401 response:

PHP Fatal error:  Uncaught GuzzleHttp\Exception\ClientException: Client error: `POST https://identity.api.company.com/v3/auth/tokens` resulted in a `401 Unauthorized` response:
{"error": {"message": "The request you have made requires authentication.", "code": 401, "title": "Unauthorized"}}
 in /vagrant/shared/swift-sandbox/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113
Stack trace:
#0 /vagrant/shared/swift-sandbox/vendor/guzzlehttp/guzzle/src/Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response))
#1 /vagrant/shared/swift-sandbox/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response))
#2 /vagrant/shared/swift-sandbox/vendor/guzzlehttp/promises/src/Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array)
#3 /vagrant/shared/swift-sandbox/vendor/guzzlehttp/promises/src/TaskQueue.php(47): GuzzleHttp\Promise\Promise::G in /vagrant/shared/swift-sandbox/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113

Env vars have been checked multiple times, and the openstack python CLI is working fine with these env variables.

I also tried other examples from the doc, but I am getting the same 401 response.

Any idea what's wrong with that?

benIT commented 6 years ago

It seems that the documentation is not up to date.

The example in the documentation are still making use of:

$openstack = new OpenStack\OpenStack([
    'authUrl' => '{authUrl}',
    'region'  => '{region}',
    'user'    => [
        'id'       => '{userId}',
        'password' => '{password}'
    ],
    'scope' => [
        'project' => [
            'id' => '{projectId}'
        ]
    ]
]);

which causes 401 error. According to the response given in issue #248 , the following snippet works:

$requestOptions = [
    'verify' => 'certificate.crt',
];

$openstack = new OpenStack\OpenStack([
    'authUrl' => $env['auth_url'],
    'region' => $env['region_name'],
    'user' => [
        'name' => $env['username'],
        'password' => $env['password'],
        'domain' => ['name' => $env['domain_name']]

    ],
    'scope' => [
        'project' => ['id' => $env['project_id']],

    ],
    'requestOptions' => $requestOptions
]);

$service = $openstack->objectStoreV1();
foreach ($service->listContainers() as $container) {
    /** @var $container \OpenStack\ObjectStore\v1\Models\Container */
    var_dump($container);
}

Greetings, Ben