1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 11:55:40 +01:00

Added JSON endpoints. Made Web UI optional.

This commit is contained in:
Sascha L. Teichmann 2021-12-05 21:20:27 +01:00
parent 565238da9a
commit 52b586d82f
4 changed files with 76 additions and 24 deletions

View file

@ -115,11 +115,19 @@ func (c *controller) tlpParam(r *http.Request) (tlp, error) {
return "", fmt.Errorf("unsupported TLP type '%s'", t) return "", fmt.Errorf("unsupported TLP type '%s'", t)
} }
func (c *controller) create(http.ResponseWriter, *http.Request) error { func (c *controller) create(*http.Request) (interface{}, error) {
return ensureFolders(c.cfg) if err := ensureFolders(c.cfg); err != nil {
return nil, err
}
return &struct {
Message string `json:"message"`
Error bool `json:"-"`
}{
Message: "Everything is setup fine now.",
}, nil
} }
func (c *controller) upload(rw http.ResponseWriter, r *http.Request) (interface{}, error) { func (c *controller) upload(r *http.Request) (interface{}, error) {
newCSAF, data, err := loadCSAF(r) newCSAF, data, err := loadCSAF(r)
if err != nil { if err != nil {

View file

@ -28,6 +28,7 @@ type config struct {
Domain string `toml:"domain"` Domain string `toml:"domain"`
NoPassphrase bool `toml:"no_passphrase"` NoPassphrase bool `toml:"no_passphrase"`
NoValidation bool `toml:"no_validation"` NoValidation bool `toml:"no_validation"`
NoWebUI bool `toml:"no_web_ui"`
DynamicProviderMetaData bool `toml:"dynamic_provider_metadata"` DynamicProviderMetaData bool `toml:"dynamic_provider_metadata"`
Publisher *csaf.Publisher `toml:"publisher"` Publisher *csaf.Publisher `toml:"publisher"`
} }

View file

@ -2,6 +2,7 @@ package main
import ( import (
"embed" "embed"
"encoding/json"
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
@ -17,6 +18,17 @@ func (me multiError) Error() string {
return strings.Join([]string(me), ", ") return strings.Join([]string(me), ", ")
} }
func asMultiError(err error) multiError {
if err == nil {
return nil
}
e, ok := err.(multiError)
if ok {
return e
}
return multiError([]string{err.Error()})
}
type controller struct { type controller struct {
cfg *config cfg *config
tmpl *template.Template tmpl *template.Template
@ -27,31 +39,35 @@ func newController(cfg *config) (*controller, error) {
c := controller{cfg: cfg} c := controller{cfg: cfg}
var err error var err error
if !cfg.NoWebUI {
if c.tmpl, err = template.ParseFS(tmplFS, "tmpl/*.html"); err != nil { if c.tmpl, err = template.ParseFS(tmplFS, "tmpl/*.html"); err != nil {
return nil, err return nil, err
} }
}
return &c, nil return &c, nil
} }
func (c *controller) bind(pim *pathInfoMux) { func (c *controller) bind(pim *pathInfoMux) {
if !c.cfg.NoWebUI {
pim.handleFunc("/", c.index) pim.handleFunc("/", c.index)
pim.handleFunc("/upload", c.uploadWeb) pim.handleFunc("/upload", c.web(c.upload, "upload.html"))
pim.handleFunc("/create", c.createWeb) pim.handleFunc("/create", c.web(c.create, "create.html"))
}
pim.handleFunc("/api/upload", api(c.upload))
pim.handleFunc("/api/create", api(c.create))
} }
func (c *controller) render(rw http.ResponseWriter, tmpl string, arg interface{}) { func (c *controller) render(rw http.ResponseWriter, tmpl string, arg interface{}) {
rw.Header().Set("Content-type", "text/html; charset=utf-8") rw.Header().Set("Content-type", "text/html; charset=utf-8")
rw.Header().Set("X-Content-Type-Options", "nosniff")
if err := c.tmpl.ExecuteTemplate(rw, tmpl, arg); err != nil { if err := c.tmpl.ExecuteTemplate(rw, tmpl, arg); err != nil {
log.Printf("warn: %v\n", err) log.Printf("warn: %v\n", err)
} }
} }
func (c *controller) failed(rw http.ResponseWriter, tmpl string, err error) { func (c *controller) failed(rw http.ResponseWriter, tmpl string, err error) {
if _, ok := err.(multiError); err != nil && !ok { result := map[string]interface{}{"Error": asMultiError(err)}
err = multiError([]string{err.Error()})
}
result := map[string]interface{}{"Error": err}
c.render(rw, tmpl, result) c.render(rw, tmpl, result)
} }
@ -61,19 +77,46 @@ func (c *controller) index(rw http.ResponseWriter, r *http.Request) {
}) })
} }
func (c *controller) createWeb(rw http.ResponseWriter, r *http.Request) { func (c *controller) web(
if err := c.create(rw, r); err != nil { fn func(*http.Request) (interface{}, error),
c.failed(rw, "create.html", err) tmpl string,
return ) func(http.ResponseWriter, *http.Request) {
return func(rw http.ResponseWriter, r *http.Request) {
if content, err := fn(r); err != nil {
c.failed(rw, tmpl, err)
} else {
c.render(rw, tmpl, content)
}
} }
c.render(rw, "create.html", nil)
} }
func (c *controller) uploadWeb(rw http.ResponseWriter, r *http.Request) { func writeJSON(rw http.ResponseWriter, content interface{}, code int) {
result, err := c.upload(rw, r) rw.Header().Set("Content-type", "application/json; charset=utf-8")
if err != nil { rw.Header().Set("X-Content-Type-Options", "nosniff")
c.failed(rw, "upload.html", err) rw.WriteHeader(code)
return if err := json.NewEncoder(rw).Encode(content); err != nil {
log.Printf("error: %v\n", err)
}
}
func errorToContent(err error) interface{} {
return &struct {
Errors multiError `json:"errors"`
}{
Errors: asMultiError(err),
}
}
func api(
fn func(*http.Request) (interface{}, error),
) func(http.ResponseWriter, *http.Request) {
return func(rw http.ResponseWriter, r *http.Request) {
if content, err := fn(r); err != nil {
writeJSON(rw, errorToContent(err), http.StatusBadRequest)
} else {
writeJSON(rw, content, http.StatusOK)
}
} }
c.render(rw, "upload.html", result)
} }

View file

@ -21,7 +21,7 @@
<p> <p>
{{ end }} {{ end }}
{{ else }} {{ else }}
Everything is setup fine now. {{ .Message }}
{{ end }} {{ end }}
</body> </body>
</html> </html>