Commit 18716d59 authored by Alp Deniz Ogut's avatar Alp Deniz Ogut
Browse files

Use plugin_id instead of plugin item

parent e8845267
Pipeline #2723 failed with stage
......@@ -21,11 +21,10 @@ logging.basicConfig(format='%(asctime)s [%(levelname)s] - %(message)s')
# Requires the plugin class to have a "run" method.
class PluginFlow:
def __init__(self, client, run_id=None, plugin=None):
def __init__(self, client, run_id=None, plugin_id=None):
self.client = client
self.run_id = run_id
self.plugin = plugin
self.plugin_id = plugin.id if plugin else None
self.plugin_id = plugin_id
self._setup_schema()
self.initialized()
......@@ -52,18 +51,18 @@ class PluginFlow:
def get_account_from_plugin(self, service=None):
# Update plugin
self.plugin = self.client.get(self.plugin_id)
plugin = self.client.get(self.plugin_id)
# get its connected accounts
account_edges = self.plugin.get_edges('account')
account_edges = plugin.get_edges('account')
# if multiple accounts are used
if len(account_edges) > 1 and service:
for account_edge in account_edges:
account = account_edge.traverse(self.plugin)
account = account_edge.traverse(plugin)
if account.service == service:
return account
# assumes there is only one account
elif len(account_edges) == 1:
return account_edges[0].traverse(self.plugin)
return account_edges[0].traverse(plugin)
def ask_user_for_accounts(self, service, view, oauth_url=None):
# start userActionNeeded flow
......@@ -100,8 +99,10 @@ class PluginFlow:
# Save accounts as an edge to the plugin
self.client.create(account)
logging.warning(f"ACCOUNT created: {account.__dict__}")
self.plugin.add_edge('account', account)
self.plugin.update(self.client)
# add the account to the plugin item
plugin = self.client.get(self.plugin_id)
plugin.add_edge('account', account)
plugin.update(self.client)
# =======================================
# ---------- USER CLIENT METHODS -------------
......@@ -111,17 +112,17 @@ class PluginFlow:
self.client.create(plugin)
# set in instance
self.plugin_id = plugin.id
self.plugin = plugin
# TODO: Add an edge from the plugin to the desired CVUStoredDefinitions
# start_plugin.view = createLoginCVUs()
return plugin.id
return plugin
def trigger_plugin(self, interval=None):
starter = StartPlugin(targetItemId=self.plugin_id, container=self.plugin.container, state=RUN_IDLE, interval=interval)
plugin = self.client.get(self.plugin_id)
starter = StartPlugin(targetItemId=self.plugin_id, container=plugin.container, state=RUN_IDLE, interval=interval)
# add cvus here
self.client.create(starter)
self.run_id = starter.id
print(f"Started plugin {self.plugin.name} - {self.plugin_id} and run id {self.run_id}")
print(f"Started plugin {plugin.name} - {self.plugin_id} and run id {self.run_id}")
def terminate_run(self):
self._set_run_vars({'interval': None})
......@@ -139,26 +140,26 @@ class PluginFlow:
def initialized(self):
logging.warning("PLUGIN run is initialized")
self.state = RUN_INITIALIZED
if self.run_id and self.plugin:
if self.run_id:
self._set_run_vars({'state':RUN_INITIALIZED})
def started(self):
logging.warning("PLUGIN run is started")
self.state = RUN_STARTED
if self.run_id and self.plugin:
if self.run_id:
self._set_run_vars({'state':RUN_STARTED})
def failed(self, error):
logging.error(f"PLUGIN run is failed: {error}")
print("Exception while running plugin:", error)
self.state = RUN_FAILED
if self.run_id and self.plugin:
if self.run_id:
self._set_run_vars({'state':RUN_FAILED, 'message': str(error)})
def completed(self):
logging.warning("PLUGIN run is completed")
self.state = RUN_COMPLETED
if self.run_id and self.plugin:
if self.run_id:
self._set_run_vars({'state':RUN_COMPLETED})
def complete_user_action(self):
......
......@@ -11,18 +11,14 @@ APP_SECRET = ""
class PluginTemplate(PluginFlow):
def __init__(self, client, plugin=None, start_plugin_id=None):
super().__init__(client=client, plugin=plugin, run_id=start_plugin_id)
def __init__(self, client, plugin_id=None, run_id=None):
super().__init__(client=client, plugin_id=plugin_id, run_id=run_id)
self.dummy_service = ServiceAPI()
# activate plugin flow
if plugin and start_plugin_id:
if plugin_id and run_id:
# init login process
# self.start_auth_oauth()
self.start_auth_userpass_and_two_factor()
if self.dummy_service.authenticated:
logging.warning("Authtentication success")
else:
logging.warning("Authtentication failure")
def run(self):
......@@ -59,17 +55,17 @@ class PluginTemplate(PluginFlow):
result = self.dummy_service.login(account.identifier, account.secret)
if result and result['success'] == True:
if result['2fa_required']:
logging.warning("Authtentication requires 2FA")
logging.warning("Authentication requires 2FA")
for _ in range(NUM_LOGIN_TRIES):
account = self.ask_user_for_accounts(SERVICE_NAME, '2fa-view')
result = self.dummy_service.complete_2fa(account.code)
if result['success']:
logging.warning("Authtentication 2FA success")
logging.warning("Authentication 2FA success")
return True
# set error message for UI
self.set_account_vars({'errorMessage': 'Incorrect 2fa code'}, service=SERVICE_NAME)
else:
logging.warning("Authtentication success")
logging.warning("Authentication success")
return True
# set error message for UI
self.set_account_vars({'errorMessage': 'Incorrect credentials'}, service=SERVICE_NAME)
......
......@@ -10,13 +10,9 @@ def main(pod_url=None, database_key=None, owner_key=None, plugin_item=None, star
# setup client
client = PodClient(url=pod_url, database_key=database_key, owner_key=owner_key, auth_json=auth_json)
# Get plugin item from the POD.
# expand? we need credential edges. Also move into PluginFlow after getting plugin_id only.
plugin = Plugin.from_json(json.loads(plugin_item))
# ----- PLUGIN STARTS ------
plugin = PluginTemplate(client, plugin, start_plugin_id)
plugin = PluginTemplate(client, plugin_id=plugin_item['id'], run_id=start_plugin_id)
try:
plugin.start()
except Exception as e:
......@@ -41,8 +37,8 @@ def parse_environment_variables():
print(f"Missing environment variable: {v}. Exiting...")
exit()
plugin_data[k] = os.environ[v]
# parse auth json
if k == 'auth_json' and plugin_data[k]:
# parse plugin and auth json
if k in ['plugin_item', 'auth_json'] and plugin_data[k]:
plugin_data[k] = json.loads(plugin_data[k])
return plugin_data
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment