github.com/pfcoder/quorum@v2.0.3-0.20180501191142-d4a1b0958135+incompatible/p2p/permissions.go (about)

     1  package p2p
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/ethereum/go-ethereum/log"
    10  	"github.com/ethereum/go-ethereum/p2p/discover"
    11  )
    12  
    13  const (
    14  	NODE_NAME_LENGTH    = 32
    15  	PERMISSIONED_CONFIG = "permissioned-nodes.json"
    16  )
    17  
    18  // check if a given node is permissioned to connect to the change
    19  func isNodePermissioned(nodename string, currentNode string, datadir string, direction string) bool {
    20  
    21  	var permissionedList []string
    22  	nodes := parsePermissionedNodes(datadir)
    23  	for _, v := range nodes {
    24  		permissionedList = append(permissionedList, v.ID.String())
    25  	}
    26  
    27  	log.Debug("isNodePermissioned", "permissionedList", permissionedList)
    28  	for _, v := range permissionedList {
    29  		if v == nodename {
    30  			log.Debug("isNodePermissioned", "connection", direction, "nodename", nodename[:NODE_NAME_LENGTH], "ALLOWED-BY", currentNode[:NODE_NAME_LENGTH])
    31  			return true
    32  		}
    33  		log.Debug("isNodePermissioned", "connection", direction, "nodename", nodename[:NODE_NAME_LENGTH], "DENIED-BY", currentNode[:NODE_NAME_LENGTH])
    34  	}
    35  	log.Debug("isNodePermissioned", "connection", direction, "nodename", nodename[:NODE_NAME_LENGTH], "DENIED-BY", currentNode[:NODE_NAME_LENGTH])
    36  	return false
    37  }
    38  
    39  //this is a shameless copy from the config.go. It is a duplication of the code
    40  //for the timebeing to allow reload of the permissioned nodes while the server is running
    41  
    42  func parsePermissionedNodes(DataDir string) []*discover.Node {
    43  
    44  	log.Debug("parsePermissionedNodes", "DataDir", DataDir, "file", PERMISSIONED_CONFIG)
    45  
    46  	path := filepath.Join(DataDir, PERMISSIONED_CONFIG)
    47  	if _, err := os.Stat(path); err != nil {
    48  		log.Error("Read Error for permissioned-nodes.json file. This is because 'permissioned' flag is specified but no permissioned-nodes.json file is present.", "err", err)
    49  		return nil
    50  	}
    51  	// Load the nodes from the config file
    52  	blob, err := ioutil.ReadFile(path)
    53  	if err != nil {
    54  		log.Error("parsePermissionedNodes: Failed to access nodes", "err", err)
    55  		return nil
    56  	}
    57  
    58  	nodelist := []string{}
    59  	if err := json.Unmarshal(blob, &nodelist); err != nil {
    60  		log.Error("parsePermissionedNodes: Failed to load nodes", "err", err)
    61  		return nil
    62  	}
    63  	// Interpret the list as a discovery node array
    64  	var nodes []*discover.Node
    65  	for _, url := range nodelist {
    66  		if url == "" {
    67  			log.Error("parsePermissionedNodes: Node URL blank")
    68  			continue
    69  		}
    70  		node, err := discover.ParseNode(url)
    71  		if err != nil {
    72  			log.Error("parsePermissionedNodes: Node URL", "url", url, "err", err)
    73  			continue
    74  		}
    75  		nodes = append(nodes, node)
    76  	}
    77  	return nodes
    78  }