Example #1
0
 private function createJob()
 {
     $tokenData = self::$sapiClient->verifyToken();
     /** @var ObjectEncryptor $configEncryptor */
     $configEncryptor = self::$kernel->getContainer()->get('syrup.object_encryptor');
     return new Job($configEncryptor, ['id' => self::$sapiClient->generateId(), 'runId' => self::$sapiClient->generateId(), 'project' => ['id' => $tokenData['owner']['id'], 'name' => $tokenData['owner']['name']], 'token' => ['id' => $tokenData['id'], 'description' => $tokenData['description'], 'token' => self::$encryptor->encrypt(self::$sapiClient->getTokenString())], 'component' => SYRUP_APP_NAME, 'command' => 'run', 'process' => ['host' => 'test', 'pid' => posix_getpid()], 'createdTime' => date('c')], null, null, null);
 }
 public function setUp()
 {
     $this->client = OrchestratorApi::factory(array('url' => FUNCTIONAL_ORCHESTRATOR_API_URL, 'token' => FUNCTIONAL_ORCHESTRATOR_API_TOKEN));
     $this->sapiClient = new StorageApi(array('token' => FUNCTIONAL_ORCHESTRATOR_API_TOKEN, 'url' => defined('FUNCTIONAL_SAPI_URL') ? FUNCTIONAL_SAPI_URL : null));
     $this->sapiClient->verifyToken();
     // clean old tests
     $this->cleanWorkspace();
 }
Example #3
0
 public function create($command, array $params = [], $lockName = null)
 {
     if (!$this->storageApiClient) {
         throw new \Exception('Storage API client must be set');
     }
     $tokenData = $this->storageApiClient->verifyToken();
     $job = new Job($this->configEncryptor, ['id' => $this->storageApiClient->generateId(), 'runId' => $this->storageApiClient->generateRunId($this->storageApiClient->getRunId()), 'project' => ['id' => $tokenData['owner']['id'], 'name' => $tokenData['owner']['name']], 'token' => ['id' => $tokenData['id'], 'description' => $tokenData['description'], 'token' => $this->encryptor->encrypt($this->storageApiClient->getTokenString())], 'component' => $this->componentName, 'command' => $command, 'params' => $params, 'process' => ['host' => gethostname(), 'pid' => getmypid()], 'nestingLevel' => 0, 'createdTime' => date('c')], null, null, null);
     if ($lockName) {
         $job->setLockName($lockName);
     }
     $componentConfiguration = $this->getComponentConfiguration();
     if (isset($componentConfiguration['flags']) && in_array('encrypt', $componentConfiguration['flags'])) {
         $job->setEncrypted(true);
     }
     return $job;
 }
 private function getSapiServiceStub()
 {
     $storageApiClient = new Client(['url' => STORAGE_API_URL, 'token' => STORAGE_API_TOKEN, 'userAgent' => 'docker-bundle']);
     $tokenData = $storageApiClient->verifyToken();
     $storageServiceStub = $this->getMockBuilder(StorageApiService::class)->disableOriginalConstructor()->getMock();
     $storageServiceStub->expects($this->any())->method("getClient")->will($this->returnValue($this->client));
     $storageServiceStub->expects($this->any())->method("getTokenData")->will($this->returnValue($tokenData));
     return $storageServiceStub;
 }
Example #5
0
 protected function verifyClient(Client $client)
 {
     try {
         $this->tokenData = $client->verifyToken();
         return $client;
     } catch (ClientException $e) {
         if ($e->getCode() == 401) {
             throw new UserException("Invalid StorageApi Token");
         }
         throw $e;
     }
 }
Example #6
0
 protected function verifyClient(Client $client)
 {
     try {
         $this->tokenData = $client->verifyToken();
         return $client;
     } catch (ClientException $e) {
         if ($e->getCode() == 401) {
             throw new SimpleException($e->getCode(), "Invalid StorageApi Token", $e);
         } elseif ($e->getCode() < 500) {
             throw new SimpleException($e->getCode(), $e->getMessage(), $e);
         }
         throw $e;
     }
 }
 public function testConnection()
 {
     $testing = $this->container->getParameter('testing');
     $tokenData = $this->storageApi->verifyToken();
     /** @var Encryptor $encryptor */
     $encryptor = $this->container->get('syrup.encryptor');
     /** @var ObjectEncryptor $objectEncryptor */
     $objectEncryptor = $this->container->get('syrup.object_encryptor');
     /** @var Executor $executor */
     $executor = $this->container->get('syrup.job_executor');
     $executor->setStorageApi($this->storageApi);
     $result = $executor->execute(new Job($objectEncryptor, ['id' => $this->storageApi->generateId(), 'runId' => $this->storageApi->generateId(), 'project' => ['id' => $tokenData['owner']['id'], 'name' => $tokenData['owner']['name']], 'token' => ['id' => $tokenData['id'], 'description' => $tokenData['description'], 'token' => $encryptor->encrypt($this->storageApi->getTokenString())], 'component' => $this->componentName, 'command' => 'test', 'params' => $testing['db']['mysql'], 'process' => ['host' => gethostname(), 'pid' => getmypid()], 'createdTime' => date('c')]));
     $this->assertArrayHasKey('status', $result);
     $this->assertEquals('ok', $result['status']);
 }
 public function externalAuthAction(Request $request)
 {
     // check token - if expired redirect to error page
     try {
         $sapi = new StorageApi(array('token' => $request->request->get('token'), 'userAgent' => $this->componentName));
         $sapi->verifyToken();
     } catch (ClientException $e) {
         if ($e->getCode() == 401) {
             return $this->render('KeboolaGoogleDriveWriterBundle:Oauth:expired.html.twig');
         } else {
             throw $e;
         }
     }
     $request->request->set('token', $request->query->get('token'));
     $request->request->set('account', $request->query->get('account'));
     $request->request->set('referrer', $request->query->get('referrer'));
     return $this->forward('KeboolaGoogleDriveWriterBundle:Oauth:oauth');
 }
Example #9
0
 public function __construct(StorageApiClient $storageApi)
 {
     $this->tokenData = $storageApi->verifyToken();
     $this->token = $storageApi->token;
 }
 /**
  * @param string $token
  * @return StorageApi
  */
 protected function getStorageApiToken($token)
 {
     $sapi = new StorageApi(["token" => $token, "userAgent" => 'oauth-v2', "url" => $this->container->getParameter('storage_api.url')]);
     try {
         $tokenInfo = $sapi->verifyToken();
     } catch (Keboola\StorageApi\ClientException $e) {
         throw new UserException($e->getMessage());
     }
     return $tokenInfo;
 }
 public function oauthCallbackAction()
 {
     /** @var Session $session */
     $session = $this->get('session');
     $googleApi = $this->getGoogleApi();
     $token = $session->get('token');
     $accountId = $session->get('account');
     $referrer = $session->get('referrer');
     $session->clear();
     $code = $this->get('request')->query->get('code');
     if (empty($token) || empty($accountId)) {
         throw new UserException('Auth session expired');
     }
     if (empty($code)) {
         throw new UserException('Could not read from Google API');
     }
     try {
         $storageApi = new StorageApi(['token' => $token, 'userAgent' => 'ex-google-analytics']);
         $tokenData = $storageApi->verifyToken();
         /** @var EncryptorInterface $encryptor */
         $encryptor = $this->get('syrup.encryptor');
         $configuration = new Configuration('ex-google-analytics', $encryptor);
         $configuration->setStorageApi($storageApi);
         $tokens = $googleApi->authorize($code, $this->container->get('router')->generate('keboola_google_analytics_oauth_callback', array(), UrlGeneratorInterface::ABSOLUTE_URL));
         $googleApi->setCredentials($tokens['access_token'], $tokens['refresh_token']);
         $userData = json_decode($googleApi->request('/oauth2/v2/userinfo')->getBody(), true);
         /** @var Account $account */
         $account = $configuration->getAccountBy('accountId', $accountId);
         if (null == $account) {
             throw new ConfigurationException("Account doesn't exist");
         }
         $userName = isset($userData['name']) ? $userData['name'] : $userData['displayName'];
         $userEmail = isset($userData['email']) ? $userData['email'] : $userData['emails'][0]['value'];
         $account->setGoogleId($userData['id'])->setGoogleName($userName)->setEmail($userEmail)->setAccessToken($tokens['access_token'])->setRefreshToken($tokens['refresh_token'])->setOwner($tokenData['description']);
         if ($account->isExternal()) {
             if ($userEmail == $tokenData['description'] || !isset($tokenData['creatorToken'])) {
                 // user generated an external link for himself or is reauthorizing himself into his config which was external before
                 $account->setExternal(false);
             } else {
                 $account->setOwner($tokenData['creatorToken']['description']);
             }
         }
         $account->save();
         if ($referrer) {
             return new RedirectResponse($referrer);
         } else {
             return new JsonResponse(array('status' => 'ok'));
         }
     } catch (ClientException $e) {
         // SAPI exception - probably invalid access token
         throw new UserException($e->getMessage());
     } catch (\Exception $e) {
         // any other exception
         throw new ApplicationException('Error finishing authorization: ' . $e->getMessage(), $e);
     }
 }
 public function testProjectEnableDisable()
 {
     $organization = $this->client->createOrganization($this->testMaintainerId, ['name' => 'My org']);
     $project = $this->client->createProject($organization['id'], ['name' => 'My test']);
     $this->assertFalse($project['isDisabled']);
     $storageToken = $this->client->createProjectStorageToken($project['id'], ['description' => 'test']);
     $disableReason = 'Disable test';
     $this->client->disableProject($project['id'], ['disableReason' => $disableReason, 'estimatedEndTime' => '+1 hour']);
     $project = $this->client->getProject($project['id']);
     $this->assertTrue($project['isDisabled']);
     $this->assertEquals($disableReason, $project['disabled']['reason']);
     $this->assertNotEmpty($project['disabled']['estimatedEndTime']);
     $client = new Client(['url' => getenv('KBC_MANAGE_API_URL'), 'token' => $storageToken['token'], 'backoffMaxTries' => 1]);
     try {
         $client->verifyToken();
         $this->fail('Token should be disabled');
     } catch (\Keboola\StorageApi\ClientException $e) {
         $this->assertEquals($e->getStringCode(), 'MAINTENANCE');
         $this->assertEquals($e->getMessage(), $disableReason);
     }
     $this->client->enableProject($project['id']);
     $project = $this->client->getProject($project['id']);
     $this->assertFalse($project['isDisabled']);
     $storageToken = $client->verifyToken();
     $this->assertNotEmpty($storageToken);
 }
 public function oauthCallbackAction()
 {
     $session = $this->get('session');
     $token = $session->get('token');
     $accountId = $session->get('account');
     $referrer = $session->get('referrer');
     if ($token == null) {
         throw new UserException("Your session expired, please try again");
     }
     $code = $this->get('request')->query->get('code');
     if (empty($code)) {
         throw new SyrupComponentException(400, 'Could not read from Google API');
     }
     $googleApi = $this->getGoogleApi();
     try {
         $storageApi = new StorageApi(['token' => $token, 'url' => null, 'userAgent' => $this->componentName]);
         $tokenData = $storageApi->verifyToken();
         /** @var EncryptorInterface $encryptor */
         $encryptor = $this->get('syrup.encryptor');
         $configuration = new Configuration($this->componentName, $encryptor);
         $configuration->setStorageApi($storageApi);
         $tokens = $googleApi->authorize($code, $this->container->get('router')->generate('keboola_google_drive_oauth_callback', array(), UrlGeneratorInterface::ABSOLUTE_URL));
         $googleApi->setCredentials($tokens['access_token'], $tokens['refresh_token']);
         $userData = json_decode($googleApi->request('https://www.googleapis.com/oauth2/v2/userinfo')->getBody(), true);
         $account = $configuration->getAccountBy('accountId', $accountId);
         if (null == $account) {
             throw new ConfigurationException("Account doesn't exist");
         }
         $account->setGoogleId($userData['id'])->setGoogleName($userData['name'])->setEmail($userData['email'])->setAccessToken($tokens['access_token'])->setRefreshToken($tokens['refresh_token'])->setOwner($tokenData['description']);
         if ($account->isExternal()) {
             if ($userData['email'] == $tokenData['description'] || !isset($tokenData['creatorToken'])) {
                 // user generated an external link for himself or is reauthorizing himself into his config which was external before
                 $account->setExternal(false);
             } else {
                 $account->setOwner($tokenData['creatorToken']['description']);
             }
         }
         $account->save();
         $this->container->get('session')->clear();
         if ($referrer) {
             return new RedirectResponse($referrer);
         } else {
             return new JsonResponse(array('status' => 'ok'));
         }
     } catch (\Exception $e) {
         throw new SyrupComponentException(500, 'Could not save API tokens', $e);
     }
 }