-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CMR-10254: Adding AccessControl functionality, tests, code structure,…
… and Docker.
- Loading branch information
Showing
13 changed files
with
507 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
zip deployment_package.zip subscription_worker.py sns.py part1_docker part_docker | ||
#zip deployment_package.zip subscription_worker.py sns.py part1_docker part_docker | ||
zip deployment_package.zip src Dockerfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
#!/bin/bash | ||
|
||
pip3 install boto3 Flask | ||
python3 -m unittest -v | ||
# This works because I did export PYTHONPATH=src | ||
|
||
pip3 install boto3 Flask requests | ||
#python3 -m unittest -v | ||
python3 -m unittest discover -v -s ./test -p "*_test.py" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import os | ||
import requests | ||
from env_vars import Env_Vars | ||
from sys import stdout | ||
from logger import logger | ||
|
||
class AccessControl: | ||
"""Encapsulates Access Control API. | ||
This class needs the following environment variables set: | ||
For local development: | ||
ACCESS_CONTROL_URL=http://localhost:3011/access-control | ||
For AWS: | ||
ENVIRONMENT_NAME=SIT | ||
CMR_ACCESS_CONTROL_PROTOCOL=https | ||
CMR_ACCESS_CONTROL_PORT=3011 | ||
CMR_ACCESS_CONTROL_HOST=cmr.sit.earthdata.nasa.gov | ||
CMR_ACCESS_CONTROL_RELATIVE_ROOT_URL=access-control | ||
Example Use of this class | ||
access_control = AccessControl() | ||
response = access_control.get_permissions('eereiter', 'C1200484253-CMR_ONLY') | ||
The call is the same as 'curl https://cmr.sit.earthdata.nasa.gov/access-control/permissions?user_id=eereiter&concept_id=C1200484253-CMR_ONLY' | ||
Return is either None (Null or Nil) (if check on response is false) or | ||
{"C1200484253-CMR_ONLY":["read","update","delete","order"]} | ||
""" | ||
|
||
def __init__(self): | ||
self.url = None | ||
|
||
def get_url_from_parameter_store(self): | ||
# Access Control URL is for local development | ||
access_control_url = os.getenv("ACCESS_CONTROL_URL") | ||
|
||
if access_control_url: | ||
self.url = access_control_url | ||
return | ||
else: | ||
# This block gets the access_control URL from the AWS parameter store. | ||
environment_name = os.getenv("ENVIRONMENT_NAME") | ||
|
||
if not environment_name: | ||
logger.error("ENVIRONMENT_NAME environment variable is not set") | ||
raise ValueError("ENVIRONMENT_NAME environment variable is not set") | ||
|
||
# construct the access control parameter names from the environment variable | ||
pre_fix = f"/{environment_name}/ingest/" | ||
protocol_param_name = f"{pre_fix}CMR_ACCESS_CONTROL_PROTOCOL" | ||
port_param_name = f"{pre_fix}CMR_ACCESS_CONTROL_PORT" | ||
host_param_name = f"{pre_fix}CMR_ACCESS_CONTROL_HOST" | ||
context_param_name = f"{pre_fix}CMR_ACCESS_CONTROL_RELATIVE_ROOT_URL" | ||
|
||
env_vars = Env_Vars | ||
protocol = env_vars.get_var(protocol_param_name) | ||
port = env_vars.get_var(port_param_name) | ||
host = env_vars.get_var(host_param_name) | ||
context = env_vars.get_var(context_param_name) | ||
self.url = f"{protocol}://{host}:{port}/{context}" | ||
logger.debug("Subscription Worker Access-Control URL:" + self.url) | ||
|
||
def get_url(self): | ||
if not self.url: | ||
self.get_url_from_parameter_store() | ||
return self.url | ||
|
||
def get_permissions(self, subscriber_id, concept_id): | ||
# Set the access-control permissions URL. | ||
url = f"{self.get_url()}/permissions" | ||
|
||
# Set the parameters | ||
params = { | ||
"user_id": subscriber_id, | ||
"concept_id": concept_id | ||
} | ||
|
||
# Make a GET request with parameters | ||
response = requests.get(url, params=params) | ||
|
||
# Check if the request was successful | ||
if response.status_code == 200: | ||
# Request was successful | ||
data = response.text | ||
logger.debug("Response data:", data) | ||
return data | ||
else: | ||
# Request failed | ||
logger.warning(f"Subscription Worker getting Access Control permissions request using URL {url} with parameters {params} failed with status code: {response.status_code}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import os | ||
import boto3 | ||
from botocore.exceptions import ClientError | ||
from sys import stdout | ||
|
||
class Env_Vars: | ||
"""Encapsulates Accessing Variables first from the OS | ||
if not there, then the parameter store.""" | ||
|
||
def __init__(self): | ||
self.ssm_client = boto3.client('ssm') | ||
|
||
def get_var(self, name, decryption=False): | ||
value = os.getenv(name) | ||
if value: | ||
print("Value: " + value) | ||
else: | ||
print("No Value") | ||
|
||
|
||
if not value: | ||
try: | ||
# Get the parameter value from AWS Parameter Store | ||
response = self.ssm_client.get_parameter(Name=name, WithDecryption=decryption) | ||
value = response['Parameter']['Value'] | ||
print("if Value: " + value) | ||
return value | ||
|
||
except ClientError as e: | ||
print(f"Error retrieving parameter from AWS Parameter Store: {e}") | ||
stdout.flush() | ||
raise | ||
else: | ||
print("Else Value: " + value) | ||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import os | ||
import logging | ||
import sys | ||
|
||
LOG_LEVEL = os.getenv("LOG_LEVEL") | ||
if not LOG_LEVEL: | ||
LOG_LEVEL = logging.INFO | ||
|
||
def setup_logger(name, log_file=None, level=logging.INFO): | ||
"""Function to setup as many loggers as you want""" | ||
|
||
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') | ||
|
||
handler = logging.StreamHandler(sys.stdout) | ||
handler.setFormatter(formatter) | ||
|
||
logger = logging.getLogger(name) | ||
logger.setLevel(level) | ||
logger.addHandler(handler) | ||
|
||
if log_file: | ||
file_handler = logging.FileHandler(log_file) | ||
file_handler.setFormatter(formatter) | ||
logger.addHandler(file_handler) | ||
|
||
return logger | ||
|
||
# Create a default logger | ||
logger = setup_logger(name='default_logger', level=LOG_LEVEL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.