-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
external_initiators_controller.go
113 lines (97 loc) · 3.67 KB
/
external_initiators_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package web
import (
"database/sql"
"fmt"
"net/http"
"regexp"
"github.com/smartcontractkit/chainlink/v2/core/auth"
"github.com/smartcontractkit/chainlink/v2/core/bridges"
"github.com/smartcontractkit/chainlink/v2/core/logger/audit"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
"github.com/smartcontractkit/chainlink/v2/core/web/presenters"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
var (
externalInitiatorNameRegexp = regexp.MustCompile("^[a-zA-Z0-9-_]+$")
)
// ValidateExternalInitiator checks whether External Initiator parameters are
// safe for processing.
func ValidateExternalInitiator(
exi *bridges.ExternalInitiatorRequest,
orm bridges.ORM,
) error {
fe := models.NewJSONAPIErrors()
if len([]rune(exi.Name)) == 0 {
fe.Add("No name specified")
} else if !externalInitiatorNameRegexp.MatchString(exi.Name) {
fe.Add("Name must be alphanumeric and may contain '_' or '-'")
} else if _, err := orm.FindExternalInitiatorByName(exi.Name); err == nil {
fe.Add(fmt.Sprintf("Name %v already exists", exi.Name))
} else if !errors.Is(err, sql.ErrNoRows) {
return errors.Wrap(err, "validating external initiator")
}
return fe.CoerceEmptyToNil()
}
// ExternalInitiatorsController manages external initiators
type ExternalInitiatorsController struct {
App chainlink.Application
}
func (eic *ExternalInitiatorsController) Index(c *gin.Context, size, page, offset int) {
eis, count, err := eic.App.BridgeORM().ExternalInitiators(offset, size)
var resources []presenters.ExternalInitiatorResource
for _, ei := range eis {
resources = append(resources, presenters.NewExternalInitiatorResource(ei))
}
paginatedResponse(c, "externalInitiators", size, page, resources, count, err)
}
// Create builds and saves a new external initiator
func (eic *ExternalInitiatorsController) Create(c *gin.Context) {
eir := &bridges.ExternalInitiatorRequest{}
if !eic.App.GetConfig().JobPipeline().ExternalInitiatorsEnabled() {
err := errors.New("The External Initiator feature is disabled by configuration")
jsonAPIError(c, http.StatusMethodNotAllowed, err)
return
}
eia := auth.NewToken()
if err := c.ShouldBindJSON(eir); err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
ei, err := bridges.NewExternalInitiator(eia, eir)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
if err := ValidateExternalInitiator(eir, eic.App.BridgeORM()); err != nil {
jsonAPIError(c, http.StatusBadRequest, err)
return
}
if err := eic.App.BridgeORM().CreateExternalInitiator(ei); err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
eic.App.GetAuditLogger().Audit(audit.ExternalInitiatorCreated, map[string]interface{}{
"externalInitiatorID": ei.ID,
"externalInitiatorName": ei.Name,
"externalInitiatorURL": ei.URL,
})
resp := presenters.NewExternalInitiatorAuthentication(*ei, *eia)
jsonAPIResponseWithStatus(c, resp, "external initiator authentication", http.StatusCreated)
}
// Destroy deletes an ExternalInitiator
func (eic *ExternalInitiatorsController) Destroy(c *gin.Context) {
name := c.Param("Name")
exi, err := eic.App.BridgeORM().FindExternalInitiatorByName(name)
if errors.Is(err, sql.ErrNoRows) {
jsonAPIError(c, http.StatusNotFound, errors.New("external initiator not found"))
return
}
if err := eic.App.BridgeORM().DeleteExternalInitiator(exi.Name); err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
eic.App.GetAuditLogger().Audit(audit.ExternalInitiatorDeleted, map[string]interface{}{"name": name})
jsonAPIResponseWithStatus(c, nil, "external initiator", http.StatusNoContent)
}