Skip to content

Commit

Permalink
Refactored the Importer/Exporter. The no_json tag now disables Import…
Browse files Browse the repository at this point in the history
… and Export for all nodes.

Moved code shared by all Importers into the api package.
Added the missing Importer and Exporter for the qldb node.
  • Loading branch information
awgh committed Jan 16, 2021
1 parent 6da612f commit cc9a2e1
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 159 deletions.
38 changes: 38 additions & 0 deletions api/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,41 @@ type JSON interface {
// MarshalJSON : Serialize this type to JSON
MarshalJSON() (b []byte, e error)
}

// ImportExport - makes a node exportable/importable when activated by build tag
type ImportExport interface {
// Import node from JSON
Import(jsonConfig []byte) error
// Export node to JSON
Export() ([]byte, error)
}

// ExportedNode - Node Config structure for export
type ExportedNode struct {
ContentKey string
ContentType string
RoutingKey string
RoutingType string
Policies []Policy

Profiles []ProfilePrivB64
Channels []ChannelPrivB64
Peers []Peer
Contacts []Contact
Router Router
}

// ImportedNode - Node Config structure for import
type ImportedNode struct {
ContentKey string
ContentType string
RoutingKey string
RoutingType string
Policies []map[string]interface{}

Profiles []ProfilePrivB64
Channels []ChannelPrivB64
Peers []Peer
Contacts []Contact
Router map[string]interface{}
}
5 changes: 4 additions & 1 deletion api/no_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
package api

// JSON - does not include the JSON serializer
type JSON interface{}
type JSON interface{} // disabled

// ImportExport - makes a node exportable/importable when activated by build tag
type ImportExport interface{} // disabled
10 changes: 6 additions & 4 deletions api/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ type Node interface {
Out() chan Msg
// Events : Returns the Err channel of this node
Events() chan Event

ImportExport
}

// Contact : object that describes a contact (named public key)
Expand All @@ -134,8 +136,8 @@ type ChannelPriv struct {
Privkey bc.KeyPair
}

// ChannelPrivDB : object that describes a channel, database version (including private key)
type ChannelPrivDB struct {
// ChannelPrivB64 : object that describes a channel, database version (including private key)
type ChannelPrivB64 struct {
Name string `db:"name"`
Privkey string `db:"privkey"`
}
Expand All @@ -154,8 +156,8 @@ type ProfilePriv struct {
Privkey bc.KeyPair
}

// ProfilePrivDB : object that describes a profile, database version (including private key)
type ProfilePrivDB struct {
// ProfilePrivB64 : object that describes a profile, database version (including private key)
type ProfilePrivB64 struct {
Name string `db:"name"`
Enabled bool `db:"enabled"`
Privkey string `db:"privkey"`
Expand Down
22 changes: 11 additions & 11 deletions nodes/db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (node *Node) dbDeleteContact(name string) {

func (node *Node) dbGetChannelPrivKey(name string) (string, error) {
res := node.db.SQL().SelectFrom("channels").Where("name = ?", name)
var channel api.ChannelPrivDB
var channel api.ChannelPrivB64
if err := res.One(&channel); err != nil {
return "", err
}
Expand All @@ -82,7 +82,7 @@ func (node *Node) dbGetChannelPrivKey(name string) (string, error) {

func (node *Node) dbGetChannels() ([]api.Channel, error) {
res := node.db.SQL().SelectFrom("channels")
var channels []api.ChannelPrivDB
var channels []api.ChannelPrivB64
if err := res.All(&channels); err != nil {
return nil, err
}
Expand All @@ -100,7 +100,7 @@ func (node *Node) dbGetChannels() ([]api.Channel, error) {
func (node *Node) dbGetChannelsPriv() ([]api.ChannelPriv, error) {
col := node.db.Collection("channels")
res := col.Find()
var channels []api.ChannelPrivDB
var channels []api.ChannelPrivB64
if err := res.All(&channels); err != nil {
return nil, err
}
Expand Down Expand Up @@ -130,7 +130,7 @@ func (node *Node) dbAddChannel(name, privkey string) error {
if err := prv.FromB64(privkey); err != nil {
return err
}
channel := api.ChannelPrivDB{Name: name, Privkey: prv.ToB64()}
channel := api.ChannelPrivB64{Name: name, Privkey: prv.ToB64()}
_, err = col.Insert(&channel)
if err != nil {
events.Error(node, err.Error())
Expand All @@ -148,7 +148,7 @@ func (node *Node) dbDeleteChannel(name string) {

func (node *Node) dbGetProfile(name string) (*api.Profile, error) {
res := node.db.SQL().SelectFrom("profiles").Where("name = ?", name)
var profile api.ProfilePrivDB
var profile api.ProfilePrivB64
if err := res.One(&profile); err != nil {
return nil, err
}
Expand All @@ -162,7 +162,7 @@ func (node *Node) dbGetProfile(name string) (*api.Profile, error) {
func (node *Node) dbGetProfiles() ([]api.Profile, error) {
col := node.db.Collection("profiles")
res := col.Find()
var profiles []api.ProfilePrivDB
var profiles []api.ProfilePrivB64
if err := res.All(&profiles); err != nil {
return nil, err
}
Expand All @@ -177,10 +177,10 @@ func (node *Node) dbGetProfiles() ([]api.Profile, error) {
return retval, nil
}

func (node *Node) dbGetProfilesPriv() ([]api.ProfilePrivDB, error) {
func (node *Node) dbGetProfilesPriv() ([]api.ProfilePrivB64, error) {
col := node.db.Collection("profiles")
res := col.Find()
var profiles []api.ProfilePrivDB
var profiles []api.ProfilePrivB64
if err := res.All(&profiles); err != nil {
return nil, err
}
Expand All @@ -194,7 +194,7 @@ func (node *Node) dbAddProfilePriv(name string, enabled bool, b64key string) err
if err != nil {
return err
}
var profile api.ProfilePrivDB
var profile api.ProfilePrivB64
if count == 0 {
// insert new profile
profile.Name = name
Expand All @@ -220,7 +220,7 @@ func (node *Node) dbAddProfile(name string, enabled bool) error {
if err != nil {
return err
}
var profile api.ProfilePrivDB
var profile api.ProfilePrivB64
if count == 0 {
// generate new profile keypair
profileKey := node.contentKey.Clone()
Expand Down Expand Up @@ -251,7 +251,7 @@ func (node *Node) dbDeleteProfile(name string) {
func (node *Node) dbGetProfilePrivateKey(name string) string {
col := node.db.Collection("profiles")
res := col.Find(db.Cond{"name": name})
var profile api.ProfilePrivDB
var profile api.ProfilePrivB64
if err := res.One(&profile); err != nil {
return ""
}
Expand Down
51 changes: 4 additions & 47 deletions nodes/db/importer_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,14 @@ import (
"github.com/awgh/ratnet/api"
)

// ProfilePrivB64 - Private Key for a Profile in base64
type ProfilePrivB64 struct {
Name string
Privkey string // base64 encoded
Enabled bool
}

// ChannelPrivB64 - Private Key for a Channel in base64
type ChannelPrivB64 struct {
Name string
Privkey string // base64 encoded
}

// ExportedNode - Node Config structure for export
type ExportedNode struct {
ContentKey string
ContentType string
RoutingKey string
RoutingType string
Policies []api.Policy

Profiles []api.ProfilePrivDB
Channels []ChannelPrivB64
Peers []api.Peer
Contacts []api.Contact
Router api.Router
}

// ImportedNode - Node Config structure for import
type ImportedNode struct {
ContentKey string
ContentType string
RoutingKey string
RoutingType string
Policies []map[string]interface{}

Profiles []ProfilePrivB64
Channels []ChannelPrivB64
Peers []api.Peer
Contacts []api.Contact
Router map[string]interface{}
}

// Import : Load a node configuration from a JSON config
func (node *Node) Import(jsonConfig []byte) error {
restartNode := false
if node.IsRunning() {
node.Stop()
restartNode = true
}
var nj ImportedNode
var nj api.ImportedNode
if err := json.Unmarshal(jsonConfig, &nj); err != nil {
return err
}
Expand Down Expand Up @@ -132,7 +89,7 @@ func (node *Node) Import(jsonConfig []byte) error {

// Export : Save a node configuration to a JSON config
func (node *Node) Export() ([]byte, error) {
var nj ExportedNode
var nj api.ExportedNode
nj.ContentKey = node.contentKey.ToB64()
nj.ContentType = node.contentKey.GetName()
nj.RoutingKey = node.routingKey.ToB64()
Expand All @@ -154,7 +111,7 @@ func (node *Node) Export() ([]byte, error) {
return nil, err
}

nj.Channels = make([]ChannelPrivB64, len(channels))
nj.Channels = make([]api.ChannelPrivB64, len(channels))
i := 0
for _, v := range channels {
nj.Channels[i].Name = v.Name
Expand All @@ -168,7 +125,7 @@ func (node *Node) Export() ([]byte, error) {
nj.Contacts[i].Pubkey = v.Pubkey
i++
}
nj.Profiles = make([]api.ProfilePrivDB, len(profiles))
nj.Profiles = make([]api.ProfilePrivB64, len(profiles))
i = 0
for _, v := range profiles {
nj.Profiles[i].Name = v.Name
Expand Down
51 changes: 4 additions & 47 deletions nodes/fs/importer_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,14 @@ import (
"github.com/awgh/ratnet/api"
)

// ProfilePrivB64 - Private Key for a Profile in base64
type ProfilePrivB64 struct {
Name string
Privkey string // base64 encoded
Enabled bool
}

// ChannelPrivB64 - Private Key for a Channel in base64
type ChannelPrivB64 struct {
Name string
Privkey string // base64 encoded
}

// ExportedNode - Node Config structure for export
type ExportedNode struct {
ContentKey string
ContentType string
RoutingKey string
RoutingType string
Policies []api.Policy

Profiles []ProfilePrivB64
Channels []ChannelPrivB64
Peers []api.Peer
Contacts []api.Contact
Router api.Router
}

// ImportedNode - Node Config structure for import
type ImportedNode struct {
ContentKey string
ContentType string
RoutingKey string
RoutingType string
Policies []map[string]interface{}

Profiles []ProfilePrivB64
Channels []ChannelPrivB64
Peers []api.Peer
Contacts []api.Contact
Router map[string]interface{}
}

// Import : Load a node configuration from a JSON config
func (node *Node) Import(jsonConfig []byte) error {
restartNode := false
if node.IsRunning() {
node.Stop()
restartNode = true
}
var nj ImportedNode
var nj api.ImportedNode
if err := json.Unmarshal(jsonConfig, &nj); err != nil {
return err
}
Expand Down Expand Up @@ -125,12 +82,12 @@ func (node *Node) Import(jsonConfig []byte) error {

// Export : Save a node configuration to a JSON config
func (node *Node) Export() ([]byte, error) {
var nj ExportedNode
var nj api.ExportedNode
nj.ContentKey = node.contentKey.ToB64()
nj.ContentType = node.contentKey.GetName()
nj.RoutingKey = node.routingKey.ToB64()
nj.RoutingType = node.routingKey.GetName()
nj.Channels = make([]ChannelPrivB64, len(node.channels))
nj.Channels = make([]api.ChannelPrivB64, len(node.channels))
i := 0
for _, v := range node.channels {
nj.Channels[i].Name = v.Name
Expand All @@ -144,7 +101,7 @@ func (node *Node) Export() ([]byte, error) {
nj.Contacts[i].Pubkey = v.Pubkey
i++
}
nj.Profiles = make([]ProfilePrivB64, len(node.profiles))
nj.Profiles = make([]api.ProfilePrivB64, len(node.profiles))
i = 0
for _, v := range node.profiles {
nj.Profiles[i].Name = v.Name
Expand Down
Loading

0 comments on commit cc9a2e1

Please sign in to comment.