added get loop and new key type xor

This commit is contained in:
ston1th 2023-03-22 22:52:19 +01:00
commit 9e68b46346
8 changed files with 122 additions and 24 deletions

View file

@ -15,6 +15,8 @@ import (
"os"
"strings"
"time"
"git.giftfish.de/ston1th/keyctl/pkg/api/types"
)
type Client struct {
@ -181,14 +183,47 @@ func (c *Client) Do(r *http.Request, v any) (resp *http.Response, err error) {
}
func (c *Client) FollowRedirect(r *http.Request, v any, delay time.Duration) (resp *http.Response, err error) {
rr := r
ctx := r.Context()
rr := r.Clone(ctx)
for {
resp, err = c.Do(rr, v)
if err == nil && resp.StatusCode == http.StatusOK {
return
}
rdr, ok := err.(Redirect)
if !ok {
return
}
rr, err = c.NewRequest(r.Context(), "GET", rdr.Location, nil)
rr, err = c.NewRequest(ctx, "GET", rdr.Location, nil)
if err != nil {
return
}
time.Sleep(delay)
}
}
func (c *Client) FollowRedirectLoop(r *http.Request, v any, delay time.Duration) (resp *http.Response, err error) {
ctx := r.Context()
rr := r.Clone(ctx)
for {
resp, err = c.Do(rr, v)
rdr, ok := err.(Redirect)
if err != nil && !ok {
if errors.Is(err, types.ErrISE) ||
errors.Is(err, types.ErrKeyNotFound) {
return
}
if errors.Is(err, types.ErrReqNotFound) ||
errors.Is(err, types.ErrKeyRequestRejected) {
rr = r.Clone(ctx)
}
time.Sleep(delay)
continue
}
if err == nil && resp.StatusCode == http.StatusOK {
return
}
rr, err = c.NewRequest(ctx, "GET", rdr.Location, nil)
if err != nil {
return
}

View file

@ -54,8 +54,14 @@ func (a *Approver) List() (m ApprovalMap) {
}
func (a *Approver) Status(id, aid, ip string) (s Status) {
a.mu.RLock()
if app, ok := a.m[aid]; ok && app.ID == id && app.IP == ip {
s = app.Status
if app, ok := a.m[aid]; ok {
if app.ID == id && app.IP == ip {
s = app.Status
} else {
s = Rejected
}
} else {
s = NotFound
}
a.mu.RUnlock()
if s == Approved || s == Rejected {
@ -66,13 +72,16 @@ func (a *Approver) Status(id, aid, ip string) (s Status) {
return
}
func (a *Approver) Update(aid string, s Status) {
func (a *Approver) Update(aid string, s Status) (err error) {
a.mu.Lock()
if app, ok := a.m[aid]; ok {
app.Status = s
a.m[aid] = app
} else {
err = ErrReqNotFound
}
a.mu.Unlock()
return
}
type Approval struct {
@ -88,6 +97,7 @@ const (
Pending Status = iota
Approved
Rejected
NotFound
)
func (s Status) String() string {
@ -98,6 +108,8 @@ func (s Status) String() string {
return "approved"
case Rejected:
return "rejected"
case NotFound:
return "notfound"
}
return "[invalid]"
}

View file

@ -83,6 +83,16 @@ func (c *Client) GetKey(ctx context.Context, id string) (key schema.Key, err err
return
}
func (c *Client) GetKeyLoop(ctx context.Context, id string) (key schema.Key, err error) {
path := fmt.Sprintf("%s/%s", keyPath, id)
req, err := c.c.NewRequest(ctx, "GET", path, nil)
if err != nil {
return
}
_, err = c.c.FollowRedirectLoop(req, &key, time.Second*2)
return
}
func (c *Client) CreateKey(ctx context.Context, newKey schema.NewKey) (key schema.Key, err error) {
buf := new(bytes.Buffer)
path := keyPath

View file

@ -90,7 +90,7 @@ 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 Size\t Encoding\t Type"
return "Key ID\t Name\t Size\t Encoding\t Type"
}
func (k Keys) Values() []string {
vals := make([]string, len(k))
@ -128,7 +128,7 @@ 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 IP"
return "Key ID\t Approval ID\t Name\t Status\t IP"
}
func (a Approvals) Values() []string {
vals := make([]string, len(a))

View file

@ -64,7 +64,7 @@ func keyHandler(ctx *types.Context) {
if err != nil {
ctx.Log.Error(err, "error reading key", "id", id)
if err == store.ErrKeyNotFound {
ctx.Err(types.ErrNotFound)
ctx.Err(types.ErrKeyNotFound)
return
}
ctx.Err(types.ErrISE)
@ -77,7 +77,7 @@ func keyHandler(ctx *types.Context) {
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)
ctx.Err(types.ErrTooManyApprovals)
return
}
ctx.Redirect(schema.ApproveURL(id, aid), http.StatusFound)
@ -91,6 +91,9 @@ func keyHandler(ctx *types.Context) {
case types.Rejected:
ctx.Err(types.ErrKeyRequestRejected)
return
case types.NotFound:
ctx.Err(types.ErrReqNotFound)
return
}
k, err := ctx.Data.DB.GetKeyWithSecret(id)
if err != nil {
@ -142,6 +145,11 @@ func reqHandler(ctx *types.Context) {
case "DELETE":
status = types.Rejected
}
ctx.Data.Approver.Update(id, status)
err := ctx.Data.Approver.Update(id, status)
if err != nil {
ctx.Log.Error(err, "error updating key request")
ctx.Err(types.ErrReqNotFound)
return
}
ctx.OK()
}