-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
202 lines (163 loc) · 5.02 KB
/
main.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
package main
import (
"bytes"
"context"
b64 "encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"runtime"
"time"
"github.com/pkg/errors"
"github.com/cdnjs/tools/audit"
"github.com/cdnjs/tools/gcp"
"github.com/cdnjs/tools/metrics"
"github.com/cdnjs/tools/packages"
"github.com/cdnjs/tools/sentry"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"cloud.google.com/go/pubsub"
)
var (
PROJECT = os.Getenv("PROJECT")
SUBSCRIPTION = os.Getenv("SUBSCRIPTION")
R2_BUCKET = os.Getenv("R2_BUCKET")
R2_KEY_ID = os.Getenv("R2_KEY_ID")
R2_KEY_SECRET = os.Getenv("R2_KEY_SECRET")
R2_ENDPOINT = os.Getenv("R2_ENDPOINT")
)
func init() {
sentry.Init()
}
func main() {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, PROJECT)
if err != nil {
log.Fatalf("could not create pubsub Client: %v", err)
}
sub := client.Subscription(SUBSCRIPTION)
sub.ReceiveSettings.Synchronous = true
sub.ReceiveSettings.MaxOutstandingMessages = 5
sub.ReceiveSettings.NumGoroutines = runtime.NumCPU()
for {
log.Printf("started consuming messages\n")
if err := consume(ctx, client, sub); err != nil {
log.Fatalf("could not pull messages: %s", err)
}
}
}
type Message struct {
GCSEvent gcp.GCSEvent `json:"gcsEvent"`
}
func consume(ctx context.Context, client *pubsub.Client, sub *pubsub.Subscription) error {
err := sub.Receive(ctx, func(ctx context.Context, msg *pubsub.Message) {
log.Printf("received message: %s\n", msg.Data)
msg.Ack()
if err := processMessage(ctx, msg.Data); err != nil {
log.Printf("failed to process message: %s\n", err)
}
})
if err != nil {
return errors.Wrap(err, "could not receive from subscription")
}
return nil
}
func processMessage(ctx context.Context, data []byte) error {
var message Message
if err := json.Unmarshal(data, &message); err != nil {
return errors.Wrap(err, "failed to parse")
}
return invoke(ctx, message.GCSEvent)
}
func invoke(ctx context.Context, e gcp.GCSEvent) error {
sentry.Init()
defer sentry.PanicHandler()
pkgName := e.Metadata["package"].(string)
version := e.Metadata["version"].(string)
log.Printf("Invoke %s %s\n", pkgName, version)
configStr, err := b64.StdEncoding.DecodeString(e.Metadata["config"].(string))
if err != nil {
return fmt.Errorf("could not decode config: %v", err)
}
archive, err := gcp.ReadObject(ctx, e.Bucket, e.Name)
if err != nil {
return fmt.Errorf("could not read object: %v", err)
}
bucket := aws.String(R2_BUCKET)
r2Resolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
URL: R2_ENDPOINT,
}, nil
})
cfg, err := config.LoadDefaultConfig(ctx,
config.WithRegion("auto"),
config.WithEndpointResolverWithOptions(r2Resolver),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(R2_KEY_ID, R2_KEY_SECRET, "")),
)
if err != nil {
return fmt.Errorf("could not load config: %s", err)
}
s3Client := s3.NewFromConfig(cfg)
keys := make([]string, 0)
onFile := func(name string, r io.Reader) error {
// remove leading slash
name = name[1:]
key := fmt.Sprintf("%s/%s/%s", pkgName, version, name)
content, err := ioutil.ReadAll(r)
if err != nil {
return errors.Wrap(err, "could not read file")
}
keys = append(keys, key)
meta := newMetadata(len(content))
s3Object := s3.PutObjectInput{
Body: bytes.NewReader(content),
Bucket: bucket,
Key: aws.String(key),
Metadata: meta,
}
if err := uploadFile(ctx, s3Client, &s3Object); err != nil {
return errors.Wrap(err, "failed to upload file")
}
return nil
}
if err := gcp.Inflate(bytes.NewReader(archive), onFile); err != nil {
return fmt.Errorf("could not inflate archive: %s", err)
}
if len(keys) == 0 {
log.Printf("%s: no files to publish\n", pkgName)
}
pkg := new(packages.Package)
if err := json.Unmarshal([]byte(configStr), &pkg); err != nil {
return fmt.Errorf("failed to parse config: %s", err)
}
if err := audit.WroteR2(ctx, pkgName, version, keys); err != nil {
log.Printf("failed to audit: %s\n", err)
}
if err := metrics.NewUpdatePublishedR2(); err != nil {
return errors.Wrap(err, "could not report metrics")
}
return nil
}
func newMetadata(size int) map[string]string {
lastModifiedTime := time.Now()
lastModifiedSeconds := lastModifiedTime.UnixNano() / int64(time.Second)
lastModifiedStr := lastModifiedTime.Format(http.TimeFormat)
etag := fmt.Sprintf("%x-%x", lastModifiedSeconds, size)
meta := make(map[string]string)
// https://github.com/cdnjs/origin-worker/blob/ff91d30586c9e924ff919407401dff6f52826b4d/src/index.js#L212-L213
meta["etag"] = etag
meta["last_modified"] = lastModifiedStr
return meta
}
func uploadFile(ctx context.Context, s3Client *s3.Client, obj *s3.PutObjectInput) error {
if _, err := s3Client.PutObject(ctx, obj); err != nil {
return errors.Wrapf(err, "failed to put Object %s", *obj.Key)
}
return nil
}