Skip to content

Commit

Permalink
Rename purpose to Target. Remove the general user secret case.
Browse files Browse the repository at this point in the history
  • Loading branch information
vimota committed Feb 1, 2019
1 parent 5438d79 commit b8ab09e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
24 changes: 11 additions & 13 deletions patches/kaggle_secrets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
"""UserSecret client classes.
This library adds support for communicating with the UserSecrets service,
currently used for retrieving an access token for supported integrations
(ie. BigQuery).
"""

import json
import os
import urllib.request
Expand All @@ -17,7 +25,7 @@ class BackendError(Exception):

class UserSecretsClient():
GET_USER_SECRET_ENDPOINT = '/requests/GetUserSecretRequest'
BIGQUERY_PURPOSE_VALUE = 1
BIGQUERY_TARGET_VALUE = 1

def __init__(self):
url_base_override = os.getenv(_KAGGLE_URL_BASE_ENV_VAR_NAME)
Expand All @@ -39,22 +47,12 @@ def _make_get_request(self, request_body):
response_json = json.loads(response.read())
return response_json

def get_user_secret(self, secret_label: str):
request_body = {
'SecretLabel': secret_label
}
response_json = self._make_get_request(request_body)
if 'Secret' not in response_json:
raise BackendError(
'Unexpected response from the service.')
return response_json['Secret']

def get_bigquery_access_token(self):
request_body = {
'Purpose': self.BIGQUERY_PURPOSE_VALUE
'Target': self.BIGQUERY_TARGET_VALUE
}
response_json = self._make_get_request(request_body)
if 'Secret' not in response_json:
raise BackendError(
'Unexpected response from UserSecrets service.')
'Unexpected response from the service.')
return response_json['Secret']
12 changes: 1 addition & 11 deletions tests/test_user_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,4 @@ def call_get_access_token():
secret_response = client.get_bigquery_access_token()
self.assertEqual(secret_response, secret)
self._test_client(call_get_access_token,
'/requests/GetUserSecretRequest?Purpose=1', secret)

def test_get_user_secret_succeeds(self):
secret = '5678'

def call_get_access_token():
client = UserSecretsClient()
secret_response = client.get_user_secret('MY_SECRET')
self.assertEqual(secret_response, secret)
self._test_client(
call_get_access_token, '/requests/GetUserSecretRequest?SecretLabel=MY_SECRET', secret)
'/requests/GetUserSecretRequest?Target=1', secret)

0 comments on commit b8ab09e

Please sign in to comment.