mirror of
https://github.com/postmannen/ctrl.git
synced 2024-12-14 12:37:31 +00:00
put stuff related to central key handling under it's own keys struct type
This commit is contained in:
parent
e782b27b09
commit
797953a3d6
3 changed files with 36 additions and 23 deletions
|
@ -14,7 +14,21 @@ import (
|
|||
// centralAuth holds the logic related to handling public keys and auth maps.
|
||||
type centralAuth struct {
|
||||
// schema map[Node]map[argsString]signatureBase32
|
||||
authorization *authorization
|
||||
authorization *authorization
|
||||
keys *keys
|
||||
}
|
||||
|
||||
// newCentralAuth will return a new and prepared *centralAuth
|
||||
func newCentralAuth(configuration *Configuration, errorKernel *errorKernel) *centralAuth {
|
||||
c := centralAuth{
|
||||
authorization: newAuthorization(),
|
||||
keys: newKeys(configuration, errorKernel),
|
||||
}
|
||||
|
||||
return &c
|
||||
}
|
||||
|
||||
type keys struct {
|
||||
nodePublicKeys *nodePublicKeys
|
||||
nodeNotAckedPublicKeys *nodeNotAckedPublicKeys
|
||||
configuration *Configuration
|
||||
|
@ -23,10 +37,9 @@ type centralAuth struct {
|
|||
errorKernel *errorKernel
|
||||
}
|
||||
|
||||
// newCentralAuth will return a prepared *centralAuth with input values set.
|
||||
func newCentralAuth(configuration *Configuration, errorKernel *errorKernel) *centralAuth {
|
||||
c := centralAuth{
|
||||
authorization: newAuthorization(),
|
||||
// newKeys will return a prepared *keys with input values set.
|
||||
func newKeys(configuration *Configuration, errorKernel *errorKernel) *keys {
|
||||
c := keys{
|
||||
// schema: make(map[Node]map[argsString]signatureBase32),
|
||||
nodePublicKeys: newNodePublicKeys(configuration),
|
||||
nodeNotAckedPublicKeys: newNodeNotAckedPublicKeys(configuration),
|
||||
|
@ -64,7 +77,7 @@ func newCentralAuth(configuration *Configuration, errorKernel *errorKernel) *cen
|
|||
}
|
||||
|
||||
// addPublicKey to the db if the node do not exist, or if it is a new value.
|
||||
func (c *centralAuth) addPublicKey(proc process, msg Message) {
|
||||
func (c *keys) addPublicKey(proc process, msg Message) {
|
||||
|
||||
// TODO: When receiviving a new or different keys for a node we should
|
||||
// have a service with it's own storage for these keys, and an operator
|
||||
|
@ -156,7 +169,7 @@ func (c *centralAuth) addPublicKey(proc process, msg Message) {
|
|||
// }
|
||||
|
||||
//dbUpdatePublicKey will update the public key for a node in the db.
|
||||
func (c *centralAuth) dbUpdatePublicKey(node string, value []byte) error {
|
||||
func (c *keys) dbUpdatePublicKey(node string, value []byte) error {
|
||||
err := c.db.Update(func(tx *bolt.Tx) error {
|
||||
//Create a bucket
|
||||
bu, err := tx.CreateBucketIfNotExists([]byte(c.bucketNamePublicKeys))
|
||||
|
@ -195,7 +208,7 @@ func (c *centralAuth) dbUpdatePublicKey(node string, value []byte) 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][]byte, error) {
|
||||
func (c *keys) dbDumpPublicKey() (map[Node][]byte, error) {
|
||||
m := make(map[Node][]byte)
|
||||
|
||||
err := c.db.View(func(tx *bolt.Tx) error {
|
||||
|
|
|
@ -441,13 +441,13 @@ func (s startup) subREQHello(p process) {
|
|||
return nil
|
||||
}
|
||||
|
||||
s.centralAuth.addPublicKey(proc, m)
|
||||
s.centralAuth.keys.addPublicKey(proc, m)
|
||||
|
||||
// update the prometheus metrics
|
||||
|
||||
s.server.centralAuth.nodePublicKeys.mu.Lock()
|
||||
mapLen := len(s.server.centralAuth.nodePublicKeys.KeyMap)
|
||||
s.server.centralAuth.nodePublicKeys.mu.Unlock()
|
||||
s.server.centralAuth.keys.nodePublicKeys.mu.Lock()
|
||||
mapLen := len(s.server.centralAuth.keys.nodePublicKeys.KeyMap)
|
||||
s.server.centralAuth.keys.nodePublicKeys.mu.Unlock()
|
||||
s.metrics.promHelloNodesTotal.Set(float64(mapLen))
|
||||
s.metrics.promHelloNodesContactLast.With(prometheus.Labels{"nodeName": string(m.FromNode)}).SetToCurrentTime()
|
||||
|
||||
|
|
22
requests.go
22
requests.go
|
@ -2060,9 +2060,9 @@ func (m methodREQPublicKeysGet) handler(proc process, message Message, node stri
|
|||
case <-ctx.Done():
|
||||
// case out := <-outCh:
|
||||
case <-outCh:
|
||||
proc.centralAuth.nodePublicKeys.mu.Lock()
|
||||
b, err := json.Marshal(proc.centralAuth.nodePublicKeys.KeyMap)
|
||||
proc.centralAuth.nodePublicKeys.mu.Unlock()
|
||||
proc.centralAuth.keys.nodePublicKeys.mu.Lock()
|
||||
b, err := json.Marshal(proc.centralAuth.keys.nodePublicKeys.KeyMap)
|
||||
proc.centralAuth.keys.nodePublicKeys.mu.Unlock()
|
||||
if err != nil {
|
||||
er := fmt.Errorf("error: REQPublicKeysGet, failed to marshal keys map: %v", err)
|
||||
proc.errorKernel.errSend(proc, message, er)
|
||||
|
@ -2178,22 +2178,22 @@ func (m methodREQPublicKeysAllow) handler(proc process, message Message, node st
|
|||
select {
|
||||
case <-ctx.Done():
|
||||
case <-outCh:
|
||||
proc.centralAuth.nodeNotAckedPublicKeys.mu.Lock()
|
||||
defer proc.centralAuth.nodeNotAckedPublicKeys.mu.Unlock()
|
||||
proc.centralAuth.keys.nodeNotAckedPublicKeys.mu.Lock()
|
||||
defer proc.centralAuth.keys.nodeNotAckedPublicKeys.mu.Unlock()
|
||||
|
||||
for _, n := range message.MethodArgs {
|
||||
key, ok := proc.centralAuth.nodeNotAckedPublicKeys.KeyMap[Node(n)]
|
||||
key, ok := proc.centralAuth.keys.nodeNotAckedPublicKeys.KeyMap[Node(n)]
|
||||
if ok {
|
||||
// Store/update the node and public key on the allowed pubKey map.
|
||||
proc.centralAuth.nodePublicKeys.mu.Lock()
|
||||
proc.centralAuth.nodePublicKeys.KeyMap[Node(n)] = key
|
||||
proc.centralAuth.nodePublicKeys.mu.Unlock()
|
||||
proc.centralAuth.keys.nodePublicKeys.mu.Lock()
|
||||
proc.centralAuth.keys.nodePublicKeys.KeyMap[Node(n)] = key
|
||||
proc.centralAuth.keys.nodePublicKeys.mu.Unlock()
|
||||
|
||||
// Add key to persistent storage.
|
||||
proc.centralAuth.dbUpdatePublicKey(string(n), key)
|
||||
proc.centralAuth.keys.dbUpdatePublicKey(string(n), key)
|
||||
|
||||
// Delete the key from the NotAcked map
|
||||
delete(proc.centralAuth.nodeNotAckedPublicKeys.KeyMap, Node(n))
|
||||
delete(proc.centralAuth.keys.nodeNotAckedPublicKeys.KeyMap, Node(n))
|
||||
|
||||
er := fmt.Errorf("info: REQPublicKeysAllow : allowed new/updated public key for %v to allowed public key map", n)
|
||||
proc.errorKernel.infoSend(proc, message, er)
|
||||
|
|
Loading…
Reference in a new issue