|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/x509" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/mitchellh/cli" |
| 12 | + "github.com/posener/complete" |
| 13 | + "github.com/roots/trellis-cli/certificates" |
| 14 | + "github.com/roots/trellis-cli/trellis" |
| 15 | + "go.step.sm/crypto/pemutil" |
| 16 | +) |
| 17 | + |
| 18 | +type CertificateInstallCommand struct { |
| 19 | + UI cli.Ui |
| 20 | + Trellis *trellis.Trellis |
| 21 | + flags *flag.FlagSet |
| 22 | + path string |
| 23 | +} |
| 24 | + |
| 25 | +func NewCertificateInstallCommand(ui cli.Ui, trellis *trellis.Trellis) *CertificateInstallCommand { |
| 26 | + c := &CertificateInstallCommand{UI: ui, Trellis: trellis} |
| 27 | + c.init() |
| 28 | + return c |
| 29 | +} |
| 30 | + |
| 31 | +func (c *CertificateInstallCommand) init() { |
| 32 | + c.flags = flag.NewFlagSet("", flag.ContinueOnError) |
| 33 | + c.flags.Usage = func() { c.UI.Info(c.Help()) } |
| 34 | + c.flags.StringVar(&c.path, "path", "", "Local path to custom root certificate to install") |
| 35 | +} |
| 36 | + |
| 37 | +func (c *CertificateInstallCommand) Run(args []string) int { |
| 38 | + if err := c.Trellis.LoadProject(); err != nil { |
| 39 | + c.UI.Error(err.Error()) |
| 40 | + return 1 |
| 41 | + } |
| 42 | + |
| 43 | + c.Trellis.CheckVirtualenv(c.UI) |
| 44 | + |
| 45 | + commandArgumentValidator := &CommandArgumentValidator{required: 0, optional: 1} |
| 46 | + commandArgumentErr := commandArgumentValidator.validate(args) |
| 47 | + if commandArgumentErr != nil { |
| 48 | + c.UI.Error(commandArgumentErr.Error()) |
| 49 | + c.UI.Output(c.Help()) |
| 50 | + return 1 |
| 51 | + } |
| 52 | + |
| 53 | + environment := "development" |
| 54 | + |
| 55 | + if len(args) == 1 { |
| 56 | + environment = args[0] |
| 57 | + environmentErr := c.Trellis.ValidateEnvironment(environment) |
| 58 | + if environmentErr != nil { |
| 59 | + c.UI.Error(environmentErr.Error()) |
| 60 | + return 1 |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + cert, err := c.fetchRootCertificate(environment) |
| 65 | + if err != nil { |
| 66 | + c.UI.Error("Error fetching root certificate from server:") |
| 67 | + c.UI.Error(err.Error()) |
| 68 | + c.UI.Error("The server (likely Vagrant virtual machine) must be running and have been provisioned with an SSL enabled site.") |
| 69 | + return 1 |
| 70 | + } |
| 71 | + |
| 72 | + if certificates.Trusted(cert) { |
| 73 | + c.UI.Info("Root certificate already installed and trusted") |
| 74 | + return 0 |
| 75 | + } |
| 76 | + |
| 77 | + if err := certificates.InstallFile(c.path); err != nil { |
| 78 | + c.UI.Error("Error installing root certificate to truststore:") |
| 79 | + c.UI.Error(err.Error()) |
| 80 | + return 1 |
| 81 | + } |
| 82 | + |
| 83 | + c.UI.Info(fmt.Sprintf("Certificate %s has been installed.\n", c.path)) |
| 84 | + if text, err := certificates.ShortText(cert); err == nil { |
| 85 | + c.UI.Info(text) |
| 86 | + } |
| 87 | + |
| 88 | + c.UI.Info("Note: your web browser(s) will need to be restarted for this to take effect.") |
| 89 | + |
| 90 | + return 0 |
| 91 | +} |
| 92 | + |
| 93 | +func (c *CertificateInstallCommand) Synopsis() string { |
| 94 | + return "Installs a root certificate in the system truststore" |
| 95 | +} |
| 96 | + |
| 97 | +func (c *CertificateInstallCommand) Help() string { |
| 98 | + helpText := ` |
| 99 | +Usage: trellis certificate install [options] [ENVIRONMENT] |
| 100 | +
|
| 101 | +Installs a root certificate in the system truststore. This allows your local |
| 102 | +computer to trust the "self-signed" root CA (certificate authority) that Trellis |
| 103 | +uses in development which avoids insecure warnings in your web browsers. |
| 104 | +
|
| 105 | +By default this integrates with a Trellis server/VM and requires that it's running. |
| 106 | +However, the --path option can be used to specify any root certificate making this |
| 107 | +command useful for non-Trellis use cases too. |
| 108 | +
|
| 109 | +Note: browsers may have to be restarted after running this command for it to take effect. |
| 110 | +
|
| 111 | +Install a non-default root certificate via a local path: |
| 112 | +
|
| 113 | + $ trellis certificate install --path ~/certs/root.crt |
| 114 | +
|
| 115 | +Arguments: |
| 116 | + ENVIRONMENT Name of environment (default: development) |
| 117 | +
|
| 118 | +Options: |
| 119 | + -h, --help show this help |
| 120 | + --path local path to custom root certificate to install |
| 121 | +` |
| 122 | + |
| 123 | + return strings.TrimSpace(helpText) |
| 124 | +} |
| 125 | + |
| 126 | +func (c *CertificateInstallCommand) AutocompleteArgs() complete.Predictor { |
| 127 | + return c.Trellis.AutocompleteEnvironment(c.flags) |
| 128 | +} |
| 129 | + |
| 130 | +func (c *CertificateInstallCommand) AutocompleteFlags() complete.Flags { |
| 131 | + return complete.Flags{ |
| 132 | + "--path": complete.PredictNothing, |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func (c *CertificateInstallCommand) fetchRootCertificate(environment string) (cert *x509.Certificate, err error) { |
| 137 | + if c.path == "" { |
| 138 | + c.path = certificates.RootCertificatePath(c.Trellis.ConfigPath()) |
| 139 | + } |
| 140 | + siteName, _ := c.Trellis.FindSiteNameFromEnvironment(environment, "") |
| 141 | + host := c.Trellis.SiteFromEnvironmentAndName(environment, siteName).MainHost() |
| 142 | + |
| 143 | + certBytes, err := certificates.FetchRootCertificate(c.path, host) |
| 144 | + |
| 145 | + if err = os.MkdirAll(filepath.Dir(c.path), os.ModePerm); err != nil { |
| 146 | + return nil, err |
| 147 | + } |
| 148 | + |
| 149 | + if err = os.WriteFile(c.path, certBytes, os.ModePerm); err != nil { |
| 150 | + return nil, err |
| 151 | + } |
| 152 | + |
| 153 | + cert, err = pemutil.ParseCertificate(certBytes) |
| 154 | + |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + |
| 159 | + return cert, nil |
| 160 | +} |
0 commit comments