-
Notifications
You must be signed in to change notification settings - Fork 17
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
Add keystone support #12
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,11 @@ class IronCore | |
protected $curl = null; | ||
protected $last_status; | ||
|
||
protected $keystone; | ||
protected $use_keystone; | ||
protected $keystone_token; | ||
protected $keystone_token_expires; | ||
|
||
protected $urlFetchContext; | ||
protected $urlFetchData; | ||
protected $urlFetchUrl; | ||
|
@@ -150,9 +155,16 @@ protected function getConfigData($config) | |
|
||
$this->loadFromHash($this->default_values); | ||
|
||
if (empty($this->token) || empty($this->project_id)) { | ||
if (empty($this->project_id)) { | ||
throw new InvalidArgumentException("token or project_id not found in any of the available sources"); | ||
} | ||
|
||
if (!empty($this->keystone)){ | ||
$required_keys = array('username', 'password', 'tenant', 'server'); | ||
if (count(array_intersect_key(array_flip($required_keys), $this->keystone)) === count($required_keys)) { | ||
$this->use_keystone = True; | ||
} | ||
} | ||
} | ||
|
||
|
||
|
@@ -168,6 +180,7 @@ protected function loadFromHash($options) | |
$this->setVarIfValue('port', $options); | ||
$this->setVarIfValue('api_version', $options); | ||
$this->setVarIfValue('encryption_key', $options); | ||
$this->setVarIfValue('keystone', $options); | ||
} | ||
|
||
protected function loadFromEnv($prefix) | ||
|
@@ -219,17 +232,13 @@ protected function loadConfigFile($file) | |
$this->loadFromHash($data); | ||
} | ||
|
||
protected function apiCall($type, $url, $params = array(), $data = null) | ||
protected function request($type, $url, $params = array(), $data = null) | ||
{ | ||
$url = "{$this->url}$url"; | ||
$this->debug("API $type", $url); | ||
|
||
if ($this->curl == null && $this->curlEnabled()) { | ||
$this->curl = curl_init(); | ||
} | ||
if (!isset($params['oauth'])) { | ||
$params['oauth'] = $this->token; | ||
} | ||
if ($this->curlEnabled()) { | ||
switch ($type) { | ||
case self::DELETE: | ||
|
@@ -305,6 +314,15 @@ protected function apiCall($type, $url, $params = array(), $data = null) | |
return $this->callWithRetries(); | ||
} | ||
|
||
protected function apiCall($type, $url, $params = array(), $data = null) | ||
{ | ||
$url = "{$this->url}$url"; | ||
if (!isset($params['oauth'])) { | ||
$params['oauth'] = $this->token; | ||
} | ||
return $this -> request($type, $url, $params, $data); | ||
} | ||
|
||
protected function callWithRetries() | ||
{ | ||
for ($retry = 0; $retry < $this->max_retries; $retry++) { | ||
|
@@ -421,6 +439,32 @@ protected function setCommonHeaders() | |
'Keep-Alive' => '300' | ||
); | ||
} | ||
|
||
protected function getToken() | ||
{ | ||
$current_time = new DateTime("now", new DateTimeZone('UTC')); | ||
if (is_null($this->keystone_token) || $current_time > $this->keystone_token_expires) { | ||
|
||
$req = array( | ||
'auth' => array( | ||
'tenantName' => $this->keystone['tenant'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tenantId as far as I understand how it works , names are not reliable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @thousandsofthem, could you clarify? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one works for sure, using it in HUD-e: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P.S. Also, tenantId always equal to project_id so could be derived from it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some notes:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any update so far? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We implemented support of format |
||
'passwordCredentials' => array( | ||
'username' => $this->keystone['username'], | ||
'password' => $this->keystone['password'] | ||
) | ||
) | ||
); | ||
$this -> setCommonHeaders(); | ||
$url = $this->keystone['server'].'tokens'; | ||
$response = json_decode($this->request(self::POST, $url, $req), true); | ||
$token = $response['access']['token']; | ||
$timespan = abs(strtotime($token['expires']) - strtotime($token['issued_at'])); | ||
$this->keystone_token_expires = $current_time->add(new DateInterval('PT'.$timespan.'S')); | ||
$this->keystone_token = $token['id']; | ||
} | ||
|
||
return $this->keystone_token; | ||
} | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't find any call of getToken :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's there - https://github.com/iron-io/iron_mq_php/pull/38/files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, sorry, it's iron_core