/** * Creates new http Request object * * @return Request Returns new http Request object */ protected function createHttpRequest() { $req = new Request(); $req->setOptions(['redirect' => 10, 'timeout' => $this->getConfig()->getRequestTimeout(), 'connecttimeout' => $this->getConfig()->getRequestTimeout(), 'cookiesession' => true]); $req->setSslOptions(['verifypeer' => false, 'verifyhost' => false]); $proxySettings = $this->getConfig()->getProxySettings(); if (!empty($proxySettings)) { $req->setOptions(['proxyhost' => $proxySettings['host'], 'proxyport' => $proxySettings['port'], 'proxytype' => $proxySettings['type']]); if ($proxySettings['user']) { $req->setOptions(['proxyauth' => "{$proxySettings['user']}:{$proxySettings['pass']}", 'proxyauthtype' => $proxySettings['authtype']]); } } return $req; }
/** * Creates new http Request object * * @return Request Returns new http Request object */ protected function createHttpRequest() { $req = new Request(); $req->setOptions(['redirect' => 10, 'cookiesession' => true]); $req->setSslOptions(['verifypeer' => false, 'verifyhost' => false]); $proxySettings = $this->cloudstack->getProxy(); if ($proxySettings !== false) { $req->setOptions(['proxyhost' => $proxySettings['host'], 'proxyport' => $proxySettings['port'], 'proxytype' => $proxySettings['type']]); if ($proxySettings['user']) { $req->setOptions(['proxyauth' => "{$proxySettings['user']}:{$proxySettings['pass']}", 'proxyauthtype' => $proxySettings['authtype']]); } } return $req; }
/** * Validates url * * @return boolean Returns true if url endpoint passes validation. * It saves updated properties itself on success * @throws \Scalr\Exception\ScalrException */ public function validateUrl() { if (!$this->isValid && $this->endpointId) { $q = new Request($this->url, "GET"); $q->addHeaders(['X-Scalr-Webhook-Enpoint-Id' => $this->endpointId, 'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8', 'Date' => gmdate('r')]); $q->setOptions(['redirect' => 10, 'timeout' => 10, 'connecttimeout' => 10]); $q->setSslOptions(['verifypeer' => false, 'verifyhost' => false]); $config = \Scalr::getContainer()->config; if ($config('scalr.system.webhooks.use_proxy') && in_array($config('scalr.connections.proxy.use_on'), ['both', 'scalr'])) { $proxySettings = $config('scalr.connections.proxy'); $q->setOptions(['proxyhost' => $proxySettings['host'], 'proxyport' => $proxySettings['port'], 'proxytype' => $proxySettings['type']]); if ($proxySettings['user']) { $q->setOptions(['proxyauth' => "{$proxySettings['user']}:{$proxySettings['pass']}", 'proxyauthtype' => $proxySettings['authtype']]); } } try { $response = \Scalr::getContainer()->http->sendRequest($q); if ($response->getResponseCode() == 200) { $code = trim($response->getBody()->toString()); $h = $response->getHeader('X-Validation-Token'); $this->isValid = $code == $this->validationToken || $h == $this->validationToken; if ($this->isValid) { $this->save(); } } else { throw new ScalrException(sprintf("Validation failed. Endpoint '%s' returned http code %s", strip_tags($this->url), $response->getResponseCode())); } } catch (\http\Exception $e) { throw new ScalrException(sprintf("Validation failed. Cannot connect to '%s'.", strip_tags($this->url))); } } return $this->isValid; }
private function request($path, $method, $data = "") { $data = trim($data); $httpRequest = new Request(); $httpRequest->setSslOptions(['verifypeer' => false, 'verifyhost' => false]); $fullUrl = "{$this->chefServerUrl}{$path}"; $chunks = parse_url($fullUrl); if (in_array($method, ["POST", "PUT"]) && $data) { $httpRequest->append($data); } $httpRequest->setRequestUrl($fullUrl); $httpRequest->setRequestMethod($method); $tz = @date_default_timezone_get(); date_default_timezone_set("UTC"); $timestamp = date("Y-m-d\\TH:i:s\\Z"); date_default_timezone_set($tz); $chunks['path'] = str_replace('//', '/', $chunks['path']); $hashedPath = base64_encode(sha1($chunks['path'], true)); $hashedBody = base64_encode(sha1($data, true)); $userId = $this->username; $str = "Method:{$method}\n" . "Hashed Path:{$hashedPath}\n" . "X-Ops-Content-Hash:{$hashedBody}\n" . "X-Ops-Timestamp:{$timestamp}\n" . "X-Ops-UserId:{$userId}"; $headers = array('x-ops-sign' => "algorithm=sha1;version=1.0", 'x-chef-version' => "0.10.8", 'x-ops-userid' => $userId, 'x-ops-timestamp' => $timestamp, 'x-ops-content-hash' => $hashedBody, 'content-type' => 'application/json', 'accept' => 'application/json'); $r = array_merge($headers, $this->sign($str)); $httpRequest->addHeaders($r); $response = \Scalr::getContainer()->http->sendRequest($httpRequest); if ($response->getResponseCode() == 401) { throw new Exception("Failed to authenticate as {$userId}. Ensure that your node_name and client key are correct."); } if ($response->getResponseCode() == 404) { throw new Exception("Client not found or parameters are not valid"); } else { if ($response->getResponseCode() <= 205) { $data = $response->getBody()->toString(); $retval = empty($data) ? true : json_decode($data); } else { if ($response->getResponseCode() >= 300 && $response->getResponseCode() < 400) { throw new Exception("Request to chef server failed. Chef server returned code {$response->getResponseCode()}. Redirect URL: {$response->getHeader("Location")}"); } else { if ($response->getResponseCode() > 400) { $data = $response->getBody()->toString(); $msg = empty($data) ? "" : json_decode($data); if (is_array($msg->error)) { $msg = $msg->error[0]; } elseif ($msg->error) { $msg = $msg->error; } else { $msg = "Unknown error. Error code: {$response->getResponseCode()}"; } throw new Exception("Request to chef server failed with error: {$msg} ({$method} {$path})"); } else { throw new Exception("Unexpected situation. Response code {$response->getResponseCode()}"); } } } } return $retval; }