Skip to content

Commit

Permalink
implemented keystore
Browse files Browse the repository at this point in the history
  • Loading branch information
JojiiOfficial committed Apr 7, 2020
1 parent 6839c10 commit 1f9c55a
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 12 deletions.
112 changes: 112 additions & 0 deletions Keystore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package libdatamanager

import (
"errors"
"io/ioutil"
"path/filepath"

"github.com/jinzhu/gorm"
)

const (
// KeystoreDBFile the sqlite DB containing the file-key associations
KeystoreDBFile = ".keys.db"

KeyringService = "DataManagerCLI-keystore"
)

var (
// ErrKeyUnavailable if keystore key is unavailable
ErrKeyUnavailable = errors.New("keyring key is unavailable")
)

// KeystoreFile the keystore row
type KeystoreFile struct {
gorm.Model
FileID uint
Key string
}

// Keystore a place to store keys
type Keystore struct {
Path string
DB *gorm.DB
}

// NewKeystore create a new keystore
func NewKeystore(path string) *Keystore {
return &Keystore{
Path: path,
}
}

// GetKeyFilepath returns the full path of file
func (store *Keystore) GetKeystoreFile(file string) string {
return filepath.Join(store.Path, file)
}

// GetKeystoreDataFile returns the keystore db filepath
func (store *Keystore) GetKeystoreDataFile() string {
return store.GetKeystoreFile(KeystoreDBFile)
}

// Open opens the keystore
func (store *Keystore) Open() error {
// Open DB into memory
var err error
store.DB, err = gorm.Open("sqlite3", store.GetKeystoreDataFile())
if err != nil {
return err
}

// Migrate DB
err = store.DB.AutoMigrate(&KeystoreFile{}).Error

return err
}

// AddKey Inserts key into keystore
func (store *Keystore) AddKey(fileID uint, keyPath string) error {
_, keyFile := filepath.Split(keyPath)
return store.DB.Create(&KeystoreFile{
FileID: fileID,
Key: keyFile,
}).Error
}

// DeleteKey Inserts key into keystore
func (store *Keystore) DeleteKey(fileID uint) error {
return store.DB.Unscoped().Where("file_id=?", fileID).Delete(&KeystoreFile{}).Error
}

// GetKeyFile returns a keyfile with assigned to the fileID
func (store *Keystore) GetKeyFile(fileID uint) (*KeystoreFile, error) {
var storeFile KeystoreFile

// Find in db
err := store.DB.Model(&KeystoreFile{}).Where("file_id=?", fileID).Find(&storeFile).Error
if err != nil {
return nil, err
}

return &storeFile, nil
}

func (store *Keystore) GetKey(fileID uint) ([]byte, error) {
// Get DB filekey
storefile, err := store.GetKeyFile(fileID)
if err != nil {
return nil, err
}

// Read keyfile
return ioutil.ReadFile(store.GetKeystoreFile(storefile.Key))
}

// Close closes the keystore
func (store *Keystore) Close() {
if store.DB == nil {
return
}
store.DB.Close()
}
2 changes: 2 additions & 0 deletions Response.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
HeaderEncryption string = "X-Encryption"
// HeaderContentLength request content length
HeaderContentLength string = "ContentLength"
// HeaderFileID fileid header
HeaderFileID string = "X-FileID"
)

// LoginResponse response for login
Expand Down
35 changes: 33 additions & 2 deletions config/Config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"errors"
"fmt"
"log"
"net/url"
Expand Down Expand Up @@ -30,6 +31,13 @@ const (
KeyringServiceName = "DataManagerCLI"
)

var (
// ErrUnlockingKeyring error if keyring is available but can't be unlocked
ErrUnlockingKeyring = errors.New("Error unlocking keyring")
// ErrKeystoreNoDir error if keystore is no directory
ErrKeystoreNoDir = errors.New("Keystore is not a directory")
)

// Config Configuration structure
type Config struct {
File string
Expand Down Expand Up @@ -58,6 +66,7 @@ type clientConfig struct {
MinFilesToDisplay uint16 `required:"true"`
AutoFilePreview bool
TrimNameAfter int
KeyStoreDir string
Defaults clientDefaults
}

Expand Down Expand Up @@ -90,7 +99,7 @@ func getDefaultConfig() Config {
},
Client: clientConfig{
MinFilesToDisplay: 100,
AutoFilePreview: true,
AutoFilePreview: false,
Defaults: clientDefaults{
DefaultDetails: 0,
DefaultOrder: "created/r",
Expand Down Expand Up @@ -340,7 +349,8 @@ func (config Config) IsDefault() bool {
return config.Client == defaultConfig.Client &&
config.User == defaultConfig.User &&
config.Server.IgnoreCert == defaultConfig.Server.IgnoreCert &&
config.Server.AlternativeURL == config.Server.AlternativeURL
config.Server.AlternativeURL == config.Server.AlternativeURL &&
config.Client.KeyStoreDir == defaultConfig.Client.KeyStoreDir
}

// MustGetRequestConfig create a libdm requestconfig from given cli client config and fatal on error
Expand Down Expand Up @@ -376,6 +386,27 @@ func (config Config) ToRequestConfig() (*libdatamanager.RequestConfig, error) {
}, nil
}

// KeystoreEnabled return true if user wants to save keyfiles
// in a specified directory
func (config *Config) KeystoreEnabled() bool {
return len(config.Client.KeyStoreDir) > 0
}

// KeystoreDirValid return true if keystore is valid
func (config *Config) KeystoreDirValid() error {
s, err := os.Stat(config.Client.KeyStoreDir)
if err != nil {
return err
}

// KeyStoreDir must be a directory
if !s.IsDir() {
return ErrKeystoreNoDir
}

return nil
}

// GenMachineID detect the machineID.
// If not detected return random string
func GenMachineID() string {
Expand Down
10 changes: 0 additions & 10 deletions config/Errors.go

This file was deleted.

5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module github.com/DataManager-Go/libdatamanager

go 1.14

require (
github.com/jinzhu/gorm v1.9.12
google.golang.org/appengine v1.6.5 // indirect
)
35 changes: 35 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd h1:83Wprp6ROGeiHFAP8WJdI2RoxALQYgdllERc3N5N2DM=
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/jinzhu/gorm v1.9.12 h1:Drgk1clyWT9t9ERbzHza6Mj/8FY/CqMyVzOiHviMo6Q=
github.com/jinzhu/gorm v1.9.12/go.mod h1:vhTjlKSJUTWNtcbQtrMBFCxy7eXTzeCAzfL5fBZT/Qs=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M=
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4=
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw=
github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd h1:GGJVjV8waZKRHrgwvtH66z9ZGVurTD1MT0n1Bb+q4aM=
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM=
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=

0 comments on commit 1f9c55a

Please sign in to comment.