-
Notifications
You must be signed in to change notification settings - Fork 1
/
ct.go
160 lines (137 loc) · 3.57 KB
/
ct.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
package ct
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/kinvolk/container-linux-config-transpiler/config"
"github.com/kinvolk/container-linux-config-transpiler/config/platform"
"go.uber.org/zap"
"net/http"
"strings"
"sync"
)
var bufPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
func init() {
caddy.RegisterModule(Ct{})
httpcaddyfile.RegisterHandlerDirective("ct", parseCaddyfile)
}
// Ct allows to transpile YAML based configuration into a JSON ignition to be used with Flatcar or Fedora CoreOS.
type Ct struct {
logger *zap.Logger
// Fail on non critical errors (default: false)
Strict bool `json:"strict,omitempty"`
// Only transpile specific MIME types (default: all)
MIMETypes []string `json:"mime_types,omitempty"`
// Only for dynamic data must be one of supported types by ct (default: none)
Platform string `json:"platform,omitempty"`
}
func (Ct) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.ct",
New: func() caddy.Module {
return Ct{}
},
}
}
func (ct Ct) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufPool.Put(buf)
shouldBuf := func(status int, header http.Header) bool {
bufferThis := true
if ct.MIMETypes != nil {
bufferThis = false
contentType := header.Get("Content-Type")
for _, mt := range ct.MIMETypes {
if strings.Contains(contentType, mt) {
return true
}
}
}
return bufferThis
}
rec := caddyhttp.NewResponseRecorder(w, buf, shouldBuf)
if err := next.ServeHTTP(rec, r); err != nil {
return err
}
if !rec.Buffered() {
return nil
}
cfg, astNode, report := config.Parse(rec.Buffer().Bytes())
if len(report.Entries) > 0 {
ct.logger.Error(report.String())
if report.IsFatal() || ct.Strict {
return errors.New("failed to parse config")
}
}
ignCfg, report := config.Convert(cfg, ct.Platform, astNode)
if len(report.Entries) > 0 {
ct.logger.Error(report.String())
if report.IsFatal() || ct.Strict {
return errors.New("failed to convert config")
}
}
data, err := json.Marshal(&ignCfg)
if err != nil {
return err
}
rec.Buffer().Reset()
rec.Header().Del("Content-Length")
if _, err = rec.Write(data); err != nil {
return err
}
rec.Header().Set("Content-Type", "application/vnd.coreos.ignition+json")
return rec.WriteResponse()
}
func (ct *Ct) Provision(ctx caddy.Context) error {
ct.logger = ctx.Logger(ct)
return nil
}
func (ct *Ct) Validate() error {
if !platform.IsSupportedPlatform(ct.Platform) {
return fmt.Errorf("platform %s is unsupported", ct.Platform)
}
return nil
}
func (ct *Ct) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
for d.NextBlock(0) {
switch d.Val() {
case "strict":
ct.Strict = true
case "mime":
ct.MIMETypes = d.RemainingArgs()
if len(ct.MIMETypes) == 0 {
return d.ArgErr()
}
case "platform":
if !d.Args(&ct.Platform) {
return d.ArgErr()
}
default:
return fmt.Errorf("unknown subdirective: %q", d.Val())
}
}
}
return nil
}
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var ct Ct
err := ct.UnmarshalCaddyfile(h.Dispenser)
return ct, err
}
// Interface guards
var (
_ caddy.Provisioner = (*Ct)(nil)
_ caddyfile.Unmarshaler = (*Ct)(nil)
_ caddy.Validator = (*Ct)(nil)
)