Skip to content

Commit

Permalink
WIP: add keystore and backup module
Browse files Browse the repository at this point in the history
  • Loading branch information
gelleson committed Aug 11, 2021
1 parent 1b30d58 commit 90c5dc1
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/backup/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package backup

import "github.com/gelleson/packup/pkg/database"

type BackupService struct {
database *database.Database
}
74 changes: 74 additions & 0 deletions internal/backup/model.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,78 @@
package backup

import (
"github.com/gelleson/packup/pkg/compress"
"github.com/pkg/errors"
"gorm.io/gorm"
"time"
)

type SourceType string

const (
PostgresType SourceType = "postgres"
MySQLType SourceType = "mysql"
OracleType SourceType = "oracle"
SqliteType SourceType = "sqlite"
FileType SourceType = "file"
)

type ExecutionType string

func (et ExecutionType) Validate() error {

if et != OnceExecution && et != CronExecution {
return errors.New("execution type should be equal to cron or once")
}

return nil
}

const (
OnceExecution ExecutionType = "once"
CronExecution ExecutionType = "cron"
)

type Template string

const DefaultBackupTemplate Template = "{{ .Namespace }}/{{ .Name }}-{{ .Timestamp }}.{{ .Ext }}"

type Backup struct {
gorm.Model
Name string `validate:"required"`
Compress compress.Type `validate:"required"`
ExecutionType ExecutionType `validate:"required"`
ExecutionTime string `validate:"required"`
Tag string `validate:"required"`
Keystore string
Bucket string `validate:"required"`
BackupNameTemplate Template
Namespace string
Timezone string
LastExecutionTime time.Time
}

type Status string

const (
FailedStatus Status = "failed"
OkStatus Status = "ok"
)

type History struct {
gorm.Model
Status Status
Message string
Size uint
Tag string
AgentID uint
BackupID uint
ExecutedAt time.Time
}

type Pending struct {
gorm.Model
BackupID uint
Backup Backup
ExecutionTime time.Time
}
26 changes: 26 additions & 0 deletions internal/keystore/keystore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package keystore

import "github.com/gelleson/packup/pkg/database"

type Cipher string

type KeystoreService struct {
db *database.Database
cipherKey string
}

func (s KeystoreService) Get(key string) (Credential, error) {

cred := Credential{}

if tx := s.db.Conn().Where("key = ?", key).First(&cred); tx.Error != nil {
return Credential{}, tx.Error
}

return cred, nil
}

func (s KeystoreService) encrypt(c Credential) (Credential, error) {

return Credential{}, nil
}
20 changes: 20 additions & 0 deletions internal/keystore/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package keystore

import (
"github.com/gelleson/packup/pkg/validators"
"gorm.io/gorm"
)

type Credential struct {
gorm.DB
Key string `validate:"required"`
Username string
Password string
Host string
Token string
Database string
}

func (c Credential) Validate() error {
return validators.Struct(c)
}
1 change: 1 addition & 0 deletions pkg/cipher/cipher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package cipher
8 changes: 8 additions & 0 deletions pkg/cipher/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cipher

type Cipher string

const (
AESCipher Cipher = "AES"
RSACipher Cipher = "RSA"
)
47 changes: 47 additions & 0 deletions pkg/compress/compresser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package compress

import (
"compress/gzip"
"io"
"os"
)

type compressObject struct {
os.File
}

func (c compressObject) Close() error {

if err := c.File.Close(); err != nil {
return err
}

return os.Remove(c.Name())
}

func Compress(compressType Type, body io.Reader, path string) (io.ReadCloser, error) {

switch compressType {
case GzipType:
return gzipCompress(body)
}

return nil, nil
}

func gzipCompress(body io.Reader) (io.ReadCloser, error) {

object, err := gzip.NewReader(body)

if err != nil {
return nil, err
}

var compressedObject compressObject

if _, err := io.Copy(&compressedObject, object); err != nil {
return nil, err
}

return &compressedObject, nil
}
8 changes: 8 additions & 0 deletions pkg/compress/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package compress

type Type string

const (
GzipType Type = "gzip"
LZType Type = "lz"
)

0 comments on commit 90c5dc1

Please sign in to comment.