|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "crypto/tls" |
| 5 | + "crypto/x509" |
4 | 6 | "errors" |
5 | 7 | "fmt" |
| 8 | + "io/ioutil" |
6 | 9 | "log" |
7 | 10 | "net/http" |
8 | 11 | "os" |
@@ -44,6 +47,7 @@ func init() { |
44 | 47 | flags.Int64Var(&server.MaxRepoSize, "max-size", server.MaxRepoSize, "the maximum size of the repository in bytes") |
45 | 48 | flags.StringVar(&server.Path, "path", server.Path, "data directory") |
46 | 49 | flags.BoolVar(&server.TLS, "tls", server.TLS, "turn on TLS support") |
| 50 | + flags.StringVar(&server.TLSCACert, "tls-cacert", server.TLSCACert, "TLS CA certificate path") |
47 | 51 | flags.StringVar(&server.TLSCert, "tls-cert", server.TLSCert, "TLS certificate path") |
48 | 52 | flags.StringVar(&server.TLSKey, "tls-key", server.TLSKey, "TLS key path") |
49 | 53 | flags.BoolVar(&server.NoAuth, "no-auth", server.NoAuth, "disable .htpasswd authentication") |
@@ -140,7 +144,29 @@ func runRoot(cmd *cobra.Command, args []string) error { |
140 | 144 | err = http.Serve(listener, handler) |
141 | 145 | } else { |
142 | 146 | log.Printf("TLS enabled, private key %s, pubkey %v", privateKey, publicKey) |
143 | | - err = http.ServeTLS(listener, handler, publicKey, privateKey) |
| 147 | + |
| 148 | + httpServer := &http.Server{ |
| 149 | + Handler: handler, |
| 150 | + } |
| 151 | + |
| 152 | + if server.TLSCACert != "" { |
| 153 | + log.Printf("TLS Client Authentication enabled, CA cert %s", server.TLSCACert) |
| 154 | + |
| 155 | + caCert, err := ioutil.ReadFile(server.TLSCACert) |
| 156 | + if err != nil { |
| 157 | + return fmt.Errorf("unable to read CA certificate: %w", err) |
| 158 | + } |
| 159 | + caCertPool := x509.NewCertPool() |
| 160 | + caCertPool.AppendCertsFromPEM(caCert) |
| 161 | + |
| 162 | + tlsConfig := &tls.Config{ |
| 163 | + ClientAuth: tls.RequireAndVerifyClientCert, |
| 164 | + ClientCAs: caCertPool, |
| 165 | + } |
| 166 | + httpServer.TLSConfig = tlsConfig |
| 167 | + } |
| 168 | + |
| 169 | + err = httpServer.ServeTLS(listener, publicKey, privateKey) |
144 | 170 | } |
145 | 171 |
|
146 | 172 | return err |
|
0 commit comments