github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/ngaut/go-zookeeper/zk/util.go (about)

     1  package zk
     2  
     3  import (
     4  	"crypto/sha1"
     5  	"encoding/base64"
     6  	"fmt"
     7  	"math/rand"
     8  )
     9  
    10  // AuthACL produces an ACL list containing a single ACL which uses the
    11  // provided permissions, with the scheme "auth", and ID "", which is used
    12  // by ZooKeeper to represent any authenticated user.
    13  func AuthACL(perms int32) []ACL {
    14  	return []ACL{{perms, "auth", ""}}
    15  }
    16  
    17  // WorldACL produces an ACL list containing a single ACL which uses the
    18  // provided permissions, with the scheme "world", and ID "anyone", which
    19  // is used by ZooKeeper to represent any user at all.
    20  func WorldACL(perms int32) []ACL {
    21  	return []ACL{{perms, "world", "anyone"}}
    22  }
    23  
    24  func DigestACL(perms int32, user, password string) []ACL {
    25  	userPass := []byte(fmt.Sprintf("%s:%s", user, password))
    26  	h := sha1.New()
    27  	if n, err := h.Write(userPass); err != nil || n != len(userPass) {
    28  		panic("SHA1 failed")
    29  	}
    30  	digest := base64.StdEncoding.EncodeToString(h.Sum(nil))
    31  	return []ACL{{perms, "digest", fmt.Sprintf("%s:%s", user, digest)}}
    32  }
    33  
    34  // stringShuffle performs a Fisher-Yates shuffle on a slice of strings
    35  func stringShuffle(s []string) {
    36  	for i := len(s) - 1; i > 0; i-- {
    37  		j := rand.Intn(i + 1)
    38  		s[i], s[j] = s[j], s[i]
    39  	}
    40  }