Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ManifestAPI+SchemaValidate+SecretKV #1

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions api/fs/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package fs

import (
"io/fs"
"os"
"path/filepath"
)

func DiscoverFiles(paths []string, ext string) ([]string, error) {
var files []string

for _, path := range paths {
pathFiles, err := listFiles(path, ext)
if err != nil {
return nil, err
}
files = append(files, pathFiles...)
}

return files, nil
}

func listFiles(path string, ext string) ([]string, error) {
var files []string
err := filepath.WalkDir(path, func(file string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

match, err := filepath.Match(ext, filepath.Ext(d.Name()))
if err != nil {
return err
}
if match {
files = append(files, file)
}

return nil
})
if err != nil {
return nil, err
}
return files, nil
}

func WriteFile(file string, bytes []byte) error {
err := os.WriteFile("/tmp/dat1", bytes, 0644)
if err != nil {
return err
}
return nil
}
86 changes: 86 additions & 0 deletions api/kv/encrypted.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package kv

import (
"dsl/api/security"
"errors"
)

type EncryptedKV struct {
kv KV
encryption security.Encryption
}

func NewEncryptedKV(kv KV, encryption security.Encryption) *EncryptedKV {
return &EncryptedKV{
kv: kv,
encryption: encryption,
}
}

func (v *EncryptedKV) Get(key string) (string, error) {
encrypted, err := v.kv.Get(key)
if errors.Is(err, KVKeyNotExistsError{}) {
return "", NewKVKeyNotExistsError(key)
} else if err != nil {
return "", err
}

decrypted, err := v.Decrypt([]byte(encrypted))
if err != nil {
return "", err
}
return string(decrypted), nil
}

func (v *EncryptedKV) Set(key, value string) error {
var encrypted = []byte("")
var err error

if value != "" {
// validation hook isAgeArmor
// NewAlreadyEncryptedError
if encrypted, err = v.Encrypt([]byte(value)); err != nil {
return err
}
}

if err = v.kv.Set(key, string(encrypted)); err != nil {
return err
}
return nil
}

func (v *EncryptedKV) GetBatch(keys []string) (KV, error) {
kv, err := v.kv.GetBatch(keys)
if err != nil {
return nil, err
}
return kv, nil
}

func (v *EncryptedKV) SetBatch(kv KV) error {
if err := v.kv.SetBatch(kv); err != nil {
return err
}
return nil
}

func (v *EncryptedKV) GetKeys() []string {
return v.GetKeys()
}

func (v *EncryptedKV) Encrypt(data []byte) ([]byte, error) {
encrypted, err := v.encryption.Encrypt(data)
if err != nil {
return nil, err
}
return encrypted, nil
}

func (v *EncryptedKV) Decrypt(data []byte) ([]byte, error) {
decrypted, err := v.encryption.Decrypt(data)
if err != nil {
return nil, err
}
return decrypted, nil
}
35 changes: 35 additions & 0 deletions api/kv/kv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package kv

import (
"dsl/api/security"
"fmt"
)

type KV interface {
Get(key string) (string, error)
Set(key, value string) error
GetBatch(keys []string) (KV, error)
SetBatch(kv KV) error
GetKeys() []string
}

type SealedKV interface {
KV
security.Encryption
}

// Defenition of kv errors
// KVKeyNotExistsError
type KVKeyNotExistsError struct {
key string
}

func NewKVKeyNotExistsError(key string) KVKeyNotExistsError {
return KVKeyNotExistsError{
key: key,
}
}

func (e KVKeyNotExistsError) Error() string {
return fmt.Sprintf("kv key '%s' not exists", e.key)
}
95 changes: 95 additions & 0 deletions api/kv/plain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package kv

import (
"fmt"
)

type PlainKV struct {
kv map[string]string
AllowOverride bool
}

type Opts func(kv *PlainKV)

func OptOverride(kv *PlainKV) {
kv.AllowOverride = true
}

func DefaultOpts(kv *PlainKV) {
kv.kv = make(map[string]string)
kv.AllowOverride = false
}

func NewPlainKV(opts ...Opts) *PlainKV {
var kv *PlainKV
DefaultOpts(kv)
for _, setOpt := range opts {
setOpt(kv)
}
return kv
}

func PlainKVFromMap(fromMap map[string]string, opts ...Opts) KV {
kv := &PlainKV{kv: fromMap}
for _, setOpt := range opts {
setOpt(kv)
}
return kv
}

func (p *PlainKV) Get(key string) (string, error) {
kv, exists := p.kv[key]
if !exists {
return "", NewKVKeyNotExistsError(key)
}
return kv, nil
}

func (p *PlainKV) Set(key string, value string) error {
if value == "" {
delete(p.kv, key)
}
if valueKV, exists := p.kv[key]; exists && valueKV != value && !p.AllowOverride {
return fmt.Errorf("key '%s' in KV with defferent value assignment found. Overriding prohibited by opt", key)
}
p.kv[key] = value
return nil
}

func (p *PlainKV) GetBatch(keys []string) (KV, error) {
var kv PlainKV
for _, key := range keys {
val, err := p.Get(key)
if err != nil {
return nil, err
}
if err := kv.Set(key, val); err != nil {
return nil, err
}
}
return &kv, nil
}

func (p *PlainKV) SetBatch(kv KV) error {
for _, key := range kv.GetKeys() {
val, err := kv.Get(key)
if err != nil {
return err
}
if err := p.Set(key, val); err != nil {
return err
}
}

return nil
}

func (p *PlainKV) GetKeys() []string {
keys := make([]string, len(p.kv))
i := 0
for key := range p.kv {
keys[i] = key
i++
}
return keys
}
7 changes: 7 additions & 0 deletions api/launchr/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package launchr

const (
CmdOutDefault = true
CmdSilentDefault = false
CmdManifestExtDefault = ".yaml"
)
Loading