Skip to content

Commit

Permalink
feat: add logger in identity and authz methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cowan-macady committed Feb 10, 2023
1 parent dbf1b36 commit 5ba11fc
Show file tree
Hide file tree
Showing 33 changed files with 368 additions and 415 deletions.
59 changes: 29 additions & 30 deletions indykite_sdk/authorization/__init__.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
import certifi
import grpc
import os

import indykite_sdk.utils.logger as logger
from indykite_sdk.authorization import helper
from indykite_sdk.indykite.authorization.v1beta1 import authorization_service_pb2_grpc as pb2


class AuthorizationClient(object):

def __init__(self, local=False):
cred = os.getenv('INDYKITE_APPLICATION_CREDENTIALS')

# Load the config from File (secondary)
if (cred is False) or (cred is None):
cred = os.getenv('INDYKITE_APPLICATION_CREDENTIALS_FILE')
try:
if (cred is False) or (cred is None):
try:
cred = os.getenv('INDYKITE_APPLICATION_CREDENTIALS')
# Load the config from File (secondary)
if not cred:
cred = os.getenv('INDYKITE_APPLICATION_CREDENTIALS_FILE')
if not cred:
raise Exception("Missing INDYKITE_APPLICATION_CREDENTIALS or "
"INDYKITE_APPLICATION_CREDENTIALS_FILE environment variable")
except Exception as exception:
print(exception)
return None
credentials = os.path.join(os.path.dirname(cred), os.path.basename(cred))
credentials = helper.load_credentials(credentials)

# Load the credential json (primary)
else:
credentials = helper.load_json(cred)
credentials = os.path.join(os.path.dirname(cred), os.path.basename(cred))
credentials = helper.load_credentials(credentials)

# Load the credential json (primary)
else:
credentials = helper.load_json(cred)

agent_token = helper.create_agent_jwt(credentials)
agent_token = helper.create_agent_jwt(credentials)

call_credentials = grpc.access_token_call_credentials(agent_token.decode("utf-8"))
call_credentials = grpc.access_token_call_credentials(agent_token.decode("utf-8"))

if local:
certificate_path = os.getenv('CAPEM')
endpoint = credentials.get("local_endpoint")
else:
certificate_path = certifi.where()
endpoint = credentials.get("endpoint")
if local:
certificate_path = os.getenv('CAPEM')
endpoint = credentials.get("local_endpoint")
else:
certificate_path = certifi.where()
endpoint = credentials.get("endpoint")

with open(certificate_path, "rb") as cert_file:
channel_credentials = grpc.ssl_channel_credentials(cert_file.read())
with open(certificate_path, "rb") as cert_file:
channel_credentials = grpc.ssl_channel_credentials(cert_file.read())

composite_credentials = grpc.composite_channel_credentials(channel_credentials,
call_credentials)
composite_credentials = grpc.composite_channel_credentials(channel_credentials,
call_credentials)

self.channel = grpc.secure_channel(endpoint, composite_credentials)
self.stub = pb2.AuthorizationAPIStub(channel=self.channel)
self.channel = grpc.secure_channel(endpoint, composite_credentials)
self.stub = pb2.AuthorizationAPIStub(channel=self.channel)

except Exception as exception:
return logger.logger_error(exception)
# Imported methods
from .is_authorized import is_authorized_token, is_authorized_digital_twin, is_authorized_property_filter
14 changes: 8 additions & 6 deletions indykite_sdk/authorization/is_authorized.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
from indykite_sdk.indykite.identity.v1beta2 import identity_management_api_pb2 as pb2_ident
from indykite_sdk.indykite.identity.v1beta2 import model_pb2 as model
from indykite_sdk.indykite.objects.v1beta1 import struct_pb2 as pb2_struct
import sys
import indykite_sdk.utils.logger as logger


def is_authorized_digital_twin(self, digital_twin_id, tenant_id, resources=[], actions=[]):
sys.excepthook = logger.handle_excepthook
try:
response = self.stub.IsAuthorized(
pb2.IsAuthorizedRequest(
Expand All @@ -19,8 +22,7 @@ def is_authorized_digital_twin(self, digital_twin_id, tenant_id, resources=[], a
)
)
except Exception as exception:
print(exception)
return None
return logger.logger_error(exception)

if not response:
return None
Expand All @@ -29,6 +31,7 @@ def is_authorized_digital_twin(self, digital_twin_id, tenant_id, resources=[], a


def is_authorized_token(self, access_token, resources=[], actions=[]):
sys.excepthook = logger.handle_excepthook
try:
response = self.stub.IsAuthorized(
pb2.IsAuthorizedRequest(
Expand All @@ -40,8 +43,7 @@ def is_authorized_token(self, access_token, resources=[], actions=[]):
)
)
except Exception as exception:
print(exception)
return None
return logger.logger_error(exception)

if not response:
return None
Expand All @@ -50,6 +52,7 @@ def is_authorized_token(self, access_token, resources=[], actions=[]):


def is_authorized_property_filter(self, type_filter, value, resources=[], actions=[]):
sys.excepthook = logger.handle_excepthook
try:
response = self.stub.IsAuthorized(
pb2.IsAuthorizedRequest(
Expand All @@ -64,8 +67,7 @@ def is_authorized_property_filter(self, type_filter, value, resources=[], action
)
)
except Exception as exception:
print(exception)
return None
return logger.logger_error(exception)

if not response:
return None
Expand Down
63 changes: 32 additions & 31 deletions indykite_sdk/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
import certifi
import grpc
import os

import indykite_sdk.utils.logger as logger
from indykite_sdk.config import helper
from indykite_sdk.indykite.config.v1beta1 import config_management_api_pb2_grpc as config_pb2_grpc


class ConfigClient(object):

def __init__(self, local=False):
cred = os.getenv('INDYKITE_SERVICE_ACCOUNT_CREDENTIALS')
try:
cred = os.getenv('INDYKITE_SERVICE_ACCOUNT_CREDENTIALS')
# Load the config from File (secondary)
if not cred:
cred = os.getenv('INDYKITE_SERVICE_ACCOUNT_CREDENTIALS_FILE')
if not cred:
raise Exception("Missing INDYKITE_SERVICE_ACCOUNT_CREDENTIALS or "
"INDYKITE_SERVICE_ACCOUNT_CREDENTIALS_FILE environment variable")

credentials = os.path.join(os.path.dirname(cred), os.path.basename(cred))
credentials = helper.load_credentials(credentials)

# Load the config from File (secondary)
if (cred is False) or (cred is None):
cred = os.getenv('INDYKITE_SERVICE_ACCOUNT_CREDENTIALS_FILE')
try:
if (cred is False) or (cred is None):
raise Exception("Missing INDYKITE_SERVICE_ACCOUNT_CREDENTIALS or INDYKITE_SERVICE_ACCOUNT_CREDENTIALS_FILE environment variable")
except Exception as exception:
print(exception)
return None
credentials = os.path.join(os.path.dirname(cred), os.path.basename(cred))
credentials = helper.load_credentials(credentials)
# Load the credential json (primary)
else:
credentials = helper.load_json(cred)

# Load the credential json (primary)
else:
credentials = helper.load_json(cred)
service_account_token = helper.create_agent_jwt(credentials)

service_account_token = helper.create_agent_jwt(credentials)
call_credentials = grpc.access_token_call_credentials(service_account_token.decode("utf-8"))

call_credentials = grpc.access_token_call_credentials(service_account_token.decode("utf-8"))
if local:
certificate_path = os.getenv('CAPEM')
endpoint = credentials.get("local_endpoint")
else:
certificate_path = certifi.where()
endpoint = credentials.get("endpoint")

if local:
certificate_path = os.getenv('CAPEM')
endpoint = credentials.get("local_endpoint")
else:
certificate_path = certifi.where()
endpoint = credentials.get("endpoint")
with open(certificate_path, "rb") as cert_file:
channel_credentials = grpc.ssl_channel_credentials(cert_file.read())

with open(certificate_path, "rb") as cert_file:
channel_credentials = grpc.ssl_channel_credentials(cert_file.read())
composite_credentials = grpc.composite_channel_credentials(channel_credentials,
call_credentials)

composite_credentials = grpc.composite_channel_credentials(channel_credentials,
call_credentials)
self.channel = grpc.secure_channel(endpoint, composite_credentials)
self.stub = config_pb2_grpc.ConfigManagementAPIStub(channel=self.channel)
self.credentials = credentials

self.channel = grpc.secure_channel(endpoint, composite_credentials)
self.stub = config_pb2_grpc.ConfigManagementAPIStub(channel=self.channel)
self.credentials = credentials
except Exception as exception:
return logger.logger_error(exception)

# Imported methods
from .customer import get_customer_by_id, get_customer_by_name
Expand Down
17 changes: 6 additions & 11 deletions indykite_sdk/config/app_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@


def get_app_space_by_id(self, app_space_id):
sys.excepthook = logger.handle_excepthook
try:
sys.excepthook = logger.handle_excepthook
response = self.stub.ReadApplicationSpace(
pb2.ReadApplicationSpaceRequest(
id=str(app_space_id)
Expand All @@ -26,9 +26,8 @@ def get_app_space_by_id(self, app_space_id):


def get_app_space_by_name(self, customer_id, app_space_name):

sys.excepthook = logger.handle_excepthook
try:
sys.excepthook = logger.handle_excepthook
response = self.stub.ReadApplicationSpace(
pb2.ReadApplicationSpaceRequest(
name=UniqueNameIdentifier(location = customer_id, name = app_space_name)
Expand All @@ -44,9 +43,8 @@ def get_app_space_by_name(self, customer_id, app_space_name):


def create_app_space(self, customer_id, name, display_name, description="", bookmarks=[]):

sys.excepthook = logger.handle_excepthook
try:
sys.excepthook = logger.handle_excepthook
response = self.stub.CreateApplicationSpace(
pb2.CreateApplicationSpaceRequest(
customer_id=customer_id,name=name, display_name=wrappers.StringValue(value=display_name),
Expand All @@ -63,9 +61,8 @@ def create_app_space(self, customer_id, name, display_name, description="", book


def update_app_space(self, app_space_id, etag, display_name, description="", bookmarks=[]):

sys.excepthook = logger.handle_excepthook
try:
sys.excepthook = logger.handle_excepthook
response = self.stub.UpdateApplicationSpace(
pb2.UpdateApplicationSpaceRequest(
id=app_space_id,etag=wrappers.StringValue(value=etag),
Expand All @@ -83,9 +80,8 @@ def update_app_space(self, app_space_id, etag, display_name, description="", boo


def list_app_spaces(self, customer_id, match=[], bookmarks=[]):

sys.excepthook = logger.handle_excepthook
try:
sys.excepthook = logger.handle_excepthook
streams = self.stub.ListApplicationSpaces(
pb2.ListApplicationSpacesRequest(
customer_id=customer_id,match=match,
Expand All @@ -109,9 +105,8 @@ def list_app_spaces(self, customer_id, match=[], bookmarks=[]):


def delete_app_space(self, app_space_id, etag, bookmarks):

sys.excepthook = logger.handle_excepthook
try:
sys.excepthook = logger.handle_excepthook
response = self.stub.DeleteApplicationSpace(
pb2.DeleteApplicationSpaceRequest(
id=app_space_id, etag=wrappers.StringValue(value=etag),
Expand Down
17 changes: 6 additions & 11 deletions indykite_sdk/config/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@


def get_application_by_id(self, application_id):
sys.excepthook = logger.handle_excepthook
try:
sys.excepthook = logger.handle_excepthook
response = self.stub.ReadApplication(
pb2.ReadApplicationRequest(
id=str(application_id)
Expand All @@ -26,9 +26,8 @@ def get_application_by_id(self, application_id):


def get_application_by_name(self, app_space_id, application_name):

sys.excepthook = logger.handle_excepthook
try:
sys.excepthook = logger.handle_excepthook
response = self.stub.ReadApplication(
pb2.ReadApplicationRequest(
name=UniqueNameIdentifier(location = app_space_id, name = application_name)
Expand All @@ -44,9 +43,8 @@ def get_application_by_name(self, app_space_id, application_name):


def create_application(self, app_space_id, name, display_name, description="", bookmarks=[]):

sys.excepthook = logger.handle_excepthook
try:
sys.excepthook = logger.handle_excepthook
response = self.stub.CreateApplication(
pb2.CreateApplicationRequest(
app_space_id=app_space_id, name=name, display_name=wrappers.StringValue(value=display_name),
Expand All @@ -63,9 +61,8 @@ def create_application(self, app_space_id, name, display_name, description="", b


def update_application(self, application_id, etag, display_name, description="", bookmarks=[]):

sys.excepthook = logger.handle_excepthook
try:
sys.excepthook = logger.handle_excepthook
response = self.stub.UpdateApplication(
pb2.UpdateApplicationRequest(
id=application_id,etag=wrappers.StringValue(value=etag),
Expand All @@ -83,9 +80,8 @@ def update_application(self, application_id, etag, display_name, description="",


def list_applications(self, app_space_id, match=[], bookmarks=[]):

sys.excepthook = logger.handle_excepthook
try:
sys.excepthook = logger.handle_excepthook
streams = self.stub.ListApplications(
pb2.ListApplicationsRequest(
app_space_id=app_space_id,match=match,
Expand All @@ -109,9 +105,8 @@ def list_applications(self, app_space_id, match=[], bookmarks=[]):


def delete_application(self, application_id, etag, bookmarks):

sys.excepthook = logger.handle_excepthook
try:
sys.excepthook = logger.handle_excepthook
response = self.stub.DeleteApplication(
pb2.DeleteApplicationRequest(
id=application_id, etag=wrappers.StringValue(value=etag),
Expand Down

0 comments on commit 5ba11fc

Please sign in to comment.