1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
package modconfig
import (
"context"
"encoding/json"
"fmt"
"io/fs"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path"
"path/filepath"
"slices"
"strings"
"sync/atomic"
"testing"
"time"
"cuelabs.dev/go/oci/ociregistry/ocimem"
"cuelabs.dev/go/oci/ociregistry/ociserver"
"github.com/go-quicktest/qt"
"golang.org/x/oauth2"
"golang.org/x/sync/errgroup"
"golang.org/x/tools/txtar"
"cuelang.org/go/internal/cueconfig"
"cuelang.org/go/internal/cueversion"
"cuelang.org/go/mod/modcache"
"cuelang.org/go/mod/modregistrytest"
"cuelang.org/go/mod/module"
)
// TODO: the test below acts as a smoke test for the functionality here,
// but more of the behavior is tested in the cmd/cue script tests.
// We should do more of it here too.
func TestNewRegistry(t *testing.T) {
modules := txtar.Parse([]byte(`
-- r1/foo.example_v0.0.1/cue.mod/module.cue --
module: "foo.example@v0"
language: version: "v0.8.0"
deps: "bar.example@v0": v: "v0.0.1"
-- r1/foo.example_v0.0.1/bar/bar.cue --
package bar
-- r1/bar.example_v0.0.1/cue.mod/module.cue --
module: "bar.example@v0"
language: version: "v0.8.0"
-- r1/bar.example_v0.0.1/y/y.cue --
package y
-- r2/auth.json --
{
"username": "bob",
"password": "somePassword"
}
-- r2/bar.example_v0.0.1/cue.mod/module.cue --
module: "bar.example@v0"
language: version: "v0.8.0"
-- r2/bar.example_v0.0.1/x/x.cue --
package x
`))
fsys, err := txtar.FS(modules)
qt.Assert(t, qt.IsNil(err))
r1fs, err := fs.Sub(fsys, "r1")
qt.Assert(t, qt.IsNil(err))
r1, err := modregistrytest.New(r1fs, "")
qt.Assert(t, qt.IsNil(err))
r2fs, err := fs.Sub(fsys, "r2")
qt.Assert(t, qt.IsNil(err))
r2, err := modregistrytest.New(r2fs, "")
qt.Assert(t, qt.IsNil(err))
dir := t.TempDir()
t.Setenv("DOCKER_CONFIG", dir)
dockerCfg, err := json.Marshal(dockerConfig{
Auths: map[string]authConfig{
r2.Host(): {
Username: "bob",
Password: "somePassword",
},
},
})
qt.Assert(t, qt.IsNil(err))
err = os.WriteFile(filepath.Join(dir, "config.json"), dockerCfg, 0o666)
qt.Assert(t, qt.IsNil(err))
t.Setenv("CUE_REGISTRY",
fmt.Sprintf("foo.example=%s+insecure,%s+insecure",
r1.Host(),
r2.Host(),
))
cacheDir := filepath.Join(dir, "cache")
t.Setenv("CUE_CACHE_DIR", cacheDir)
t.Cleanup(func() {
modcache.RemoveAll(cacheDir)
})
var transportInvoked atomic.Bool
r, err := NewRegistry(&Config{
Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
transportInvoked.Store(true)
return http.DefaultTransport.RoundTrip(req)
}),
})
qt.Assert(t, qt.IsNil(err))
ctx := context.Background()
gotRequirements, err := r.Requirements(ctx, module.MustNewVersion("foo.example@v0", "v0.0.1"))
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.DeepEquals(gotRequirements, []module.Version{
module.MustNewVersion("bar.example@v0", "v0.0.1"),
}))
loc, err := r.Fetch(ctx, module.MustNewVersion("bar.example@v0", "v0.0.1"))
qt.Assert(t, qt.IsNil(err))
data, err := fs.ReadFile(loc.FS, path.Join(loc.Dir, "x/x.cue"))
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(string(data), "package x\n"))
// Check that we can make a Resolver with the same configuration.
resolver, err := NewResolver(nil)
qt.Assert(t, qt.IsNil(err))
gotAllHosts := resolver.AllHosts()
wantAllHosts := []Host{{Name: r1.Host(), Insecure: true}, {Name: r2.Host(), Insecure: true}}
byHostname := func(a, b Host) int { return strings.Compare(a.Name, b.Name) }
slices.SortFunc(gotAllHosts, byHostname)
slices.SortFunc(wantAllHosts, byHostname)
qt.Assert(t, qt.DeepEquals(gotAllHosts, wantAllHosts))
// Check that the underlying custom transport was used.
qt.Assert(t, qt.IsTrue(transportInvoked.Load()))
}
func TestDefaultTransportSetsUserAgent(t *testing.T) {
// This test also checks that providing a nil Config.Transport
// does the right thing.
regFS, err := txtar.FS(txtar.Parse([]byte(`
-- bar.example_v0.0.1/cue.mod/module.cue --
module: "bar.example@v0"
language: version: "v0.8.0"
-- bar.example_v0.0.1/x/x.cue --
package x
`)))
qt.Assert(t, qt.IsNil(err))
ctx := context.Background()
rmem := ocimem.NewWithConfig(&ocimem.Config{ImmutableTags: true})
err = modregistrytest.Upload(ctx, rmem, regFS)
qt.Assert(t, qt.IsNil(err))
rh := ociserver.New(rmem, nil)
agent := cueversion.UserAgent("cuelang.org/go")
checked := false
checkUserAgentHandler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
qt.Check(t, qt.Equals(req.UserAgent(), agent))
checked = true
rh.ServeHTTP(w, req)
})
srv := httptest.NewServer(checkUserAgentHandler)
u, err := url.Parse(srv.URL)
qt.Assert(t, qt.IsNil(err))
dir := t.TempDir()
t.Setenv("DOCKER_CONFIG", dir)
t.Setenv("CUE_REGISTRY", u.Host+"+insecure")
cacheDir := filepath.Join(dir, "cache")
t.Setenv("CUE_CACHE_DIR", cacheDir)
t.Cleanup(func() {
modcache.RemoveAll(cacheDir)
})
r, err := NewRegistry(nil)
qt.Assert(t, qt.IsNil(err))
gotRequirements, err := r.Requirements(ctx, module.MustNewVersion("bar.example@v0", "v0.0.1"))
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.HasLen(gotRequirements, 0))
qt.Assert(t, qt.IsTrue(checked))
}
// TestConcurrentTokenRefresh verifies that concurrent OAuth token refreshes,
// including logins.json updates, are properly synchronized.
func TestConcurrentTokenRefresh(t *testing.T) {
// Start N registry instances, each containing one CUE module and running
// in its own HTTP server instance. Each instance is protected with its
// own OAuth token, which is initially expired, requiring a refresh token
// request upon first invocation.
var registries [20]struct {
mod string
host string
}
var counter int32 = 0
for i := range registries {
reg := ®istries[i]
reg.mod = fmt.Sprintf("foo.mod%02d", i)
fsys, err := txtar.FS(txtar.Parse([]byte(fmt.Sprintf(`
-- %s_v0.0.1/cue.mod/module.cue --
module: "%s@v0"
language: version: "v0.8.0"
-- %s_v0.0.1/bar/bar.cue --
package bar
`, reg.mod, reg.mod, reg.mod))))
qt.Assert(t, qt.IsNil(err))
mux := http.NewServeMux()
r := ocimem.New()
err = modregistrytest.Upload(context.Background(), r, fsys)
qt.Assert(t, qt.IsNil(err))
rh := ociserver.New(r, nil)
mux.HandleFunc("/v2/", func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
if !strings.HasPrefix(auth, fmt.Sprintf("Bearer access_%d_", i)) {
w.WriteHeader(401)
fmt.Fprintf(w, "server %d: unexpected auth header: %s", i, auth)
return
}
rh.ServeHTTP(w, r)
})
mux.HandleFunc("/login/oauth/token", func(w http.ResponseWriter, r *http.Request) {
ctr := atomic.AddInt32(&counter, 1)
writeJSON(w, 200, oauth2.Token{
AccessToken: fmt.Sprintf("access_%d_%d", i, ctr),
TokenType: "Bearer",
RefreshToken: fmt.Sprintf("refresh_%d", ctr),
ExpiresIn: 300,
})
})
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)
u, err := url.Parse(srv.URL)
qt.Assert(t, qt.IsNil(err))
reg.host = u.Host
}
expiry := time.Now()
logins := &cueconfig.Logins{
Registries: map[string]cueconfig.RegistryLogin{},
}
registryConf := ""
for i, reg := range registries {
logins.Registries[reg.host] = cueconfig.RegistryLogin{
AccessToken: fmt.Sprintf("access_%d_x", i),
TokenType: "Bearer",
RefreshToken: "refresh_x",
Expiry: &expiry,
}
if registryConf != "" {
registryConf += ","
}
registryConf += fmt.Sprintf("%s=%s+insecure", reg.mod, reg.host)
}
dir := t.TempDir()
configDir := filepath.Join(dir, "config")
t.Setenv("CUE_CONFIG_DIR", configDir)
err := os.MkdirAll(configDir, 0o777)
qt.Assert(t, qt.IsNil(err))
// Check logins.json validation.
logins.Registries["blank"] = cueconfig.RegistryLogin{TokenType: "Bearer"}
err = cueconfig.WriteLogins(filepath.Join(configDir, "logins.json"), logins)
delete(logins.Registries, "blank")
qt.Assert(t, qt.IsNil(err))
_, err = cueconfig.ReadLogins(filepath.Join(configDir, "logins.json"))
qt.Assert(t, qt.ErrorMatches(err, "invalid .*logins.json: missing access_token for registry blank"))
// Check write-read round-trip.
err = cueconfig.WriteLogins(filepath.Join(configDir, "logins.json"), logins)
qt.Assert(t, qt.IsNil(err))
logins2, err := cueconfig.ReadLogins(filepath.Join(configDir, "logins.json"))
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.DeepEquals(logins2, logins))
t.Setenv("CUE_REGISTRY", registryConf)
cacheDir := filepath.Join(dir, "cache")
t.Setenv("CUE_CACHE_DIR", cacheDir)
t.Cleanup(func() {
modcache.RemoveAll(cacheDir)
})
r, err := NewRegistry(nil)
qt.Assert(t, qt.IsNil(err))
g := new(errgroup.Group)
for i := range registries {
mod := registries[i].mod
g.Go(func() error {
ctx := context.Background()
loc, err := r.Fetch(ctx, module.MustNewVersion(mod+"@v0", "v0.0.1"))
if err != nil {
return err
}
data, err := fs.ReadFile(loc.FS, path.Join(loc.Dir, "bar/bar.cue"))
if err != nil {
return err
}
if string(data) != "package bar\n" {
return fmt.Errorf("unexpected data: %q", string(data))
}
return nil
})
}
err = g.Wait()
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(int(counter), len(registries)))
}
// dockerConfig describes the minimal subset of the docker
// configuration file necessary to check that authentication
// is correction hooked up.
type dockerConfig struct {
Auths map[string]authConfig `json:"auths"`
}
// authConfig contains authorization information for connecting to a Registry.
type authConfig struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}
type roundTripperFunc func(*http.Request) (*http.Response, error)
func (f roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}
func writeJSON(w http.ResponseWriter, statusCode int, v any) {
b, err := json.Marshal(v)
if err != nil {
// should never happen
panic(err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
w.Write(b)
}
|