Ejemplo n.º 1
0
 /**
  * @param string $secret String to encrypt
  * @param string $componentId
  * @param string $token SAPI token
  * @return string Encrypted $secret by application $componentId
  */
 public static function encrypt($secret, $componentId, $token = null, $toConfig = false, $sapiUrl)
 {
     if (empty($sapiUrl)) {
         throw new ApplicationException("StorageApi url is empty and must be set");
     }
     $storageApiClient = new StorageApi(["token" => $token, "userAgent" => 'oauth-v2', "url" => $sapiUrl]);
     $components = $storageApiClient->indexAction()["components"];
     $syrupApiUrl = null;
     foreach ($components as $component) {
         if ($component["id"] == 'queue') {
             // strip the component uri to syrup api uri
             // eg https://syrup.keboola.com/docker/docker-demo => https://syrup.keboola.com
             $syrupApiUrl = substr($component["uri"], 0, strpos($component["uri"], "/", 8));
             break;
         }
     }
     if (empty($syrupApiUrl)) {
         throw new ApplicationException("SyrupApi url is empty");
     }
     $config = ['super' => 'docker', "url" => $syrupApiUrl];
     if (!is_null($token)) {
         $config['token'] = $token;
     }
     $client = Client::factory($config);
     try {
         return $client->encryptString($componentId, $secret, $toConfig ? ["path" => "configs"] : []);
     } catch (ClientException $e) {
         throw new UserException("Component based encryption of the app secret failed: " . $e->getMessage());
     }
 }
Ejemplo n.º 2
0
 /**
  * Test runJob method with different states.
  */
 public function testCustomUrlCustomQueueUrl()
 {
     $mock = new MockHandler([new Response(200, ['Content-Type' => 'application/json'], '{
                 "id": 123456,
                 "url": "https://syrup-testing.keboola.com/queue/job/123456",
                 "status": "waiting"
             }'), new Response(200, ['Content-Type' => 'application/json'], '{
                 "id": 123456,
                 "status": "processing"
             }'), new Response(200, ['Content-Type' => 'application/json'], '{
                 "id": 123456,
                 "status": "success"
             }')]);
     // Add the history middleware to the handler stack.
     $container = [];
     $history = Middleware::history($container);
     $stack = HandlerStack::create($mock);
     $stack->push($history);
     $client = new Client(['token' => 'test', 'handler' => $stack, 'url' => 'http://syrup.local', 'queueUrl' => 'http://queue.local']);
     $response = $client->runJob("test-component", ["config" => 1]);
     $this->assertCount(3, $container);
     /** @var Request $request */
     $request = $container[0]['request'];
     $this->assertEquals("http://syrup.local/test-component/run", $request->getUri()->__toString());
     $request = $container[1]['request'];
     $this->assertEquals("http://queue.local/queue/job/123456", $request->getUri()->__toString());
     $request = $container[2]['request'];
     $this->assertEquals("http://queue.local/queue/job/123456", $request->getUri()->__toString());
     $this->assertEquals("success", $response["status"]);
 }