|
| 1 | +// Package registry contains client primitives to interact with a remote Docker registry. |
| 2 | +package registry |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "crypto/tls" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + |
| 12 | + "github.com/docker/distribution/registry/client/transport" |
| 13 | + "github.com/docker/go-connections/tlsconfig" |
| 14 | + "github.com/sirupsen/logrus" |
| 15 | +) |
| 16 | + |
| 17 | +func hasFile(files []os.DirEntry, name string) bool { |
| 18 | + for _, f := range files { |
| 19 | + if f.Name() == name { |
| 20 | + return true |
| 21 | + } |
| 22 | + } |
| 23 | + return false |
| 24 | +} |
| 25 | + |
| 26 | +// ReadCertsDirectory reads the directory for TLS certificates |
| 27 | +// including roots and certificate pairs and updates the |
| 28 | +// provided TLS configuration. |
| 29 | +func ReadCertsDirectory(tlsConfig *tls.Config, directory string) error { |
| 30 | + return loadTLSConfig(context.TODO(), directory, tlsConfig) |
| 31 | +} |
| 32 | + |
| 33 | +// loadTLSConfig reads the directory for TLS certificates including roots and |
| 34 | +// certificate pairs, and updates the provided TLS configuration. |
| 35 | +func loadTLSConfig(ctx context.Context, directory string, tlsConfig *tls.Config) error { |
| 36 | + fs, err := os.ReadDir(directory) |
| 37 | + if err != nil { |
| 38 | + if os.IsNotExist(err) { |
| 39 | + return nil |
| 40 | + } |
| 41 | + return invalidParam(err) |
| 42 | + } |
| 43 | + |
| 44 | + for _, f := range fs { |
| 45 | + if ctx.Err() != nil { |
| 46 | + return ctx.Err() |
| 47 | + } |
| 48 | + switch filepath.Ext(f.Name()) { |
| 49 | + case ".crt": |
| 50 | + if tlsConfig.RootCAs == nil { |
| 51 | + systemPool, err := tlsconfig.SystemCertPool() |
| 52 | + if err != nil { |
| 53 | + return invalidParam(fmt.Errorf("unable to get system cert pool: %w", err)) |
| 54 | + } |
| 55 | + tlsConfig.RootCAs = systemPool |
| 56 | + } |
| 57 | + fileName := filepath.Join(directory, f.Name()) |
| 58 | + logrus.Debugf("crt: %s", fileName) |
| 59 | + data, err := os.ReadFile(fileName) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + tlsConfig.RootCAs.AppendCertsFromPEM(data) |
| 64 | + case ".cert": |
| 65 | + certName := f.Name() |
| 66 | + keyName := certName[:len(certName)-5] + ".key" |
| 67 | + logrus.Debugf("cert: %s", filepath.Join(directory, certName)) |
| 68 | + if !hasFile(fs, keyName) { |
| 69 | + return invalidParamf("missing key %s for client certificate %s. CA certificates must use the extension .crt", keyName, certName) |
| 70 | + } |
| 71 | + cert, err := tls.LoadX509KeyPair(filepath.Join(directory, certName), filepath.Join(directory, keyName)) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + tlsConfig.Certificates = append(tlsConfig.Certificates, cert) |
| 76 | + case ".key": |
| 77 | + keyName := f.Name() |
| 78 | + certName := keyName[:len(keyName)-4] + ".cert" |
| 79 | + logrus.Debugf("key: %s", filepath.Join(directory, keyName)) |
| 80 | + if !hasFile(fs, certName) { |
| 81 | + return invalidParamf("missing client certificate %s for key %s", certName, keyName) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +// Headers returns request modifiers with a User-Agent and metaHeaders |
| 90 | +func Headers(userAgent string, metaHeaders http.Header) []transport.RequestModifier { |
| 91 | + modifiers := []transport.RequestModifier{} |
| 92 | + if userAgent != "" { |
| 93 | + modifiers = append(modifiers, transport.NewHeaderRequestModifier(http.Header{ |
| 94 | + "User-Agent": []string{userAgent}, |
| 95 | + })) |
| 96 | + } |
| 97 | + if metaHeaders != nil { |
| 98 | + modifiers = append(modifiers, transport.NewHeaderRequestModifier(metaHeaders)) |
| 99 | + } |
| 100 | + return modifiers |
| 101 | +} |
0 commit comments