first compiling version

This commit is contained in:
ston1th 2023-03-15 01:51:45 +01:00
commit 3d54199faa
27 changed files with 908 additions and 243 deletions

View file

@ -8,6 +8,7 @@ import (
"encoding/json"
"errors"
"fmt"
"time"
apiclient "git.giftfish.de/ston1th/keyctl/pkg/api/client"
"git.giftfish.de/ston1th/keyctl/pkg/api/v1/schema"
@ -15,7 +16,8 @@ import (
var (
ErrClientIsNil = errors.New("client is nil")
base = schema.LBPath
keyPath = schema.KeyPath
reqPath = schema.ReqPath
)
type Client struct {
@ -29,42 +31,66 @@ func NewClient(c *apiclient.Client) (*Client, error) {
return &Client{c}, nil
}
func (c *Client) GetLoadBalancers(ctx context.Context) (lbs map[string]*schema.LoadBalancer, err error) {
return c.GetLoadBalancersWithCluster(ctx, "")
}
func (c *Client) GetLoadBalancersWithCluster(ctx context.Context, cluster string) (lbs map[string]*schema.LoadBalancer, err error) {
path := base
if cluster != "" {
path += "/" + cluster
}
func (c *Client) GetApprovals(ctx context.Context) (approvals schema.Approvals, err error) {
path := reqPath
req, err := c.c.NewRequest(ctx, "GET", path, nil)
if err != nil {
return
}
lbs = make(map[string]*schema.LoadBalancer)
_, err = c.c.Do(req, &lbs)
_, err = c.c.Do(req, &approvals)
return
}
func (c *Client) GetLoadBalancer(ctx context.Context, cluster, name string) (lb *schema.LoadBalancer, err error) {
path := fmt.Sprintf("%s/%s/%s", base, cluster, name)
func (c *Client) RejectApproval(ctx context.Context, id string) error {
return c.sendApproval(ctx, id, false)
}
func (c *Client) AcceptApproval(ctx context.Context, id string) error {
return c.sendApproval(ctx, id, true)
}
func (c *Client) sendApproval(ctx context.Context, id string, approve bool) (err error) {
path := fmt.Sprintf("%s/%s", reqPath, id)
method := "DELETE"
if approve {
method = "PUT"
}
req, err := c.c.NewRequest(ctx, method, path, nil)
if err != nil {
return
}
_, err = c.c.Do(req, nil)
return
}
func (c *Client) GetKeys(ctx context.Context) (keys schema.Keys, err error) {
path := keyPath
req, err := c.c.NewRequest(ctx, "GET", path, nil)
if err != nil {
return
}
_, err = c.c.Do(req, &lb)
_, err = c.c.Do(req, &keys)
return
}
func (c *Client) CreateLoadBalancer(ctx context.Context, cluster, name string, lb *schema.LoadBalancer) (err error) {
func (c *Client) GetKey(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.FollowRedirect(req, &key, time.Second*2)
return
}
func (c *Client) CreateKey(ctx context.Context, key schema.NewKey) (err error) {
buf := new(bytes.Buffer)
path := fmt.Sprintf("%s/%s/%s", base, cluster, name)
err = lb.ValidateClient()
path := keyPath
err = key.Validate()
if err != nil {
return
}
err = json.NewEncoder(buf).Encode(lb)
err = json.NewEncoder(buf).Encode(key)
if err != nil {
return
}
@ -76,21 +102,8 @@ func (c *Client) CreateLoadBalancer(ctx context.Context, cluster, name string, l
return
}
func (c *Client) DeleteLoadBalancer(ctx context.Context, cluster, name string) (err error) {
path := fmt.Sprintf("%s/%s/%s", base, cluster, name)
req, err := c.c.NewRequest(ctx, "DELETE", path, nil)
if err != nil {
return
}
_, err = c.c.Do(req, nil)
return
}
func (c *Client) DeleteLoadBalancersWithCluster(ctx context.Context, cluster string) (err error) {
if cluster == "" {
return errors.New("cluster value can not be empty")
}
path := base + "/" + cluster
func (c *Client) DeleteKey(ctx context.Context, id string) (err error) {
path := fmt.Sprintf("%s/%s", keyPath, id)
req, err := c.c.NewRequest(ctx, "DELETE", path, nil)
if err != nil {
return

View file

@ -54,17 +54,20 @@ type Key struct {
Created int64 `json:"created"`
Size int `json:"size"`
Encoding core.Encoding `json:"encoding"`
Key string `json:"key"`
Key string `json:"key,omitempty"`
}
type Keys []Key
func (Keys) Less(a, b Key) { return a.Name < b.Name }
func (k Keys) Sort() { slices.SortStableFunc(k, k.Less) }
func (Keys) Less(a, b Key) bool { return a.Name < b.Name }
func (k Keys) Sort() { slices.SortStableFunc(k, k.Less) }
func NewKeys(keys core.Keys) (k Keys) {
k = Keys(keys)
for _, key := range keys {
k = append(k, Key(key))
}
k.Sort()
return
}
type Approval struct {
@ -77,8 +80,8 @@ type Approval struct {
type Approvals []Approval
func (Approvals) Less(a, b Approval) { return a.Name < b.Name }
func (a Approvals) Sort() { slices.SortStableFunc(a, a.Less) }
func (Approvals) Less(a, b Approval) bool { return a.Name < b.Name }
func (a Approvals) Sort() { slices.SortStableFunc(a, a.Less) }
func NewApprovals(m types.ApprovalMap) (a Approvals) {
for k, v := range m {

View file

@ -9,6 +9,7 @@ import (
"git.giftfish.de/ston1th/keyctl/pkg/api/types"
"git.giftfish.de/ston1th/keyctl/pkg/api/v1/schema"
"git.giftfish.de/ston1th/keyctl/pkg/store"
)
func local(h types.CtxHandler) types.CtxHandler {
@ -53,7 +54,7 @@ func newHandler(ctx *types.Context) {
}
key, err := ctx.Data.DB.CreateKey(newKey.Name, newKey.Size, newKey.Encoding)
if err != nil {
ctx.Log.Error(err, "error writing loadbalancer", "cluster", cl, "name", n)
ctx.Log.Error(err, "error writing key", "name", newKey.Name)
ctx.Err(types.ErrISE)
return
}
@ -65,8 +66,11 @@ func keyHandler(ctx *types.Context) {
id := ctx.Var("id")
k, err := ctx.Data.DB.GetKey(id)
if err != nil {
//TODO error 404
ctx.Log.Error(err, "error reading key", "id", id)
if err == store.ErrKeyNotFound {
ctx.Err(types.ErrNotFound)
return
}
ctx.Err(types.ErrISE)
return
}
@ -74,7 +78,7 @@ func keyHandler(ctx *types.Context) {
case "GET":
aid := ctx.Var("aid")
if aid == "" {
aid, err = ctx.Data.Approvals.New(id, k.Name)
aid, err = ctx.Data.Approver.New(id, k.Name)
if err != nil {
ctx.Log.Error(err, "error creating approval", "id", id, "name", k.Name)
ctx.Err(types.ErrISE)
@ -83,14 +87,22 @@ func keyHandler(ctx *types.Context) {
ctx.Redirect(schema.ApproveURL(id, aid), http.StatusFound)
return
}
if !ctx.Data.Approvals.Approved(id, aid) {
s := ctx.Data.Approver.Status(id, aid)
switch s {
case types.Pending:
ctx.Redirect(schema.ApproveURL(id, aid), http.StatusFound)
return
case types.Rejected:
ctx.Err(types.ErrKeyRequestRejected)
return
}
k, err := ctx.Data.DB.GetKeyWithSecret(id)
if err != nil {
//TODO error 404
ctx.Log.Error(err, "error reading key", "id", id)
if err == store.ErrKeyNotFound {
ctx.Err(types.ErrNotFound)
return
}
ctx.Err(types.ErrISE)
return
}
@ -99,6 +111,10 @@ func keyHandler(ctx *types.Context) {
err = ctx.Data.DB.DeleteKey(id)
if err != nil {
ctx.Log.Error(err, "error deleting key", "id", id, "name", k.Name)
if err == store.ErrKeyNotFound {
ctx.Err(types.ErrNotFound)
return
}
ctx.Err(types.ErrISE)
return
}
@ -107,14 +123,13 @@ func keyHandler(ctx *types.Context) {
}
func reqListHandler(ctx *types.Context) {
l := ctx.Data.Approvals.List()
l := ctx.Data.Approver.List()
ctx.JSON(schema.NewApprovals(l))
}
func keyListHandler(ctx *types.Context) {
k, err := ctx.Data.DB.GetKeys()
if err != nil {
//TODO error 404
ctx.Log.Error(err, "error reading keys")
ctx.Err(types.ErrISE)
return
@ -131,6 +146,6 @@ func reqHandler(ctx *types.Context) {
case "DELETE":
status = types.Rejected
}
ctx.Data.Approvals.Update(id, status)
ctx.Data.Approver.Update(id, status)
ctx.OK()
}