Create a administration interface

This commit is contained in:
nemunaire 2020-06-08 21:07:53 +02:00
parent f166e5b59b
commit fb9860176e
15 changed files with 471 additions and 21 deletions

118
admin/db-domain.go Normal file
View File

@ -0,0 +1,118 @@
// Copyright or © or Copr. happyDNS (2020)
//
// contact@happydns.org
//
// This software is a computer program whose purpose is to provide a modern
// interface to interact with DNS systems.
//
// This software is governed by the CeCILL license under French law and abiding
// by the rules of distribution of free software. You can use, modify and/or
// redistribute the software under the terms of the CeCILL license as
// circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy, modify
// and redistribute granted by the license, users are provided only with a
// limited warranty and the software's author, the holder of the economic
// rights, and the successive licensors have only limited liability.
//
// In this respect, the user's attention is drawn to the risks associated with
// loading, using, modifying and/or developing or reproducing the software by
// the user in light of its specific status of free software, that may mean
// that it is complicated to manipulate, and that also therefore means that it
// is reserved for developers and experienced professionals having in-depth
// computer knowledge. Users are therefore encouraged to load and test the
// software's suitability as regards their requirements in conditions enabling
// the security of their systems and/or data to be ensured and, more generally,
// to use and operate it in the same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
package admin
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"github.com/julienschmidt/httprouter"
"git.happydns.org/happydns/api"
"git.happydns.org/happydns/config"
"git.happydns.org/happydns/model"
"git.happydns.org/happydns/storage"
)
func init() {
router.GET("/api/users/:userid/domains", api.ApiHandler(userHandler(getUserDomains)))
router.POST("/api/users/:userid/domains", api.ApiHandler(userHandler(newUserDomain)))
router.GET("/api/users/:userid/domains/:domain", api.ApiHandler(userHandler(domainHandler(getUserDomain))))
router.PUT("/api/users/:userid/domains/:domain", api.ApiHandler(userHandler(domainHandler(updateUserDomain))))
router.DELETE("/api/users/:userid/domains/:domain", api.ApiHandler(userHandler(domainHandler(deleteUserDomain))))
}
func getUserDomains(_ *config.Options, user *happydns.User, _ httprouter.Params, _ io.Reader) api.Response {
return api.NewAPIResponse(storage.MainStore.GetDomains(user))
}
func newUserDomain(_ *config.Options, user *happydns.User, _ httprouter.Params, body io.Reader) api.Response {
ud := &happydns.Domain{}
err := json.NewDecoder(body).Decode(&ud)
if err != nil {
return api.NewAPIErrorResponse(http.StatusBadRequest, fmt.Errorf("Something is wrong in received data: %w", err))
}
ud.Id = 0
ud.IdUser = user.Id
return api.NewAPIResponse(ud, storage.MainStore.CreateDomain(user, ud))
}
func domainHandler(f func(*config.Options, *happydns.Domain, httprouter.Params, io.Reader) api.Response) func(*config.Options, *happydns.User, httprouter.Params, io.Reader) api.Response {
return func(opts *config.Options, user *happydns.User, ps httprouter.Params, body io.Reader) api.Response {
domainid, err := strconv.ParseInt(ps.ByName("domain"), 10, 64)
if err != nil {
domain, err := storage.MainStore.GetDomainByDN(user, ps.ByName("domain"))
if err != nil {
return api.NewAPIErrorResponse(http.StatusNotFound, err)
} else {
return f(opts, domain, ps, body)
}
} else {
domain, err := storage.MainStore.GetDomain(user, domainid)
if err != nil {
return api.NewAPIErrorResponse(http.StatusNotFound, err)
} else {
return f(opts, domain, ps, body)
}
}
}
}
func getUserDomain(_ *config.Options, domain *happydns.Domain, _ httprouter.Params, _ io.Reader) api.Response {
return api.NewAPIResponse(domain, nil)
}
func updateUserDomain(_ *config.Options, domain *happydns.Domain, _ httprouter.Params, body io.Reader) api.Response {
ud := &happydns.Domain{}
err := json.NewDecoder(body).Decode(&ud)
if err != nil {
return api.NewAPIErrorResponse(http.StatusBadRequest, fmt.Errorf("Something is wrong in received data: %w", err))
}
ud.Id = domain.Id
if ud.IdUser != domain.IdUser {
if err := storage.MainStore.UpdateDomainOwner(domain, &happydns.User{Id: ud.IdUser}); err != nil {
return api.NewAPIErrorResponse(http.StatusBadRequest, err)
}
}
return api.NewAPIResponse(ud, storage.MainStore.UpdateDomain(ud))
}
func deleteUserDomain(_ *config.Options, domain *happydns.Domain, _ httprouter.Params, _ io.Reader) api.Response {
return api.NewAPIResponse(true, storage.MainStore.DeleteDomain(domain))
}

103
admin/db-source.go Normal file
View File

@ -0,0 +1,103 @@
// Copyright or © or Copr. happyDNS (2020)
//
// contact@happydns.org
//
// This software is a computer program whose purpose is to provide a modern
// interface to interact with DNS systems.
//
// This software is governed by the CeCILL license under French law and abiding
// by the rules of distribution of free software. You can use, modify and/or
// redistribute the software under the terms of the CeCILL license as
// circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy, modify
// and redistribute granted by the license, users are provided only with a
// limited warranty and the software's author, the holder of the economic
// rights, and the successive licensors have only limited liability.
//
// In this respect, the user's attention is drawn to the risks associated with
// loading, using, modifying and/or developing or reproducing the software by
// the user in light of its specific status of free software, that may mean
// that it is complicated to manipulate, and that also therefore means that it
// is reserved for developers and experienced professionals having in-depth
// computer knowledge. Users are therefore encouraged to load and test the
// software's suitability as regards their requirements in conditions enabling
// the security of their systems and/or data to be ensured and, more generally,
// to use and operate it in the same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
package admin
import (
"fmt"
"io"
"net/http"
"strconv"
"github.com/julienschmidt/httprouter"
"git.happydns.org/happydns/api"
"git.happydns.org/happydns/config"
"git.happydns.org/happydns/model"
"git.happydns.org/happydns/storage"
)
func init() {
router.GET("/api/users/:userid/sources", api.ApiHandler(userHandler(getUserSources)))
router.POST("/api/users/:userid/sources", api.ApiHandler(userHandler(newUserSource)))
router.GET("/api/users/:userid/sources/:source", api.ApiHandler(userHandler(sourceHandler(getUserSource))))
router.PUT("/api/users/:userid/sources/:source", api.ApiHandler(userHandler(sourceHandler(updateUserSource))))
router.DELETE("/api/users/:userid/sources/:source", api.ApiHandler(userHandler(sourceHandler(deleteUserSource))))
}
func getUserSources(_ *config.Options, user *happydns.User, _ httprouter.Params, _ io.Reader) api.Response {
return api.NewAPIResponse(storage.MainStore.GetSourceTypes(user))
}
func newUserSource(_ *config.Options, user *happydns.User, _ httprouter.Params, body io.Reader) api.Response {
us, err := api.DecodeSource(body)
if err != nil {
return api.NewAPIErrorResponse(http.StatusBadRequest, fmt.Errorf("Something is wrong in received data: %w", err))
}
us.Id = 0
return api.NewAPIResponse(storage.MainStore.CreateSource(user, us, ""))
}
func sourceHandler(f func(*config.Options, *happydns.SourceCombined, httprouter.Params, io.Reader) api.Response) func(*config.Options, *happydns.User, httprouter.Params, io.Reader) api.Response {
return func(opts *config.Options, user *happydns.User, ps httprouter.Params, body io.Reader) api.Response {
sourceid, err := strconv.ParseInt(ps.ByName("source"), 10, 64)
if err != nil {
return api.NewAPIErrorResponse(http.StatusNotFound, err)
} else {
source, err := storage.MainStore.GetSource(user, sourceid)
if err != nil {
return api.NewAPIErrorResponse(http.StatusNotFound, err)
} else {
return f(opts, source, ps, body)
}
}
}
}
func getUserSource(_ *config.Options, source *happydns.SourceCombined, _ httprouter.Params, _ io.Reader) api.Response {
return api.NewAPIResponse(source, nil)
}
func updateUserSource(_ *config.Options, source *happydns.SourceCombined, _ httprouter.Params, body io.Reader) api.Response {
us, err := api.DecodeSource(body)
if err != nil {
return api.NewAPIErrorResponse(http.StatusBadRequest, fmt.Errorf("Something is wrong in received data: %w", err))
}
us.Id = source.Id
return api.NewAPIResponse(us, storage.MainStore.UpdateSource(us))
}
func deleteUserSource(_ *config.Options, source *happydns.SourceCombined, _ httprouter.Params, _ io.Reader) api.Response {
return api.NewAPIResponse(true, storage.MainStore.DeleteSource(&source.SourceType))
}

116
admin/db-user.go Normal file
View File

@ -0,0 +1,116 @@
// Copyright or © or Copr. happyDNS (2020)
//
// contact@happydns.org
//
// This software is a computer program whose purpose is to provide a modern
// interface to interact with DNS systems.
//
// This software is governed by the CeCILL license under French law and abiding
// by the rules of distribution of free software. You can use, modify and/or
// redistribute the software under the terms of the CeCILL license as
// circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy, modify
// and redistribute granted by the license, users are provided only with a
// limited warranty and the software's author, the holder of the economic
// rights, and the successive licensors have only limited liability.
//
// In this respect, the user's attention is drawn to the risks associated with
// loading, using, modifying and/or developing or reproducing the software by
// the user in light of its specific status of free software, that may mean
// that it is complicated to manipulate, and that also therefore means that it
// is reserved for developers and experienced professionals having in-depth
// computer knowledge. Users are therefore encouraged to load and test the
// software's suitability as regards their requirements in conditions enabling
// the security of their systems and/or data to be ensured and, more generally,
// to use and operate it in the same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
package admin
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"github.com/julienschmidt/httprouter"
"git.happydns.org/happydns/api"
"git.happydns.org/happydns/config"
"git.happydns.org/happydns/model"
"git.happydns.org/happydns/storage"
)
func init() {
router.GET("/api/users", api.ApiHandler(getUsers))
router.POST("/api/users", api.ApiHandler(newUser))
router.DELETE("/api/users", api.ApiHandler(deleteUsers))
router.GET("/api/users/:userid", api.ApiHandler(userHandler(getUser)))
router.PUT("/api/users/:userid", api.ApiHandler(userHandler(updateUser)))
router.DELETE("/api/users/:userid", api.ApiHandler(userHandler(deleteUser)))
}
func getUsers(_ *config.Options, _ httprouter.Params, _ io.Reader) api.Response {
return api.NewAPIResponse(storage.MainStore.GetUsers())
}
func newUser(_ *config.Options, _ httprouter.Params, body io.Reader) api.Response {
uu := &happydns.User{}
err := json.NewDecoder(body).Decode(&uu)
if err != nil {
return api.NewAPIErrorResponse(http.StatusBadRequest, fmt.Errorf("Something is wrong in received data: %w", err))
}
uu.Id = 0
return api.NewAPIResponse(uu, storage.MainStore.CreateUser(uu))
}
func deleteUsers(_ *config.Options, _ httprouter.Params, _ io.Reader) api.Response {
return api.NewAPIResponse(true, storage.MainStore.ClearUsers())
}
func userHandler(f func(*config.Options, *happydns.User, httprouter.Params, io.Reader) api.Response) func(*config.Options, httprouter.Params, io.Reader) api.Response {
return func(opts *config.Options, ps httprouter.Params, body io.Reader) api.Response {
userid, err := strconv.ParseInt(ps.ByName("userid"), 10, 64)
if err != nil {
user, err := storage.MainStore.GetUserByEmail(ps.ByName("userid"))
if err != nil {
return api.NewAPIErrorResponse(http.StatusNotFound, err)
} else {
return f(opts, user, ps, body)
}
} else {
user, err := storage.MainStore.GetUser(userid)
if err != nil {
return api.NewAPIErrorResponse(http.StatusNotFound, err)
} else {
return f(opts, user, ps, body)
}
}
}
}
func getUser(_ *config.Options, user *happydns.User, _ httprouter.Params, _ io.Reader) api.Response {
return api.NewAPIResponse(user, nil)
}
func updateUser(_ *config.Options, user *happydns.User, _ httprouter.Params, body io.Reader) api.Response {
uu := &happydns.User{}
err := json.NewDecoder(body).Decode(&uu)
if err != nil {
return api.NewAPIErrorResponse(http.StatusBadRequest, fmt.Errorf("Something is wrong in received data: %w", err))
}
uu.Id = user.Id
return api.NewAPIResponse(uu, storage.MainStore.UpdateUser(uu))
}
func deleteUser(_ *config.Options, user *happydns.User, _ httprouter.Params, _ io.Reader) api.Response {
return api.NewAPIResponse(true, storage.MainStore.DeleteUser(user))
}

42
admin/router.go Normal file
View File

@ -0,0 +1,42 @@
// Copyright or © or Copr. happyDNS (2020)
//
// contact@happydns.org
//
// This software is a computer program whose purpose is to provide a modern
// interface to interact with DNS systems.
//
// This software is governed by the CeCILL license under French law and abiding
// by the rules of distribution of free software. You can use, modify and/or
// redistribute the software under the terms of the CeCILL license as
// circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy, modify
// and redistribute granted by the license, users are provided only with a
// limited warranty and the software's author, the holder of the economic
// rights, and the successive licensors have only limited liability.
//
// In this respect, the user's attention is drawn to the risks associated with
// loading, using, modifying and/or developing or reproducing the software by
// the user in light of its specific status of free software, that may mean
// that it is complicated to manipulate, and that also therefore means that it
// is reserved for developers and experienced professionals having in-depth
// computer knowledge. Users are therefore encouraged to load and test the
// software's suitability as regards their requirements in conditions enabling
// the security of their systems and/or data to be ensured and, more generally,
// to use and operate it in the same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
package admin
import (
"github.com/julienschmidt/httprouter"
)
var router = httprouter.New()
func Router() *httprouter.Router {
return router
}

View File

@ -92,6 +92,19 @@ func (r APIResponse) WriteResponse(w http.ResponseWriter) {
}
}
func NewAPIResponse(response interface{}, err error) Response {
if err != nil {
return APIErrorResponse{
status: http.StatusBadRequest,
err: err,
}
} else {
return APIResponse{
response: response,
}
}
}
type APIErrorResponse struct {
status int
err error
@ -111,7 +124,14 @@ func (r APIErrorResponse) WriteResponse(w http.ResponseWriter) {
}
}
func apiHandler(f func(*config.Options, httprouter.Params, io.Reader) Response) func(http.ResponseWriter, *http.Request, httprouter.Params) {
func NewAPIErrorResponse(status int, err error) APIErrorResponse {
return APIErrorResponse{
status: status,
err: err,
}
}
func ApiHandler(f func(*config.Options, httprouter.Params, io.Reader) Response) func(http.ResponseWriter, *http.Request, httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if addr := r.Header.Get("X-Forwarded-For"); addr != "" {
r.RemoteAddr = addr

View File

@ -46,8 +46,8 @@ import (
)
func init() {
router.GET("/api/service_specs", apiHandler(getServiceSpecs))
router.GET("/api/service_specs/*ssid", apiHandler(getServiceSpec))
router.GET("/api/service_specs", ApiHandler(getServiceSpecs))
router.GET("/api/service_specs/*ssid", ApiHandler(getServiceSpec))
}
type service_field struct {

View File

@ -43,8 +43,8 @@ import (
)
func init() {
router.GET("/api/services", apiHandler(listServices))
//router.POST("/api/services", apiHandler(newService))
router.GET("/api/services", ApiHandler(listServices))
//router.POST("/api/services", ApiHandler(newService))
router.POST("/api/domains/:domain/analyze", apiAuthHandler(domainHandler(analyzeDomain)))
}

View File

@ -46,8 +46,8 @@ import (
)
func init() {
router.GET("/api/source_specs", apiHandler(getSourceSpecs))
router.GET("/api/source_specs/*ssid", apiHandler(getSourceSpec))
router.GET("/api/source_specs", ApiHandler(getSourceSpecs))
router.GET("/api/source_specs/*ssid", ApiHandler(getSourceSpec))
}
type source_field struct {

View File

@ -91,7 +91,7 @@ func getSource(_ *config.Options, s *happydns.SourceCombined, u *happydns.User,
}
}
func decodeSource(body io.Reader) (*happydns.SourceCombined, error) {
func DecodeSource(body io.Reader) (*happydns.SourceCombined, error) {
cnt, err := ioutil.ReadAll(body)
if err != nil {
return nil, err
@ -127,7 +127,7 @@ func decodeSource(body io.Reader) (*happydns.SourceCombined, error) {
}
func addSource(_ *config.Options, u *happydns.User, p httprouter.Params, body io.Reader) Response {
src, err := decodeSource(body)
src, err := DecodeSource(body)
if err != nil {
return APIErrorResponse{
err: err,
@ -146,7 +146,7 @@ func addSource(_ *config.Options, u *happydns.User, p httprouter.Params, body io
}
func updateSource(_ *config.Options, s *happydns.SourceCombined, u *happydns.User, body io.Reader) Response {
src, err := decodeSource(body)
src, err := DecodeSource(body)
if err != nil {
return APIErrorResponse{
err: err,

View File

@ -51,10 +51,10 @@ var AuthFunc = checkAuth
func init() {
router.GET("/api/auth", apiAuthHandler(displayAuthToken))
router.POST("/api/auth", apiHandler(func(opts *config.Options, ps httprouter.Params, b io.Reader) Response {
router.POST("/api/auth", ApiHandler(func(opts *config.Options, ps httprouter.Params, b io.Reader) Response {
return AuthFunc(opts, ps, b)
}))
router.POST("/api/auth/logout", apiHandler(logout))
router.POST("/api/auth/logout", ApiHandler(logout))
}
type DisplayUser struct {

View File

@ -51,11 +51,11 @@ import (
)
func init() {
router.POST("/api/users", apiHandler(registerUser))
router.PATCH("/api/users", apiHandler(specialUserOperations))
router.POST("/api/users", ApiHandler(registerUser))
router.PATCH("/api/users", ApiHandler(specialUserOperations))
router.GET("/api/users/:uid", apiAuthHandler(sameUserHandler(getUser)))
router.POST("/api/users/:uid/email", apiHandler(userHandler(validateUserAddress)))
router.POST("/api/users/:uid/recovery", apiHandler(userHandler(recoverUserAccount)))
router.POST("/api/users/:uid/email", ApiHandler(userHandler(validateUserAddress)))
router.POST("/api/users/:uid/recovery", ApiHandler(userHandler(recoverUserAccount)))
}
type UploadedUser struct {

View File

@ -40,7 +40,7 @@ import (
)
func init() {
router.GET("/api/version", apiHandler(showVersion))
router.GET("/api/version", ApiHandler(showVersion))
}
func showVersion(_ *config.Options, _ httprouter.Params, _ io.Reader) Response {

View File

@ -37,6 +37,7 @@ import (
func (o *Options) parseCLI() error {
flag.StringVar(&o.DevProxy, "dev", o.DevProxy, "Proxify traffic to this host for static assets")
flag.StringVar(&o.AdminBind, "adminbind", o.AdminBind, "Bind port/socket for administration interface")
flag.StringVar(&o.Bind, "bind", ":8081", "Bind port/socket")
flag.StringVar(&o.DSN, "dsn", o.DSN, "DSN to connect to the MySQL server")
flag.StringVar(&o.ExternalURL, "exernalurl", o.ExternalURL, "Begining of the URL, before the base, that should be used eg. in mails")

View File

@ -43,6 +43,7 @@ import (
type Options struct {
Bind string
AdminBind string
ExternalURL string
BaseURL string
DevProxy string
@ -54,6 +55,7 @@ func ConsolidateConfig() (opts *Options, err error) {
// Define defaults options
opts = &Options{
Bind: ":8081",
AdminBind: "./happydns.sock",
ExternalURL: "http://localhost:8081",
BaseURL: "/",
DSN: database.DSNGenerator(),

56
main.go
View File

@ -33,13 +33,19 @@ package main
import (
"context"
"fmt"
"log"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"
"git.happydns.org/happydns/admin"
"git.happydns.org/happydns/api"
"git.happydns.org/happydns/config"
"git.happydns.org/happydns/storage"
@ -116,9 +122,51 @@ func main() {
log.Fatal("Cannot migrate database: ", err)
}
// Serve content
log.Println("Ready, listening on", opts.Bind)
if err = http.ListenAndServe(opts.Bind, StripPrefix(opts, api.Router())); err != nil {
log.Fatal("Unable to listen and serve: ", err)
// Prepare graceful shutdown
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
if opts.AdminBind != "" {
adminSrv := &http.Server{
Addr: opts.AdminBind,
Handler: StripPrefix(opts, admin.Router()),
}
go func() {
if !strings.Contains(opts.AdminBind, ":") {
if _, err := os.Stat(opts.AdminBind); !os.IsNotExist(err) {
if err := os.Remove(opts.AdminBind); err != nil {
log.Fatal(err)
}
}
unixListener, err := net.Listen("unix", opts.AdminBind)
if err != nil {
log.Fatal(err)
}
log.Fatal(adminSrv.Serve(unixListener))
} else {
log.Fatal(adminSrv.ListenAndServe())
}
}()
log.Println(fmt.Sprintf("Admin listening on %s", opts.AdminBind))
}
srv := &http.Server{
Addr: opts.Bind,
Handler: StripPrefix(opts, api.Router()),
}
// Serve content
go func() {
log.Fatal(srv.ListenAndServe())
}()
log.Println(fmt.Sprintf("Ready, listening on %s", opts.Bind))
// Wait shutdown signal
<-interrupt
log.Print("The service is shutting down...")
srv.Shutdown(context.Background())
log.Println("done")
}