@@ -20,6 +20,7 @@ import (
20
20
"github.com/ory/dockertest/v3"
21
21
"github.com/ory/dockertest/v3/docker"
22
22
"github.com/stretchr/testify/require"
23
+ "golang.org/x/crypto/bcrypt"
23
24
"golang.org/x/xerrors"
24
25
25
26
"github.com/coder/envbox/buildlog"
@@ -361,6 +362,7 @@ type RegistryConfig struct {
361
362
HostKeyPath string
362
363
TLSPort string
363
364
Image string
365
+ PasswordPath string
364
366
}
365
367
366
368
type RegistryImage string
@@ -381,14 +383,31 @@ func RunLocalDockerRegistry(t testing.TB, pool *dockertest.Pool, conf RegistryCo
381
383
keyPath = "/certs/key.pem"
382
384
)
383
385
384
- resource , err := pool .RunWithOptions (& dockertest.RunOptions {
385
- Repository : registryImage ,
386
- Tag : registryTag ,
387
- Env : []string {
386
+ var (
387
+ envs = []string {
388
+ EnvVar ("REGISTRY_HTTP_ADDR" , "0.0.0.0:443" ),
389
+ }
390
+ )
391
+
392
+ if conf .HostCertPath != "" && conf .HostKeyPath != "" {
393
+ envs = append (envs ,
388
394
EnvVar ("REGISTRY_HTTP_TLS_CERTIFICATE" , certPath ),
389
395
EnvVar ("REGISTRY_HTTP_TLS_KEY" , keyPath ),
390
- EnvVar ("REGISTRY_HTTP_ADDR" , "0.0.0.0:443" ),
391
- },
396
+ )
397
+ }
398
+
399
+ if conf .PasswordPath != "" {
400
+ envs = append (envs ,
401
+ EnvVar ("REGISTRY_AUTH" , "htpasswd" ),
402
+ EnvVar ("REGISTRY_AUTH_HTPASSWD_REALM" , "Test Registry" ),
403
+ EnvVar ("REGISTRY_AUTH_HTPASSWD_PATH" , conf .PasswordPath ),
404
+ )
405
+ }
406
+
407
+ resource , err := pool .RunWithOptions (& dockertest.RunOptions {
408
+ Repository : registryImage ,
409
+ Tag : registryTag ,
410
+ Env : envs ,
392
411
ExposedPorts : []string {"443/tcp" },
393
412
}, func (host * docker.HostConfig ) {
394
413
host .Binds = []string {
@@ -516,3 +535,15 @@ func BindMount(src, dst string, ro bool) docker.HostMount {
516
535
Type : "bind" ,
517
536
}
518
537
}
538
+
539
+ func GenerateRegistryAuth (t * testing.T , directory , username , password string ) string {
540
+ t .Helper ()
541
+
542
+ p , err := bcrypt .GenerateFromPassword ([]byte (password ), bcrypt .DefaultCost )
543
+ require .NoError (t , err )
544
+
545
+ authFile := filepath .Join (directory , "htpasswd" )
546
+ WriteFile (t , authFile , fmt .Sprintf ("%s:%s" , username , string (p )))
547
+
548
+ return authFile
549
+ }
0 commit comments