-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert.go
166 lines (150 loc) · 4.27 KB
/
convert.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
package main
import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"strings"
"github.com/volvo-cars/lingon/pkg/kube"
"github.com/veggiemonk/lingonweb/knowntypes"
)
type Meta struct {
Length string `json:"length,omitempty"`
UUID string `json:"uuid,omitempty"`
Digest []uint8 `json:"digest,omitempty"`
AddMethods bool `json:"addmethods"`
GroupByKind bool `json:"groupbykind"`
IgnoreErrors bool `json:"ignoreerrors"`
RemoveAppName bool `json:"removeappname"`
Verbose bool `json:"verbose"`
}
type Input struct {
Meta Meta `json:"meta"`
Data string `json:"data"`
}
type MetaOut struct {
Length string `json:"length,omitempty"`
UUID string `json:"uuid,omitempty"`
Digest []uint8 `json:"digest,omitempty"`
Logs string `json:"logs,omitempty"`
}
type Output struct {
Meta MetaOut `json:"meta"`
Data string `json:"data"`
Errors string `json:"errors,omitempty"`
}
func convert(log *slog.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Set the CORS headers to the response.
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set(
"Access-Control-Allow-Methods",
"POST, OPTIONS",
)
w.Header().Set(
"Access-Control-Allow-Headers",
"Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization",
)
if r.Method == http.MethodOptions {
return
}
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
defer func(rc io.ReadCloser) {
if err := rc.Close(); err != nil {
log.Error("close body", "err", err)
}
}(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
log.Error("read body", "err", err)
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write(
[]byte(fmt.Sprintf("{\"errors\":%q}", err.Error())),
)
}
var msg Input
if err := json.NewDecoder(bytes.NewReader(body)).Decode(&msg); err != nil {
log.Error("failed to decode body", "err", err)
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write(
[]byte(fmt.Sprintf("{\"errors\":%q}", err.Error())),
)
return
}
if msg.Meta.Digest == nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("{\"errors\":\"digest is required\"}"))
return
}
// calculate the digest SHA-256 of the message
h := sha256.New()
h.Write([]byte(msg.Data))
received := fmt.Sprintf("%x", h.Sum(nil))
computed := fmt.Sprintf("%x", sha256.Sum256([]byte(msg.Data)))
if computed != received {
w.WriteHeader(http.StatusBadRequest)
errMsg := fmt.Sprintf(
"{\"errors\":\"digest is invalid: %s\"}",
received,
)
log.Info("digest is invalid", "err", errMsg)
_, _ = w.Write([]byte(errMsg))
return
}
var resw, lb bytes.Buffer
nl := makeLogger(&lb)
if err := kube.Import(
kube.WithImportReader(strings.NewReader(msg.Data)),
kube.WithImportWriter(&resw),
kube.WithImportLogger(nl), // send this to the client
kube.WithImportAppName(serviceName),
kube.WithImportPackageName(serviceName),
kube.WithImportSerializer(knowntypes.Codecs.UniversalDeserializer()),
kube.WithImportVerbose(msg.Meta.Verbose),
kube.WithImportGroupByKind(msg.Meta.GroupByKind),
kube.WithImportAddMethods(msg.Meta.AddMethods),
kube.WithImportRemoveAppName(msg.Meta.RemoveAppName),
kube.WithImportIgnoreErrors(msg.Meta.IgnoreErrors),
); err != nil {
o := Output{
Meta: MetaOut{
Length: fmt.Sprintf("%d", resw.Len()),
UUID: msg.Meta.UUID,
Digest: []byte(fmt.Sprintf(
"%x",
sha256.Sum256(resw.Bytes()),
)),
Logs: lb.String(),
},
Errors: err.Error(),
}
log.Error("failed to import", "err", err, slog.Any("output", o))
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(o)
return
}
out := Output{
Meta: MetaOut{
Length: fmt.Sprintf("%d", resw.Len()),
UUID: msg.Meta.UUID,
Digest: []byte(fmt.Sprintf("%x", sha256.Sum256(resw.Bytes()))),
Logs: lb.String(),
},
Data: resw.String(),
}
_, _ = fmt.Fprint(os.Stderr, out.Meta.Logs)
if err := json.NewEncoder(w).Encode(out); err != nil {
log.Error("failed to encode response", "err", err)
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
}
}