| 1 | package fileserver |
| 2 | |
| 3 | import ( |
| 4 | "crypto/tls" |
| 5 | "crypto/x509" |
| 6 | "fmt" |
| 7 | "net" |
| 8 | "net/http" |
| 9 | "os" |
| 10 | |
| 11 | "github.com/pkg/errors" |
| 12 | |
| 13 | "github.com/smallstep/cli/errs" |
| 14 | "github.com/smallstep/cli/utils" |
| 15 | |
| 16 | "github.com/smallstep/cli/command" |
| 17 | "github.com/urfave/cli" |
| 18 | ) |
| 19 | |
| 20 | func init() { |
| 21 | cmd := cli.Command{ |
| 22 | Name: "fileserver", |
| 23 | Hidden: true, |
| 24 | Action: command.ActionFunc(fileServerAction), |
| 25 | Usage: "start an HTTP(S) server serving the contents of a path", |
| 26 | UsageText: `step fileserver <dir> |
| 27 | [**--address**=<address>] [**--cert**=<path>] [**--key**=<path>] [**--roots**=<path>]`, |
| 28 | Description: `**step fileserver** command starts an HTTP(S) server serving the contents of a file |
| 29 | system. |
| 30 | |
| 31 | This command is experimental and only intended for test purposes. |
| 32 | |
| 33 | ## POSITIONAL ARGUMENTS |
| 34 | |
| 35 | <dir> |
| 36 | : The directory used as root for the HTTP file server. |
| 37 | |
| 38 | ## EXAMPLES |
| 39 | |
| 40 | Start an HTTP file server on port 8080. |
| 41 | ''' |
| 42 | $ step fileserver --address :8080 /path/to/web-root |
| 43 | ''' |
| 44 | |
| 45 | Start an HTTPS file server on 127.0.0.1:8443. |
| 46 | ''' |
| 47 | $ step ca certificate 127.0.0.1 localhost.crt localhost.key |
| 48 | ... |
| 49 | $ step fileserver --address 127.0.0.1:8443 \ |
| 50 | --cert localhost.crt --key localhost.key /path/to/web-root |
| 51 | ''' |
| 52 | |
| 53 | Start an HTTPS file server on a random port and require client certificates. |
| 54 | ''' |
| 55 | $ step fileserver --cert localhost.crt --key localhost.key \ |
| 56 | --roots $(step path)/certs/root_ca.crt /path/to/web-root |
| 57 | '''`, |
| 58 | Flags: []cli.Flag{ |
| 59 | cli.StringFlag{ |
| 60 | Name: "address", |
| 61 | Usage: "The TCP <address> to listen on (e.g. \":8443\").", |
| 62 | Value: ":0", |
| 63 | }, |
| 64 | cli.StringFlag{ |
| 65 | Name: "cert", |
| 66 | Usage: `The <path> to the TLS certificate to use.`, |
| 67 | }, |
| 68 | cli.StringFlag{ |
| 69 | Name: "key", |
| 70 | Usage: `The <path> to the key corresponding to the certificate.`, |
| 71 | }, |
| 72 | cli.StringFlag{ |
| 73 | Name: "roots", |
| 74 | Usage: "The <path> of the root certificates that will be used to verify the client certificates.", |
| 75 | }, |
| 76 | }, |
| 77 | } |
| 78 | command.Register(cmd) |
| 79 | } |
| 80 | |
| 81 | func fileServerAction(ctx *cli.Context) error { |
| 82 | if err := errs.NumberOfArguments(ctx, 1); err != nil { |
| 83 | return err |
| 84 | } |
| 85 | |
| 86 | root := ctx.Args().First() |
| 87 | f, err := os.Stat(root) |
| 88 | if err != nil { |
| 89 | return errs.FileError(err, root) |
| 90 | } |
| 91 | if !f.Mode().IsDir() { |
| 92 | return errors.New("positional argument <dir> must be a directory") |
| 93 | } |
| 94 | |
| 95 | address := ctx.String("address") |
| 96 | cert := ctx.String("cert") |
| 97 | key := ctx.String("key") |
| 98 | roots := ctx.String("roots") |
| 99 | |
| 100 | switch { |
| 101 | case address == "": |
| 102 | return errs.RequiredFlag(ctx, "address") |
| 103 | case roots != "" && cert == "": |
| 104 | return errs.RequiredWithFlag(ctx, "roots", "cert") |
| 105 | case roots != "" && key == "": |
| 106 | return errs.RequiredWithFlag(ctx, "roots", "key") |
| 107 | case cert != "" && key == "": |
| 108 | return errs.RequiredWithFlag(ctx, "cert", "key") |
| 109 | case key != "" && cert == "": |
| 110 | return errs.RequiredWithFlag(ctx, "key", "cert") |
| 111 | } |
| 112 | |
| 113 | var tlsConfig *tls.Config |
| 114 | if roots != "" { |
| 115 | b, err := utils.ReadFile(roots) |
| 116 | if err != nil { |
| 117 | return err |
| 118 | } |
| 119 | pool := x509.NewCertPool() |
| 120 | pool.AppendCertsFromPEM(b) |
| 121 | tlsConfig = &tls.Config{ |
| 122 | ClientCAs: pool, |
| 123 | ClientAuth: tls.RequireAndVerifyClientCert, |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | l, err := net.Listen("tcp", address) |
| 128 | if err != nil { |
| 129 | return errors.Wrapf(err, "failed to listen on at %s", address) |
| 130 | } |
| 131 | |
| 132 | srv := &http.Server{ |
| 133 | Handler: http.FileServer(http.Dir(root)), |
| 134 | TLSConfig: tlsConfig, |
| 135 | } |
| 136 | if cert != "" && key != "" { |
| 137 | fmt.Printf("Serving HTTPS at %s ...\n", l.Addr().String()) |
| 138 | err = srv.ServeTLS(l, cert, key) |
| 139 | } else { |
| 140 | fmt.Printf("Serving HTTP at %s...\n", l.Addr().String()) |
| 141 | err = srv.Serve(l) |
| 142 | } |
| 143 | if err != nil && err != http.ErrServerClosed { |
| 144 | return errors.Wrap(err, "file server failed") |
| 145 | } |
| 146 | |
| 147 | return nil |
| 148 | } |