1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2024-12-14 12:37:31 +00:00

using flag isCentralAuth to start subREQPublicKeysGet, also handling the keys as a []byte

This commit is contained in:
postmannen 2022-04-07 14:18:28 +02:00
parent b669dc537c
commit eaf164c9d7
3 changed files with 26 additions and 35 deletions

View file

@ -1,6 +1,7 @@
package steward
import (
"bytes"
"fmt"
"log"
"os"
@ -13,21 +14,21 @@ import (
type signatureBase32 string
type argsString string
type centralAuth struct {
schema map[Node]map[argsString]signatureBase32
nodePublicKeys *nodePublicKeys
configuration *Configuration
db *bolt.DB
bucketPublicKeys string
errorKernel *errorKernel
// schema map[Node]map[argsString]signatureBase32
nodePublicKeys *nodePublicKeys
configuration *Configuration
db *bolt.DB
bucketNamePublicKeys string
errorKernel *errorKernel
}
func newCentralAuth(configuration *Configuration, errorKernel *errorKernel) *centralAuth {
c := centralAuth{
schema: make(map[Node]map[argsString]signatureBase32),
nodePublicKeys: newNodePublicKeys(configuration),
configuration: configuration,
bucketPublicKeys: "publicKeys",
errorKernel: errorKernel,
// schema: make(map[Node]map[argsString]signatureBase32),
nodePublicKeys: newNodePublicKeys(configuration),
configuration: configuration,
bucketNamePublicKeys: "publicKeys",
errorKernel: errorKernel,
}
databaseFilepath := filepath.Join(configuration.DatabaseFolder, "auth.db")
@ -66,14 +67,14 @@ func (c *centralAuth) addPublicKey(proc process, msg Message) {
// Check if a key for the current node already exists in the map.
existingKey, ok := c.nodePublicKeys.KeyMap[msg.FromNode]
if ok && existingKey == string(msg.Data) {
if ok && bytes.Equal(existingKey, msg.Data) {
fmt.Printf(" * key value for node %v is the same, doing nothing\n", msg.FromNode)
c.nodePublicKeys.mu.Unlock()
return
}
// New key
c.nodePublicKeys.KeyMap[msg.FromNode] = string(msg.Data)
c.nodePublicKeys.KeyMap[msg.FromNode] = msg.Data
c.nodePublicKeys.mu.Unlock()
// Add key to persistent storage.
@ -99,9 +100,9 @@ func (c *centralAuth) dbGetPublicKey(node string) ([]byte, error) {
// View is a help function to get values out of the database.
err := c.db.View(func(tx *bolt.Tx) error {
//Open a bucket to get key's and values from.
bu := tx.Bucket([]byte(c.bucketPublicKeys))
bu := tx.Bucket([]byte(c.bucketNamePublicKeys))
if bu == nil {
log.Printf("info: no db bucket exist: %v\n", c.bucketPublicKeys)
log.Printf("info: no db bucket exist: %v\n", c.bucketNamePublicKeys)
return nil
}
@ -123,7 +124,7 @@ func (c *centralAuth) dbGetPublicKey(node string) ([]byte, error) {
func (c *centralAuth) dbUpdatePublicKey(node string, value []byte) error {
err := c.db.Update(func(tx *bolt.Tx) error {
//Create a bucket
bu, err := tx.CreateBucketIfNotExists([]byte(c.bucketPublicKeys))
bu, err := tx.CreateBucketIfNotExists([]byte(c.bucketNamePublicKeys))
if err != nil {
return fmt.Errorf("error: CreateBuckerIfNotExists failed: %v", err)
}
@ -144,11 +145,11 @@ func (c *centralAuth) dbUpdatePublicKey(node string, value []byte) error {
// bucket if it exists.
func (c *centralAuth) dbDeletePublicKey(key string) error {
err := c.db.Update(func(tx *bolt.Tx) error {
bu := tx.Bucket([]byte(c.bucketPublicKeys))
bu := tx.Bucket([]byte(c.bucketNamePublicKeys))
err := bu.Delete([]byte(key))
if err != nil {
log.Printf("error: delete key in bucket %v failed: %v\n", c.bucketPublicKeys, err)
log.Printf("error: delete key in bucket %v failed: %v\n", c.bucketNamePublicKeys, err)
}
return nil
@ -159,18 +160,18 @@ func (c *centralAuth) dbDeletePublicKey(key string) error {
// dumpBucket will dump out all they keys and values in the
// specified bucket, and return a sorted []samDBValue
func (c *centralAuth) dbDumpPublicKey() (map[Node]string, error) {
m := make(map[Node]string)
func (c *centralAuth) dbDumpPublicKey() (map[Node][]byte, error) {
m := make(map[Node][]byte)
err := c.db.View(func(tx *bolt.Tx) error {
bu := tx.Bucket([]byte(c.bucketPublicKeys))
bu := tx.Bucket([]byte(c.bucketNamePublicKeys))
if bu == nil {
return fmt.Errorf("error: dumpBucket: tx.bucket returned nil")
}
// For each element found in the DB, print it.
bu.ForEach(func(k, v []byte) error {
m[Node(k)] = string(v)
m[Node(k)] = v
return nil
})
@ -188,13 +189,13 @@ func (c *centralAuth) dbDumpPublicKey() (map[Node]string, error) {
// The keys will be written to a k/v store for persistence.
type nodePublicKeys struct {
mu sync.Mutex
KeyMap map[Node]string
KeyMap map[Node][]byte
}
// newNodePublicKeys will return a prepared type of nodePublicKeys.
func newNodePublicKeys(configuration *Configuration) *nodePublicKeys {
n := nodePublicKeys{
KeyMap: make(map[Node]string),
KeyMap: make(map[Node][]byte),
}
return &n

View file

@ -91,8 +91,6 @@ type Configuration struct {
StartPubREQHello int
// Publisher for asking central for public keys
StartPubREQPublicKeysGet bool
// Subscriber for receiving reqests to get public keys registered on central
StartSubREQPublicKeysGet bool
// Subscriber for receiving updates of public keys from central
StartSubREQPublicKeysPut bool
// Start the central error logger.
@ -170,7 +168,6 @@ type ConfigurationFromFile struct {
StartPubREQHello *int
StartPubREQPublicKeysGet *bool
StartSubREQPublicKeysGet *bool
StartSubREQPublicKeysPut *bool
StartSubREQErrorLog *bool
StartSubREQHello *bool
@ -235,7 +232,6 @@ func newConfigurationDefaults() Configuration {
StartPubREQHello: 30,
StartPubREQPublicKeysGet: true,
StartSubREQPublicKeysGet: false,
StartSubREQPublicKeysPut: true,
StartSubREQErrorLog: false,
StartSubREQHello: true,
@ -441,11 +437,6 @@ func checkConfigValues(cf ConfigurationFromFile) Configuration {
} else {
conf.StartPubREQPublicKeysGet = *cf.StartPubREQPublicKeysGet
}
if cf.StartSubREQPublicKeysGet == nil {
conf.StartSubREQPublicKeysGet = cd.StartSubREQPublicKeysGet
} else {
conf.StartSubREQPublicKeysGet = *cf.StartSubREQPublicKeysGet
}
if cf.StartSubREQPublicKeysPut == nil {
conf.StartSubREQPublicKeysPut = cd.StartSubREQPublicKeysPut
} else {
@ -600,7 +591,6 @@ func (c *Configuration) CheckFlags() error {
flag.IntVar(&c.StartPubREQHello, "startPubREQHello", fc.StartPubREQHello, "Make the current node send hello messages to central at given interval in seconds")
flag.BoolVar(&c.StartPubREQPublicKeysGet, "startPubREQPublicKeysGet", fc.StartPubREQPublicKeysGet, "true/false")
flag.BoolVar(&c.StartSubREQPublicKeysGet, "startSubREQPublicKeysGet", fc.StartSubREQPublicKeysGet, "true/false")
flag.BoolVar(&c.StartSubREQPublicKeysPut, "startSubREQPublicKeysPut", fc.StartSubREQPublicKeysPut, "true/false")
flag.BoolVar(&c.StartSubREQErrorLog, "startSubREQErrorLog", fc.StartSubREQErrorLog, "true/false")
flag.BoolVar(&c.StartSubREQHello, "startSubREQHello", fc.StartSubREQHello, "true/false")

View file

@ -170,7 +170,7 @@ func (p *processes) Start(proc process) {
proc.startup.pubREQPublicKeysGet(proc)
}
if proc.configuration.StartSubREQPublicKeysGet {
if proc.configuration.IsCentralAuth {
proc.startup.subREQPublicKeysGet(proc)
}