mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Add API key migration support
This commit is contained in:
1 parent
86843355dc
commit
1461c131c1
7 files changed
+248
-7
No files matched your search
@@ -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<UtopiaDocument>
|
||||
*/
|
||||
@@ -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<string, string>|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)
|
||||
{
|
||||
/**
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace Utopia\Migration\Resources\Integrations;
|
||||
|
||||
use Utopia\Migration\Resource;
|
||||
use Utopia\Migration\Transfer;
|
||||
|
||||
class ApiKey extends Resource
|
||||
{
|
||||
/**
|
||||
* @param string $id
|
||||
* @param string $name
|
||||
* @param array<string> $scopes
|
||||
* @param string $expire
|
||||
* @param string $accessedAt
|
||||
* @param array<string> $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<string, mixed> $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<string, mixed>
|
||||
*/
|
||||
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<string>
|
||||
*/
|
||||
public function getScopes(): array
|
||||
{
|
||||
return $this->scopes;
|
||||
}
|
||||
|
||||
public function getExpire(): string
|
||||
{
|
||||
return $this->expire;
|
||||
}
|
||||
|
||||
public function getAccessedAt(): string
|
||||
{
|
||||
return $this->accessedAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string>
|
||||
*/
|
||||
public function getSdks(): array
|
||||
{
|
||||
return $this->sdks;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user