remove dependencies

This commit is contained in:
ston1th 2024-10-13 03:06:35 +02:00
commit 1656b91e3b
107 changed files with 567 additions and 7772 deletions

View file

@ -11,7 +11,6 @@ import (
"net"
"net/http"
"net/http/httputil"
"net/url"
neturl "net/url"
"os"
"strings"
@ -38,7 +37,7 @@ type ClientOption func(*Client)
func WithEndpoint(endpoint string) ClientOption {
return func(client *Client) {
u, err := url.Parse(endpoint)
u, err := neturl.Parse(endpoint)
if err != nil {
return
}

View file

@ -15,36 +15,32 @@ import (
"git.giftfish.de/ston1th/keyctl/pkg/db"
"github.com/go-logr/logr"
"github.com/gorilla/mux"
)
// Server is the webapp and api server
// Server is the api server
type Server struct {
srv *http.Server
mux *mux.Router
log logr.Logger
Data *types.ContextData
data *types.ContextData
listen net.Listener
socket net.Listener
}
type notFoundHandler struct {
s *Server
log logr.Logger
}
func (nf *notFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
types.NewContext(w, r, nil, nf.s.log).Err(types.ErrInvalidAPIRoute)
types.NewContext(w, r, nil, nf.log).Err(types.ErrInvalidAPIRoute)
}
// NewHTTPServer returns a new HTTPServer
func NewServer(log logr.Logger, listen, socket string, db *db.DB) (*Server, error) {
m := mux.NewRouter()
m := http.NewServeMux()
s := &Server{
mux: m,
log: log,
Data: &types.ContextData{
data: &types.ContextData{
Approver: types.NewApprover(),
DB: db,
},
@ -66,9 +62,11 @@ func NewServer(log logr.Logger, listen, socket string, db *db.DB) (*Server, erro
}
s.socket = sock
m.NotFoundHandler = &notFoundHandler{s}
m.Handle("/", &notFoundHandler{log})
for _, v := range serverv1.Routes {
m.HandleFunc(v.Path, s.contextWrapper(v.Handler)).Methods(v.Methods...)
for _, method := range v.Methods {
m.HandleFunc(method+" "+v.Path, s.contextWrapper(v.Handler))
}
}
go func() {
@ -105,7 +103,7 @@ func unixListener(socket string) (sock net.Listener, err error) {
func (s *Server) contextWrapper(h types.CtxHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h(types.NewContext(w, r, s.Data, s.log))
h(types.NewContext(w, r, s.data, s.log))
}
}

View file

@ -8,7 +8,6 @@ import (
"net/http"
"github.com/go-logr/logr"
"github.com/gorilla/mux"
"git.giftfish.de/ston1th/keyctl/pkg/db"
)
@ -22,23 +21,23 @@ func NewContext(w http.ResponseWriter, r *http.Request, data *ContextData, log l
h := w.Header()
h.Set("Content-Type", "application/json")
return &Context{
Code: http.StatusOK,
Request: r,
Response: w,
Data: data,
Log: log,
ip: clientip(r.RemoteAddr),
code: http.StatusOK,
r: r,
w: w,
data: data,
Log: log,
ip: clientip(r.RemoteAddr),
}
}
// Context is the context object shared between http handlers
type Context struct {
Code int
code int
Request *http.Request
Response http.ResponseWriter
r *http.Request
w http.ResponseWriter
Data *ContextData
data *ContextData
Log logr.Logger
ip string
@ -46,28 +45,27 @@ type Context struct {
// Method returns the request method
func (c *Context) Method() string {
return c.Request.Method
return c.r.Method
}
// Path returns the request URL path
func (c *Context) Path() string {
return c.Request.URL.Path
return c.r.URL.Path
}
// Header returns the given http header
func (c *Context) Header(name string) string {
return c.Request.Header.Get(name)
return c.r.Header.Get(name)
}
// SetHeader sets the given http header
func (c *Context) SetHeader(name, value string) {
h := c.Response.Header()
h.Set(name, value)
c.w.Header().Set(name, value)
}
// Form returns the given form value
func (c *Context) Form(name string) string {
return c.Request.FormValue(name)
return c.r.FormValue(name)
}
func (c *Context) ClientIP() string {
@ -76,15 +74,16 @@ func (c *Context) ClientIP() string {
// Var returns the given url variable
func (c *Context) Var(name string) string {
return mux.Vars(c.Request)[name]
return c.r.PathValue(name)
}
func (c *Context) log() {
r := c.r
c.Log.Info("access",
"addr", c.Request.RemoteAddr,
"method", c.Request.Method,
"path", c.Request.URL.Path,
"code", c.Code,
"addr", r.RemoteAddr,
"method", r.Method,
"path", r.URL.Path,
"code", c.code,
)
}
@ -92,25 +91,33 @@ func (c *Context) NotFound() {
c.Err(ErrNotFound)
}
func (c *Context) DB() *db.DB {
return c.data.DB
}
func (c *Context) Approver() *Approver {
return c.data.Approver
}
// OK is a empty HTTP 200 JSON response
func (c *Context) OK() {
c.log()
}
func (c *Context) ReadBody() (b []byte, err error) {
b, err = io.ReadAll(c.Request.Body)
c.Request.Body.Close()
b, err = io.ReadAll(c.r.Body)
c.r.Body.Close()
return
}
func (c *Context) Body(b []byte) {
c.Response.Write(b)
c.w.Write(b)
c.log()
}
// JSON is a json response
func (c *Context) JSON(v any) {
err := json.NewEncoder(c.Response).Encode(v)
err := json.NewEncoder(c.w).Encode(v)
if err != nil {
c.Err(ErrISE)
return
@ -119,8 +126,8 @@ func (c *Context) JSON(v any) {
}
func (c *Context) Redirect(url string, code int) {
c.Code = code
http.Redirect(c.Response, c.Request, url, code)
c.code = code
http.Redirect(c.w, c.r, url, code)
c.log()
}
@ -131,8 +138,8 @@ func (c *Context) Err(err Error) {
// HTTPErr is a http.Error wrapper
func (c *Context) HTTPErr(err string, code int) {
c.Code = code
http.Error(c.Response, err, code)
c.code = code
http.Error(c.w, err, code)
c.log()
}

View file

@ -43,7 +43,13 @@ func newHandler(ctx *types.Context) {
ctx.Err(types.ErrInvalid)
return
}
key, err := ctx.Data.DB.CreateKey(newKey.Name, newKey.Existing, newKey.Size, newKey.Encoding, newKey.Type)
key, err := ctx.DB().CreateKey(
newKey.Name,
newKey.Existing,
newKey.Size,
newKey.Encoding,
newKey.Type,
)
if err != nil {
ctx.Log.Error(err, "error writing key", "name", newKey.Name)
ctx.Err(types.ErrISE)
@ -56,7 +62,8 @@ func newHandler(ctx *types.Context) {
func keyHandler(ctx *types.Context) {
id := ctx.Var("id")
ip := ctx.ClientIP()
k, err := ctx.Data.DB.GetKey(id)
db := ctx.DB()
k, err := db.GetKey(id)
if err != nil {
ctx.Log.Error(err, "error reading key", "id", id)
if err == store.ErrKeyNotFound {
@ -68,9 +75,10 @@ func keyHandler(ctx *types.Context) {
}
switch ctx.Method() {
case "GET":
a := ctx.Approver()
aid := ctx.Var("aid")
if aid == "" {
aid, err = ctx.Data.Approver.New(id, k.Name, ip)
aid, err = a.New(id, k.Name, ip)
if err != nil {
ctx.Log.Error(err, "error creating approval", "id", id, "name", k.Name)
ctx.Err(types.ErrTooManyApprovals)
@ -79,7 +87,7 @@ func keyHandler(ctx *types.Context) {
ctx.Redirect(schema.ApproveURL(id, aid), http.StatusFound)
return
}
s := ctx.Data.Approver.Status(id, aid, ip)
s := a.Status(id, aid, ip)
switch s {
case types.Pending:
ctx.Redirect(schema.ApproveURL(id, aid), http.StatusFound)
@ -91,7 +99,7 @@ func keyHandler(ctx *types.Context) {
ctx.Err(types.ErrReqNotFound)
return
}
k, err := ctx.Data.DB.GetKeyWithSecret(id)
k, err := db.GetKeyWithSecret(id)
if err != nil {
ctx.Log.Error(err, "error reading key", "id", id)
if err == store.ErrKeyNotFound {
@ -103,7 +111,7 @@ func keyHandler(ctx *types.Context) {
}
ctx.JSON(schema.Key(k))
case "DELETE":
err = ctx.Data.DB.DeleteKey(id)
err = db.DeleteKey(id)
if err != nil {
ctx.Log.Error(err, "error deleting key", "id", id, "name", k.Name)
if err == store.ErrKeyNotFound {
@ -118,12 +126,12 @@ func keyHandler(ctx *types.Context) {
}
func reqListHandler(ctx *types.Context) {
l := ctx.Data.Approver.List()
l := ctx.Approver().List()
ctx.JSON(schema.NewApprovals(l))
}
func keyListHandler(ctx *types.Context) {
k, err := ctx.Data.DB.GetKeys()
k, err := ctx.DB().GetKeys()
if err != nil {
ctx.Log.Error(err, "error reading keys")
ctx.Err(types.ErrISE)
@ -141,7 +149,7 @@ func reqHandler(ctx *types.Context) {
case "DELETE":
status = types.Rejected
}
err := ctx.Data.Approver.Update(id, status)
err := ctx.Approver().Update(id, status)
if err != nil {
ctx.Log.Error(err, "error updating key request")
ctx.Err(types.ErrReqNotFound)