From 82843c0537293549464440891db18382b9382c07 Mon Sep 17 00:00:00 2001 From: Twarit Verma Date: Thu, 19 Sep 2024 20:01:21 +0530 Subject: [PATCH] updated documentation and sample scripts to align with security best practices --- README.md | 48 +++++++++++++++++++++++++++++++++++++++++++----- sample.py | 22 ++++++++++++++-------- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 256adda..17ed168 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,15 @@ client = RestApiClient(args) ```python from ultra_rest_client import RestApiClient +import os + +# Fetch credentials from environment variables +username = os.getenv('USERNAME') +password = os.getenv('PASSWORD') + +# Check if credentials are available +if not username or not password: + raise ValueError("Username and password must be set in environment variables.") client = RestApiClient(your_username, your_password) @@ -52,8 +61,17 @@ print(f"Get metadata for zone {domain}: {client.get_zone_metadata(domain)}") ```python from ultra_rest_client import RestApiClient +import os + +# Fetch tokens from environment variables +access_token = os.getenv('ACCESS_TOKEN') +refresh_token = os.getenv('REFRESH_TOKEN') -client = RestApiClient(your_bearer_token, your_refresh_token, use_token=True) +# Check if tokens are available +if not access_token or not refresh_token: + raise ValueError("Access token and refresh token must be set in environment variables.") + +client = RestApiClient(access_token, refresh_token, use_token=True) domain = "udns-python-rest-client-test.com." @@ -65,8 +83,16 @@ print(f"Get metadata for zone {domain}: {client.get_zone_metadata(domain)}") ```python from ultra_rest_client import RestApiClient +import os + +# Fetch token from environment variables +access_token = os.getenv('ACCESS_TOKEN') + +# Check if token are available +if not access_token: + raise ValueError("Access token must be set in environment variables.") -client = RestApiClient(your_bearer_token, use_token=True) +client = RestApiClient(access_token, use_token=True) domain = "udns-python-rest-client-test.com." @@ -81,7 +107,7 @@ This example shows a complete working python file which will create a primary zo #!/usr/bin/env python3 from ultra_rest_client import RestApiClient -import sys +import os def create_zone(client, domain): """Create a zone in UltraDNS. This function will create a zone with the name specified in the domain argument. @@ -134,10 +160,15 @@ def main(): """The main function. This is the entry point for the script. It parses the command line arguments and calls the create_zone, create_a_record, and create_cname_record functions.""" - username = sys.argv[1] - password = sys.argv[2] + # Fetch credentials from environment variables + username = os.getenv('USERNAME') + password = os.getenv('PASSWORD') domain = "ultra-rest-client-test.com." + # Check if credentials are available + if not username or not password: + raise ValueError("Username and password must be set in environment variables.") + # Create an instance of your client client = RestApiClient(username, password) @@ -157,6 +188,13 @@ if __name__ == "__main__": main() ``` +To set the environment variables, use the following commands in your terminal: + +```python +export USERNAME='your_username' +export PASSWORD='your_password' +``` + ## Functionality The sample code does not attempt to implement a client for all available UltraDNS REST API functionality. It provides access to basic functionality. Adding additional functionality should be relatively straightforward, and any contributions from the UltraDNS community would be greatly appreciated. See [sample.py](sample.py) for an example of how to use this library in your own code. diff --git a/sample.py b/sample.py index 42045de..f6e3406 100644 --- a/sample.py +++ b/sample.py @@ -8,20 +8,26 @@ from ultra_rest_client import RestApiClient import sys import time +import os -if len(sys.argv) != 6 and len(sys.argv) != 3: - raise Exception("Expected use: python sample.py username password [use_token use_http host:port]") +if len(sys.argv) != 4 and len(sys.argv) != 1: + raise Exception("Expected use: python sample.py [use_token use_http host:port]") -username = sys.argv[1] -password = sys.argv[2] +# Fetch credentials from environment variables +username = os.getenv('USERNAME') +password = os.getenv('PASSWORD') use_http = 'False' use_token = 'False' domain = 'api.ultradns.com' -if len(sys.argv) == 6: - use_token = sys.argv[3] - use_http = sys.argv[4] - domain = sys.argv[5] +# Check if credentials are available +if not username or not password: + raise ValueError("Username and password must be set in environment variables.") + +if len(sys.argv) == 4: + use_token = sys.argv[1] + use_http = sys.argv[2] + domain = sys.argv[3] test_zone_name='udns-python-rest-client-test.com.'