-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Raviv Schaffer <[email protected]>
- Loading branch information
Showing
11 changed files
with
239 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
|
||
# Test scrips | ||
run.log | ||
update.sh | ||
install-sample.sh |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
Empty file.
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,43 @@ | ||
# | ||
# Copyright 2020 IBM Corp. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
import yaml | ||
|
||
|
||
class Config: | ||
def __init__(self, config_path): | ||
# TODO: change to schemed yaml using schemed-yaml-config | ||
with open(config_path, 'r') as stream: | ||
self.values = yaml.safe_load(stream) | ||
|
||
def for_asset(self, asset_name: str, capability="") -> dict: | ||
for asset_info in self.values.get('data', []): | ||
if asset_info['name'] == asset_name and (capability == "" or asset_info['capability'] == capability): | ||
return asset_info | ||
raise ValueError( | ||
"Requested config for undefined asset: {}".format(asset_name)) | ||
|
||
@property | ||
def app_uuid(self) -> str: | ||
return self.values.get('app-uuid', '') | ||
|
||
@property | ||
def workers(self) -> list: | ||
return self.values.get('workers', []) | ||
|
||
@property | ||
def auth(self) -> dict: | ||
return self.values.get('auth', {}) | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
pass | ||
|
||
def connection_type(self, asset_name: str, capability="") -> str: | ||
asset_info = self.for_asset(asset_name, capability) | ||
if 'connection' in asset_info: | ||
return asset_info['connection'].get('type') | ||
return None |
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 @@ | ||
from fybrik_python_logging import logger, Error, DataSetID, ForUser | ||
from fybrik_python_vault import get_jwt_from_file, get_raw_secret_from_vault | ||
|
||
|
||
def get_credentials_from_vault(vault_credentials, datasetID): | ||
jwt_file_path = vault_credentials.get('jwt_file_path', '/var/run/secrets/kubernetes.io/serviceaccount/token') | ||
jwt = get_jwt_from_file(jwt_file_path) | ||
vault_address = vault_credentials.get('address', 'https://localhost:8200') | ||
secret_path = vault_credentials.get('secretPath', '/v1/secret/data/cred') | ||
vault_auth = vault_credentials.get('authPath', '/v1/auth/kubernetes/login') | ||
role = vault_credentials.get('role', 'demo') | ||
logger.trace('getting vault credentials', | ||
extra={'jwt_file_path': str(jwt_file_path), | ||
'vault_address': str(vault_address), | ||
'secret_path': str(secret_path), | ||
'vault_auth': str(vault_auth), | ||
'role': str(role), | ||
DataSetID: datasetID, | ||
ForUser: True}) | ||
credentials = get_raw_secret_from_vault(jwt, secret_path, vault_address, vault_auth, role, datasetID) | ||
if not credentials: | ||
raise ValueError("Vault credentials are missing") | ||
if 'access_key' in credentials and 'secret_key' in credentials: | ||
if credentials['access_key'] and credentials['secret_key']: | ||
return credentials['access_key'], credentials['secret_key'] | ||
else: | ||
if not credentials['access_key']: | ||
logger.error("'access_key' must be non-empty", | ||
extra={DataSetID: datasetID, ForUser: True}) | ||
if not credentials['secret_key']: | ||
logger.error("'secret_key' must be non-empty", | ||
extra={DataSetID: datasetID, ForUser: True}) | ||
logger.error("Expected both 'access_key' and 'secret_key' fields in vault secret", | ||
extra={DataSetID: datasetID, ForUser: True}) | ||
raise ValueError("Vault credentials are missing") |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from deletem.config import Config | ||
import deletem.vault as vault | ||
from fybrik_python_logging import init_logger, logger | ||
import boto3 | ||
|
||
def s3_connection(endpoint, aws_access_key, aws_secret_key): | ||
try: | ||
logger.info("Connecting S3 client") | ||
s3_resource = boto3.resource("s3", endpoint_url=endpoint, aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key) | ||
s3_client = boto3.client("s3", endpoint_url=endpoint, aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key) | ||
except: | ||
logger.info("Could not connect to S3 client") | ||
raise | ||
else: | ||
return s3_resource, s3_client | ||
|
||
def delete_object(s3_client, bucket_name, object_key): | ||
try: | ||
logger.info(f"Deleting object: '{object_key}' from bucket: '{bucket_name}'") | ||
response = s3_client.delete_object(Bucket=bucket_name, Key=object_key) | ||
except: | ||
logger.error("Could not delete object") | ||
raise | ||
else: | ||
return response | ||
|
||
|
||
if __name__ == "__main__": | ||
init_logger("TRACE", "7d320bd3-df69-4c66-ba58-f6de26fa1744", 'delete-module') | ||
logger.info('Delete module initialized') | ||
|
||
conf = Config("/etc/conf/conf.yaml") | ||
conf_data = conf.values["data"][0] | ||
dataID = conf_data["name"] | ||
endpoint = conf_data["connection"]["s3"]["endpoint_url"] | ||
vault_cred = conf_data["connection"]["s3"]["vault_credentials"] | ||
|
||
access_key, secret_key = vault.get_credentials_from_vault(vault_cred, dataID) | ||
|
||
s3_resource, s3_client = s3_connection(endpoint, access_key, secret_key) | ||
|
||
bucket_name, object_key = conf_data["path"].split("/") | ||
logger.info("Deleting object") | ||
delete_object(s3_client, bucket_name, object_key) | ||
logger.info(f"Object '{object_key}' deleted.") | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
File renamed without changes.