Skip to content

Commit

Permalink
SK-1731: Added service account samples and vault api samples
Browse files Browse the repository at this point in the history
  • Loading branch information
saileshwar-skyflow committed Nov 12, 2024
1 parent c73aa65 commit 1e40d2f
Show file tree
Hide file tree
Showing 19 changed files with 107 additions and 5 deletions.
27 changes: 27 additions & 0 deletions samples/service_account/scoped_token_generation_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from skyflow.service_account import generate_bearer_token, generate_bearer_token_from_creds, is_expired

file_path = 'CREDENTIALS_FILE_PATH'
bearer_token = ''

skyflow_credentials_string = '{"clientID":"<YOUR_CLIENT_ID>","clientName":"<YOUR_CLIENT_NAME>","tokenURI":"<YOUR_TOKEN_URI>","keyID":"<YOUR_KEY_ID>","privateKey":"<YOUR_PRIVATE_KEY>"}'


# Generate bearer token from credentials file path
options = {
"ctx": "<CONTEXT_ID>"
}

if is_expired(bearer_token):
bearer_token, token_type = generate_bearer_token(
"<YOUR_CREDENTIALS_FILE_PATH>", options
)

print(bearer_token, token_type)


# Generate bearer token from credentials string
if is_expired(bearer_token):
bearer_token, token_type = generate_bearer_token_from_creds(skyflow_credentials_string, options)

print(bearer_token, token_type)

27 changes: 27 additions & 0 deletions samples/service_account/signed_token_generation_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from skyflow.service_account import is_expired, generate_signed_data_tokens, generate_signed_data_tokens_from_creds

file_path = 'CREDENTIALS_FILE_PATH'
bearer_token = ''

skyflow_credentials_string = '{"clientID":"<YOUR_CLIENT_ID>","clientName":"<YOUR_CLIENT_NAME>","tokenURI":"<YOUR_TOKEN_URI>","keyID":"<YOUR_KEY_ID>","privateKey":"<YOUR_PRIVATE_KEY>"}'



options = {
"ctx": "CONTEX_ID",
"data_tokens": ["DATA_TOKEN1", "DATA_TOKEN2"],
"time_to_live": 90 # in seconds
}

# Generate bearer token from credentials file path
if is_expired(bearer_token):
actual_token, signed_token = generate_signed_data_tokens(
"<YOUR_CREDENTIALS_FILE_PATH>", options
)


# Generate bearer token from credentials string
if is_expired(bearer_token):
actual_token, signed_token = generate_signed_data_tokens_from_creds(
"<CREDENTIALS_STRING>", options
)
23 changes: 23 additions & 0 deletions samples/service_account/token_generation_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from skyflow.service_account import generate_bearer_token, generate_bearer_token_from_creds, is_expired

file_path = 'CREDENTIALS_FILE_PATH'
bearer_token = ''

skyflow_credentials_string = '{"clientID":"<YOUR_CLIENT_ID>","clientName":"<YOUR_CLIENT_NAME>","tokenURI":"<YOUR_TOKEN_URI>","keyID":"<YOUR_KEY_ID>","privateKey":"<YOUR_PRIVATE_KEY>"}'


# Generate bearer token from credentials file path
if is_expired(bearer_token):
bearer_token, token_type = generate_bearer_token(
"<YOUR_CREDENTIALS_FILE_PATH>"
)

print(bearer_token, token_type)


# Generate bearer token from credentials string
if is_expired(bearer_token):
bearer_token, token_type = generate_bearer_token_from_creds(skyflow_credentials_string)

print(bearer_token, token_type)

26 changes: 26 additions & 0 deletions samples/service_account/token_generation_with_context_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from skyflow.service_account import generate_bearer_token, generate_bearer_token_from_creds, is_expired

file_path = 'CREDENTIALS_FILE_PATH'
bearer_token = ''

skyflow_credentials_string = '{"clientID":"<YOUR_CLIENT_ID>","clientName":"<YOUR_CLIENT_NAME>","tokenURI":"<YOUR_TOKEN_URI>","keyID":"<YOUR_KEY_ID>","privateKey":"<YOUR_PRIVATE_KEY>"}'


# Generate bearer token from credentials file path
options = {
"role_ids": ["ROLE_ID1", "ROLE_ID2"]
}
if is_expired(bearer_token):
bearer_token, token_type = generate_bearer_token(
"<YOUR_CREDENTIALS_FILE_PATH>", options
)

print(bearer_token, token_type)


# Generate bearer token from credentials string
if is_expired(bearer_token):
bearer_token, token_type = generate_bearer_token_from_creds(skyflow_credentials_string, options)

print(bearer_token, token_type)

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion skyflow/service_account/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from ._utils import generate_bearer_token, generate_bearer_token_from_creds, is_expired
from ._utils import generate_bearer_token, generate_bearer_token_from_creds, is_expired, generate_signed_data_tokens, generate_signed_data_tokens_from_creds
2 changes: 1 addition & 1 deletion skyflow/service_account/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,4 @@ def get_signed_data_token_response_object(signed_token, actual_token):
"token": actual_token,
"signed_token": signed_token
}
return response_object
return response_object.get("token"), response_object.get("signed_token")
5 changes: 2 additions & 3 deletions tests/service_account/test__utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,12 @@ def test_get_signed_jwt_invalid_format(self, mock_jwt_encode):
get_signed_jwt({}, "client_id", "key_id", "token_uri", "private_key", None)
self.assertEqual(context.exception.message, SkyflowMessages.Error.JWT_INVALID_FORMAT.value)


def test_get_signed_data_token_response_object(self):
token = "sample_token"
signed_token = "signed_sample_token"
response = get_signed_data_token_response_object(signed_token, token)
self.assertEqual(response["token"], token)
self.assertEqual(response["signed_token"], signed_token)
self.assertEqual(response[0], token)
self.assertEqual(response[1], signed_token)

def test_generate_signed_data_tokens_from_file_path(self):
creds_path = os.path.join(os.path.dirname(__file__), "valid_credentials.json")
Expand Down

0 comments on commit 1e40d2f

Please sign in to comment.