Skip to content

Commit bf78fb7

Browse files
rkuo-danswerRichard Kuo (Danswer)
andauthored
possible fix for gdrive oauth in the cloud (#3642)
* possible fix for gd oauth in the cloud * missed code in rename/merge --------- Co-authored-by: Richard Kuo (Danswer) <rkuo@onyx.app>
1 parent d972a78 commit bf78fb7

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

backend/onyx/connectors/google_utils/google_kv.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
from onyx.configs.constants import KV_GOOGLE_DRIVE_SERVICE_ACCOUNT_KEY
1818
from onyx.connectors.google_utils.resources import get_drive_service
1919
from onyx.connectors.google_utils.resources import get_gmail_service
20+
from onyx.connectors.google_utils.shared_constants import (
21+
DB_CREDENTIALS_AUTHENTICATION_METHOD,
22+
)
2023
from onyx.connectors.google_utils.shared_constants import (
2124
DB_CREDENTIALS_DICT_SERVICE_ACCOUNT_KEY,
2225
)
@@ -29,6 +32,9 @@
2932
from onyx.connectors.google_utils.shared_constants import (
3033
GOOGLE_SCOPES,
3134
)
35+
from onyx.connectors.google_utils.shared_constants import (
36+
GoogleOAuthAuthenticationMethod,
37+
)
3238
from onyx.connectors.google_utils.shared_constants import (
3339
MISSING_SCOPES_ERROR_STR,
3440
)
@@ -96,6 +102,7 @@ def update_credential_access_tokens(
96102
user: User,
97103
db_session: Session,
98104
source: DocumentSource,
105+
auth_method: GoogleOAuthAuthenticationMethod,
99106
) -> OAuthCredentials | None:
100107
app_credentials = get_google_app_cred(source)
101108
flow = InstalledAppFlow.from_client_config(
@@ -119,6 +126,7 @@ def update_credential_access_tokens(
119126
new_creds_dict = {
120127
DB_CREDENTIALS_DICT_TOKEN_KEY: token_json_str,
121128
DB_CREDENTIALS_PRIMARY_ADMIN_KEY: email,
129+
DB_CREDENTIALS_AUTHENTICATION_METHOD: auth_method.value,
122130
}
123131

124132
if not update_credential_json(credential_id, new_creds_dict, user, db_session):
@@ -129,6 +137,7 @@ def update_credential_access_tokens(
129137
def build_service_account_creds(
130138
source: DocumentSource,
131139
primary_admin_email: str | None = None,
140+
name: str | None = None,
132141
) -> CredentialBase:
133142
service_account_key = get_service_account_key(source=source)
134143

@@ -138,10 +147,15 @@ def build_service_account_creds(
138147
if primary_admin_email:
139148
credential_dict[DB_CREDENTIALS_PRIMARY_ADMIN_KEY] = primary_admin_email
140149

150+
credential_dict[
151+
DB_CREDENTIALS_AUTHENTICATION_METHOD
152+
] = GoogleOAuthAuthenticationMethod.UPLOADED.value
153+
141154
return CredentialBase(
142155
credential_json=credential_dict,
143156
admin_public=True,
144157
source=source,
158+
name=name,
145159
)
146160

147161

backend/onyx/server/documents/connector.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@
5353
upsert_service_account_key,
5454
)
5555
from onyx.connectors.google_utils.google_kv import verify_csrf
56+
from onyx.connectors.google_utils.shared_constants import DB_CREDENTIALS_DICT_TOKEN_KEY
5657
from onyx.connectors.google_utils.shared_constants import (
57-
DB_CREDENTIALS_DICT_TOKEN_KEY,
58+
GoogleOAuthAuthenticationMethod,
5859
)
5960
from onyx.db.connector import create_connector
6061
from onyx.db.connector import delete_connector
@@ -314,6 +315,7 @@ def upsert_service_account_credential(
314315
credential_base = build_service_account_creds(
315316
DocumentSource.GOOGLE_DRIVE,
316317
primary_admin_email=service_account_credential_request.google_primary_admin,
318+
name="Service Account (uploaded)",
317319
)
318320
except KvKeyNotFoundError as e:
319321
raise HTTPException(status_code=400, detail=str(e))
@@ -408,6 +410,38 @@ def upload_files(
408410
return FileUploadResponse(file_paths=deduped_file_paths)
409411

410412

413+
@router.get("/admin/connector")
414+
def get_connectors_by_credential(
415+
_: User = Depends(current_curator_or_admin_user),
416+
db_session: Session = Depends(get_session),
417+
credential: int | None = None,
418+
) -> list[ConnectorSnapshot]:
419+
"""Get a list of connectors. Allow filtering by a specific credential id."""
420+
421+
connectors = fetch_connectors(db_session)
422+
423+
filtered_connectors = []
424+
for connector in connectors:
425+
if connector.source == DocumentSource.INGESTION_API:
426+
# don't include INGESTION_API, as it's a system level
427+
# connector not manageable by the user
428+
continue
429+
430+
if credential is not None:
431+
found = False
432+
for cc_pair in connector.credentials:
433+
if credential == cc_pair.credential_id:
434+
found = True
435+
break
436+
437+
if not found:
438+
continue
439+
440+
filtered_connectors.append(ConnectorSnapshot.from_connector_db_model(connector))
441+
442+
return filtered_connectors
443+
444+
411445
# Retrieves most recent failure cases for connectors that are currently failing
412446
@router.get("/admin/connector/failed-indexing-status")
413447
def get_currently_failed_indexing_status(
@@ -987,7 +1021,12 @@ def gmail_callback(
9871021
credential_id = int(credential_id_cookie)
9881022
verify_csrf(credential_id, callback.state)
9891023
credentials: Credentials | None = update_credential_access_tokens(
990-
callback.code, credential_id, user, db_session, DocumentSource.GMAIL
1024+
callback.code,
1025+
credential_id,
1026+
user,
1027+
db_session,
1028+
DocumentSource.GMAIL,
1029+
GoogleOAuthAuthenticationMethod.UPLOADED,
9911030
)
9921031
if credentials is None:
9931032
raise HTTPException(
@@ -1013,7 +1052,12 @@ def google_drive_callback(
10131052
verify_csrf(credential_id, callback.state)
10141053

10151054
credentials: Credentials | None = update_credential_access_tokens(
1016-
callback.code, credential_id, user, db_session, DocumentSource.GOOGLE_DRIVE
1055+
callback.code,
1056+
credential_id,
1057+
user,
1058+
db_session,
1059+
DocumentSource.GOOGLE_DRIVE,
1060+
GoogleOAuthAuthenticationMethod.UPLOADED,
10171061
)
10181062
if credentials is None:
10191063
raise HTTPException(

backend/onyx/server/documents/credential.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from onyx.auth.users import current_user
1010
from onyx.db.credentials import alter_credential
1111
from onyx.db.credentials import cleanup_gmail_credentials
12-
from onyx.db.credentials import cleanup_google_drive_credentials
1312
from onyx.db.credentials import create_credential
1413
from onyx.db.credentials import CREDENTIAL_PERMISSIONS_TO_IGNORE
1514
from onyx.db.credentials import delete_credential
@@ -133,8 +132,6 @@ def create_credential_from_model(
133132
# Temporary fix for empty Google App credentials
134133
if credential_info.source == DocumentSource.GMAIL:
135134
cleanup_gmail_credentials(db_session=db_session)
136-
if credential_info.source == DocumentSource.GOOGLE_DRIVE:
137-
cleanup_google_drive_credentials(db_session=db_session)
138135

139136
credential = create_credential(credential_info, user, db_session)
140137
return ObjectCreationIdResponse(

0 commit comments

Comments
 (0)