diff --git a/src/Migration/Destinations/Appwrite.php b/src/Migration/Destinations/Appwrite.php index 405d811a72..4cf947ecbf 100644 --- a/src/Migration/Destinations/Appwrite.php +++ b/src/Migration/Destinations/Appwrite.php @@ -3558,30 +3558,17 @@ class Appwrite extends Destination $project = $this->dbForPlatform->getDocument('projects', $this->projectId); $oAuthProviders = $project->getAttribute('oAuthProviders', []); - if ($key === 'apple') { - $serviceId = (string) $resource->getSetting('serviceId'); - if ($serviceId !== '') { - $oAuthProviders[$key . 'Appid'] = $serviceId; - } - $oAuthProviders[$key . 'Secret'] = $this->mergeAppleSecret( + $appId = $resource->getDestinationAppId(); + if (!$this->isEmptyOAuth2Setting($appId)) { + $oAuthProviders[$key . 'Appid'] = $appId; + } + + $secretFields = $resource->getDestinationSecretFields(); + if (!empty($secretFields)) { + $oAuthProviders[$key . 'Secret'] = $this->mergeJsonSecret( $oAuthProviders[$key . 'Secret'] ?? '', - (string) $resource->getSetting('keyId'), - (string) $resource->getSetting('teamId'), + $secretFields, ); - } else { - $clientId = (string) $resource->getSetting('clientId'); - if ($clientId !== '') { - $oAuthProviders[$key . 'Appid'] = $clientId; - } - foreach (['endpoint', 'tenant', 'prompt'] as $field) { - $value = $resource->getSetting($field); - if ($value !== null && $value !== '' && $value !== []) { - $oAuthProviders[$key . 'Secret'] = $this->mergeJsonSecret( - $oAuthProviders[$key . 'Secret'] ?? '', - [$field => $value], - ); - } - } } $oAuthProviders[$key . 'Enabled'] = $resource->getEnabled(); @@ -3597,27 +3584,15 @@ class Appwrite extends Destination return true; } - /** - * Preserve Apple's existing p8 secret while updating readable metadata. - */ - private function mergeAppleSecret(string $existing, string $keyId, string $teamId): string - { - $fields = []; - if ($keyId !== '') { - $fields['keyID'] = $keyId; - } - if ($teamId !== '') { - $fields['teamID'] = $teamId; - } - - return $this->mergeJsonSecret($existing, $fields); - } - /** * @param array $fields */ private function mergeJsonSecret(string $existing, array $fields): string { + if (empty($fields)) { + return $existing; + } + $decoded = $existing === '' ? [] : (\json_decode($existing, true) ?: []); if (!\is_array($decoded)) { $decoded = []; @@ -3629,6 +3604,11 @@ class Appwrite extends Destination return \json_encode($decoded) ?: ''; } + private function isEmptyOAuth2Setting(mixed $value): bool + { + return $value === null || $value === '' || $value === []; + } + /** * Direct DB write — SDK policy setters reject `total: 0` but `0` is the * storage value for "disabled". Shares the `auths` map with diff --git a/src/Migration/Resources/Auth/OAuth2/OAuth2Provider.php b/src/Migration/Resources/Auth/OAuth2/OAuth2Provider.php index bc62cfd345..ee6ed00559 100644 --- a/src/Migration/Resources/Auth/OAuth2/OAuth2Provider.php +++ b/src/Migration/Resources/Auth/OAuth2/OAuth2Provider.php @@ -10,54 +10,61 @@ use Utopia\Migration\Transfer; */ final class OAuth2Provider extends Resource { + private const TARGET_APP_ID = 'appId'; + private const TARGET_SECRET = 'secret'; + /** * Allow-list of readable provider fields that are safe to migrate. * - * @var array> + * @var array> */ public const PROVIDERS = [ - 'amazon' => ['clientId'], - 'apple' => ['serviceId', 'keyId', 'teamId'], - 'auth0' => ['clientId', 'endpoint'], - 'authentik' => ['clientId', 'endpoint'], - 'autodesk' => ['clientId'], - 'bitbucket' => ['clientId'], - 'bitly' => ['clientId'], - 'box' => ['clientId'], - 'dailymotion' => ['clientId'], - 'discord' => ['clientId'], - 'disqus' => ['clientId'], - 'dropbox' => ['clientId'], - 'etsy' => ['clientId'], - 'facebook' => ['clientId'], - 'figma' => ['clientId'], - 'fusionauth' => ['clientId', 'endpoint'], - 'github' => ['clientId'], - 'gitlab' => ['clientId', 'endpoint'], - 'google' => ['clientId', 'prompt'], - 'keycloak' => ['clientId', 'endpoint'], - 'kick' => ['clientId'], - 'linkedin' => ['clientId'], - 'microsoft' => ['clientId', 'tenant'], - 'notion' => ['clientId'], - 'oidc' => ['clientId', 'endpoint'], - 'okta' => ['clientId', 'endpoint'], - 'paypal' => ['clientId'], - 'paypalSandbox' => ['clientId'], - 'podio' => ['clientId'], - 'salesforce' => ['clientId'], - 'slack' => ['clientId'], - 'spotify' => ['clientId'], - 'stripe' => ['clientId'], - 'tradeshift' => ['clientId'], - 'tradeshiftBox' => ['clientId'], - 'twitch' => ['clientId'], - 'wordpress' => ['clientId'], - 'x' => ['clientId'], - 'yahoo' => ['clientId'], - 'yandex' => ['clientId'], - 'zoho' => ['clientId'], - 'zoom' => ['clientId'], + 'amazon' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'apple' => [ + 'serviceId' => ['target' => self::TARGET_APP_ID], + 'keyId' => ['target' => self::TARGET_SECRET, 'key' => 'keyID'], + 'teamId' => ['target' => self::TARGET_SECRET, 'key' => 'teamID'], + ], + 'auth0' => ['clientId' => ['target' => self::TARGET_APP_ID], 'endpoint' => ['target' => self::TARGET_SECRET]], + 'authentik' => ['clientId' => ['target' => self::TARGET_APP_ID], 'endpoint' => ['target' => self::TARGET_SECRET]], + 'autodesk' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'bitbucket' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'bitly' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'box' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'dailymotion' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'discord' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'disqus' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'dropbox' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'etsy' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'facebook' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'figma' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'fusionauth' => ['clientId' => ['target' => self::TARGET_APP_ID], 'endpoint' => ['target' => self::TARGET_SECRET]], + 'github' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'gitlab' => ['clientId' => ['target' => self::TARGET_APP_ID], 'endpoint' => ['target' => self::TARGET_SECRET]], + 'google' => ['clientId' => ['target' => self::TARGET_APP_ID], 'prompt' => ['target' => self::TARGET_SECRET]], + 'keycloak' => ['clientId' => ['target' => self::TARGET_APP_ID], 'endpoint' => ['target' => self::TARGET_SECRET]], + 'kick' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'linkedin' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'microsoft' => ['clientId' => ['target' => self::TARGET_APP_ID], 'tenant' => ['target' => self::TARGET_SECRET]], + 'notion' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'oidc' => ['clientId' => ['target' => self::TARGET_APP_ID], 'endpoint' => ['target' => self::TARGET_SECRET]], + 'okta' => ['clientId' => ['target' => self::TARGET_APP_ID], 'endpoint' => ['target' => self::TARGET_SECRET]], + 'paypal' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'paypalSandbox' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'podio' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'salesforce' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'slack' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'spotify' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'stripe' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'tradeshift' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'tradeshiftBox' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'twitch' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'wordpress' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'x' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'yahoo' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'yandex' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'zoho' => ['clientId' => ['target' => self::TARGET_APP_ID]], + 'zoom' => ['clientId' => ['target' => self::TARGET_APP_ID]], ]; public function __construct( @@ -94,7 +101,7 @@ final class OAuth2Provider extends Resource } $settings = []; - foreach ($allowed as $field) { + foreach (\array_keys($allowed) as $field) { if (\array_key_exists($field, $array)) { $settings[$field] = $array[$field]; } @@ -148,10 +155,54 @@ final class OAuth2Provider extends Resource return $this->settings[$field] ?? null; } + public function getDestinationAppId(): mixed + { + foreach ($this->getDescriptor() as $field => $metadata) { + if ($metadata['target'] === self::TARGET_APP_ID && \array_key_exists($field, $this->settings)) { + return $this->settings[$field]; + } + } + + return null; + } + + /** + * @return array + */ + public function getDestinationSecretFields(): array + { + $fields = []; + foreach ($this->getDescriptor() as $field => $metadata) { + if ($metadata['target'] !== self::TARGET_SECRET || !\array_key_exists($field, $this->settings)) { + continue; + } + + $value = $this->settings[$field]; + if (self::isEmpty($value)) { + continue; + } + + $fields[$metadata['key'] ?? $field] = $value; + } + + return $fields; + } + public function isConfigured(): bool { - return $this->enabled - || ($this->settings['clientId'] ?? '') !== '' - || ($this->settings['serviceId'] ?? '') !== ''; + return $this->enabled || !self::isEmpty($this->getDestinationAppId()); + } + + /** + * @return array + */ + private function getDescriptor(): array + { + return self::PROVIDERS[$this->providerKey] ?? []; + } + + private static function isEmpty(mixed $value): bool + { + return $value === null || $value === '' || $value === []; } } diff --git a/src/Migration/Sources/Appwrite.php b/src/Migration/Sources/Appwrite.php index 08feff128a..4cf069aed9 100644 --- a/src/Migration/Sources/Appwrite.php +++ b/src/Migration/Sources/Appwrite.php @@ -394,19 +394,7 @@ class Appwrite extends Source } if (\in_array(Resource::TYPE_OAUTH2_PROVIDER, $resources)) { - $count = 0; - foreach ($this->project->listOAuth2Providers()->providers ?? [] as $provider) { - $key = (string) ($provider['$id'] ?? ''); - if ($key === '') { - continue; - } - $provider['id'] = $key; - $resource = OAuth2Provider::fromArray($key, $provider); - if ($resource !== null && $resource->isConfigured()) { - $count++; - } - } - $report[Resource::TYPE_OAUTH2_PROVIDER] = $count; + $report[Resource::TYPE_OAUTH2_PROVIDER] = \count($this->getOAuth2ProviderResources()); } if (\in_array(Resource::TYPE_POLICIES, $resources)) { @@ -756,10 +744,20 @@ class Appwrite extends Source private function exportOAuth2Providers(): void { - $response = $this->project->listOAuth2Providers(); + $resources = $this->getOAuth2ProviderResources(true); - $emitted = []; - foreach ($response->providers ?? [] as $provider) { + if (!empty($resources)) { + $this->callback($resources); + } + } + + /** + * @return array + */ + private function getOAuth2ProviderResources(bool $reportUnknownProviders = false): array + { + $resources = []; + foreach ($this->project->listOAuth2Providers()->providers ?? [] as $provider) { $key = (string) ($provider['$id'] ?? ''); if ($key === '') { continue; @@ -770,12 +768,14 @@ class Appwrite extends Source $resource = OAuth2Provider::fromArray($key, $payload); if ($resource === null) { - $this->addError(new Exception( - Resource::TYPE_OAUTH2_PROVIDER, - Transfer::GROUP_AUTH, - message: "No migration resource for OAuth2 provider '{$key}'; skipped.", - code: Exception::CODE_INTERNAL, - )); + if ($reportUnknownProviders) { + $this->addError(new Exception( + Resource::TYPE_OAUTH2_PROVIDER, + Transfer::GROUP_AUTH, + message: "No migration resource for OAuth2 provider '{$key}'; skipped.", + code: Exception::CODE_INTERNAL, + )); + } continue; } @@ -783,12 +783,10 @@ class Appwrite extends Source continue; } - $emitted[] = $resource; + $resources[] = $resource; } - if (!empty($emitted)) { - $this->callback($emitted); - } + return $resources; } /** diff --git a/tests/Migration/Unit/Destinations/AppwriteOAuth2SecretTest.php b/tests/Migration/Unit/Destinations/AppwriteOAuth2SecretTest.php new file mode 100644 index 0000000000..efb7ef94ce --- /dev/null +++ b/tests/Migration/Unit/Destinations/AppwriteOAuth2SecretTest.php @@ -0,0 +1,59 @@ + 'KEEP']); + $merged = $this->invokeMergeJsonSecret($existing, ['tenant' => 'contoso']); + + $this->assertSame( + ['clientSecret' => 'KEEP', 'tenant' => 'contoso'], + \json_decode($merged, true), + ); + } + + public function testJsonSecretPreservesExistingWhenNoFieldsAreMigrated(): void + { + $existing = \json_encode(['p8' => 'PRIVATE']); + + $this->assertSame($existing, $this->invokeMergeJsonSecret($existing, [])); + } + + public function testJsonSecretTreatsNonArrayExistingAsEmpty(): void + { + $merged = $this->invokeMergeJsonSecret('5', ['endpoint' => 'https://idp.example']); + + $this->assertSame(['endpoint' => 'https://idp.example'], \json_decode($merged, true)); + } + + private function invokeMergeJsonSecret(string $existing, array $fields): string + { + $destination = new AppwriteDestination( + project: 'destination-project', + endpoint: 'http://example.test/v1', + key: 'test-key', + dbForProject: $this->createStub(UtopiaDatabase::class), + getDatabasesDB: fn (UtopiaDocument $database): UtopiaDatabase => $this->createStub(UtopiaDatabase::class), + collectionStructure: ['attributes' => [], 'indexes' => []], + dbForPlatform: $this->createStub(UtopiaDatabase::class), + projectInternalId: '1', + onDuplicate: OnDuplicate::Fail, + ); + + $method = (new ReflectionClass(AppwriteDestination::class))->getMethod('mergeJsonSecret'); + /** @var string $value */ + $value = $method->invoke($destination, $existing, $fields); + + return $value; + } +} diff --git a/tests/Migration/Unit/General/OAuth2ProviderTest.php b/tests/Migration/Unit/General/OAuth2ProviderTest.php new file mode 100644 index 0000000000..db11154e10 --- /dev/null +++ b/tests/Migration/Unit/General/OAuth2ProviderTest.php @@ -0,0 +1,77 @@ + 'project-google', + 'enabled' => true, + 'clientId' => 'google-client', + 'prompt' => ['consent', 'select_account'], + 'clientSecret' => 'secret-must-not-migrate', + ]); + + $this->assertInstanceOf(OAuth2Provider::class, $provider); + $this->assertSame('google-client', $provider->getDestinationAppId()); + $this->assertSame(['prompt' => ['consent', 'select_account']], $provider->getDestinationSecretFields()); + $this->assertArrayNotHasKey('clientSecret', $provider->getSettings()); + $this->assertTrue($provider->isConfigured()); + } + + public function testRoutesAppleFieldsToDestinationSecretNames(): void + { + $provider = OAuth2Provider::fromArray('apple', [ + 'id' => 'project-apple', + 'enabled' => false, + 'serviceId' => 'service-id', + 'keyId' => 'KEY123', + 'teamId' => 'TEAM456', + 'p8File' => 'secret-must-not-migrate', + ]); + + $this->assertInstanceOf(OAuth2Provider::class, $provider); + $this->assertSame('service-id', $provider->getDestinationAppId()); + $this->assertSame(['keyID' => 'KEY123', 'teamID' => 'TEAM456'], $provider->getDestinationSecretFields()); + $this->assertArrayNotHasKey('p8File', $provider->getSettings()); + $this->assertTrue($provider->isConfigured()); + } + + public function testUnknownProviderReturnsNull(): void + { + $this->assertNull(OAuth2Provider::fromArray('unknown', [ + 'id' => 'project-unknown', + 'enabled' => true, + 'clientId' => 'client', + ])); + } + + public function testConfiguredWhenEnabledEvenWithoutAppId(): void + { + $provider = OAuth2Provider::fromArray('github', [ + 'id' => 'project-github', + 'enabled' => true, + 'clientId' => '', + ]); + + $this->assertInstanceOf(OAuth2Provider::class, $provider); + $this->assertTrue($provider->isConfigured()); + } + + public function testSkipsDisabledProviderWithoutAppId(): void + { + $provider = OAuth2Provider::fromArray('github', [ + 'id' => 'project-github', + 'enabled' => false, + 'clientId' => '', + ]); + + $this->assertInstanceOf(OAuth2Provider::class, $provider); + $this->assertFalse($provider->isConfigured()); + } +}