| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5c3ef7b commit 5d5d7a8
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,18 +1,21 @@ | |||
| 1 | 1 | import os | |
| 2 | 2 | from google.auth import credentials | |
| 3 | - from google.auth.exceptions import RefreshError | ||
| 3 | + from google.auth.exceptions import RefreshError | ||
| 4 | 4 | from google.cloud import bigquery | |
| 5 | 5 | from google.cloud.bigquery._http import Connection | |
| 6 | 6 | from kaggle_secrets import UserSecretsClient | |
| 7 | 7 | ||
| 8 | + | ||
| 8 | 9 | def get_integrations(): | |
| 9 | 10 | kernel_integrations_var = os.getenv("KAGGLE_KERNEL_INTEGRATIONS") | |
| 10 | 11 | kernel_integrations = KernelIntegrations() | |
| 11 | 12 | if kernel_integrations_var is None: | |
| 12 | 13 | return kernel_integrations | |
| 13 | - map(lambda x: kernel_integrations.add_integration(x.lower()), kernel_integrations_var.split(':')) | ||
| 14 | + for integration in kernel_integrations_var.split(':'): | ||
| 15 | + kernel_integrations.add_integration(integration.lower()) | ||
| 14 | 16 | return kernel_integrations | |
| 15 | 17 | ||
| 18 | + | ||
| 16 | 19 | class KernelIntegrations(): | |
| 17 | 20 | def __init__(self): | |
| 18 | 21 | self.integrations = {} | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,7 @@ | |||
| 3 | 3 | kaggle_proxy_token = os.getenv("KAGGLE_DATA_PROXY_TOKEN") | |
| 4 | 4 | kernel_integrations_var = os.getenv("KAGGLE_KERNEL_INTEGRATIONS") | |
| 5 | 5 | ||
| 6 | - bq_user_jwt = os.getenv("KAGGLE_BQ_USER_JWT") | ||
| 6 | + bq_user_jwt = os.getenv("KAGGLE_USER_SECRETS_TOKEN") | ||
| 7 | 7 | if kaggle_proxy_token or bq_user_jwt: | |
| 8 | 8 | from google.auth import credentials | |
| 9 | 9 | from google.cloud import bigquery | |
@@ -13,7 +13,6 @@ | |||
| 13 | 13 | from kaggle_gcp import PublicBigqueryClient, KaggleKernelCredentials | |
| 14 | 14 | ||
| 15 | 15 | def monkeypatch_bq(bq_client, *args, **kwargs): | |
| 16 | - data_proxy_project = os.getenv("KAGGLE_DATA_PROXY_PROJECT") | ||
| 17 | 16 | specified_project = kwargs.get('project') | |
| 18 | 17 | specified_credentials = kwargs.get('credentials') | |
| 19 | 18 | kernel_integrations = get_integrations() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,106 +12,117 @@ | |||
| 12 | 12 | ||
| 13 | 13 | ||
| 14 | 14 | class TestBigQuery(unittest.TestCase): | |
| 15 | - | ||
| 15 | + | ||
| 16 | 16 | def _test_proxy(self, client, should_use_proxy): | |
| 17 | 17 | class HTTPHandler(BaseHTTPRequestHandler): | |
| 18 | 18 | called = False | |
| 19 | 19 | header_found = False | |
| 20 | 20 | ||
| 21 | - def do_HEAD(s): | ||
| 22 | - s.send_response(200) | ||
| 21 | + def do_HEAD(self): | ||
| 22 | + self.send_response(200) | ||
| 23 | 23 | ||
| 24 | - def do_GET(s): | ||
| 24 | + def do_GET(self): | ||
| 25 | 25 | HTTPHandler.called = True | |
| 26 | - HTTPHandler.header_found = any(k for k in s.headers if k == "X-KAGGLE-PROXY-DATA" and s.headers[k] == "test-key") | ||
| 27 | - s.send_response(200) | ||
| 26 | + HTTPHandler.header_found = any( | ||
| 27 | + k for k in self.headers if k == "X-KAGGLE-PROXY-DATA" and self.headers[k] == "test-key") | ||
| 28 | + self.send_response(200) | ||
| 28 | 29 | ||
| 29 | 30 | server_address = urlparse(os.getenv('KAGGLE_DATA_PROXY_URL')) | |
| 30 | 31 | with HTTPServer((server_address.hostname, server_address.port), HTTPHandler) as httpd: | |
| 31 | 32 | threading.Thread(target=httpd.serve_forever).start() | |
| 32 | 33 | ||
| 33 | 34 | try: | |
| 34 | - for ds in client.list_datasets(): pass | ||
| 35 | + for _ in client.list_datasets(): | ||
| 36 | + pass | ||
| 35 | 37 | except: | |
| 36 | 38 | pass | |
| 37 | 39 | ||
| 38 | 40 | httpd.shutdown() | |
| 39 | 41 | if should_use_proxy: | |
| 40 | - self.assertTrue(HTTPHandler.called, msg="Fake server did not receive a request from the BQ client.") | ||
| 41 | - self.assertTrue(HTTPHandler.header_found, msg="X-KAGGLE-PROXY-DATA header was missing from the BQ request.") | ||
| 42 | + self.assertTrue( | ||
| 43 | + HTTPHandler.called, msg="Fake server did not receive a request from the BQ client.") | ||
| 44 | + self.assertTrue( | ||
| 45 | + HTTPHandler.header_found, msg="X-KAGGLE-PROXY-DATA header was missing from the BQ request.") | ||
| 42 | 46 | else: | |
| 43 | - self.assertFalse(HTTPHandler.called, msg="Fake server was called from the BQ client, but should not have been.") | ||
| 47 | + self.assertFalse( | ||
| 48 | + HTTPHandler.called, msg="Fake server was called from the BQ client, but should not have been.") | ||
| 44 | 49 | ||
| 45 | 50 | def test_proxy_using_library(self): | |
| 46 | 51 | env = EnvironmentVarGuard() | |
| 47 | - env.unset('KAGGLE_BQ_USER_JWT') | ||
| 52 | + env.unset('KAGGLE_USER_SECRETS_TOKEN') | ||
| 48 | 53 | with env: | |
| 49 | 54 | client = PublicBigqueryClient() | |
| 50 | 55 | self._test_proxy(client, should_use_proxy=True) | |
| 51 | 56 | ||
| 52 | 57 | def test_proxy_no_project(self): | |
| 53 | 58 | env = EnvironmentVarGuard() | |
| 54 | - env.unset('KAGGLE_BQ_USER_JWT') | ||
| 59 | + env.unset('KAGGLE_USER_SECRETS_TOKEN') | ||
| 55 | 60 | with env: | |
| 56 | 61 | client = bigquery.Client() | |
| 57 | 62 | self._test_proxy(client, should_use_proxy=True) | |
| 58 | 63 | ||
| 59 | 64 | def test_proxy_with_kwargs(self): | |
| 60 | 65 | env = EnvironmentVarGuard() | |
| 61 | - env.unset('KAGGLE_BQ_USER_JWT') | ||
| 66 | + env.unset('KAGGLE_USER_SECRETS_TOKEN') | ||
| 62 | 67 | with env: | |
| 63 | - client = bigquery.Client(default_query_job_config=bigquery.QueryJobConfig(maximum_bytes_billed=1e9)) | ||
| 68 | + client = bigquery.Client( | ||
| 69 | + default_query_job_config=bigquery.QueryJobConfig(maximum_bytes_billed=1e9)) | ||
| 64 | 70 | self._test_proxy(client, should_use_proxy=True) | |
| 65 | 71 | ||
| 66 | 72 | def test_project_with_connected_account(self): | |
| 67 | 73 | env = EnvironmentVarGuard() | |
| 68 | - env.set('KAGGLE_BQ_USER_JWT', 'foobar') | ||
| 74 | + env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') | ||
| 69 | 75 | with env: | |
| 70 | - client = bigquery.Client(project='ANOTHER_PROJECT', credentials=KaggleKernelCredentials()) | ||
| 76 | + client = bigquery.Client( | ||
| 77 | + project='ANOTHER_PROJECT', credentials=KaggleKernelCredentials()) | ||
| 71 | 78 | self._test_proxy(client, should_use_proxy=False) | |
| 72 | 79 | ||
| 73 | 80 | def test_project_with_empty_integrations(self): | |
| 74 | 81 | env = EnvironmentVarGuard() | |
| 75 | - env.set('KAGGLE_BQ_USER_JWT', 'foobar') | ||
| 82 | + env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') | ||
| 76 | 83 | env.set('KAGGLE_KERNEL_INTEGRATIONS', '') | |
| 77 | 84 | with env: | |
| 78 | - client = bigquery.Client(project='ANOTHER_PROJECT', credentials=KaggleKernelCredentials()) | ||
| 85 | + client = bigquery.Client( | ||
| 86 | + project='ANOTHER_PROJECT', credentials=KaggleKernelCredentials()) | ||
| 79 | 87 | self._test_proxy(client, should_use_proxy=False) | |
| 80 | 88 | ||
| 81 | - def test_project_with_connected_account_default_credentials(self): | ||
| 89 | + def test_project_with_connected_account_unrelated_integrations(self): | ||
| 82 | 90 | env = EnvironmentVarGuard() | |
| 83 | - env.set('KAGGLE_BQ_USER_JWT', 'foobar') | ||
| 91 | + env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') | ||
| 84 | 92 | env.set('KAGGLE_KERNEL_INTEGRATIONS', 'GCS:ANOTHER_ONE') | |
| 85 | 93 | with env: | |
| 86 | - client = bigquery.Client(project='ANOTHER_PROJECT', credentials=KaggleKernelCredentials()) | ||
| 94 | + client = bigquery.Client( | ||
| 95 | + project='ANOTHER_PROJECT', credentials=KaggleKernelCredentials()) | ||
| 87 | 96 | self._test_proxy(client, should_use_proxy=False) | |
| 88 | 97 | ||
| 89 | - def test_project_with_connected_account_unrelated_integrations(self): | ||
| 98 | + def test_project_with_connected_account_default_credentials(self): | ||
| 90 | 99 | env = EnvironmentVarGuard() | |
| 91 | - env.set('KAGGLE_BQ_USER_JWT', 'foobar') | ||
| 100 | + env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') | ||
| 92 | 101 | env.set('KAGGLE_KERNEL_INTEGRATIONS', 'BIGQUERY') | |
| 93 | 102 | with env: | |
| 94 | - client = bigquery.Client(project='ANOTHER_PROJECT') | ||
| 103 | + client = bigquery.Client(project='ANOTHER_PROJECT') | ||
| 95 | 104 | self._test_proxy(client, should_use_proxy=False) | |
| 96 | 105 | ||
| 97 | 106 | def test_simultaneous_clients(self): | |
| 98 | 107 | env = EnvironmentVarGuard() | |
| 99 | - env.set('KAGGLE_BQ_USER_JWT', 'foobar') | ||
| 108 | + env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') | ||
| 100 | 109 | with env: | |
| 101 | 110 | proxy_client = bigquery.Client() | |
| 102 | 111 | self._test_proxy(proxy_client, should_use_proxy=True) | |
| 103 | - bq_client = bigquery.Client(project='ANOTHER_PROJECT', credentials=KaggleKernelCredentials()) | ||
| 112 | + bq_client = bigquery.Client( | ||
| 113 | + project='ANOTHER_PROJECT', credentials=KaggleKernelCredentials()) | ||
| 104 | 114 | self._test_proxy(bq_client, should_use_proxy=False) | |
| 105 | 115 | # Verify that proxy client is still going to proxy to ensure global Connection | |
| 106 | 116 | # isn't being modified. | |
| 107 | 117 | self._test_proxy(proxy_client, should_use_proxy=True) | |
| 108 | 118 | ||
| 109 | 119 | def test_no_project_with_connected_account(self): | |
| 110 | 120 | env = EnvironmentVarGuard() | |
| 111 | - env.set('KAGGLE_BQ_USER_JWT', 'foobar') | ||
| 121 | + env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar') | ||
| 122 | + env.set('KAGGLE_KERNEL_INTEGRATIONS', 'BIGQUERY') | ||
| 112 | 123 | with env: | |
| 113 | 124 | with self.assertRaises(DefaultCredentialsError): | |
| 114 | 125 | # TODO(vimota): Handle this case, either default to Kaggle Proxy or use some default project | |
| 115 | 126 | # by the user or throw a custom exception. | |
| 116 | - client = bigquery.Client(credentials=KaggleKernelCredentials()) | ||
| 127 | + client = bigquery.Client() | ||
| 117 | 128 | self._test_proxy(client, should_use_proxy=False) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments