This commit is contained in:
ston1th 2023-03-21 01:00:13 +01:00
commit 05a68df896
15 changed files with 158 additions and 135 deletions

View file

@ -34,21 +34,30 @@ type notFoundHandler struct {
}
func (nf *notFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
types.NewContext(w, r, nf.s.Data, nf.s.log).Err(types.ErrInvalidAPIRoute)
types.NewContext(w, r, nil, nf.s.log).Err(types.ErrInvalidAPIRoute)
}
// NewHTTPServer returns a new HTTPServer
func NewServer(log logr.Logger, listen, socket string) (*Server, error) {
func NewServer(log logr.Logger, listen, socket string, db *db.DB) (*Server, error) {
m := mux.NewRouter()
s := &Server{
mux: mux.NewRouter(),
log: log,
Data: &types.ContextData{Approver: types.NewApprover()},
mux: m,
log: log,
Data: &types.ContextData{
Approver: types.NewApprover(),
DB: db,
},
srv: &http.Server{
Handler: m,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
},
}
l, err := net.Listen("tcp", listen)
list, err := net.Listen("tcp", listen)
if err != nil {
return nil, err
}
s.listen = l
s.listen = list
sock, err := unixListener(socket)
if err != nil {
@ -56,29 +65,11 @@ func NewServer(log logr.Logger, listen, socket string) (*Server, error) {
}
s.socket = sock
s.mux.NotFoundHandler = &notFoundHandler{s}
m.NotFoundHandler = &notFoundHandler{s}
for _, v := range serverv1.Routes {
s.mux.HandleFunc(v.Path, s.contextWrapper(v.Handler)).Methods(v.Methods...)
m.HandleFunc(v.Path, s.contextWrapper(v.Handler)).Methods(v.Methods...)
}
return s, nil
}
func unixListener(socket string) (sock net.Listener, err error) {
sock, err = net.Listen("unix", socket)
if err != nil {
return
}
err = os.Chmod(socket, 0660)
return
}
func (s *Server) Start(db *db.DB) {
s.srv = &http.Server{
Handler: s.mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
s.Data.DB = db
go func() {
err := s.srv.Serve(s.listen)
if err != nil && err != http.ErrServerClosed {
@ -91,6 +82,15 @@ func (s *Server) Start(db *db.DB) {
s.log.Error(err, "")
}
}()
return s, nil
}
func unixListener(socket string) (sock net.Listener, err error) {
sock, err = net.Listen("unix", socket)
if err != nil {
return
}
err = os.Chmod(socket, 0660)
return
}

View file

@ -4,7 +4,6 @@ package types
import (
"sync"
"time"
"git.giftfish.de/ston1th/keyctl/pkg/key"
"golang.org/x/exp/maps"
@ -17,7 +16,8 @@ type ApprovalMap map[string]Approval
type Approver struct {
// protects m
mu sync.RWMutex
m ApprovalMap
m ApprovalMap
}
func NewApprover() *Approver {
@ -39,10 +39,9 @@ func (a *Approver) New(id, name, ip string) (aid string, err error) {
return
}
a.m[aid] = Approval{
ID: id,
Name: name,
IP: ip,
Created: time.Now().Unix(),
ID: id,
Name: name,
IP: ip,
}
return
}
@ -77,11 +76,10 @@ func (a *Approver) Update(aid string, s Status) {
}
type Approval struct {
ID string
Name string
IP string
Created int64
Status Status
ID string
Name string
IP string
Status Status
}
type Status int

View file

@ -15,8 +15,8 @@ import (
)
type ContextData struct {
DB *db.DB
Approver *Approver
DB *db.DB
}
func NewContext(w http.ResponseWriter, r *http.Request, data *ContextData, log logr.Logger) *Context {
@ -84,9 +84,8 @@ func (c *Context) ClientIP() (host string) {
}
// Var returns the given url variable
func (c *Context) Var(name string) (ret string) {
ret, _ = mux.Vars(c.Request)[name]
return
func (c *Context) Var(name string) string {
return mux.Vars(c.Request)[name]
}
func (c *Context) log() {

View file

@ -6,7 +6,6 @@ import (
"encoding/hex"
"errors"
"fmt"
"time"
"git.giftfish.de/ston1th/keyctl/pkg/api/types"
"git.giftfish.de/ston1th/keyctl/pkg/core"
@ -59,7 +58,6 @@ func ApproveURL(id, aid string) string {
type Key struct {
ID string `json:"id"`
Name string `json:"name"`
Created int64 `json:"created"`
Size int `json:"size"`
Encoding core.Encoding `json:"encoding"`
Type core.Type `json:"type"`
@ -92,15 +90,14 @@ func (Keys) Less(a, b Key) bool { return a.Name < b.Name }
func (k Keys) Sort() { slices.SortStableFunc(k, k.Less) }
func (k Keys) Len() int { return len(k) }
func (k Keys) Fields() string {
return "ID\t Name\t Created\t Size\t Encoding\t Type"
return "ID\t Name\t Size\t Encoding\t Type"
}
func (k Keys) Values() []string {
vals := make([]string, len(k))
for i, v := range k {
vals[i] = fmt.Sprintf("%s\t %s\t %s\t %d\t %s\t %s",
vals[i] = fmt.Sprintf("%s\t %s\t %d\t %s\t %s",
v.ID,
v.Name,
timeFmt(v.Created),
v.Size,
v.Encoding,
v.Type,
@ -118,12 +115,11 @@ func NewKeys(keys core.Keys) (k Keys) {
}
type Approval struct {
ID string `json:"id"`
AID string `json:"aid"`
Name string `json:"name"`
IP string `json:"ip"`
Created int64 `json:"created"`
Status types.Status `json:"status"`
ID string `json:"id"`
AID string `json:"aid"`
Name string `json:"name"`
IP string `json:"ip"`
Status types.Status `json:"status"`
}
type Approvals []Approval
@ -132,17 +128,16 @@ func (Approvals) Less(a, b Approval) bool { return a.Name < b.Name }
func (a Approvals) Sort() { slices.SortStableFunc(a, a.Less) }
func (a Approvals) Len() int { return len(a) }
func (a Approvals) Fields() string {
return "ID\t Approval ID\t Name\t Status\t Created\t IP"
return "ID\t Approval ID\t Name\t Status\t IP"
}
func (a Approvals) Values() []string {
vals := make([]string, len(a))
for i, v := range a {
vals[i] = fmt.Sprintf("%s\t %s\t %s\t %s\t %s\t %s",
vals[i] = fmt.Sprintf("%s\t %s\t %s\t %s\t %s",
v.ID,
v.AID,
v.Name,
v.Status,
timeFmt(v.Created),
v.IP,
)
}
@ -152,18 +147,13 @@ func (a Approvals) Values() []string {
func NewApprovals(m types.ApprovalMap) (a Approvals) {
for k, v := range m {
a = append(a, Approval{
ID: v.ID,
AID: k,
Name: v.Name,
IP: v.IP,
Created: v.Created,
Status: v.Status,
ID: v.ID,
AID: k,
Name: v.Name,
IP: v.IP,
Status: v.Status,
})
}
a.Sort()
return
}
func timeFmt(t int64) string {
return time.Unix(t, 0).Format(time.RFC3339)
}

View file

@ -18,15 +18,6 @@ func local(h types.CtxHandler) types.CtxHandler {
h(ctx)
return
}
//addr, err := netip.ParseAddr(ip)
//if err != nil {
// ctx.Err(types.ErrForbidden)
// return
//}
//if addr.IsLoopback() {
// h(ctx)
// return
//}
ctx.Err(types.ErrForbidden)
}
}

View file

@ -17,43 +17,43 @@ const (
var Routes = []types.Route{
{
healthz,
healthzHandler,
[]string{"GET"},
Path: healthz,
Handler: healthzHandler,
Methods: []string{"GET"},
},
{
schema.KeyPath,
local(keyListHandler),
[]string{"GET"},
Path: schema.KeyPath,
Handler: local(keyListHandler),
Methods: []string{"GET"},
},
{
schema.KeyPath,
local(newHandler),
[]string{"POST"},
Path: schema.KeyPath,
Handler: local(newHandler),
Methods: []string{"POST"},
},
{
keyID,
keyHandler,
[]string{"GET"},
Path: keyID,
Handler: keyHandler,
Methods: []string{"GET"},
},
{
keyReqID,
keyHandler,
[]string{"GET"},
Path: keyReqID,
Handler: keyHandler,
Methods: []string{"GET"},
},
{
keyID,
local(keyHandler),
[]string{"DELETE"},
Path: keyID,
Handler: local(keyHandler),
Methods: []string{"DELETE"},
},
{
schema.ReqPath,
local(reqListHandler),
[]string{"GET"},
Path: schema.ReqPath,
Handler: local(reqListHandler),
Methods: []string{"GET"},
},
{
reqID,
local(reqHandler),
[]string{"PUT", "DELETE"},
Path: reqID,
Handler: local(reqHandler),
Methods: []string{"PUT", "DELETE"},
},
}

View file

@ -25,7 +25,6 @@ func exit(err error) {
}
func Run(arg string) {
var err error
ctx := context.Background()
switch arg {
case "new":
@ -58,6 +57,9 @@ func Run(arg string) {
}
c := client()
err = c.DeleteKey(ctx, id)
if err != nil {
exit(err)
}
case "approve":
id, err := idFlags("approval id")
if err != nil {
@ -65,6 +67,9 @@ func Run(arg string) {
}
c := client()
err = c.AcceptApproval(ctx, id)
if err != nil {
exit(err)
}
case "reject":
id, err := idFlags("approval id")
if err != nil {
@ -72,6 +77,9 @@ func Run(arg string) {
}
c := client()
err = c.RejectApproval(ctx, id)
if err != nil {
exit(err)
}
case "get":
id, share, json, err := getFlags()
if err != nil {
@ -114,9 +122,6 @@ func Run(arg string) {
}
print(approvals)
}
if err != nil {
exit(err)
}
os.Exit(0)
}

View file

@ -12,6 +12,13 @@ var b32raw = base32.StdEncoding.WithPadding(base32.NoPadding)
type Encoding int
const (
Hex Encoding = iota
Base32
Base64
Base64URL
)
func (e Encoding) String() string {
switch e {
case Base32:
@ -59,10 +66,3 @@ func EncodingFromString(e string) Encoding {
}
return Hex
}
const (
Hex Encoding = iota
Base32
Base64
Base64URL
)

View file

@ -17,7 +17,6 @@ var NameRe = regexp.MustCompile(nameReReverse)
type Key struct {
ID string
Name string
Created int64
Size int
Encoding Encoding
Type Type

View file

@ -10,6 +10,11 @@ import (
type Type int
const (
Plain Type = iota
Shamir
)
func (t Type) String() string {
switch t {
case Shamir:
@ -48,8 +53,3 @@ func TypeFromString(t string) Type {
}
return Plain
}
const (
Plain Type = iota
Shamir
)

View file

@ -6,7 +6,6 @@ import (
"crypto/rand"
"encoding/hex"
"io"
"time"
"git.giftfish.de/ston1th/keyctl/pkg/core"
)
@ -46,7 +45,6 @@ func Generate(name string, size int, enc core.Encoding, t core.Type) (kstore, k
Size: size,
Encoding: enc,
Type: t,
Created: time.Now().Unix(),
}
k = kstore
if t == core.Shamir {