added shamir keys

This commit is contained in:
ston1th 2023-03-20 21:18:44 +01:00
commit be0f532628
14 changed files with 238 additions and 43 deletions

View file

@ -83,14 +83,14 @@ func (c *Client) GetKey(ctx context.Context, id string) (key schema.Key, err err
return
}
func (c *Client) CreateKey(ctx context.Context, key schema.NewKey) (err error) {
func (c *Client) CreateKey(ctx context.Context, newKey schema.NewKey) (key schema.Key, err error) {
buf := new(bytes.Buffer)
path := keyPath
err = key.Validate()
err = newKey.Validate()
if err != nil {
return
}
err = json.NewEncoder(buf).Encode(key)
err = json.NewEncoder(buf).Encode(newKey)
if err != nil {
return
}
@ -98,7 +98,7 @@ func (c *Client) CreateKey(ctx context.Context, key schema.NewKey) (err error) {
if err != nil {
return
}
_, err = c.c.Do(req, nil)
_, err = c.c.Do(req, &key)
return
}

View file

@ -3,7 +3,10 @@
package schema
import (
"encoding/hex"
"errors"
"fmt"
"time"
"git.giftfish.de/ston1th/keyctl/pkg/api/types"
"git.giftfish.de/ston1th/keyctl/pkg/core"
@ -22,6 +25,7 @@ type NewKey struct {
Name string `json:"name"`
Size int `json:"size"`
Encoding core.Encoding `json:"encoding"`
Type core.Type `json:"type"`
}
var (
@ -58,13 +62,52 @@ type Key struct {
Created int64 `json:"created"`
Size int `json:"size"`
Encoding core.Encoding `json:"encoding"`
Type core.Type `json:"type"`
Key string `json:"key,omitempty"`
}
func (k Key) Print(share string) (string, error) {
switch k.Type {
case core.Shamir:
part1, err := k.Encoding.Decode(k.Key)
if err != nil {
return "", err
}
part2, err := hex.DecodeString(share)
if err != nil {
return "", err
}
dec, err := k.Type.Decode(append(part1, part2...))
if err != nil {
return "", err
}
return k.Encoding.EncodeToString(dec), nil
}
return k.Key, nil
}
type Keys []Key
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"
}
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",
v.ID,
v.Name,
timeFmt(v.Created),
v.Size,
v.Encoding,
v.Type,
)
}
return vals
}
func NewKeys(keys core.Keys) (k Keys) {
for _, key := range keys {
@ -87,6 +130,24 @@ type Approvals []Approval
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"
}
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",
v.ID,
v.AID,
v.Name,
v.Status,
timeFmt(v.Created),
v.IP,
)
}
return vals
}
func NewApprovals(m types.ApprovalMap) (a Approvals) {
for k, v := range m {
@ -102,3 +163,7 @@ func NewApprovals(m types.ApprovalMap) (a Approvals) {
a.Sort()
return
}
func timeFmt(t int64) string {
return time.Unix(t, 0).Format(time.RFC3339)
}

View file

@ -56,7 +56,7 @@ func newHandler(ctx *types.Context) {
ctx.Err(types.ErrInvalid)
return
}
key, err := ctx.Data.DB.CreateKey(newKey.Name, newKey.Size, newKey.Encoding)
key, err := ctx.Data.DB.CreateKey(newKey.Name, newKey.Size, newKey.Encoding, newKey.Type)
if err != nil {
ctx.Log.Error(err, "error writing key", "name", newKey.Name)
ctx.Err(types.ErrISE)