diff --git a/src/Migration/Destinations/Appwrite.php b/src/Migration/Destinations/Appwrite.php index 32de97586b..34ad58d2ea 100644 --- a/src/Migration/Destinations/Appwrite.php +++ b/src/Migration/Destinations/Appwrite.php @@ -50,6 +50,7 @@ use Utopia\Migration\Resources\Database\Table; use Utopia\Migration\Resources\Functions\Deployment; use Utopia\Migration\Resources\Functions\EnvVar; use Utopia\Migration\Resources\Functions\Func; +use Utopia\Migration\Resources\Integrations\ApiKey; use Utopia\Migration\Resources\Integrations\Platform; use Utopia\Migration\Resources\Messaging\Message; use Utopia\Migration\Resources\Messaging\Provider; @@ -81,6 +82,11 @@ class Appwrite extends Destination */ protected $getDatabasesDB; + private bool $consoleKeyFetched = false; + + private ?string $consoleKey = null; + + /** * @var array */ @@ -120,9 +126,39 @@ class Appwrite extends Destination $this->teams = new Teams($this->client); $this->users = new Users($this->client); + $this->headers['x-appwrite-project'] = $this->project; + $this->headers['x-appwrite-key'] = $this->key; + $this->getDatabasesDB = $getDatabasesDB; } + /** + * @return array|null + */ + protected function getConsoleHeaders(): ?array + { + if (!$this->consoleKeyFetched) { + $this->consoleKeyFetched = true; + + try { + $response = $this->call('POST', '/migrations/appwrite/console-key'); + $this->consoleKey = $response['key'] ?? null; + } catch (\Throwable) { + $this->consoleKey = null; + } + } + + if ($this->consoleKey === null) { + return null; + } + + return [ + 'Content-Type' => 'application/json', + 'x-appwrite-project' => 'console', + 'x-appwrite-key' => $this->consoleKey, + ]; + } + public static function getName(): string { return 'Appwrite'; @@ -175,6 +211,7 @@ class Appwrite extends Destination // Integrations Resource::TYPE_PLATFORM, + Resource::TYPE_API_KEY, ]; } @@ -2206,9 +2243,13 @@ class Appwrite extends Destination /** @var Platform $resource */ $this->createPlatform($resource); break; + case Resource::TYPE_API_KEY: + /** @var ApiKey $resource */ + $this->createApiKey($resource); + break; } - if ($resource->getStatus() !== Resource::STATUS_SKIPPED) { + if ($resource->getStatus() !== Resource::STATUS_SKIPPED && $resource->getStatus() !== Resource::STATUS_ERROR) { $resource->setStatus(Resource::STATUS_SUCCESS); } @@ -2258,6 +2299,44 @@ class Appwrite extends Destination return true; } + protected function createApiKey(ApiKey $resource): bool + { + $consoleHeaders = $this->getConsoleHeaders(); + + if ($consoleHeaders === null) { + throw new \Exception('Failed to get console headers'); + } + + try { + $params = [ + 'keyId' => ID::unique(), + 'name' => $resource->getApiKeyName(), + 'scopes' => $resource->getScopes(), + ]; + + $expire = $resource->getExpire(); + if (!empty($expire)) { + $params['expire'] = $expire; + } + + $this->call( + 'POST', + '/projects/' . $this->project . '/keys', + $consoleHeaders, + $params + ); + } catch (\Throwable $e) { + if ($e->getCode() === 409) { + $resource->setStatus(Resource::STATUS_SKIPPED, 'API key already exists'); + return false; + } + + throw $e; + } + + return true; + } + private function validateFieldsForIndexes(Index $resource, UtopiaDocument $table, array &$lengths) { /** diff --git a/src/Migration/Resource.php b/src/Migration/Resource.php index e624fe2cac..1eb4f77d64 100644 --- a/src/Migration/Resource.php +++ b/src/Migration/Resource.php @@ -73,6 +73,7 @@ abstract class Resource implements \JsonSerializable // Integrations public const TYPE_PLATFORM = 'platform'; + public const TYPE_API_KEY = 'api-key'; public const TYPE_SUBSCRIBER = 'subscriber'; public const TYPE_MESSAGE = 'message'; @@ -109,6 +110,7 @@ abstract class Resource implements \JsonSerializable self::TYPE_TEAM, self::TYPE_MEMBERSHIP, self::TYPE_PLATFORM, + self::TYPE_API_KEY, self::TYPE_PROVIDER, self::TYPE_TOPIC, self::TYPE_SUBSCRIBER, diff --git a/src/Migration/Resources/Integrations/ApiKey.php b/src/Migration/Resources/Integrations/ApiKey.php new file mode 100644 index 0000000000..f3d402951d --- /dev/null +++ b/src/Migration/Resources/Integrations/ApiKey.php @@ -0,0 +1,110 @@ + $scopes + * @param string $expire + * @param string $accessedAt + * @param array $sdks + * @param string $createdAt + * @param string $updatedAt + */ + public function __construct( + string $id, + private readonly string $name, + private readonly array $scopes = [], + private readonly string $expire = '', + private readonly string $accessedAt = '', + private readonly array $sdks = [], + string $createdAt = '', + string $updatedAt = '', + ) { + $this->id = $id; + $this->createdAt = $createdAt; + $this->updatedAt = $updatedAt; + } + + /** + * @param array $array + * @return self + */ + public static function fromArray(array $array): self + { + return new self( + $array['id'], + $array['name'], + $array['scopes'] ?? [], + $array['expire'] ?? '', + $array['accessedAt'] ?? '', + $array['sdks'] ?? [], + createdAt: $array['createdAt'] ?? '', + updatedAt: $array['updatedAt'] ?? '', + ); + } + + /** + * @return array + */ + public function jsonSerialize(): array + { + return [ + 'id' => $this->id, + 'name' => $this->name, + 'scopes' => $this->scopes, + 'expire' => $this->expire, + 'accessedAt' => $this->accessedAt, + 'sdks' => $this->sdks, + 'createdAt' => $this->createdAt, + 'updatedAt' => $this->updatedAt, + ]; + } + + public static function getName(): string + { + return Resource::TYPE_API_KEY; + } + + public function getGroup(): string + { + return Transfer::GROUP_INTEGRATIONS; + } + + public function getApiKeyName(): string + { + return $this->name; + } + + /** + * @return array + */ + public function getScopes(): array + { + return $this->scopes; + } + + public function getExpire(): string + { + return $this->expire; + } + + public function getAccessedAt(): string + { + return $this->accessedAt; + } + + /** + * @return array + */ + public function getSdks(): array + { + return $this->sdks; + } +} diff --git a/src/Migration/Sources/Appwrite.php b/src/Migration/Sources/Appwrite.php index 1f51fa6b97..cd2358105f 100644 --- a/src/Migration/Sources/Appwrite.php +++ b/src/Migration/Sources/Appwrite.php @@ -53,6 +53,7 @@ use Utopia\Migration\Resources\Database\VectorsDB; use Utopia\Migration\Resources\Functions\Deployment; use Utopia\Migration\Resources\Functions\EnvVar; use Utopia\Migration\Resources\Functions\Func; +use Utopia\Migration\Resources\Integrations\ApiKey; use Utopia\Migration\Resources\Integrations\Platform; use Utopia\Migration\Resources\Messaging\Message; use Utopia\Migration\Resources\Messaging\Provider; @@ -239,6 +240,7 @@ class Appwrite extends Source // Integrations Resource::TYPE_PLATFORM, + Resource::TYPE_API_KEY, ]; } @@ -2234,13 +2236,13 @@ class Appwrite extends Source */ private function reportIntegrations(array $resources, array &$report, array $resourceIds = []): void { + $consoleHeaders = $this->getConsoleHeaders(); + + if ($consoleHeaders === null) { + return; + } + if (\in_array(Resource::TYPE_PLATFORM, $resources)) { - $consoleHeaders = $this->getConsoleHeaders(); - - if ($consoleHeaders === null) { - return; - } - try { $response = $this->call('GET', '/projects/' . $this->project . '/platforms', $consoleHeaders); $report[Resource::TYPE_PLATFORM] = $response['total'] ?? 0; @@ -2248,6 +2250,15 @@ class Appwrite extends Source $report[Resource::TYPE_PLATFORM] = 0; } } + + if (\in_array(Resource::TYPE_API_KEY, $resources)) { + try { + $response = $this->call('GET', '/projects/' . $this->project . '/keys', $consoleHeaders); + $report[Resource::TYPE_API_KEY] = $response['total'] ?? 0; + } catch (\Throwable) { + $report[Resource::TYPE_API_KEY] = 0; + } + } } /** @@ -2287,6 +2298,14 @@ class Appwrite extends Source $this->exportPlatforms(...) ); } + + if (\in_array(Resource::TYPE_API_KEY, $resources)) { + $this->exportWithConsoleHeaders( + Resource::TYPE_API_KEY, + Transfer::GROUP_INTEGRATIONS, + $this->exportApiKeys(...) + ); + } } protected function exportWithConsoleHeaders(string $resourceType, string $group, callable $callback): void @@ -2374,6 +2393,32 @@ class Appwrite extends Source $this->callback($platforms); } + private function exportApiKeys(array $consoleHeaders): void + { + $response = $this->call('GET', '/projects/' . $this->project . '/keys', $consoleHeaders); + + if (empty($response['keys'])) { + return; + } + + $apiKeys = []; + + foreach ($response['keys'] as $key) { + $apiKeys[] = new ApiKey( + $key['$id'] ?? '', + $key['name'] ?? '', + $key['scopes'] ?? [], + $key['expire'] ?? '', + $key['accessedAt'] ?? '', + $key['sdks'] ?? [], + createdAt: $key['$createdAt'] ?? '', + updatedAt: $key['$updatedAt'] ?? '', + ); + } + + $this->callback($apiKeys); + } + /** * eg.,documents/attributes * @param string $databaseType diff --git a/src/Migration/Transfer.php b/src/Migration/Transfer.php index 59ed486c80..92bf53c68f 100644 --- a/src/Migration/Transfer.php +++ b/src/Migration/Transfer.php @@ -60,6 +60,7 @@ class Transfer public const GROUP_INTEGRATIONS_RESOURCES = [ Resource::TYPE_PLATFORM, + Resource::TYPE_API_KEY, ]; public const GROUP_DOCUMENTSDB_RESOURCES = [ Resource::TYPE_DATABASE_DOCUMENTSDB, @@ -122,6 +123,7 @@ class Transfer // Integrations Resource::TYPE_PLATFORM, + Resource::TYPE_API_KEY, // legacy Resource::TYPE_DOCUMENT, @@ -139,6 +141,7 @@ class Transfer Resource::TYPE_USER, Resource::TYPE_TEAM, Resource::TYPE_PLATFORM, + Resource::TYPE_API_KEY, Resource::TYPE_PROVIDER, Resource::TYPE_TOPIC, Resource::TYPE_MESSAGE, diff --git a/tests/Migration/Unit/Adapters/MockDestination.php b/tests/Migration/Unit/Adapters/MockDestination.php index 2aa528a8d4..68f0eec98e 100644 --- a/tests/Migration/Unit/Adapters/MockDestination.php +++ b/tests/Migration/Unit/Adapters/MockDestination.php @@ -52,6 +52,7 @@ class MockDestination extends Destination Resource::TYPE_TEAM, Resource::TYPE_MEMBERSHIP, Resource::TYPE_PLATFORM, + Resource::TYPE_API_KEY, Resource::TYPE_PROVIDER, Resource::TYPE_TOPIC, Resource::TYPE_SUBSCRIBER, diff --git a/tests/Migration/Unit/Adapters/MockSource.php b/tests/Migration/Unit/Adapters/MockSource.php index 1524dea8c7..f7caa6e35e 100644 --- a/tests/Migration/Unit/Adapters/MockSource.php +++ b/tests/Migration/Unit/Adapters/MockSource.php @@ -81,6 +81,7 @@ class MockSource extends Source Resource::TYPE_TEAM, Resource::TYPE_MEMBERSHIP, Resource::TYPE_PLATFORM, + Resource::TYPE_API_KEY, Resource::TYPE_PROVIDER, Resource::TYPE_TOPIC, Resource::TYPE_SUBSCRIBER,