github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/tools/users/users.go (about)

     1  // Copyright 2019 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  // users signs up a bunch of users in a development environment
     5  
     6  package main
     7  
     8  import (
     9  	"crypto/rand"
    10  	"flag"
    11  	"fmt"
    12  	"os"
    13  	"os/exec"
    14  )
    15  
    16  var usersCount int
    17  var walletsCount int
    18  var bin string
    19  var home string
    20  
    21  func main() {
    22  	parseFlags()
    23  	su := createStandardUsers()
    24  	wu := createWalletUsers()
    25  	fmt.Printf("created %d standard users\n", len(su))
    26  	fmt.Printf("created %d wallet users\n", len(wu))
    27  
    28  	f, err := os.Create("out.json")
    29  	if err != nil {
    30  		fmt.Printf("error opening out.json: %s\n", err)
    31  		return
    32  	}
    33  	defer f.Close()
    34  	fmt.Fprintf(f, "[\n")
    35  	for i, u := range su {
    36  		if i != 0 {
    37  			fmt.Fprintf(f, ",\n")
    38  		}
    39  		fmt.Fprintf(f, "\t{\"username\": %q}", u)
    40  	}
    41  	for i, u := range wu {
    42  		if i != 0 || len(su) > 0 {
    43  			fmt.Fprintf(f, ",\n")
    44  		}
    45  		fmt.Fprintf(f, "\t{\"username\": %q}", u)
    46  	}
    47  	fmt.Fprintf(f, "\n]\n")
    48  }
    49  
    50  func parseFlags() {
    51  	flag.IntVar(&usersCount, "users", 0, "number of standard users to create")
    52  	flag.IntVar(&walletsCount, "wallets", 0, "number of users with wallets to create")
    53  	flag.StringVar(&bin, "bin", "/usr/local/bin/keybase", "keybase binary path")
    54  	flag.StringVar(&home, "home", "/tmp", "home directory")
    55  	flag.Parse()
    56  
    57  	if usersCount == 0 && walletsCount == 0 {
    58  		fmt.Fprintf(os.Stderr, "must specify at least one type of user to create\n\n")
    59  		fmt.Fprintf(os.Stderr, "Usage:\n\n")
    60  		fmt.Fprintf(os.Stderr, "  users [options]\n\n")
    61  		flag.PrintDefaults()
    62  		os.Exit(1)
    63  	}
    64  }
    65  
    66  func createStandardUsers() []string {
    67  	if usersCount == 0 {
    68  		return nil
    69  	}
    70  	fmt.Printf("Creating %d standard users:\n", usersCount)
    71  	var users []string
    72  	for i := 0; i < usersCount; i++ {
    73  		username, err := createStandardUser()
    74  		if err != nil {
    75  			fmt.Printf("%d: error %s\n", i+1, err)
    76  			continue
    77  		}
    78  		fmt.Printf("%d: created %s\n", i+1, username)
    79  		users = append(users, username)
    80  	}
    81  
    82  	return users
    83  }
    84  
    85  func createWalletUsers() []string {
    86  	if walletsCount == 0 {
    87  		return nil
    88  	}
    89  	fmt.Printf("Creating %d users with wallets:\n", walletsCount)
    90  	var users []string
    91  	for i := 0; i < walletsCount; i++ {
    92  		username, err := createWalletUser()
    93  		if err != nil {
    94  			fmt.Printf("%d: error %s\n", i+1, err)
    95  			continue
    96  		}
    97  		fmt.Printf("%d: created %s (with wallet)\n", i+1, username)
    98  		users = append(users, username)
    99  	}
   100  
   101  	return users
   102  }
   103  
   104  func createStandardUser() (string, error) {
   105  	logout()
   106  	buf := make([]byte, 6)
   107  	if _, err := rand.Read(buf); err != nil {
   108  		return "", err
   109  	}
   110  	username := fmt.Sprintf("ad_%x", buf)
   111  	cmd := exec.Command(bin, "-home", home, "signup", "-batch", "-username", username, "-passphrase", "00000000", "-no-email", "-invite-code", "202020202020202020202020")
   112  	if out, err := cmd.CombinedOutput(); err != nil {
   113  		fmt.Printf("signup error: %s\n", err)
   114  		fmt.Printf("output: %s\n", string(out))
   115  		return "", err
   116  	}
   117  	return username, nil
   118  }
   119  
   120  func createWalletUser() (string, error) {
   121  	username, err := createStandardUser()
   122  	if err != nil {
   123  		return "", err
   124  	}
   125  
   126  	// accept the disclaimer
   127  	cmd := exec.Command(bin, "-home", home, "wallet", "api", "-m", `{"method": "setup-wallet"}`)
   128  	if out, err := cmd.CombinedOutput(); err != nil {
   129  		fmt.Printf("setup-wallet error: %s\n", err)
   130  		fmt.Printf("output: %s\n", string(out))
   131  		return "", err
   132  	}
   133  
   134  	return username, nil
   135  }
   136  
   137  func logout() {
   138  	cmd := exec.Command(bin, "-home", home, "logout")
   139  	if out, err := cmd.CombinedOutput(); err != nil {
   140  		fmt.Printf("logout error: %s\n", err)
   141  		fmt.Printf("output: %s\n", string(out))
   142  	}
   143  }