openreplay/ee/api/chalicelib/core/webhook.py
Kraiem Taha Yassine 877081fc7c
Api v1.15.0 (#1574)
* feat(chalice): upgraded dependencies

* feat(chalice): changed path analysis schema

* feat(DB): click coordinate support

* feat(chalice): changed path analysis issues schema
feat(chalice): upgraded dependencies

* fix(chalice): fixed pydantic issue

* refactor(chalice): refresh token validator

* feat(chalice): role restrictions

* feat(chalice): EE path analysis changes

* refactor(DB): changed creation queries
refactor(DB): changed delte queries
feat(DB): support new path analysis payload

* feat(chalice): save path analysis card

* feat(chalice): restrict access

* feat(chalice): restrict access

* feat(chalice): EE save new path analysis card

* refactor(chalice): path analysis

* feat(chalice): path analysis new query

* fix(chalice): configurable CH config

* fix(chalice): assist autocomplete

* refactor(chalice): refactored permissions

* refactor(chalice): changed log level

* refactor(chalice): upgraded dependencies

* refactor(chalice): changed path analysis query

* refactor(chalice): changed path analysis query

* refactor(chalice): upgraded dependencies
refactor(alerts): upgraded dependencies
refactor(crons): upgraded dependencies

* feat(chalice): path analysis ignore start point

* feat(chalice): path analysis in progress

* refactor(chalice): path analysis changed link sort

* refactor(chalice): path analysis changed link sort

* refactor(chalice): path analysis changed link sort

* refactor(chalice): path analysis new query
refactor(chalice): authorizers

* refactor(chalice): refactored authorizer

* fix(chalice): fixed create card of PathAnalysis

* refactor(chalice): compute link-percentage for Path Analysis

* refactor(chalice): remove null starting point from Path Analysis

* feat(chalice): path analysis CH query

* refactor(chalice): changed Path Analysis links-value
fix(chalice): fixed search notes for EE

* feat(chalice): path analysis enhanced query results

* feat(chalice): include timezone in search sessions response

* refactor(chalice): refactored logs

* refactor(chalice): refactored logs
feat(chalice): get path analysis issues

* fix(chalice): fixed path analysis issues pagination

* fix(chalice): sessions-search handle null values

* feat(chalice): PathAnalysis start event support middle-event matching

* feat(chalice): PathAnalysis start event support middle-event matching

* feat(chalice): PathAnalysis support mixed events with start-point

* fix(chalice): PathAnalysis fixed eventType value when metricValue is missing

* fix(chalice): PathAnalysis fixed wrong super-class model for update card

* fix(chalice): PathAnalysis fixed search issues
refactor(chalice): upgraded dependencies

* fix(chalice): enforce isEvent if missing

* fix(chalice): enforce isEvent if missing

* refactor(chalice): refactored custom-metrics

* refactor(chalice): small changes

* feat(chalice): path analysis EE new query

* fix(chalice): fixed hide-excess state for Path Analysis

* fix(chalice): fixed update start point and excludes for Path Analysis

* fix(chalice): fix payload validation
fix(chalice): fix update widget endpoint

* fix(chalice): fix payload validation
fix(chalice): fix update widget endpoint

* fix(chalice): fix add member

* refactor(chalice): upgraded dependencies
refactor!(chalice): upgraded SAML dependencies

* feat(chalice): ios-project support 1/5

* refactor(chalice): changed logs handling

* fix(chalice): fix path analysis issues list

* Api v1.15.0 (#1542)

* refactor(chalice): changed default dev env vars

* refactor(chalice): changes

* refactor(chalice): changed payload fixer

* refactor(chalice): changed payload fixer
refactor(chalice): support duplicate filters

* Api v1.15.0 no merge (#1546)

* refactor(chalice): changed default dev env vars

* refactor(chalice): changes

* refactor(chalice): changed payload fixer

* refactor(chalice): changed payload fixer
refactor(chalice): support duplicate filters

* refactor(chalice): changes

* feature(chalice): mobile sessions search

* Api v1.15.0 no merge (#1549)

* refactor(chalice): changed default dev env vars

* refactor(chalice): changes

* refactor(chalice): changed payload fixer

* refactor(chalice): changed payload fixer
refactor(chalice): support duplicate filters

* refactor(chalice): changes

* feature(chalice): mobile sessions search

* refactor(chalice): fix EE refactored schema

* Api v1.15.0 no merge (#1552)

* refactor(chalice): changed default dev env vars

* refactor(chalice): changes

* refactor(chalice): changed payload fixer

* refactor(chalice): changed payload fixer
refactor(chalice): support duplicate filters

* refactor(chalice): changes

* feature(chalice): mobile sessions search

* refactor(chalice): fix EE refactored schema

* fix(chalice): fix missing platform for EE

* Api v1.15.0 no merge (#1554)

* refactor(chalice): changed default dev env vars

* refactor(chalice): changes

* refactor(chalice): changed payload fixer

* refactor(chalice): changed payload fixer
refactor(chalice): support duplicate filters

* refactor(chalice): changes

* feature(chalice): mobile sessions search

* refactor(chalice): fix EE refactored schema

* fix(chalice): fix missing platform for EE

* fix(DB): fixed init_schema

* Api v1.15.0 no merge (#1554) (#1555)

* refactor(chalice): changed default dev env vars

* refactor(chalice): changes

* refactor(chalice): changed payload fixer

* refactor(chalice): changed payload fixer
refactor(chalice): support duplicate filters

* refactor(chalice): changes

* feature(chalice): mobile sessions search

* refactor(chalice): fix EE refactored schema

* fix(chalice): fix missing platform for EE

* fix(DB): fixed init_schema

* Api v1.15.0 no merge (#1557)

* refactor(chalice): changed default dev env vars

* refactor(chalice): changes

* refactor(chalice): changed payload fixer

* refactor(chalice): changed payload fixer
refactor(chalice): support duplicate filters

* refactor(chalice): changes

* feature(chalice): mobile sessions search

* refactor(chalice): fix EE refactored schema

* fix(chalice): fix missing platform for EE

* fix(DB): fixed init_schema

* feat(DB): changes to support mobile sessions

* feat(chalice): mobile sessions search support

* fix(chalice): fixed signup

* Api v1.15.0 (#1559)

* refactor(chalice): changed default dev env vars

* refactor(chalice): changes

* refactor(chalice): changed payload fixer

* refactor(chalice): changed payload fixer
refactor(chalice): support duplicate filters

* refactor(chalice): changes

* feature(chalice): mobile sessions search

* refactor(chalice): fix EE refactored schema

* fix(chalice): fix missing platform for EE

* fix(DB): fixed init_schema

* feat(DB): changes to support mobile sessions

* feat(chalice): mobile sessions search support

* fix(chalice): fixed signup

* fix(DB): added missing column

* Api v1.15.0 no merge (#1562)

* refactor(chalice): changed default dev env vars

* refactor(chalice): changes

* refactor(chalice): changed payload fixer

* refactor(chalice): changed payload fixer
refactor(chalice): support duplicate filters

* refactor(chalice): changes

* feature(chalice): mobile sessions search

* refactor(chalice): fix EE refactored schema

* fix(chalice): fix missing platform for EE

* fix(DB): fixed init_schema

* feat(DB): changes to support mobile sessions

* feat(chalice): mobile sessions search support

* fix(chalice): fixed signup

* fix(DB): added missing column

* refactor(DB): changed queries

* fix(DB): added missing tables

* Api v1.15.0 (#1565)

* refactor(chalice): changed default dev env vars

* refactor(chalice): changes

* refactor(chalice): changed payload fixer

* refactor(chalice): changed payload fixer
refactor(chalice): support duplicate filters

* refactor(chalice): changes

* feature(chalice): mobile sessions search

* refactor(chalice): fix EE refactored schema

* fix(chalice): fix missing platform for EE

* fix(DB): fixed init_schema

* feat(DB): changes to support mobile sessions

* feat(chalice): mobile sessions search support

* fix(chalice): fixed signup

* fix(DB): added missing column

* refactor(DB): changed queries

* fix(chalice): fixed global try sessions

* fix(chalice): fixed global try card

* Api v1.15.0 (#1567)

* refactor(chalice): changed default dev env vars

* refactor(chalice): changes

* refactor(chalice): changed payload fixer

* refactor(chalice): changed payload fixer
refactor(chalice): support duplicate filters

* refactor(chalice): changes

* feature(chalice): mobile sessions search

* refactor(chalice): fix EE refactored schema

* fix(chalice): fix missing platform for EE

* fix(DB): fixed init_schema

* feat(DB): changes to support mobile sessions

* feat(chalice): mobile sessions search support

* fix(chalice): fixed signup

* fix(DB): added missing column

* refactor(DB): changed queries

* fix(chalice): fixed global try sessions

* fix(chalice): fixed global try card

* fix(DB): added missing column

* Api v1.15.0 (#1569)

* refactor(chalice): changed default dev env vars

* refactor(chalice): changes

* refactor(chalice): changed payload fixer

* refactor(chalice): changed payload fixer
refactor(chalice): support duplicate filters

* refactor(chalice): changes

* feature(chalice): mobile sessions search

* refactor(chalice): fix EE refactored schema

* fix(chalice): fix missing platform for EE

* fix(DB): fixed init_schema

* feat(DB): changes to support mobile sessions

* feat(chalice): mobile sessions search support

* fix(chalice): fixed signup

* fix(DB): added missing column

* refactor(DB): changed queries

* fix(chalice): fixed global try sessions

* fix(chalice): fixed global try card

* fix(DB): added missing column

* fix(DB): added missing columns

* Api v1.15.0 (#1571)

* refactor(chalice): changed default dev env vars

* refactor(chalice): changes

* refactor(chalice): changed payload fixer

* refactor(chalice): changed payload fixer
refactor(chalice): support duplicate filters

* refactor(chalice): changes

* feature(chalice): mobile sessions search

* refactor(chalice): fix EE refactored schema

* fix(chalice): fix missing platform for EE

* fix(DB): fixed init_schema

* feat(DB): changes to support mobile sessions

* feat(chalice): mobile sessions search support

* fix(chalice): fixed signup

* fix(DB): added missing column

* refactor(DB): changed queries

* fix(chalice): fixed global try sessions

* fix(chalice): fixed global try card

* fix(DB): added missing column

* fix(DB): added missing columns

* Small assist refactoring (#1534)

* feat(assist): splitted socket methods in ee/websockets

* feat(assist): splitted socket methods in ee/websockets-cluster

* feat(assist): moved socket handlers to separate file

* feat(assist): changed the way how to get ws server

* feat(assist): use method to get io server instead of global object

* feat(assist): removed duplicated code from ee assists

* feat(assist): removed copy-paste http handlers

* feat(assist): added getServer call for some http handlers

* feat(assist): added creds=true to cors settings

* feat(backend): added debug log flag to assist stats

* feat(backend): removed unused ObjectStorage method from interface definition

* fix(backend): added missing ch config to connector service

* fix(backend): added missing Database field to CH connector config

* fetch limits and other initial data

* fix(DB): added missing columns

* Api v1.15.0 (#1573)

* refactor(chalice): changed default dev env vars

* refactor(chalice): changes

* refactor(chalice): changed payload fixer

* refactor(chalice): changed payload fixer
refactor(chalice): support duplicate filters

* refactor(chalice): changes

* feature(chalice): mobile sessions search

* refactor(chalice): fix EE refactored schema

* fix(chalice): fix missing platform for EE

* fix(DB): fixed init_schema

* feat(DB): changes to support mobile sessions

* feat(chalice): mobile sessions search support

* fix(chalice): fixed signup

* fix(DB): added missing column

* refactor(DB): changed queries

* fix(chalice): fixed global try sessions

* fix(chalice): fixed global try card

* fix(DB): added missing column

* fix(DB): added missing columns

* Small assist refactoring (#1534)

* feat(assist): splitted socket methods in ee/websockets

* feat(assist): splitted socket methods in ee/websockets-cluster

* feat(assist): moved socket handlers to separate file

* feat(assist): changed the way how to get ws server

* feat(assist): use method to get io server instead of global object

* feat(assist): removed duplicated code from ee assists

* feat(assist): removed copy-paste http handlers

* feat(assist): added getServer call for some http handlers

* feat(assist): added creds=true to cors settings

* feat(backend): added debug log flag to assist stats

* feat(backend): removed unused ObjectStorage method from interface definition

* fix(backend): added missing ch config to connector service

* fix(backend): added missing Database field to CH connector config

* fetch limits and other initial data

* fix(DB): added missing columns

* fix(chalice): fixed add-edit webhooks
2023-10-26 17:42:21 +02:00

196 lines
7.9 KiB
Python

import logging
from typing import Optional
import requests
from fastapi import HTTPException, status
import schemas
from chalicelib.utils import pg_client, helper
from chalicelib.utils.TimeUTC import TimeUTC
def get_by_id(webhook_id):
with pg_client.PostgresClient() as cur:
cur.execute(
cur.mogrify("""\
SELECT w.*
FROM public.webhooks AS w
WHERE w.webhook_id =%(webhook_id)s AND deleted_at ISNULL;""",
{"webhook_id": webhook_id})
)
w = helper.dict_to_camel_case(cur.fetchone())
if w:
w["createdAt"] = TimeUTC.datetime_to_timestamp(w["createdAt"])
return w
def get_webhook(tenant_id, webhook_id, webhook_type='webhook'):
with pg_client.PostgresClient() as cur:
cur.execute(
cur.mogrify("""SELECT w.*
FROM public.webhooks AS w
WHERE w.webhook_id =%(webhook_id)s AND w.tenant_id =%(tenant_id)s
AND deleted_at ISNULL AND type=%(webhook_type)s;""",
{"webhook_id": webhook_id, "webhook_type": webhook_type, "tenant_id": tenant_id})
)
w = helper.dict_to_camel_case(cur.fetchone())
if w:
w["createdAt"] = TimeUTC.datetime_to_timestamp(w["createdAt"])
return w
def get_by_type(tenant_id, webhook_type):
with pg_client.PostgresClient() as cur:
cur.execute(
cur.mogrify("""SELECT w.webhook_id,w.webhook_id,w.endpoint,w.auth_header,w.type,w.index,w.name,w.created_at
FROM public.webhooks AS w
WHERE w.tenant_id =%(tenant_id)s
AND w.type =%(type)s
AND deleted_at ISNULL;""",
{"type": webhook_type, "tenant_id": tenant_id})
)
webhooks = helper.list_to_camel_case(cur.fetchall())
for w in webhooks:
w["createdAt"] = TimeUTC.datetime_to_timestamp(w["createdAt"])
return webhooks
def get_by_tenant(tenant_id, replace_none=False):
with pg_client.PostgresClient() as cur:
cur.execute(
cur.mogrify("""SELECT w.*
FROM public.webhooks AS w
WHERE w.tenant_id =%(tenant_id)s
AND deleted_at ISNULL;""",
{"tenant_id": tenant_id})
)
all = helper.list_to_camel_case(cur.fetchall())
for w in all:
w["createdAt"] = TimeUTC.datetime_to_timestamp(w["createdAt"])
return all
def update(tenant_id, webhook_id, changes, replace_none=False):
allow_update = ["name", "index", "authHeader", "endpoint"]
with pg_client.PostgresClient() as cur:
sub_query = [f"{helper.key_to_snake_case(k)} = %({k})s" for k in changes.keys() if k in allow_update]
cur.execute(
cur.mogrify(f"""\
UPDATE public.webhooks
SET {','.join(sub_query)}
WHERE tenant_id =%(tenant_id)s AND webhook_id =%(id)s AND deleted_at ISNULL
RETURNING *;""",
{"tenant_id": tenant_id, "id": webhook_id, **changes})
)
w = helper.dict_to_camel_case(cur.fetchone())
if w is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"webhook not found.")
w["createdAt"] = TimeUTC.datetime_to_timestamp(w["createdAt"])
if replace_none:
for k in w.keys():
if w[k] is None:
w[k] = ''
return w
def add(tenant_id, endpoint, auth_header=None, webhook_type='webhook', name="", replace_none=False):
with pg_client.PostgresClient() as cur:
query = cur.mogrify("""\
INSERT INTO public.webhooks(tenant_id, endpoint,auth_header,type,name)
VALUES (%(tenant_id)s, %(endpoint)s, %(auth_header)s, %(type)s,%(name)s)
RETURNING *;""",
{"tenant_id": tenant_id, "endpoint": endpoint, "auth_header": auth_header,
"type": webhook_type, "name": name})
cur.execute(
query
)
w = helper.dict_to_camel_case(cur.fetchone())
w["createdAt"] = TimeUTC.datetime_to_timestamp(w["createdAt"])
if replace_none:
for k in w.keys():
if w[k] is None:
w[k] = ''
return w
def exists_by_name(tenant_id: int, name: str, exclude_id: Optional[int],
webhook_type: str = schemas.WebhookType.webhook) -> bool:
with pg_client.PostgresClient() as cur:
query = cur.mogrify(f"""SELECT EXISTS(SELECT 1
FROM public.webhooks
WHERE name ILIKE %(name)s
AND deleted_at ISNULL
AND tenant_id=%(tenant_id)s
AND type=%(webhook_type)s
{"AND webhook_id!=%(exclude_id)s" if exclude_id else ""}) AS exists;""",
{"tenant_id": tenant_id, "name": name, "exclude_id": exclude_id,
"webhook_type": webhook_type})
cur.execute(query)
row = cur.fetchone()
return row["exists"]
def add_edit(tenant_id, data: schemas.WebhookSchema, replace_none=None):
if len(data.name) > 0 \
and exists_by_name(name=data.name, exclude_id=data.webhook_id, tenant_id=tenant_id):
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"name already exists.")
if data.webhook_id is not None:
return update(tenant_id=tenant_id, webhook_id=data.webhook_id,
changes={"endpoint": data.endpoint.unicode_string(),
"authHeader": data.auth_header,
"name": data.name},
replace_none=replace_none)
else:
return add(tenant_id=tenant_id,
endpoint=data.endpoint.unicode_string(),
auth_header=data.auth_header,
name=data.name,
replace_none=replace_none)
def delete(tenant_id, webhook_id):
with pg_client.PostgresClient() as cur:
cur.execute(
cur.mogrify("""\
UPDATE public.webhooks
SET deleted_at = (now() at time zone 'utc')
WHERE tenant_id =%(tenant_id)s AND webhook_id =%(id)s AND deleted_at ISNULL
RETURNING *;""",
{"tenant_id": tenant_id, "id": webhook_id})
)
return {"data": {"state": "success"}}
def trigger_batch(data_list):
webhooks_map = {}
for w in data_list:
if w["destination"] not in webhooks_map:
webhooks_map[w["destination"]] = get_by_id(webhook_id=w["destination"])
if webhooks_map[w["destination"]] is None:
logging.error(f"!!Error webhook not found: webhook_id={w['destination']}")
else:
__trigger(hook=webhooks_map[w["destination"]], data=w["data"])
def __trigger(hook, data):
if hook is not None and hook["type"] == 'webhook':
headers = {}
if hook["authHeader"] is not None and len(hook["authHeader"]) > 0:
headers = {"Authorization": hook["authHeader"]}
r = requests.post(url=hook["endpoint"], json=data, headers=headers)
if r.status_code != 200:
logging.error("=======> webhook: something went wrong for:")
logging.error(hook)
logging.error(r.status_code)
logging.error(r.text)
return
response = None
try:
response = r.json()
except:
try:
response = r.text
except:
logging.info("no response found")
return response