(ctx context.Context, t testing.TB, c *dagger.Client, hostname string, dir *dagger.Directory, username string, token *dagger.Secret)
| 2375 | } |
| 2376 | |
| 2377 | func httpServiceDirAuth(ctx context.Context, t testing.TB, c *dagger.Client, hostname string, dir *dagger.Directory, username string, token *dagger.Secret) (*dagger.Service, string) { |
| 2378 | t.Helper() |
| 2379 | |
| 2380 | if username == "" { |
| 2381 | username = "x-access-token" |
| 2382 | } |
| 2383 | |
| 2384 | var tokenPlaintext string |
| 2385 | if token != nil { |
| 2386 | var err error |
| 2387 | tokenPlaintext, err = token.Plaintext(ctx) |
| 2388 | require.NoError(t, err) |
| 2389 | } |
| 2390 | |
| 2391 | tmpl, err := template.New("").Parse(` |
| 2392 | server { |
| 2393 | listen 80; |
| 2394 | server_name localhost; |
| 2395 | |
| 2396 | location / { |
| 2397 | root /usr/share/nginx/html; |
| 2398 | index index.html index.htm; |
| 2399 | } |
| 2400 | |
| 2401 | {{ if .token }} |
| 2402 | auth_basic "Restricted"; |
| 2403 | auth_basic_user_file /usr/share/nginx/htpasswd; |
| 2404 | {{ end }} |
| 2405 | } |
| 2406 | `) |
| 2407 | require.NoError(t, err) |
| 2408 | |
| 2409 | var config bytes.Buffer |
| 2410 | err = tmpl.Execute(&config, map[string]any{ |
| 2411 | "token": tokenPlaintext, |
| 2412 | }) |
| 2413 | require.NoError(t, err) |
| 2414 | |
| 2415 | svc := c.Container(). |
| 2416 | From("nginx"). |
| 2417 | WithNewFile("/etc/nginx/conf.d/default.conf", config.String()). |
| 2418 | WithMountedDirectory("/usr/share/nginx/html", dir). |
| 2419 | WithEnvVariable("CACHE_BUSTER", fmt.Sprintf("%s-%d", username, time.Now().UnixNano())). |
| 2420 | WithMountedSecret("/usr/share/nginx/htpasswd", c.SetSecret("htpasswd-"+identity.NewID(), username+":{PLAIN}"+tokenPlaintext), dagger.ContainerWithMountedSecretOpts{ |
| 2421 | Owner: "nginx", |
| 2422 | }). |
| 2423 | WithExposedPort(80). |
| 2424 | AsService(dagger.ContainerAsServiceOpts{UseEntrypoint: true}) |
| 2425 | |
| 2426 | if hostname == "" { |
| 2427 | hostname, err = svc.Hostname(ctx) |
| 2428 | require.NoError(t, err) |
| 2429 | } else { |
| 2430 | svc = svc.WithHostname(hostname) |
| 2431 | } |
| 2432 | url := fmt.Sprintf("http://%s", hostname) |
| 2433 | return svc, url |
| 2434 | } |
no test coverage detected