first working version

This commit is contained in:
ston1th 2023-03-15 11:07:23 +01:00
commit 39cae64854
8 changed files with 23 additions and 16 deletions

View file

@ -55,10 +55,11 @@ func main() {
//flag.Parse() //flag.Parse()
cli.Run(arg) // does not return cli.Run(arg) // does not return
case "server": case "server":
klog.InitFlags(nil) fs := flag.NewFlagSet("", flag.ExitOnError)
flag.StringVar(&listen, "listen", ":7070", "listen ip:port") klog.InitFlags(fs)
flag.StringVar(&path, "path", "/var/keyctl", "db storage dir") fs.StringVar(&listen, "listen", ":7070", "listen ip:port")
flag.Parse() fs.StringVar(&path, "path", "/var/keyctl", "db storage dir")
fs.Parse(os.Args[2:])
default: default:
usage() usage()
} }

View file

@ -168,10 +168,10 @@ func (c *Client) FollowRedirect(r *http.Request, v any, delay time.Duration) (re
rr := r rr := r
for { for {
resp, err = c.Do(rr, v) resp, err = c.Do(rr, v)
if !errors.Is(ErrRedirect, err) { rdr, ok := err.(Redirect)
if !ok {
return return
} }
rdr := err.(Redirect)
rr, err = c.NewRequest(r.Context(), "GET", rdr.Location, nil) rr, err = c.NewRequest(r.Context(), "GET", rdr.Location, nil)
if err != nil { if err != nil {
return return

View file

@ -64,7 +64,7 @@ func (s *Server) Start(db *db.DB) {
s.Data.DB = db s.Data.DB = db
go func() { go func() {
err := s.srv.Serve(s.listen) err := s.srv.Serve(s.listen)
if err != nil { if err != nil && err != http.ErrServerClosed {
s.log.Error(err, "") s.log.Error(err, "")
} }
}() }()

View file

@ -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() a.mu.Lock()
defer a.mu.Unlock() defer a.mu.Unlock()
size := len(a.m) size := len(a.m)
@ -41,6 +41,7 @@ func (a *Approver) New(id, name string) (aid string, err error) {
a.m[aid] = Approval{ a.m[aid] = Approval{
ID: id, ID: id,
Name: name, Name: name,
IP: ip,
Created: time.Now().Unix(), Created: time.Now().Unix(),
} }
return return
@ -52,13 +53,13 @@ func (a *Approver) List() (m ApprovalMap) {
a.mu.RUnlock() a.mu.RUnlock()
return return
} }
func (a *Approver) Status(id, aid string) (s Status) { func (a *Approver) Status(id, aid, ip string) (s Status) {
a.mu.RLock() 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 s = app.Status
} }
a.mu.RUnlock() a.mu.RUnlock()
if s == Rejected { if s == Approved || s == Rejected {
a.mu.Lock() a.mu.Lock()
delete(a.m, aid) delete(a.m, aid)
a.mu.Unlock() a.mu.Unlock()
@ -78,6 +79,7 @@ func (a *Approver) Update(aid string, s Status) {
type Approval struct { type Approval struct {
ID string ID string
Name string Name string
IP string
Created int64 Created int64
Status Status Status Status
} }

View file

@ -51,7 +51,7 @@ var (
ErrKeyNotFound = newError("key not found", http.StatusNotFound) ErrKeyNotFound = newError("key not found", http.StatusNotFound)
ErrReqNotFound = newError("key request not found", http.StatusNotFound) ErrReqNotFound = newError("key request not found", http.StatusNotFound)
ErrTooManyApprovals = newError("too many approvals pending", http.StatusConflict) 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) ErrMNA = newError("method not allowed", http.StatusMethodNotAllowed)
ErrISE = newError("internal server error", http.StatusInternalServerError) ErrISE = newError("internal server error", http.StatusInternalServerError)
) )

View file

@ -78,6 +78,7 @@ type Approval struct {
ID string `json:"id"` ID string `json:"id"`
AID string `json:"aid"` AID string `json:"aid"`
Name string `json:"name"` Name string `json:"name"`
IP string `json:"ip"`
Created int64 `json:"created"` Created int64 `json:"created"`
Status types.Status `json:"status"` Status types.Status `json:"status"`
} }
@ -93,6 +94,7 @@ func NewApprovals(m types.ApprovalMap) (a Approvals) {
ID: v.ID, ID: v.ID,
AID: k, AID: k,
Name: v.Name, Name: v.Name,
IP: v.IP,
Created: v.Created, Created: v.Created,
Status: v.Status, Status: v.Status,
}) })

View file

@ -64,6 +64,7 @@ func newHandler(ctx *types.Context) {
func keyHandler(ctx *types.Context) { func keyHandler(ctx *types.Context) {
id := ctx.Var("id") id := ctx.Var("id")
ip := ctx.ClientIP()
k, err := ctx.Data.DB.GetKey(id) k, err := ctx.Data.DB.GetKey(id)
if err != nil { if err != nil {
ctx.Log.Error(err, "error reading key", "id", id) ctx.Log.Error(err, "error reading key", "id", id)
@ -78,7 +79,7 @@ func keyHandler(ctx *types.Context) {
case "GET": case "GET":
aid := ctx.Var("aid") aid := ctx.Var("aid")
if 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 { if err != nil {
ctx.Log.Error(err, "error creating approval", "id", id, "name", k.Name) ctx.Log.Error(err, "error creating approval", "id", id, "name", k.Name)
ctx.Err(types.ErrISE) ctx.Err(types.ErrISE)
@ -87,7 +88,7 @@ func keyHandler(ctx *types.Context) {
ctx.Redirect(schema.ApproveURL(id, aid), http.StatusFound) ctx.Redirect(schema.ApproveURL(id, aid), http.StatusFound)
return return
} }
s := ctx.Data.Approver.Status(id, aid) s := ctx.Data.Approver.Status(id, aid, ip)
switch s { switch s {
case types.Pending: case types.Pending:
ctx.Redirect(schema.ApproveURL(id, aid), http.StatusFound) ctx.Redirect(schema.ApproveURL(id, aid), http.StatusFound)

View file

@ -69,7 +69,7 @@ func Run(arg string) {
jsonOutput(keys) // does not return jsonOutput(keys) // does not return
} }
for _, k := range keys { for _, k := range keys {
fmt.Printf("%s %s %d %d %s\n", fmt.Printf("%s %s %d %s %d\n",
k.ID, k.ID,
k.Name, k.Name,
k.Size, k.Size,
@ -88,12 +88,13 @@ func Run(arg string) {
jsonOutput(approvals) // does not return jsonOutput(approvals) // does not return
} }
for _, a := range approvals { for _, a := range approvals {
fmt.Printf("%s %s %s %d %s\n", fmt.Printf("%s %s %s %s %d %s\n",
a.ID, a.ID,
a.AID, a.AID,
a.Name, a.Name,
a.Status, a.Status,
a.Created, a.Created,
a.IP,
) )
} }
} }