loadTLSConfig reads the directory for TLS certificates including roots and certificate pairs, and updates the provided TLS configuration.
(ctx context.Context, directory string, tlsConfig *tls.Config)
| 60 | // loadTLSConfig reads the directory for TLS certificates including roots and |
| 61 | // certificate pairs, and updates the provided TLS configuration. |
| 62 | func loadTLSConfig(ctx context.Context, directory string, tlsConfig *tls.Config) error { |
| 63 | fs, err := os.ReadDir(directory) |
| 64 | if err != nil { |
| 65 | if os.IsNotExist(err) { |
| 66 | return nil |
| 67 | } |
| 68 | return invalidParam(err) |
| 69 | } |
| 70 | |
| 71 | for _, f := range fs { |
| 72 | if ctx.Err() != nil { |
| 73 | return ctx.Err() |
| 74 | } |
| 75 | switch filepath.Ext(f.Name()) { |
| 76 | case ".crt": |
| 77 | if tlsConfig.RootCAs == nil { |
| 78 | systemPool, err := x509.SystemCertPool() |
| 79 | if err != nil { |
| 80 | return invalidParam(fmt.Errorf("unable to get system cert pool: %w", err)) |
| 81 | } |
| 82 | tlsConfig.RootCAs = systemPool |
| 83 | } |
| 84 | fileName := filepath.Join(directory, f.Name()) |
| 85 | log.G(ctx).Debugf("crt: %s", fileName) |
| 86 | data, err := os.ReadFile(fileName) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | tlsConfig.RootCAs.AppendCertsFromPEM(data) |
| 91 | case ".cert": |
| 92 | certName := f.Name() |
| 93 | keyName := certName[:len(certName)-5] + ".key" |
| 94 | log.G(ctx).Debugf("cert: %s", filepath.Join(directory, certName)) |
| 95 | if !hasFile(fs, keyName) { |
| 96 | return invalidParamf("missing key %s for client certificate %s. CA certificates must use the extension .crt", keyName, certName) |
| 97 | } |
| 98 | cert, err := tls.LoadX509KeyPair(filepath.Join(directory, certName), filepath.Join(directory, keyName)) |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | tlsConfig.Certificates = append(tlsConfig.Certificates, cert) |
| 103 | case ".key": |
| 104 | keyName := f.Name() |
| 105 | certName := keyName[:len(keyName)-4] + ".cert" |
| 106 | log.G(ctx).Debugf("key: %s", filepath.Join(directory, keyName)) |
| 107 | if !hasFile(fs, certName) { |
| 108 | return invalidParamf("missing client certificate %s for key %s", certName, keyName) |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return nil |
| 114 | } |
| 115 | |
| 116 | // Headers returns request modifiers with a User-Agent and metaHeaders |
| 117 | func Headers(userAgent string, metaHeaders http.Header) []transport.RequestModifier { |
no test coverage detected
searching dependent graphs…