Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
329 lines (279 loc) · 8.54 KB
/
Copy pathmain.go
File metadata and controls
329 lines (279 loc) · 8.54 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Nx Caching Server
package main
import (
"context"
"crypto/sha256"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
func GetEnv(key, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}
const (
storageDirKey = "STORAGE_DIR"
cleanupThresholdKey = "CLEANUP_THRESHOLD"
portKey = "PORT"
authTokenKey = "AUTH_TOKEN"
authTokensKey = "AUTH_TOKENS"
)
// validHash allows only characters that cannot form path separators or
// traversal sequences, so the hash is always a single file name.
var validHash = regexp.MustCompile(`^[A-Za-z0-9_-]{1,128}$`)
// cacheFilePath returns the storage path for hash within namespace, or false
// if hash is invalid. An empty namespace stores files directly in STORAGE_DIR.
func cacheFilePath(namespace, hash string) (string, bool) {
if !validHash.MatchString(hash) {
return "", false
}
storageDir := GetEnv(storageDirKey, os.TempDir())
return filepath.Join(storageDir, namespace, fmt.Sprintf("%s.cache", hash)), true
}
func UploadTaskOutput(w http.ResponseWriter, req *http.Request) {
filePath, ok := cacheFilePath(namespaceFrom(req), req.PathValue("hash"))
if !ok {
http.Error(w, "Invalid hash", http.StatusBadRequest)
return
}
contentLengthString := req.Header.Get("Content-Length")
if contentLengthString == "" {
http.Error(w, "Content-Length header is required", http.StatusBadRequest)
return
}
contentLength, err := strconv.ParseInt(contentLengthString, 10, 64)
if err != nil {
http.Error(w, "Invalid Content-Length header", http.StatusBadRequest)
return
}
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
if os.IsExist(err) {
w.WriteHeader(http.StatusConflict)
return
}
http.Error(w, "Failed to create file", http.StatusInternalServerError)
return
}
_, err = io.CopyN(file, req.Body, contentLength)
if err != nil {
file.Close()
os.Remove(filePath) // Clean up incomplete file
http.Error(w, "Failed to write file", http.StatusInternalServerError)
return
}
file.Close()
w.WriteHeader(http.StatusOK)
}
func CheckTaskOutput(w http.ResponseWriter, req *http.Request) {
filePath, ok := cacheFilePath(namespaceFrom(req), req.PathValue("hash"))
if !ok {
http.Error(w, "Invalid hash", http.StatusBadRequest)
return
}
_, err := os.Stat(filePath)
if err != nil {
if os.IsNotExist(err) {
w.WriteHeader(http.StatusNotFound)
return
}
http.Error(w, "Failed to check the file", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func DownloadTaskOutput(w http.ResponseWriter, req *http.Request) {
filePath, ok := cacheFilePath(namespaceFrom(req), req.PathValue("hash"))
if !ok {
http.Error(w, "Invalid hash", http.StatusBadRequest)
return
}
file, err := os.Open(filePath)
if err != nil {
if os.IsNotExist(err) {
w.WriteHeader(http.StatusNotFound)
return
}
http.Error(w, "Failed to read the file", http.StatusInternalServerError)
return
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
http.Error(w, "Failed to read the file", http.StatusInternalServerError)
return
}
// Mark the entry as recently used so cleanup keeps it. Filesystem atime
// is not reliable for this (noatime/relatime mounts, Windows).
now := time.Now()
if err := os.Chtimes(filePath, now, now); err != nil {
log.Printf("Failed to update access time for %s: %v", filePath, err)
}
w.Header().Set("Content-Length", strconv.FormatInt(stat.Size(), 10))
w.Header().Set("Content-Type", "application/octet-stream")
io.Copy(w, file)
}
// loadAuthTokens returns the SHA-256 hashes of the configured tokens mapped to
// their namespace. It returns nil when authentication is disabled. A token set
// through AUTH_TOKEN maps to the empty namespace (flat STORAGE_DIR).
func loadAuthTokens() (map[[32]byte]string, error) {
authToken := os.Getenv(authTokenKey)
authTokens := os.Getenv(authTokensKey)
if authToken != "" && authTokens != "" {
return nil, fmt.Errorf("%s and %s cannot be used together", authTokenKey, authTokensKey)
}
if authToken != "" {
return map[[32]byte]string{sha256.Sum256([]byte(authToken)): ""}, nil
}
if authTokens == "" {
return nil, nil
}
tokens := make(map[[32]byte]string)
names := make(map[string]bool)
for i, entry := range strings.Split(authTokens, ",") {
name, token, found := strings.Cut(strings.TrimSpace(entry), ":")
name, token = strings.TrimSpace(name), strings.TrimSpace(token)
if !found || name == "" || token == "" {
return nil, fmt.Errorf("%s entry %d: expected name:token", authTokensKey, i+1)
}
if !validHash.MatchString(name) {
return nil, fmt.Errorf("%s entry %d: invalid name %q (allowed: letters, digits, '_' and '-')", authTokensKey, i+1, name)
}
if names[name] {
return nil, fmt.Errorf("%s: duplicate name %q", authTokensKey, name)
}
hash := sha256.Sum256([]byte(token))
if _, ok := tokens[hash]; ok {
return nil, fmt.Errorf("%s: duplicate token for name %q", authTokensKey, name)
}
names[name] = true
tokens[hash] = name
}
return tokens, nil
}
type namespaceCtxKey struct{}
// namespaceFrom returns the namespace assigned by CheckBearerTokenMiddleware,
// or "" when none was set.
func namespaceFrom(req *http.Request) string {
ns, _ := req.Context().Value(namespaceCtxKey{}).(string)
return ns
}
// CheckBearerTokenMiddleware authenticates the request against tokens and
// stores the matching namespace in the request context. A nil map disables
// authentication. Looking up the SHA-256 hash in a map is safe against timing
// attacks: any timing difference leaks information about the hash, not the token.
func CheckBearerTokenMiddleware(tokens map[[32]byte]string, next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
if tokens == nil {
next(w, req)
return
}
authHeader := req.Header.Get("Authorization")
if authHeader == "" {
w.WriteHeader(http.StatusUnauthorized)
return
}
parts := strings.SplitN(authHeader, " ", 2)
if len(parts) != 2 || parts[0] != "Bearer" {
w.WriteHeader(http.StatusUnauthorized)
return
}
ns, ok := tokens[sha256.Sum256([]byte(parts[1]))]
if !ok {
w.WriteHeader(http.StatusUnauthorized)
return
}
next(w, req.WithContext(context.WithValue(req.Context(), namespaceCtxKey{}, ns)))
}
}
func HandleHealth(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "OK")
}
func HandleTask(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case "PUT":
UploadTaskOutput(w, req)
case "HEAD":
CheckTaskOutput(w, req)
case "GET":
DownloadTaskOutput(w, req)
default:
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
func cleanupOldRecords(cleanupThreshold time.Duration) {
storageDir := GetEnv(storageDirKey, os.TempDir())
if !strings.HasSuffix(storageDir, "/") {
storageDir = storageDir + "/"
}
err := filepath.Walk(storageDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(path, ".cache") {
return nil
}
// ModTime is refreshed on every download, so it tracks last use.
lastUsed := info.ModTime()
if time.Since(lastUsed) > cleanupThreshold {
log.Printf("Removing %s: last accessed %s ago", path, time.Since(lastUsed))
return os.Remove(path)
}
return nil
})
if err != nil {
log.Printf("Error walking the storage directory: %v", err)
}
}
func main() {
cleanupThreshold, err := time.ParseDuration(GetEnv(cleanupThresholdKey, "1h"))
if err != nil {
log.Fatalf("Invalid cleanup threshold: %v", err)
}
tokens, err := loadAuthTokens()
if err != nil {
log.Fatalf("Invalid auth configuration: %v", err)
}
storageDir := GetEnv(storageDirKey, os.TempDir())
var namespaces []string
for _, name := range tokens {
if name == "" {
continue
}
if err := os.MkdirAll(filepath.Join(storageDir, name), 0755); err != nil {
log.Fatalf("Failed to create namespace directory %q: %v", name, err)
}
namespaces = append(namespaces, name)
}
if len(namespaces) > 0 {
sort.Strings(namespaces)
log.Printf("Loaded namespaces: %s", strings.Join(namespaces, ", "))
}
go func() {
ticker := time.NewTicker(cleanupThreshold)
defer ticker.Stop()
for {
cleanupOldRecords(cleanupThreshold)
<-ticker.C
}
}()
http.HandleFunc("/health", HandleHealth)
http.HandleFunc("/v1/cache/{hash}", CheckBearerTokenMiddleware(tokens, HandleTask))
port := GetEnv(portKey, "8090")
http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
}