Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support checking whether send-webhook API has error #2944

Merged
merged 5 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/casdoor/notify v0.45.0
github.com/casdoor/oss v1.6.0
github.com/casdoor/xorm-adapter/v3 v3.1.0
github.com/casvisor/casvisor-go-sdk v1.3.0
github.com/casvisor/casvisor-go-sdk v1.4.0
github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f
github.com/denisenkom/go-mssqldb v0.9.0
github.com/elazarl/go-bindata-assetfs v1.0.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1095,8 +1095,8 @@ github.com/casdoor/oss v1.6.0 h1:IOWrGLJ+VO82qS796eaRnzFPPA1Sn3cotYTi7O/VIlQ=
github.com/casdoor/oss v1.6.0/go.mod h1:rJAWA0hLhtu94t6IRpotLUkXO1NWMASirywQYaGizJE=
github.com/casdoor/xorm-adapter/v3 v3.1.0 h1:NodWayRtSLVSeCvL9H3Hc61k0G17KhV9IymTCNfh3kk=
github.com/casdoor/xorm-adapter/v3 v3.1.0/go.mod h1:4WTcUw+bTgBylGHeGHzTtBvuTXRS23dtwzFLl9tsgFM=
github.com/casvisor/casvisor-go-sdk v1.3.0 h1:HVgm2g3lWpNX2wBNidzR743QY4O5kAjLUJ9tS2juO8g=
github.com/casvisor/casvisor-go-sdk v1.3.0/go.mod h1:frnNtH5GA0wxzAQLyZxxfL0RSsSub9GQPi2Ybe86ocE=
github.com/casvisor/casvisor-go-sdk v1.4.0 h1:hbZEGGJ1cwdHFAxeXrMoNw6yha6Oyg2F0qQhBNCN/dg=
dacongda marked this conversation as resolved.
Show resolved Hide resolved
github.com/casvisor/casvisor-go-sdk v1.4.0/go.mod h1:frnNtH5GA0wxzAQLyZxxfL0RSsSub9GQPi2Ybe86ocE=
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/cenkalti/backoff/v4 v4.1.2/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4=
Expand Down
52 changes: 48 additions & 4 deletions object/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,18 @@ func NewRecord(ctx *context.Context) (*casvisorsdk.Record, error) {
Action: action,
Language: languageCode,
Object: object,
StatusCode: 200,
Response: fmt.Sprintf("{status:\"%s\", msg:\"%s\"}", resp.Status, resp.Msg),
IsTriggered: false,
}
return &record, nil
}

func addRecord(record *casvisorsdk.Record) (int64, error) {
affected, err := ormer.Engine.Insert(record)
return affected, err
}

func AddRecord(record *casvisorsdk.Record) bool {
if logPostOnly {
if record.Method == "GET" {
Expand All @@ -108,7 +114,6 @@ func AddRecord(record *casvisorsdk.Record) bool {
}

record.Owner = record.Organization

record.Object = maskPassword(record.Object)

errWebhook := SendWebhooks(record)
Expand All @@ -119,7 +124,7 @@ func AddRecord(record *casvisorsdk.Record) bool {
}

if casvisorsdk.GetClient() == nil {
affected, err := ormer.Engine.Insert(record)
affected, err := addRecord(record)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -224,6 +229,40 @@ func getFilteredWebhooks(webhooks []*Webhook, organization string, action string
return res
}

func addWebhookRecord(webhook *Webhook, record *casvisorsdk.Record, statusCode int, respBody string, sendError error) error {
if statusCode == 200 {
return nil
}

if len(respBody) > 300 {
respBody = respBody[0:300]
}

webhookRecord := &casvisorsdk.Record{
Owner: record.Owner,
Name: util.GenerateId(),
CreatedTime: util.GetCurrentTime(),
Organization: record.Organization,
User: record.User,

Method: webhook.Method,
Action: "send-webhook",
RequestUri: webhook.Url,
StatusCode: statusCode,
Response: respBody,
Language: record.Language,
IsTriggered: false,
}

if sendError != nil {
webhookRecord.Response = sendError.Error()
}

_, err := addRecord(webhookRecord)

return err
}

func SendWebhooks(record *casvisorsdk.Record) error {
webhooks, err := getWebhooksByOrganization("")
if err != nil {
Expand All @@ -248,11 +287,16 @@ func SendWebhooks(record *casvisorsdk.Record) error {
}
}

err = sendWebhook(webhook, record, user)
statusCode, respBody, err := sendWebhook(webhook, record, user)
dacongda marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
errs = append(errs, err)
}

err = addWebhookRecord(webhook, record, statusCode, respBody, err)
if err != nil {
errs = append(errs, err)
continue
}

}

if len(errs) > 0 {
Expand Down
18 changes: 14 additions & 4 deletions object/webhook_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
package object

import (
"io"
"net/http"
"strings"

"github.com/casdoor/casdoor/util"
"github.com/casvisor/casvisor-go-sdk/casvisorsdk"
)

func sendWebhook(webhook *Webhook, record *casvisorsdk.Record, extendedUser *User) error {
func sendWebhook(webhook *Webhook, record *casvisorsdk.Record, extendedUser *User) (int, string, error) {
client := &http.Client{}

type RecordEx struct {
Expand All @@ -38,7 +39,7 @@ func sendWebhook(webhook *Webhook, record *casvisorsdk.Record, extendedUser *Use

req, err := http.NewRequest(webhook.Method, webhook.Url, body)
if err != nil {
return err
return 0, "", err
}

req.Header.Set("Content-Type", webhook.ContentType)
Expand All @@ -47,6 +48,15 @@ func sendWebhook(webhook *Webhook, record *casvisorsdk.Record, extendedUser *Use
req.Header.Set(header.Name, header.Value)
}

_, err = client.Do(req)
return err
resp, err := client.Do(req)
if err != nil {
return 0, "", err
}

defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return 0, "", err
}
return resp.StatusCode, string(bodyBytes), err
}
8 changes: 8 additions & 0 deletions web/src/RecordListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ class RecordListPage extends BaseListPage {
sorter: true,
...this.getColumnSearchProps("language"),
},
{
title: i18next.t("record:Status code"),
dataIndex: "statusCode",
key: "statusCode",
width: "90px",
sorter: true,
...this.getColumnSearchProps("statusCode"),
},
{
title: i18next.t("record:Response"),
dataIndex: "response",
Expand Down
4 changes: 3 additions & 1 deletion web/src/locales/ar/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,8 @@
"record": {
"Is triggered": "Is triggered",
"Object": "Object",
"Response": "Response"
"Response": "Response",
"Status code": "Status code"
},
"resource": {
"Copy Link": "Copy Link",
Expand Down Expand Up @@ -1171,6 +1172,7 @@
"input password": "input password"
},
"verification": {
"Is used": "Is used",
"Receiver": "Receiver"
},
"webhook": {
Expand Down
4 changes: 3 additions & 1 deletion web/src/locales/de/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,8 @@
"record": {
"Is triggered": "Is triggered",
"Object": "Object",
"Response": "Response"
"Response": "Response",
"Status code": "Status code"
},
"resource": {
"Copy Link": "Kopiere den Link",
Expand Down Expand Up @@ -1171,6 +1172,7 @@
"input password": "Eingabe des Passworts"
},
"verification": {
"Is used": "Is used",
"Receiver": "Receiver"
},
"webhook": {
Expand Down
4 changes: 3 additions & 1 deletion web/src/locales/en/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,8 @@
"record": {
"Is triggered": "Is triggered",
"Object": "Object",
"Response": "Response"
"Response": "Response",
"Status code": "Status code"
},
"resource": {
"Copy Link": "Copy Link",
Expand Down Expand Up @@ -1171,6 +1172,7 @@
"input password": "input password"
},
"verification": {
"Is used": "Is used",
"Receiver": "Receiver"
},
"webhook": {
Expand Down
4 changes: 3 additions & 1 deletion web/src/locales/es/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,8 @@
"record": {
"Is triggered": "Is triggered",
"Object": "Object",
"Response": "Response"
"Response": "Response",
"Status code": "Status code"
},
"resource": {
"Copy Link": "Copiar enlace",
Expand Down Expand Up @@ -1171,6 +1172,7 @@
"input password": "Ingresar contraseña"
},
"verification": {
"Is used": "Is used",
"Receiver": "Receiver"
},
"webhook": {
Expand Down
4 changes: 3 additions & 1 deletion web/src/locales/fa/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,8 @@
"record": {
"Is triggered": "Is triggered",
"Object": "Object",
"Response": "Response"
"Response": "Response",
"Status code": "Status code"
},
"resource": {
"Copy Link": "Copy Link",
Expand Down Expand Up @@ -1171,6 +1172,7 @@
"input password": "input password"
},
"verification": {
"Is used": "Is used",
"Receiver": "Receiver"
},
"webhook": {
Expand Down
4 changes: 3 additions & 1 deletion web/src/locales/fi/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,8 @@
"record": {
"Is triggered": "Is triggered",
"Object": "Object",
"Response": "Response"
"Response": "Response",
"Status code": "Status code"
},
"resource": {
"Copy Link": "Copy Link",
Expand Down Expand Up @@ -1171,6 +1172,7 @@
"input password": "input password"
},
"verification": {
"Is used": "Is used",
"Receiver": "Receiver"
},
"webhook": {
Expand Down
4 changes: 3 additions & 1 deletion web/src/locales/fr/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,8 @@
"record": {
"Is triggered": "Is triggered",
"Object": "Object",
"Response": "Response"
"Response": "Response",
"Status code": "Status code"
},
"resource": {
"Copy Link": "Copier le lien",
Expand Down Expand Up @@ -1171,6 +1172,7 @@
"input password": "saisir le mot de passe"
},
"verification": {
"Is used": "Is used",
"Receiver": "Receiver"
},
"webhook": {
Expand Down
4 changes: 3 additions & 1 deletion web/src/locales/he/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,8 @@
"record": {
"Is triggered": "Is triggered",
"Object": "Object",
"Response": "Response"
"Response": "Response",
"Status code": "Status code"
},
"resource": {
"Copy Link": "Copy Link",
Expand Down Expand Up @@ -1171,6 +1172,7 @@
"input password": "input password"
},
"verification": {
"Is used": "Is used",
"Receiver": "Receiver"
},
"webhook": {
Expand Down
4 changes: 3 additions & 1 deletion web/src/locales/id/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,8 @@
"record": {
"Is triggered": "Is triggered",
"Object": "Object",
"Response": "Response"
"Response": "Response",
"Status code": "Status code"
},
"resource": {
"Copy Link": "Salin Tautan",
Expand Down Expand Up @@ -1171,6 +1172,7 @@
"input password": "masukkan kata sandi"
},
"verification": {
"Is used": "Is used",
"Receiver": "Receiver"
},
"webhook": {
Expand Down
4 changes: 3 additions & 1 deletion web/src/locales/it/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,8 @@
"record": {
"Is triggered": "Is triggered",
"Object": "Object",
"Response": "Response"
"Response": "Response",
"Status code": "Status code"
},
"resource": {
"Copy Link": "Copy Link",
Expand Down Expand Up @@ -1171,6 +1172,7 @@
"input password": "input password"
},
"verification": {
"Is used": "Is used",
"Receiver": "Receiver"
},
"webhook": {
Expand Down
4 changes: 3 additions & 1 deletion web/src/locales/ja/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,8 @@
"record": {
"Is triggered": "Is triggered",
"Object": "Object",
"Response": "Response"
"Response": "Response",
"Status code": "Status code"
},
"resource": {
"Copy Link": "コピー リンク",
Expand Down Expand Up @@ -1171,6 +1172,7 @@
"input password": "パスワードを入力してください"
},
"verification": {
"Is used": "Is used",
"Receiver": "Receiver"
},
"webhook": {
Expand Down
4 changes: 3 additions & 1 deletion web/src/locales/kk/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,8 @@
"record": {
"Is triggered": "Is triggered",
"Object": "Object",
"Response": "Response"
"Response": "Response",
"Status code": "Status code"
},
"resource": {
"Copy Link": "Copy Link",
Expand Down Expand Up @@ -1171,6 +1172,7 @@
"input password": "input password"
},
"verification": {
"Is used": "Is used",
"Receiver": "Receiver"
},
"webhook": {
Expand Down