diff --git a/requests_acl.go b/requests_acl.go index 58961b0..57615d5 100644 --- a/requests_acl.go +++ b/requests_acl.go @@ -1,9 +1,79 @@ package steward import ( + "bytes" + "encoding/json" "fmt" ) +// ---- + +type methodREQAclRequestUpdate struct { + event Event +} + +func (m methodREQAclRequestUpdate) getKind() Event { + return m.event +} + +// Handler to get all acl's from a central server. +func (m methodREQAclRequestUpdate) handler(proc process, message Message, node string) ([]byte, error) { + // Get a context with the timeout specified in message.MethodTimeout. + ctx, _ := getContextForMethodTimeout(proc.ctx, message) + + proc.processes.wg.Add(1) + go func() { + defer proc.processes.wg.Done() + outCh := make(chan []byte) + + go func() { + + select { + case <-ctx.Done(): + // Done + case outCh <- []byte{}: + } + }() + + select { + case <-ctx.Done(): + // case out := <-outCh: + case <-outCh: + // Using a func here to set the scope of the lock, and then be able to + // defer the unlock when leaving that scope. + func() { + proc.centralAuth.pki.nodesAcked.mu.Lock() + defer proc.centralAuth.pki.nodesAcked.mu.Unlock() + + fmt.Printf(" <---- methodREQKeysRequestUpdate: received acl hash from NODE=%v, HASH=%v\n", message.FromNode, message.Data) + + // Check if the received hash is the same as the one currently active, + // TODO: Replace this with checking the ACL hash for the node. + if bytes.Equal(proc.centralAuth.pki.nodesAcked.keysAndHash.Hash[:], message.Data) { + fmt.Printf("\n ------------ NODE AND CENTRAL ARE EQUAL, NOTHING TO DO, EXITING HANDLER\n\n") + return + } + + fmt.Printf("\n ------------ NODE AND CENTRAL WERE NOT EQUAL, PREPARING TO SEND NEW VERSION OF KEYS\n\n") + + fmt.Printf(" * methodREQKeysRequestUpdate: marshalling new keys and hash to send: map=%v, hash=%v\n\n", proc.centralAuth.pki.nodesAcked.keysAndHash.Keys, proc.centralAuth.pki.nodesAcked.keysAndHash.Hash) + + b, err := json.Marshal(proc.centralAuth.pki.nodesAcked.keysAndHash) + + if err != nil { + er := fmt.Errorf("error: methodREQKeysRequestUpdate, failed to marshal keys map: %v", err) + proc.errorKernel.errSend(proc, message, er) + } + fmt.Printf("\n ----> methodREQKeysRequestUpdate: SENDING KEYS TO NODE=%v\n", message.FromNode) + newReplyMessage(proc, message, b) + }() + } + }() + + // NB: We're not sending an ACK message for this request type. + return nil, nil +} + // --- type methodREQAclAddCommand struct {