2022-07-06 05:08:21 +00:00
|
|
|
package cassandradb
|
|
|
|
|
|
|
|
import (
|
2022-07-10 16:19:33 +00:00
|
|
|
"context"
|
2022-07-09 06:46:54 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
2022-07-08 13:39:23 +00:00
|
|
|
"time"
|
|
|
|
|
2022-07-06 05:08:21 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2022-07-09 06:46:54 +00:00
|
|
|
"github.com/gocql/gocql"
|
2022-07-08 13:39:23 +00:00
|
|
|
"github.com/google/uuid"
|
2022-07-06 05:08:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AddWebhook to add webhook
|
2023-07-31 11:12:11 +00:00
|
|
|
func (p *provider) AddWebhook(ctx context.Context, webhook *models.Webhook) (*model.Webhook, error) {
|
2022-07-08 13:39:23 +00:00
|
|
|
if webhook.ID == "" {
|
|
|
|
webhook.ID = uuid.New().String()
|
|
|
|
}
|
|
|
|
webhook.Key = webhook.ID
|
|
|
|
webhook.CreatedAt = time.Now().Unix()
|
|
|
|
webhook.UpdatedAt = time.Now().Unix()
|
2023-03-26 01:50:45 +00:00
|
|
|
// Add timestamp to make event name unique for legacy version
|
|
|
|
webhook.EventName = fmt.Sprintf("%s-%d", webhook.EventName, time.Now().Unix())
|
2023-03-26 02:18:06 +00:00
|
|
|
insertQuery := fmt.Sprintf("INSERT INTO %s (id, event_description, event_name, endpoint, headers, enabled, created_at, updated_at) VALUES ('%s', '%s', '%s', '%s', '%s', %t, %d, %d)", KeySpace+"."+models.Collections.Webhook, webhook.ID, webhook.EventDescription, webhook.EventName, webhook.EndPoint, webhook.Headers, webhook.Enabled, webhook.CreatedAt, webhook.UpdatedAt)
|
2022-07-09 06:46:54 +00:00
|
|
|
err := p.db.Query(insertQuery).Exec()
|
|
|
|
if err != nil {
|
2022-07-10 16:19:33 +00:00
|
|
|
return nil, err
|
2022-07-09 06:46:54 +00:00
|
|
|
}
|
2022-07-10 16:19:33 +00:00
|
|
|
return webhook.AsAPIWebhook(), nil
|
2022-07-06 05:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateWebhook to update webhook
|
2023-07-31 11:12:11 +00:00
|
|
|
func (p *provider) UpdateWebhook(ctx context.Context, webhook *models.Webhook) (*model.Webhook, error) {
|
2022-07-08 13:39:23 +00:00
|
|
|
webhook.UpdatedAt = time.Now().Unix()
|
2023-03-26 01:50:45 +00:00
|
|
|
// Event is changed
|
|
|
|
if !strings.Contains(webhook.EventName, "-") {
|
|
|
|
webhook.EventName = fmt.Sprintf("%s-%d", webhook.EventName, time.Now().Unix())
|
|
|
|
}
|
2022-07-09 06:46:54 +00:00
|
|
|
bytes, err := json.Marshal(webhook)
|
|
|
|
if err != nil {
|
2022-07-10 16:19:33 +00:00
|
|
|
return nil, err
|
2022-07-09 06:46:54 +00:00
|
|
|
}
|
|
|
|
// use decoder instead of json.Unmarshall, because it converts int64 -> float64 after unmarshalling
|
|
|
|
decoder := json.NewDecoder(strings.NewReader(string(bytes)))
|
|
|
|
decoder.UseNumber()
|
|
|
|
webhookMap := map[string]interface{}{}
|
|
|
|
err = decoder.Decode(&webhookMap)
|
|
|
|
if err != nil {
|
2022-07-10 16:19:33 +00:00
|
|
|
return nil, err
|
2022-07-09 06:46:54 +00:00
|
|
|
}
|
|
|
|
updateFields := ""
|
|
|
|
for key, value := range webhookMap {
|
|
|
|
if key == "_id" {
|
|
|
|
continue
|
|
|
|
}
|
2022-07-12 06:18:42 +00:00
|
|
|
if key == "_key" {
|
|
|
|
continue
|
|
|
|
}
|
2022-07-09 06:46:54 +00:00
|
|
|
if value == nil {
|
|
|
|
updateFields += fmt.Sprintf("%s = null,", key)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
valueType := reflect.TypeOf(value)
|
|
|
|
if valueType.Name() == "string" {
|
|
|
|
updateFields += fmt.Sprintf("%s = '%s', ", key, value.(string))
|
|
|
|
} else {
|
|
|
|
updateFields += fmt.Sprintf("%s = %v, ", key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updateFields = strings.Trim(updateFields, " ")
|
|
|
|
updateFields = strings.TrimSuffix(updateFields, ",")
|
|
|
|
query := fmt.Sprintf("UPDATE %s SET %s WHERE id = '%s'", KeySpace+"."+models.Collections.Webhook, updateFields, webhook.ID)
|
|
|
|
err = p.db.Query(query).Exec()
|
|
|
|
if err != nil {
|
2022-07-10 16:19:33 +00:00
|
|
|
return nil, err
|
2022-07-09 06:46:54 +00:00
|
|
|
}
|
2022-07-10 16:19:33 +00:00
|
|
|
return webhook.AsAPIWebhook(), nil
|
2022-07-06 05:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListWebhooks to list webhook
|
2023-07-31 11:12:11 +00:00
|
|
|
func (p *provider) ListWebhook(ctx context.Context, pagination *model.Pagination) (*model.Webhooks, error) {
|
2022-07-09 06:46:54 +00:00
|
|
|
webhooks := []*model.Webhook{}
|
|
|
|
paginationClone := pagination
|
|
|
|
totalCountQuery := fmt.Sprintf(`SELECT COUNT(*) FROM %s`, KeySpace+"."+models.Collections.Webhook)
|
2023-08-01 10:39:17 +00:00
|
|
|
err := p.db.Query(totalCountQuery).Consistency(gocql.One).Scan(&paginationClone.Total)
|
2022-07-09 06:46:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// there is no offset in cassandra
|
|
|
|
// so we fetch till limit + offset
|
|
|
|
// and return the results from offset to limit
|
2023-03-26 02:18:06 +00:00
|
|
|
query := fmt.Sprintf("SELECT id, event_description, event_name, endpoint, headers, enabled, created_at, updated_at FROM %s LIMIT %d", KeySpace+"."+models.Collections.Webhook, pagination.Limit+pagination.Offset)
|
2022-07-09 06:46:54 +00:00
|
|
|
scanner := p.db.Query(query).Iter().Scanner()
|
|
|
|
counter := int64(0)
|
|
|
|
for scanner.Next() {
|
|
|
|
if counter >= pagination.Offset {
|
2023-08-01 10:39:17 +00:00
|
|
|
var webhook models.Webhook
|
2023-03-26 02:18:06 +00:00
|
|
|
err := scanner.Scan(&webhook.ID, &webhook.EventDescription, &webhook.EventName, &webhook.EndPoint, &webhook.Headers, &webhook.Enabled, &webhook.CreatedAt, &webhook.UpdatedAt)
|
2022-07-09 06:46:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
webhooks = append(webhooks, webhook.AsAPIWebhook())
|
|
|
|
}
|
|
|
|
counter++
|
|
|
|
}
|
|
|
|
|
|
|
|
return &model.Webhooks{
|
2023-07-31 11:12:11 +00:00
|
|
|
Pagination: paginationClone,
|
2022-07-09 06:46:54 +00:00
|
|
|
Webhooks: webhooks,
|
|
|
|
}, nil
|
2022-07-06 05:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetWebhookByID to get webhook by id
|
2022-07-10 16:19:33 +00:00
|
|
|
func (p *provider) GetWebhookByID(ctx context.Context, webhookID string) (*model.Webhook, error) {
|
2023-08-01 10:39:17 +00:00
|
|
|
var webhook models.Webhook
|
2023-03-26 02:18:06 +00:00
|
|
|
query := fmt.Sprintf(`SELECT id, event_description, event_name, endpoint, headers, enabled, created_at, updated_at FROM %s WHERE id = '%s' LIMIT 1`, KeySpace+"."+models.Collections.Webhook, webhookID)
|
|
|
|
err := p.db.Query(query).Consistency(gocql.One).Scan(&webhook.ID, &webhook.EventDescription, &webhook.EventName, &webhook.EndPoint, &webhook.Headers, &webhook.Enabled, &webhook.CreatedAt, &webhook.UpdatedAt)
|
2022-07-09 06:46:54 +00:00
|
|
|
if err != nil {
|
2022-07-10 16:19:33 +00:00
|
|
|
return nil, err
|
2022-07-09 06:46:54 +00:00
|
|
|
}
|
2022-07-10 16:19:33 +00:00
|
|
|
return webhook.AsAPIWebhook(), nil
|
2022-07-06 05:08:21 +00:00
|
|
|
}
|
|
|
|
|
2022-07-09 05:51:32 +00:00
|
|
|
// GetWebhookByEventName to get webhook by event_name
|
2023-03-26 01:50:45 +00:00
|
|
|
func (p *provider) GetWebhookByEventName(ctx context.Context, eventName string) ([]*model.Webhook, error) {
|
2023-03-29 01:36:33 +00:00
|
|
|
query := fmt.Sprintf(`SELECT id, event_description, event_name, endpoint, headers, enabled, created_at, updated_at FROM %s WHERE event_name LIKE '%s' ALLOW FILTERING`, KeySpace+"."+models.Collections.Webhook, eventName+"%")
|
2023-03-26 01:50:45 +00:00
|
|
|
scanner := p.db.Query(query).Iter().Scanner()
|
|
|
|
webhooks := []*model.Webhook{}
|
|
|
|
for scanner.Next() {
|
2023-08-01 10:39:17 +00:00
|
|
|
var webhook models.Webhook
|
2023-03-26 02:18:06 +00:00
|
|
|
err := scanner.Scan(&webhook.ID, &webhook.EventDescription, &webhook.EventName, &webhook.EndPoint, &webhook.Headers, &webhook.Enabled, &webhook.CreatedAt, &webhook.UpdatedAt)
|
2023-03-26 01:50:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
webhooks = append(webhooks, webhook.AsAPIWebhook())
|
2022-07-09 06:46:54 +00:00
|
|
|
}
|
2023-03-26 01:50:45 +00:00
|
|
|
return webhooks, nil
|
2022-07-06 05:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteWebhook to delete webhook
|
2022-07-10 16:19:33 +00:00
|
|
|
func (p *provider) DeleteWebhook(ctx context.Context, webhook *model.Webhook) error {
|
2022-07-09 06:46:54 +00:00
|
|
|
query := fmt.Sprintf("DELETE FROM %s WHERE id = '%s'", KeySpace+"."+models.Collections.Webhook, webhook.ID)
|
|
|
|
err := p.db.Query(query).Exec()
|
2022-07-11 14:10:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-12 06:18:42 +00:00
|
|
|
getWebhookLogQuery := fmt.Sprintf("SELECT id FROM %s WHERE webhook_id = '%s' ALLOW FILTERING", KeySpace+"."+models.Collections.WebhookLog, webhook.ID)
|
|
|
|
scanner := p.db.Query(getWebhookLogQuery).Iter().Scanner()
|
|
|
|
webhookLogIDs := ""
|
|
|
|
for scanner.Next() {
|
|
|
|
var wlID string
|
|
|
|
err = scanner.Scan(&wlID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
webhookLogIDs += fmt.Sprintf("'%s',", wlID)
|
|
|
|
}
|
|
|
|
webhookLogIDs = strings.TrimSuffix(webhookLogIDs, ",")
|
|
|
|
query = fmt.Sprintf("DELETE FROM %s WHERE id IN (%s)", KeySpace+"."+models.Collections.WebhookLog, webhookLogIDs)
|
2022-07-11 14:10:54 +00:00
|
|
|
err = p.db.Query(query).Exec()
|
2022-07-09 06:46:54 +00:00
|
|
|
return err
|
2022-07-06 05:08:21 +00:00
|
|
|
}
|