-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
external_initiator_manager.go
215 lines (188 loc) · 6.92 KB
/
external_initiator_manager.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package webhook
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/lib/pq"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"github.com/smartcontractkit/sqlx"
"github.com/smartcontractkit/chainlink/core/bridges"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/services/job"
"github.com/smartcontractkit/chainlink/core/services/pg"
"github.com/smartcontractkit/chainlink/core/static"
"github.com/smartcontractkit/chainlink/core/store/models"
)
//go:generate mockery --quiet --name ExternalInitiatorManager --output ./mocks/ --case=underscore
// ExternalInitiatorManager manages HTTP requests to remote external initiators
type ExternalInitiatorManager interface {
Notify(webhookSpecID int32) error
DeleteJob(webhookSpecID int32) error
FindExternalInitiatorByName(name string) (bridges.ExternalInitiator, error)
}
//go:generate mockery --quiet --name HTTPClient --output ./mocks/ --case=underscore
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
type externalInitiatorManager struct {
q pg.Q
httpclient HTTPClient
}
var _ ExternalInitiatorManager = (*externalInitiatorManager)(nil)
// NewExternalInitiatorManager returns the concrete externalInitiatorManager
func NewExternalInitiatorManager(db *sqlx.DB, httpclient HTTPClient, lggr logger.Logger, cfg pg.QConfig) *externalInitiatorManager {
namedLogger := lggr.Named("ExternalInitiatorManager")
return &externalInitiatorManager{
q: pg.NewQ(db, namedLogger, cfg),
httpclient: httpclient,
}
}
// Notify sends a POST notification to the External Initiator
// responsible for initiating the Job Spec.
func (m externalInitiatorManager) Notify(webhookSpecID int32) error {
eiWebhookSpecs, jobID, err := m.Load(webhookSpecID)
if err != nil {
return err
}
for _, eiWebhookSpec := range eiWebhookSpecs {
ei := eiWebhookSpec.ExternalInitiator
if ei.URL == nil {
continue
}
notice := JobSpecNotice{
JobID: jobID,
Type: ei.Name,
Params: eiWebhookSpec.Spec,
}
buf, err := json.Marshal(notice)
if err != nil {
return errors.Wrap(err, "new Job Spec notification")
}
req, err := newNotifyHTTPRequest(buf, ei)
if err != nil {
return errors.Wrap(err, "creating notify HTTP request")
}
resp, err := m.httpclient.Do(req)
if err != nil {
return errors.Wrap(err, "could not notify '%s' (%s)")
}
if err := resp.Body.Close(); err != nil {
return err
}
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return fmt.Errorf(" notify '%s' (%s) received bad response '%d: %s'", ei.Name, ei.URL, resp.StatusCode, resp.Status)
}
}
return nil
}
func (m externalInitiatorManager) Load(webhookSpecID int32) (eiWebhookSpecs []job.ExternalInitiatorWebhookSpec, jobID uuid.UUID, err error) {
err = m.q.Transaction(func(tx pg.Queryer) error {
if err = tx.Get(&jobID, "SELECT external_job_id FROM jobs WHERE webhook_spec_id = $1", webhookSpecID); err != nil {
if err = errors.Wrapf(err, "failed to load job ID from job for webhook spec with ID %d", webhookSpecID); err != nil {
return err
}
}
if err = tx.Select(&eiWebhookSpecs, "SELECT * FROM external_initiator_webhook_specs WHERE external_initiator_webhook_specs.webhook_spec_id = $1", webhookSpecID); err != nil {
if err = errors.Wrapf(err, "failed to load external_initiator_webhook_specs for webhook_spec_id %d", webhookSpecID); err != nil {
return err
}
}
if err = m.eagerLoadExternalInitiator(tx, eiWebhookSpecs); err != nil {
if err = errors.Wrapf(err, "failed to preload ExternalInitiator for webhook_spec_id %d", webhookSpecID); err != nil {
return err
}
}
return nil
})
return
}
func (m externalInitiatorManager) eagerLoadExternalInitiator(q pg.Queryer, txs []job.ExternalInitiatorWebhookSpec) error {
var ids []int64
for _, tx := range txs {
ids = append(ids, tx.ExternalInitiatorID)
}
if len(ids) == 0 {
return nil
}
var externalInitiators []bridges.ExternalInitiator
if err := sqlx.Select(q, &externalInitiators, `SELECT * FROM external_initiators WHERE external_initiators.id = ANY($1);`, pq.Array(ids)); err != nil {
return err
}
eiMap := make(map[int64]bridges.ExternalInitiator)
for _, externalInitiator := range externalInitiators {
eiMap[externalInitiator.ID] = externalInitiator
}
for i := range txs {
txs[i].ExternalInitiator = eiMap[txs[i].ExternalInitiatorID]
}
return nil
}
func (m externalInitiatorManager) DeleteJob(webhookSpecID int32) error {
eiWebhookSpecs, jobID, err := m.Load(webhookSpecID)
if err != nil {
return err
}
for _, eiWebhookSpec := range eiWebhookSpecs {
ei := eiWebhookSpec.ExternalInitiator
if ei.URL == nil {
continue
}
req, err := newDeleteJobFromExternalInitiatorHTTPRequest(ei, jobID)
if err != nil {
return errors.Wrap(err, "creating delete HTTP request")
}
resp, err := m.httpclient.Do(req)
if err != nil {
return errors.Wrapf(err, "could not delete job from remote external initiator at %s", req.URL)
}
if err := resp.Body.Close(); err != nil {
return err
}
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return fmt.Errorf(" delete '%s' (%s) received bad response '%d: %s'", ei.Name, ei.URL, resp.StatusCode, resp.Status)
}
}
return nil
}
func (m externalInitiatorManager) FindExternalInitiatorByName(name string) (bridges.ExternalInitiator, error) {
var exi bridges.ExternalInitiator
err := m.q.Get(&exi, "SELECT * FROM external_initiators WHERE lower(external_initiators.name) = lower($1)", name)
return exi, err
}
// JobSpecNotice is sent to the External Initiator when JobSpecs are created.
type JobSpecNotice struct {
JobID uuid.UUID `json:"jobId"`
Type string `json:"type"`
Params models.JSON `json:"params,omitempty"`
}
func newNotifyHTTPRequest(buf []byte, ei bridges.ExternalInitiator) (*http.Request, error) {
req, err := http.NewRequest(http.MethodPost, ei.URL.String(), bytes.NewBuffer(buf))
if err != nil {
return nil, err
}
setHeaders(req, ei)
return req, nil
}
func newDeleteJobFromExternalInitiatorHTTPRequest(ei bridges.ExternalInitiator, jobID uuid.UUID) (*http.Request, error) {
url := fmt.Sprintf("%s/%s", ei.URL.String(), jobID)
req, err := http.NewRequest(http.MethodDelete, url, nil)
if err != nil {
return nil, err
}
setHeaders(req, ei)
return req, nil
}
func setHeaders(req *http.Request, ei bridges.ExternalInitiator) {
req.Header.Set("Content-Type", "application/json")
req.Header.Set(static.ExternalInitiatorAccessKeyHeader, ei.OutgoingToken)
req.Header.Set(static.ExternalInitiatorSecretHeader, ei.OutgoingSecret)
}
type NullExternalInitiatorManager struct{}
var _ ExternalInitiatorManager = (*NullExternalInitiatorManager)(nil)
func (NullExternalInitiatorManager) Notify(int32) error { return nil }
func (NullExternalInitiatorManager) DeleteJob(int32) error { return nil }
func (NullExternalInitiatorManager) FindExternalInitiatorByName(name string) (bridges.ExternalInitiator, error) {
return bridges.ExternalInitiator{}, nil
}