diff --git a/central_auth.go b/central_auth.go index 3fe9d62..e794d57 100644 --- a/central_auth.go +++ b/central_auth.go @@ -15,21 +15,21 @@ import ( type centralAuth struct { // schema map[Node]map[argsString]signatureBase32 authorization *authorization - keys *keys + pki *pki } // newCentralAuth will return a new and prepared *centralAuth func newCentralAuth(configuration *Configuration, errorKernel *errorKernel) *centralAuth { c := centralAuth{ authorization: newAuthorization(), - keys: newKeys(configuration, errorKernel), + pki: newPKI(configuration, errorKernel), } return &c } -type keys struct { - NodePublicKeys *nodePublicKeys +type pki struct { + nodeKeysAndHash *nodeKeysAndHash nodeNotAckedPublicKeys *nodeNotAckedPublicKeys configuration *Configuration db *bolt.DB @@ -38,10 +38,10 @@ type keys struct { } // newKeys will return a prepared *keys with input values set. -func newKeys(configuration *Configuration, errorKernel *errorKernel) *keys { - c := keys{ +func newPKI(configuration *Configuration, errorKernel *errorKernel) *pki { + p := pki{ // schema: make(map[Node]map[argsString]signatureBase32), - NodePublicKeys: newNodePublicKeys(configuration), + nodeKeysAndHash: newNodeKeysAndHash(configuration), nodeNotAckedPublicKeys: newNodeNotAckedPublicKeys(configuration), configuration: configuration, bucketNamePublicKeys: "publicKeys", @@ -57,27 +57,27 @@ func newKeys(configuration *Configuration, errorKernel *errorKernel) *keys { os.Exit(1) } - c.db = db + p.db = db // Get public keys from db storage. - keys, err := c.dbDumpPublicKey() + keys, err := p.dbDumpPublicKey() if err != nil { log.Printf("debug: dbPublicKeyDump failed, probably empty db: %v\n", err) } // Only assign from storage to in memory map if the storage contained any values. if keys != nil { - c.NodePublicKeys.KeyMap = keys + p.nodeKeysAndHash.KeyMap = keys for k, v := range keys { log.Printf("info: public keys db contains: %v, %v\n", k, []byte(v)) } } - return &c + return &p } // addPublicKey to the db if the node do not exist, or if it is a new value. -func (c *keys) addPublicKey(proc process, msg Message) { +func (p *pki) 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 @@ -90,32 +90,32 @@ func (c *keys) addPublicKey(proc process, msg Message) { // key for a host. // Check if a key for the current node already exists in the map. - c.NodePublicKeys.mu.Lock() - existingKey, ok := c.NodePublicKeys.KeyMap[msg.FromNode] - c.NodePublicKeys.mu.Unlock() + p.nodeKeysAndHash.mu.Lock() + existingKey, ok := p.nodeKeysAndHash.KeyMap[msg.FromNode] + p.nodeKeysAndHash.mu.Unlock() if ok && bytes.Equal(existingKey, msg.Data) { fmt.Printf(" * \nkey value for REGISTERED node %v is the same, doing nothing\n\n", msg.FromNode) return } - c.nodeNotAckedPublicKeys.mu.Lock() - existingNotAckedKey, ok := c.nodeNotAckedPublicKeys.KeyMap[msg.FromNode] + p.nodeNotAckedPublicKeys.mu.Lock() + existingNotAckedKey, ok := p.nodeNotAckedPublicKeys.KeyMap[msg.FromNode] // We only want to send one notification to the error kernel about new key detection, // so we check if the values are the same as the one we already got before we continue // with registering and logging for the the new key. if ok && bytes.Equal(existingNotAckedKey, msg.Data) { fmt.Printf(" * \nkey value for NOT-REGISTERED node %v is the same, doing nothing\n\n", msg.FromNode) - c.nodeNotAckedPublicKeys.mu.Unlock() + p.nodeNotAckedPublicKeys.mu.Unlock() return } - c.nodeNotAckedPublicKeys.KeyMap[msg.FromNode] = msg.Data - c.nodeNotAckedPublicKeys.mu.Unlock() + p.nodeNotAckedPublicKeys.KeyMap[msg.FromNode] = msg.Data + p.nodeNotAckedPublicKeys.mu.Unlock() er := fmt.Errorf("info: detected new public key for node: %v. This key will need to be authorized by operator to be allowed into the system", msg.FromNode) fmt.Printf(" * %v\n", er) - c.errorKernel.infoSend(proc, msg, er) + p.errorKernel.infoSend(proc, msg, er) } // // dbGetPublicKey will look up and return a specific value if it exists for a key in a bucket in a DB. @@ -145,10 +145,10 @@ func (c *keys) addPublicKey(proc process, msg Message) { // } //dbUpdatePublicKey will update the public key for a node in the db. -func (c *keys) dbUpdatePublicKey(node string, value []byte) error { - err := c.db.Update(func(tx *bolt.Tx) error { +func (p *pki) dbUpdatePublicKey(node string, value []byte) error { + err := p.db.Update(func(tx *bolt.Tx) error { //Create a bucket - bu, err := tx.CreateBucketIfNotExists([]byte(c.bucketNamePublicKeys)) + bu, err := tx.CreateBucketIfNotExists([]byte(p.bucketNamePublicKeys)) if err != nil { return fmt.Errorf("error: CreateBuckerIfNotExists failed: %v", err) } @@ -184,11 +184,11 @@ func (c *keys) 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 *keys) dbDumpPublicKey() (map[Node][]byte, error) { +func (p *pki) dbDumpPublicKey() (map[Node][]byte, error) { m := make(map[Node][]byte) - err := c.db.View(func(tx *bolt.Tx) error { - bu := tx.Bucket([]byte(c.bucketNamePublicKeys)) + err := p.db.View(func(tx *bolt.Tx) error { + bu := tx.Bucket([]byte(p.bucketNamePublicKeys)) if bu == nil { return fmt.Errorf("error: dumpBucket: tx.bucket returned nil") } @@ -209,9 +209,9 @@ func (c *keys) dbDumpPublicKey() (map[Node][]byte, error) { return m, nil } -// nodePublicKeys holds all the gathered public keys of nodes in the system. +// nodeKeysAndHash holds all the gathered public keys of nodes in the system. // The keys will be written to a k/v store for persistence. -type nodePublicKeys struct { +type nodeKeysAndHash struct { mu sync.RWMutex KeyMap map[Node][]byte // TODO TOMOROW: implement sorting of KeyMap, @@ -221,9 +221,9 @@ type nodePublicKeys struct { Hash [32]byte } -// newNodePublicKeys will return a prepared type of nodePublicKeys. -func newNodePublicKeys(configuration *Configuration) *nodePublicKeys { - n := nodePublicKeys{ +// newNnodeKeysAndHash will return a prepared type of nodeKeysAndHash. +func newNodeKeysAndHash(configuration *Configuration) *nodeKeysAndHash { + n := nodeKeysAndHash{ KeyMap: make(map[Node][]byte), } diff --git a/processes.go b/processes.go index 8b01f5b..d5b84c1 100644 --- a/processes.go +++ b/processes.go @@ -445,13 +445,13 @@ func (s startup) subREQHello(p process) { return nil } - s.centralAuth.keys.addPublicKey(proc, m) + s.centralAuth.pki.addPublicKey(proc, m) // update the prometheus metrics - s.server.centralAuth.keys.NodePublicKeys.mu.Lock() - mapLen := len(s.server.centralAuth.keys.NodePublicKeys.KeyMap) - s.server.centralAuth.keys.NodePublicKeys.mu.Unlock() + s.server.centralAuth.pki.nodeKeysAndHash.mu.Lock() + mapLen := len(s.server.centralAuth.pki.nodeKeysAndHash.KeyMap) + s.server.centralAuth.pki.nodeKeysAndHash.mu.Unlock() s.metrics.promHelloNodesTotal.Set(float64(mapLen)) s.metrics.promHelloNodesContactLast.With(prometheus.Labels{"nodeName": string(m.FromNode)}).SetToCurrentTime() diff --git a/requests.go b/requests.go index f85bfd2..d360810 100644 --- a/requests.go +++ b/requests.go @@ -2064,14 +2064,14 @@ func (m methodREQPublicKeysGet) handler(proc process, message Message, node stri case <-ctx.Done(): // case out := <-outCh: case <-outCh: - proc.centralAuth.keys.NodePublicKeys.mu.Lock() + proc.centralAuth.pki.nodeKeysAndHash.mu.Lock() // TODO: We should probably create a hash of the current map content, // store it alongside the KeyMap, and send both the KeyMap and hash // back. We can then later send that hash when asking for keys, compare // it with the current one for the KeyMap, and know if we need to send // and update back to the node who published the request to here. - b, err := json.Marshal(proc.centralAuth.keys.NodePublicKeys.KeyMap) - proc.centralAuth.keys.NodePublicKeys.mu.Unlock() + b, err := json.Marshal(proc.centralAuth.pki.nodeKeysAndHash.KeyMap) + proc.centralAuth.pki.nodeKeysAndHash.mu.Unlock() if err != nil { er := fmt.Errorf("error: REQPublicKeysGet, failed to marshal keys map: %v", err) proc.errorKernel.errSend(proc, message, er) @@ -2193,28 +2193,28 @@ func (m methodREQPublicKeysAllow) handler(proc process, message Message, node st select { case <-ctx.Done(): case <-outCh: - proc.centralAuth.keys.nodeNotAckedPublicKeys.mu.Lock() - defer proc.centralAuth.keys.nodeNotAckedPublicKeys.mu.Unlock() + proc.centralAuth.pki.nodeNotAckedPublicKeys.mu.Lock() + defer proc.centralAuth.pki.nodeNotAckedPublicKeys.mu.Unlock() // Range over all the MethodArgs, where each element represents a node to allow, // and move the node from the notAcked map to the allowed map. for _, n := range message.MethodArgs { - key, ok := proc.centralAuth.keys.nodeNotAckedPublicKeys.KeyMap[Node(n)] + key, ok := proc.centralAuth.pki.nodeNotAckedPublicKeys.KeyMap[Node(n)] if ok { func() { - proc.centralAuth.keys.NodePublicKeys.mu.Lock() - defer proc.centralAuth.keys.NodePublicKeys.mu.Unlock() + proc.centralAuth.pki.nodeKeysAndHash.mu.Lock() + defer proc.centralAuth.pki.nodeKeysAndHash.mu.Unlock() // Store/update the node and public key on the allowed pubKey map. - proc.centralAuth.keys.NodePublicKeys.KeyMap[Node(n)] = key + proc.centralAuth.pki.nodeKeysAndHash.KeyMap[Node(n)] = key }() // Add key to persistent storage. - proc.centralAuth.keys.dbUpdatePublicKey(string(n), key) + proc.centralAuth.pki.dbUpdatePublicKey(string(n), key) // Delete the key from the NotAcked map - delete(proc.centralAuth.keys.nodeNotAckedPublicKeys.KeyMap, Node(n)) + delete(proc.centralAuth.pki.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) @@ -2224,8 +2224,8 @@ func (m methodREQPublicKeysAllow) handler(proc process, message Message, node st // All new elements are now added, and we can create a new hash // representing the current keys in the allowed map. func() { - proc.centralAuth.keys.NodePublicKeys.mu.Lock() - defer proc.centralAuth.keys.NodePublicKeys.mu.Unlock() + proc.centralAuth.pki.nodeKeysAndHash.mu.Lock() + defer proc.centralAuth.pki.nodeKeysAndHash.mu.Unlock() type NodesAndKeys struct { Node Node @@ -2236,7 +2236,7 @@ func (m methodREQPublicKeysAllow) handler(proc process, message Message, node st sortedNodesAndKeys := []NodesAndKeys{} // Range the map, and add each k/v to the sorted slice, to be sorted later. - for k, v := range proc.centralAuth.keys.NodePublicKeys.KeyMap { + for k, v := range proc.centralAuth.pki.nodeKeysAndHash.KeyMap { nk := NodesAndKeys{ Node: k, Key: v, @@ -2262,7 +2262,7 @@ func (m methodREQPublicKeysAllow) handler(proc process, message Message, node st return } - proc.centralAuth.keys.NodePublicKeys.Hash = sha256.Sum256(b) + proc.centralAuth.pki.nodeKeysAndHash.Hash = sha256.Sum256(b) }()