From 39cae648545a8aecdb8f378d203a15e2843c20e0 Mon Sep 17 00:00:00 2001 From: ston1th Date: Wed, 15 Mar 2023 11:07:23 +0100 Subject: [PATCH] first working version --- cmd/keyctl/main.go | 9 +++++---- pkg/api/client/client.go | 4 ++-- pkg/api/server.go | 2 +- pkg/api/types/approver.go | 10 ++++++---- pkg/api/types/error.go | 2 +- pkg/api/v1/schema/schema.go | 2 ++ pkg/api/v1/server/handler.go | 5 +++-- pkg/cli/cli.go | 5 +++-- 8 files changed, 23 insertions(+), 16 deletions(-) diff --git a/cmd/keyctl/main.go b/cmd/keyctl/main.go index 0de233c..97b5ee5 100644 --- a/cmd/keyctl/main.go +++ b/cmd/keyctl/main.go @@ -55,10 +55,11 @@ func main() { //flag.Parse() cli.Run(arg) // does not return case "server": - klog.InitFlags(nil) - flag.StringVar(&listen, "listen", ":7070", "listen ip:port") - flag.StringVar(&path, "path", "/var/keyctl", "db storage dir") - flag.Parse() + fs := flag.NewFlagSet("", flag.ExitOnError) + klog.InitFlags(fs) + fs.StringVar(&listen, "listen", ":7070", "listen ip:port") + fs.StringVar(&path, "path", "/var/keyctl", "db storage dir") + fs.Parse(os.Args[2:]) default: usage() } diff --git a/pkg/api/client/client.go b/pkg/api/client/client.go index 44e3f96..c59bb00 100644 --- a/pkg/api/client/client.go +++ b/pkg/api/client/client.go @@ -168,10 +168,10 @@ func (c *Client) FollowRedirect(r *http.Request, v any, delay time.Duration) (re rr := r for { resp, err = c.Do(rr, v) - if !errors.Is(ErrRedirect, err) { + rdr, ok := err.(Redirect) + if !ok { return } - rdr := err.(Redirect) rr, err = c.NewRequest(r.Context(), "GET", rdr.Location, nil) if err != nil { return diff --git a/pkg/api/server.go b/pkg/api/server.go index a728636..6f80b85 100644 --- a/pkg/api/server.go +++ b/pkg/api/server.go @@ -64,7 +64,7 @@ func (s *Server) Start(db *db.DB) { s.Data.DB = db go func() { err := s.srv.Serve(s.listen) - if err != nil { + if err != nil && err != http.ErrServerClosed { s.log.Error(err, "") } }() diff --git a/pkg/api/types/approver.go b/pkg/api/types/approver.go index de62e47..84ca101 100644 --- a/pkg/api/types/approver.go +++ b/pkg/api/types/approver.go @@ -26,7 +26,7 @@ func NewApprover() *Approver { } } -func (a *Approver) New(id, name string) (aid string, err error) { +func (a *Approver) New(id, name, ip string) (aid string, err error) { a.mu.Lock() defer a.mu.Unlock() size := len(a.m) @@ -41,6 +41,7 @@ func (a *Approver) New(id, name string) (aid string, err error) { a.m[aid] = Approval{ ID: id, Name: name, + IP: ip, Created: time.Now().Unix(), } return @@ -52,13 +53,13 @@ func (a *Approver) List() (m ApprovalMap) { a.mu.RUnlock() return } -func (a *Approver) Status(id, aid string) (s Status) { +func (a *Approver) Status(id, aid, ip string) (s Status) { a.mu.RLock() - if app, ok := a.m[aid]; ok && app.ID == id { + if app, ok := a.m[aid]; ok && app.ID == id && app.IP == ip { s = app.Status } a.mu.RUnlock() - if s == Rejected { + if s == Approved || s == Rejected { a.mu.Lock() delete(a.m, aid) a.mu.Unlock() @@ -78,6 +79,7 @@ func (a *Approver) Update(aid string, s Status) { type Approval struct { ID string Name string + IP string Created int64 Status Status } diff --git a/pkg/api/types/error.go b/pkg/api/types/error.go index ce2a0c7..df249b1 100644 --- a/pkg/api/types/error.go +++ b/pkg/api/types/error.go @@ -51,7 +51,7 @@ var ( ErrKeyNotFound = newError("key not found", http.StatusNotFound) ErrReqNotFound = newError("key request not found", http.StatusNotFound) ErrTooManyApprovals = newError("too many approvals pending", http.StatusConflict) - ErrKeyRequestRejected = newError("key request rejected", http.StatusConflict) + ErrKeyRequestRejected = newError("key request rejected", http.StatusForbidden) ErrMNA = newError("method not allowed", http.StatusMethodNotAllowed) ErrISE = newError("internal server error", http.StatusInternalServerError) ) diff --git a/pkg/api/v1/schema/schema.go b/pkg/api/v1/schema/schema.go index 1af00b8..136e75c 100644 --- a/pkg/api/v1/schema/schema.go +++ b/pkg/api/v1/schema/schema.go @@ -78,6 +78,7 @@ 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"` } @@ -93,6 +94,7 @@ func NewApprovals(m types.ApprovalMap) (a Approvals) { ID: v.ID, AID: k, Name: v.Name, + IP: v.IP, Created: v.Created, Status: v.Status, }) diff --git a/pkg/api/v1/server/handler.go b/pkg/api/v1/server/handler.go index 68b153e..e10d438 100644 --- a/pkg/api/v1/server/handler.go +++ b/pkg/api/v1/server/handler.go @@ -64,6 +64,7 @@ func newHandler(ctx *types.Context) { func keyHandler(ctx *types.Context) { id := ctx.Var("id") + ip := ctx.ClientIP() k, err := ctx.Data.DB.GetKey(id) if err != nil { ctx.Log.Error(err, "error reading key", "id", id) @@ -78,7 +79,7 @@ func keyHandler(ctx *types.Context) { case "GET": aid := ctx.Var("aid") if aid == "" { - aid, err = ctx.Data.Approver.New(id, k.Name) + aid, err = ctx.Data.Approver.New(id, k.Name, ip) if err != nil { ctx.Log.Error(err, "error creating approval", "id", id, "name", k.Name) ctx.Err(types.ErrISE) @@ -87,7 +88,7 @@ func keyHandler(ctx *types.Context) { ctx.Redirect(schema.ApproveURL(id, aid), http.StatusFound) return } - s := ctx.Data.Approver.Status(id, aid) + s := ctx.Data.Approver.Status(id, aid, ip) switch s { case types.Pending: ctx.Redirect(schema.ApproveURL(id, aid), http.StatusFound) diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 012d390..5a0def7 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -69,7 +69,7 @@ func Run(arg string) { jsonOutput(keys) // does not return } for _, k := range keys { - fmt.Printf("%s %s %d %d %s\n", + fmt.Printf("%s %s %d %s %d\n", k.ID, k.Name, k.Size, @@ -88,12 +88,13 @@ func Run(arg string) { jsonOutput(approvals) // does not return } for _, a := range approvals { - fmt.Printf("%s %s %s %d %s\n", + fmt.Printf("%s %s %s %s %d %s\n", a.ID, a.AID, a.Name, a.Status, a.Created, + a.IP, ) } }