|
| 1 | +// Copyright The Enterprise Contract Contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +//type Closer func() |
| 15 | +// SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +package registry |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "crypto/tls" |
| 22 | + "crypto/x509" |
| 23 | + _ "embed" |
| 24 | + "fmt" |
| 25 | + "log" |
| 26 | + "net" |
| 27 | + "net/http" |
| 28 | + "net/http/httptest" |
| 29 | + "net/url" |
| 30 | + "os" |
| 31 | + "path" |
| 32 | + "time" |
| 33 | + |
| 34 | + "github.com/docker/docker/api/types/container" |
| 35 | + "github.com/smarty/cproxy/v2" |
| 36 | + "github.com/testcontainers/testcontainers-go" |
| 37 | + "github.com/testcontainers/testcontainers-go/modules/registry" |
| 38 | + "github.com/testcontainers/testcontainers-go/wait" |
| 39 | + |
| 40 | + "github.com/enterprise-contract/ec-cli/benchmark/internal/suite" |
| 41 | +) |
| 42 | + |
| 43 | +//go:embed fake_quay.cer |
| 44 | +var certificate []byte |
| 45 | + |
| 46 | +//go:embed fake_quay.key |
| 47 | +var key []byte |
| 48 | + |
| 49 | +type registryCloser struct { |
| 50 | + tempDir string |
| 51 | + container *registry.RegistryContainer |
| 52 | + proxy *httptest.Server |
| 53 | +} |
| 54 | + |
| 55 | +func (r *registryCloser) Close() { |
| 56 | + if r == nil { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + if r.tempDir != "" { |
| 61 | + _ = os.RemoveAll(r.tempDir) |
| 62 | + } |
| 63 | + |
| 64 | + if r.container != nil { |
| 65 | + _ = r.container.Terminate(context.Background()) |
| 66 | + } |
| 67 | + |
| 68 | + if r.proxy != nil { |
| 69 | + r.proxy.Close() |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +type registryProxy struct { |
| 74 | + registry string |
| 75 | +} |
| 76 | + |
| 77 | +func (f *registryProxy) IsAuthorized(_ http.ResponseWriter, req *http.Request) bool { |
| 78 | + req.RequestURI = fmt.Sprintf("https://%s/", f.registry) |
| 79 | + req.URL, _ = url.Parse(req.RequestURI) |
| 80 | + req.Host = f.registry |
| 81 | + |
| 82 | + return true |
| 83 | +} |
| 84 | + |
| 85 | +func Launch(data string) (suite.Closer, error) { |
| 86 | + ctx := context.Background() |
| 87 | + |
| 88 | + env := testcontainers.WithEnv(map[string]string{ |
| 89 | + "REGISTRY_HTTP_TLS_CERTIFICATE": "/tls/fake_quay.cer", |
| 90 | + "REGISTRY_HTTP_TLS_KEY": "/tls/fake_quay.key", |
| 91 | + }) |
| 92 | + |
| 93 | + dir, err := os.MkdirTemp("", "ec-benchmark-tls-*") |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + closer := ®istryCloser{dir, nil, nil} |
| 98 | + |
| 99 | + if err := os.Setenv("https_proxy", "http://localhost:3128"); err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + |
| 103 | + if err := os.Chmod(dir, 0755); err != nil { |
| 104 | + return closer.Close, err |
| 105 | + } |
| 106 | + |
| 107 | + certPath := path.Join(dir, "fake_quay.cer") |
| 108 | + if err := os.WriteFile(certPath, certificate, 0600); err != nil { |
| 109 | + return closer.Close, err |
| 110 | + } |
| 111 | + |
| 112 | + if err := os.Setenv("SSL_CERT_FILE", certPath); err != nil { |
| 113 | + return closer.Close, err |
| 114 | + } |
| 115 | + |
| 116 | + if err := os.WriteFile(path.Join(dir, "fake_quay.key"), key, 0600); err != nil { |
| 117 | + return closer.Close, err |
| 118 | + } |
| 119 | + |
| 120 | + rp := registryProxy{} |
| 121 | + proxy := cproxy.New(cproxy.Options.Filter(&rp), cproxy.Options.Logger(log.Default())) |
| 122 | + proxyServer := httptest.NewUnstartedServer(proxy) |
| 123 | + proxyServer.Listener, err = net.Listen("tcp", "127.0.0.1:3128") |
| 124 | + if err != nil { |
| 125 | + return closer.Close, err |
| 126 | + } |
| 127 | + proxyServer.Config.ErrorLog = log.Default() |
| 128 | + proxyServer.Start() |
| 129 | + closer.proxy = proxyServer |
| 130 | + |
| 131 | + tlsMount := testcontainers.WithHostConfigModifier(func(hostConfig *container.HostConfig) { |
| 132 | + hostConfig.Binds = append(hostConfig.Binds, fmt.Sprintf("%s:/tls:ro,Z", dir)) |
| 133 | + }) |
| 134 | + |
| 135 | + roots := x509.NewCertPool() |
| 136 | + roots.AppendCertsFromPEM(certificate) |
| 137 | + waitStrategy := testcontainers.WithWaitStrategy(wait.ForHTTP("/"). |
| 138 | + WithPort("5000/tcp"). |
| 139 | + WithTLS(true, &tls.Config{ |
| 140 | + RootCAs: roots, |
| 141 | + MinVersion: tls.VersionTLS13, |
| 142 | + }). |
| 143 | + WithStartupTimeout(10 * time.Second)) |
| 144 | + |
| 145 | + opts := []testcontainers.ContainerCustomizer{ |
| 146 | + registry.WithData(data), |
| 147 | + env, |
| 148 | + tlsMount, |
| 149 | + waitStrategy, |
| 150 | + } |
| 151 | + |
| 152 | + if false { |
| 153 | + opts = append(opts, |
| 154 | + testcontainers.WithConfigModifier(func(config *container.Config) { |
| 155 | + config.AttachStdout = true |
| 156 | + }), |
| 157 | + testcontainers.WithLogger(log.Default())) |
| 158 | + } |
| 159 | + |
| 160 | + r, err := registry.Run(ctx, "registry:2.8.3", opts...) |
| 161 | + if err != nil { |
| 162 | + return closer.Close, err |
| 163 | + } |
| 164 | + closer.container = r |
| 165 | + |
| 166 | + rp.registry = r.RegistryName |
| 167 | + |
| 168 | + return closer.Close, nil |
| 169 | +} |
0 commit comments