Repository navigation
Expand file tree
/
Copy pathtarget_runtime.go
More file actions
132 lines (121 loc) · 3.38 KB
/
Copy pathtarget_runtime.go
File metadata and controls
132 lines (121 loc) · 3.38 KB
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
//go:build !wasm
package goflare
import (
"bytes"
"encoding/json"
"fmt"
"mime/multipart"
"net/textproto"
"webtyp.com/deploy"
)
// placeholderWorker is uploaded, as Wrangler does, when a secret is put
// before the first deploy. The first real deploy replaces the script and
// Cloudflare keeps its secret_text bindings.
const placeholderWorker = `export default { fetch() { return new Response("", {status: 503}) } }`
// runtimeDestination puts Worker secrets with the person's bootstrap token,
// never the CI token. err holds a session failure, returned by every call.
type runtimeDestination struct {
client *CfClient
accountID string
script string
err error
}
func (r *runtimeDestination) secretsPath() string {
return fmt.Sprintf("/accounts/%s/workers/scripts/%s/secrets", r.accountID, r.script)
}
// secretNames lists the Worker's secret names; exists is false when the
// script does not exist yet.
func (r *runtimeDestination) secretNames() (names []string, exists bool, err error) {
if r.err != nil {
return nil, false, r.err
}
res, err := r.client.get(r.secretsPath())
if err != nil {
if isNotFound(err) {
return nil, false, nil
}
return nil, false, fmt.Errorf("list Worker secrets: %w", err)
}
var secrets []struct {
Name string `json:"name"`
}
if err := json.Unmarshal(res, &secrets); err != nil {
return nil, false, fmt.Errorf("parse Worker secrets: %w", err)
}
for _, s := range secrets {
names = append(names, s.Name)
}
return names, true, nil
}
func (r *runtimeDestination) Has(key string) (bool, error) {
names, _, err := r.secretNames()
if err != nil {
return false, err
}
for _, n := range names {
if n == key {
return true, nil
}
}
return false, nil
}
func (r *runtimeDestination) Put(key, value string) error {
_, exists, err := r.secretNames()
if err != nil {
return err
}
if !exists {
if err := r.uploadPlaceholder(); err != nil {
return err
}
}
body, err := json.Marshal(map[string]string{
"name": key,
"text": value,
"type": "secret_text",
})
if err != nil {
return err
}
if _, err := r.client.put(r.secretsPath(), body); err != nil {
return fmt.Errorf("put Worker secret %s: %w", key, err)
}
return nil
}
func (r *runtimeDestination) uploadPlaceholder() error {
var buf bytes.Buffer
mw := multipart.NewWriter(&buf)
metadata, err := json.Marshal(map[string]any{
"main_module": "index.js",
"compatibility_date": DefaultCompatibilityDate,
})
if err != nil {
return err
}
if err := mw.WriteField("metadata", string(metadata)); err != nil {
return err
}
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", `form-data; name="index.js"; filename="index.js"`)
h.Set("Content-Type", contentTypeESModule)
part, err := mw.CreatePart(h)
if err != nil {
return err
}
if _, err := part.Write([]byte(placeholderWorker)); err != nil {
return err
}
if err := mw.Close(); err != nil {
return err
}
path := fmt.Sprintf("/accounts/%s/workers/scripts/%s", r.accountID, r.script)
if _, err := r.client.putMultipart(path, &buf, mw.FormDataContentType()); err != nil {
return fmt.Errorf("upload placeholder Worker: %w", err)
}
return nil
}
// Runtime is where ToRuntime secrets go: the Worker's secrets.
func (t *Target) Runtime(p deploy.Plan) deploy.Destination {
client, accountID, err := t.session()
return &runtimeDestination{client: client, accountID: accountID, script: p.Name, err: err}
}