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

View file

@ -91,8 +91,6 @@ type Configuration struct {
StartPubREQHello int StartPubREQHello int
// Publisher for asking central for public keys // Publisher for asking central for public keys
StartPubREQPublicKeysGet bool StartPubREQPublicKeysGet bool
// Subscriber for receiving reqests to get public keys registered on central
StartSubREQPublicKeysGet bool
// Subscriber for receiving updates of public keys from central // Subscriber for receiving updates of public keys from central
StartSubREQPublicKeysPut bool StartSubREQPublicKeysPut bool
// Start the central error logger. // Start the central error logger.
@ -170,7 +168,6 @@ type ConfigurationFromFile struct {
StartPubREQHello *int StartPubREQHello *int
StartPubREQPublicKeysGet *bool StartPubREQPublicKeysGet *bool
StartSubREQPublicKeysGet *bool
StartSubREQPublicKeysPut *bool StartSubREQPublicKeysPut *bool
StartSubREQErrorLog *bool StartSubREQErrorLog *bool
StartSubREQHello *bool StartSubREQHello *bool
@ -235,7 +232,6 @@ func newConfigurationDefaults() Configuration {
StartPubREQHello: 30, StartPubREQHello: 30,
StartPubREQPublicKeysGet: true, StartPubREQPublicKeysGet: true,
StartSubREQPublicKeysGet: false,
StartSubREQPublicKeysPut: true, StartSubREQPublicKeysPut: true,
StartSubREQErrorLog: false, StartSubREQErrorLog: false,
StartSubREQHello: true, StartSubREQHello: true,
@ -441,11 +437,6 @@ func checkConfigValues(cf ConfigurationFromFile) Configuration {
} else { } else {
conf.StartPubREQPublicKeysGet = *cf.StartPubREQPublicKeysGet conf.StartPubREQPublicKeysGet = *cf.StartPubREQPublicKeysGet
} }
if cf.StartSubREQPublicKeysGet == nil {
conf.StartSubREQPublicKeysGet = cd.StartSubREQPublicKeysGet
} else {
conf.StartSubREQPublicKeysGet = *cf.StartSubREQPublicKeysGet
}
if cf.StartSubREQPublicKeysPut == nil { if cf.StartSubREQPublicKeysPut == nil {
conf.StartSubREQPublicKeysPut = cd.StartSubREQPublicKeysPut conf.StartSubREQPublicKeysPut = cd.StartSubREQPublicKeysPut
} else { } 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.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.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.StartSubREQPublicKeysPut, "startSubREQPublicKeysPut", fc.StartSubREQPublicKeysPut, "true/false")
flag.BoolVar(&c.StartSubREQErrorLog, "startSubREQErrorLog", fc.StartSubREQErrorLog, "true/false") flag.BoolVar(&c.StartSubREQErrorLog, "startSubREQErrorLog", fc.StartSubREQErrorLog, "true/false")
flag.BoolVar(&c.StartSubREQHello, "startSubREQHello", fc.StartSubREQHello, "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) proc.startup.pubREQPublicKeysGet(proc)
} }
if proc.configuration.StartSubREQPublicKeysGet { if proc.configuration.IsCentralAuth {
proc.startup.subREQPublicKeysGet(proc) proc.startup.subREQPublicKeysGet(proc)
} }