github.com/dougneal/terraform@v0.6.15-0.20170330092735-b6a3840768a4/helper/acctest/random.go (about)

     1  package acctest
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	crand "crypto/rand"
     7  	"crypto/rsa"
     8  	"crypto/x509"
     9  	"encoding/pem"
    10  	"fmt"
    11  	"math/rand"
    12  	"strings"
    13  	"time"
    14  
    15  	"golang.org/x/crypto/ssh"
    16  )
    17  
    18  // Helpers for generating random tidbits for use in identifiers to prevent
    19  // collisions in acceptance tests.
    20  
    21  // RandInt generates a random integer
    22  func RandInt() int {
    23  	reseed()
    24  	return rand.New(rand.NewSource(time.Now().UnixNano())).Int()
    25  }
    26  
    27  func RandIntRange(min int, max int) int {
    28  	reseed()
    29  	source := rand.New(rand.NewSource(time.Now().UnixNano()))
    30  	rangeMax := max - min
    31  
    32  	return int(source.Int31n(int32(rangeMax)))
    33  }
    34  
    35  // RandString generates a random alphanumeric string of the length specified
    36  func RandString(strlen int) string {
    37  	return RandStringFromCharSet(strlen, CharSetAlphaNum)
    38  }
    39  
    40  // RandStringFromCharSet generates a random string by selecting characters from
    41  // the charset provided
    42  func RandStringFromCharSet(strlen int, charSet string) string {
    43  	reseed()
    44  	result := make([]byte, strlen)
    45  	for i := 0; i < strlen; i++ {
    46  		result[i] = charSet[rand.Intn(len(charSet))]
    47  	}
    48  	return string(result)
    49  }
    50  
    51  // RandSSHKeyPair generates a public and private SSH key pair. The public key is
    52  // returned in OpenSSH format, and the private key is PEM encoded.
    53  func RandSSHKeyPair(comment string) (string, string, error) {
    54  	privateKey, err := rsa.GenerateKey(crand.Reader, 1024)
    55  	if err != nil {
    56  		return "", "", err
    57  	}
    58  
    59  	var privateKeyBuffer bytes.Buffer
    60  	privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
    61  	if err := pem.Encode(bufio.NewWriter(&privateKeyBuffer), privateKeyPEM); err != nil {
    62  		return "", "", err
    63  	}
    64  
    65  	publicKey, err := ssh.NewPublicKey(&privateKey.PublicKey)
    66  	if err != nil {
    67  		return "", "", err
    68  	}
    69  	keyMaterial := strings.TrimSpace(string(ssh.MarshalAuthorizedKey(publicKey)))
    70  	return fmt.Sprintf("%s %s", keyMaterial, comment), privateKeyBuffer.String(), nil
    71  }
    72  
    73  // Seeds random with current timestamp
    74  func reseed() {
    75  	rand.Seed(time.Now().UTC().UnixNano())
    76  }
    77  
    78  const (
    79  	// CharSetAlphaNum is the alphanumeric character set for use with
    80  	// RandStringFromCharSet
    81  	CharSetAlphaNum = "abcdefghijklmnopqrstuvwxyz012346789"
    82  
    83  	// CharSetAlpha is the alphabetical character set for use with
    84  	// RandStringFromCharSet
    85  	CharSetAlpha = "abcdefghijklmnopqrstuvwxyz"
    86  )