Skip to content

Commit f9633ff

Browse files
authored
Revert "fix(platform): Remove migrate and encrypt function" (#8654)
Reverts c707ee9 (#8646) The problem analysis that led to #8646 contained some errors, so the migration removed in the PR doesn't seem to have been the cause of the problem we were hunting. Also, this migration is an essential part of the security improvement that we made 2 weeks ago.
1 parent e140873 commit f9633ff

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

autogpt_platform/backend/backend/data/user.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,43 @@ async def update_user_integrations(user_id: str, data: UserIntegrations):
9494
where={"id": user_id},
9595
data={"integrations": encrypted_data},
9696
)
97+
98+
99+
async def migrate_and_encrypt_user_integrations():
100+
"""Migrate integration credentials and OAuth states from metadata to integrations column."""
101+
users = await User.prisma().find_many(
102+
where={
103+
"metadata": {
104+
"path": ["integration_credentials"],
105+
"not": Json({"a": "yolo"}), # bogus value works to check if key exists
106+
} # type: ignore
107+
}
108+
)
109+
logger.info(f"Migrating integration credentials for {len(users)} users")
110+
111+
for user in users:
112+
raw_metadata = cast(UserMetadataRaw, user.metadata)
113+
metadata = UserMetadata.model_validate(raw_metadata)
114+
115+
# Get existing integrations data
116+
integrations = await get_user_integrations(user_id=user.id)
117+
118+
# Copy credentials and oauth states from metadata if they exist
119+
if metadata.integration_credentials and not integrations.credentials:
120+
integrations.credentials = metadata.integration_credentials
121+
if metadata.integration_oauth_states:
122+
integrations.oauth_states = metadata.integration_oauth_states
123+
124+
# Save to integrations column
125+
await update_user_integrations(user_id=user.id, data=integrations)
126+
127+
# Remove from metadata
128+
raw_metadata = dict(raw_metadata)
129+
raw_metadata.pop("integration_credentials", None)
130+
raw_metadata.pop("integration_oauth_states", None)
131+
132+
# Update metadata without integration data
133+
await User.prisma().update(
134+
where={"id": user.id},
135+
data={"metadata": Json(raw_metadata)},
136+
)

autogpt_platform/backend/backend/server/rest_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
async def lifespan_context(app: fastapi.FastAPI):
2323
await backend.data.db.connect()
2424
await backend.data.block.initialize_blocks()
25+
await backend.data.user.migrate_and_encrypt_user_integrations()
2526
yield
2627
await backend.data.db.disconnect()
2728

0 commit comments

Comments
 (0)