|
| 1 | +package demo |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/hmac" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/base64" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/secrethub/secrethub-cli/internals/cli/ui" |
| 10 | + "github.com/secrethub/secrethub-cli/internals/secrethub/command" |
| 11 | + |
| 12 | + "github.com/secrethub/secrethub-go/internals/api" |
| 13 | + "github.com/secrethub/secrethub-go/pkg/secrethub" |
| 14 | + "github.com/secrethub/secrethub-go/pkg/secretpath" |
| 15 | +) |
| 16 | + |
| 17 | +type newClientFunc func() (secrethub.ClientInterface, error) |
| 18 | + |
| 19 | +const defaultDemoRepo = "demo" |
| 20 | + |
| 21 | +type InitCommand struct { |
| 22 | + repo api.RepoPath |
| 23 | + |
| 24 | + io ui.IO |
| 25 | + newClient newClientFunc |
| 26 | +} |
| 27 | + |
| 28 | +func NewInitCommand(io ui.IO, newClient newClientFunc) *InitCommand { |
| 29 | + return &InitCommand{ |
| 30 | + io: io, |
| 31 | + newClient: newClient, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// Register registers the command, arguments and flags on the provided Registerer. |
| 36 | +func (cmd *InitCommand) Register(r command.Registerer) { |
| 37 | + clause := r.Command("init", "Create the secrets necessary to connect with the demo application.") |
| 38 | + clause.HelpLong("demo init creates a repository with the username and password needed to connect to the demo API.") |
| 39 | + |
| 40 | + clause.Flag("repo", "The path of the repository to create. Defaults to a "+defaultDemoRepo+" repo in your personal namespace.").SetValue(&cmd.repo) |
| 41 | + |
| 42 | + command.BindAction(clause, cmd.Run) |
| 43 | +} |
| 44 | + |
| 45 | +// Run handles the command with the options as specified in the command. |
| 46 | +func (cmd *InitCommand) Run() error { |
| 47 | + client, err := cmd.newClient() |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + var repoPath string |
| 53 | + var username string |
| 54 | + if cmd.repo == "" { |
| 55 | + me, err := client.Me().GetUser() |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + username = me.Username |
| 60 | + repoPath = secretpath.Join(me.Username, defaultDemoRepo) |
| 61 | + } else { |
| 62 | + username = secretpath.Namespace(cmd.repo.Value()) |
| 63 | + repoPath = cmd.repo.Value() |
| 64 | + } |
| 65 | + |
| 66 | + _, err = client.Repos().Create(repoPath) |
| 67 | + if err == api.ErrRepoAlreadyExists && cmd.repo == "" { |
| 68 | + return fmt.Errorf("demo repo %s already exists, use --repo to specify another repo to use", repoPath) |
| 69 | + } else if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + usernamePath := secretpath.Join(repoPath, "username") |
| 74 | + _, err = client.Secrets().Write(usernamePath, []byte(username)) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + h := hmac.New(sha256.New, []byte("this-is-no-good-way-to-generate-a-password-that-is-why-we-only-use-it-for-demo-purposes")) |
| 80 | + password := base64.RawStdEncoding.EncodeToString(h.Sum([]byte(username)))[:20] |
| 81 | + |
| 82 | + passwordPath := secretpath.Join(repoPath, "password") |
| 83 | + _, err = client.Secrets().Write(passwordPath, []byte(password)) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + fmt.Printf("Created the following secrets:\n%s\n%s\n", usernamePath, passwordPath) |
| 89 | + |
| 90 | + return nil |
| 91 | +} |
0 commit comments