Repository navigation
Expand file tree
/
Copy pathhandler.go
More file actions
119 lines (98 loc) · 2.83 KB
/
Copy pathhandler.go
File metadata and controls
119 lines (98 loc) · 2.83 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package lmds
import (
"fmt"
"log/slog"
"net/http"
"strings"
"sync/atomic"
"time"
)
type Metadata struct {
AvailabilityZoneID string `json:"AvailabilityZoneID"`
}
type MetadataConfig struct {
Data []byte
MaxAge time.Duration
}
const URIPath = "/2026-01-15/metadata/execution-environment"
type Metrics struct {
ClientErrors uint64
ServerErrors uint64
SuccessfulCalls uint64
}
type MetricStore struct {
clientErrors atomic.Uint64
serverErrors atomic.Uint64
successfulCalls atomic.Uint64
}
func (m *MetricStore) incClientErrors() {
m.clientErrors.Add(1)
}
func (m *MetricStore) incServerErrors() {
m.serverErrors.Add(1)
}
func (m *MetricStore) incSuccessfulCalls() {
m.successfulCalls.Add(1)
}
func (m *MetricStore) Take() Metrics {
return Metrics{
ClientErrors: m.clientErrors.Swap(0),
ServerErrors: m.serverErrors.Swap(0),
SuccessfulCalls: m.successfulCalls.Swap(0),
}
}
type Service struct {
token string
metadata atomic.Value
Metrics *MetricStore
}
func NewService(token string) *Service {
return &Service{
token: token,
Metrics: &MetricStore{},
}
}
func (s *Service) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if request.Method != http.MethodGet {
s.Metrics.incClientErrors()
http.Error(writer, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
authHeader := request.Header.Get("Authorization")
const bearerPrefix = "Bearer "
if !strings.HasPrefix(authHeader, bearerPrefix) {
s.Metrics.incClientErrors()
slog.Warn("metadata API handler received authorization header without Bearer prefix")
http.Error(writer, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
token := strings.TrimPrefix(authHeader, bearerPrefix)
if token != s.token {
s.Metrics.incClientErrors()
slog.Warn("metadata API handler received unexpected auth token")
http.Error(writer, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
config, ok := s.metadata.Load().(MetadataConfig)
if !ok {
s.Metrics.incServerErrors()
slog.Error("metadata API handler called before UpdateMetadata")
http.Error(writer, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
return
}
cacheControl := fmt.Sprintf("private, max-age=%d, immutable", int(config.MaxAge.Seconds()))
writer.Header().Set("Content-Type", "application/json")
writer.Header().Set("Cache-Control", cacheControl)
writer.WriteHeader(http.StatusOK)
if _, err := writer.Write(config.Data); err != nil {
s.Metrics.incClientErrors()
slog.Error("could not write metadata", "err", err)
return
}
s.Metrics.incSuccessfulCalls()
}
func (s *Service) UpdateMetadata(config MetadataConfig) {
s.metadata.Store(config)
}