@@ -27,13 +27,20 @@ var (
2727 // testVaultAddress is the HTTP/S address of the Vault server, it is set
2828 // by TestMain after booting it.
2929 testVaultAddress string
30+ // Whether to skip all Docker-based tests.
31+ testSkipDocker = false
3032)
3133
3234// TestMain initializes a Vault server using Docker, writes the HTTP address to
3335// testVaultAddress, waits for it to become ready to serve requests, and enables
3436// Vault Transit on the testEnginePath. It then runs all the tests, which can
3537// make use of the various `test*` variables.
3638func TestMain (m * testing.M ) {
39+ if testSkipDocker {
40+ os .Exit (m .Run ())
41+ return
42+ }
43+
3744 // Uses a sensible default on Windows (TCP/HTTP) and Linux/MacOS (socket)
3845 pool , err := dockertest .NewPool ("" )
3946 if err != nil {
@@ -179,6 +186,10 @@ func TestNewMasterKeyFromURI(t *testing.T) {
179186}
180187
181188func TestMasterKey_Encrypt (t * testing.T ) {
189+ if testSkipDocker {
190+ return
191+ }
192+
182193 key := NewMasterKey (testVaultAddress , testEnginePath , "encrypt" )
183194 (Token (testVaultToken )).ApplyToMasterKey (key )
184195 assert .NoError (t , createVaultKey (key ))
@@ -207,6 +218,10 @@ func TestMasterKey_Encrypt(t *testing.T) {
207218}
208219
209220func TestMasterKey_EncryptIfNeeded (t * testing.T ) {
221+ if testSkipDocker {
222+ return
223+ }
224+
210225 key := NewMasterKey (testVaultAddress , testEnginePath , "encrypt-if-needed" )
211226 (Token (testVaultToken )).ApplyToMasterKey (key )
212227 assert .NoError (t , createVaultKey (key ))
@@ -226,6 +241,10 @@ func TestMasterKey_EncryptedDataKey(t *testing.T) {
226241}
227242
228243func TestMasterKey_Decrypt (t * testing.T ) {
244+ if testSkipDocker {
245+ return
246+ }
247+
229248 key := NewMasterKey (testVaultAddress , testEnginePath , "decrypt" )
230249 (Token (testVaultToken )).ApplyToMasterKey (key )
231250 assert .NoError (t , createVaultKey (key ))
@@ -254,6 +273,10 @@ func TestMasterKey_Decrypt(t *testing.T) {
254273}
255274
256275func TestMasterKey_EncryptDecrypt_RoundTrip (t * testing.T ) {
276+ if testSkipDocker {
277+ return
278+ }
279+
257280 token := Token (testVaultToken )
258281
259282 encryptKey := NewMasterKey (testVaultAddress , testEnginePath , "roundtrip" )
@@ -519,3 +542,144 @@ func createVaultKey(key *MasterKey) error {
519542 _ , err = client .Logical ().Read (p )
520543 return err
521544}
545+
546+ func TestAllowlistParse (t * testing.T ) {
547+ t .Run ("success" , func (t * testing.T ) {
548+ al , err := parseAllowlistString ("all" )
549+ assert .NoError (t , err )
550+ assert .Equal (t , allowList {
551+ All : true ,
552+ URIs : nil ,
553+ }, al )
554+
555+ al , err = parseAllowlistString ("none" )
556+ assert .NoError (t , err )
557+ assert .Equal (t , allowList {
558+ All : false ,
559+ URIs : nil ,
560+ }, al )
561+
562+ al , err = parseAllowlistString ("non" )
563+ assert .NoError (t , err )
564+ assert .Equal (t , allowList {
565+ All : false ,
566+ URIs : []string {
567+ "non/" ,
568+ },
569+ }, al )
570+
571+ al , err = parseAllowlistString ("foo,bar/,baz" )
572+ assert .NoError (t , err )
573+ assert .Equal (t , allowList {
574+ All : false ,
575+ URIs : []string {
576+ "foo/" ,
577+ "bar/" ,
578+ "baz/" ,
579+ },
580+ }, al )
581+
582+ al , err = parseAllowlistString (" foo/ , bar, baz " )
583+ assert .NoError (t , err )
584+ assert .Equal (t , allowList {
585+ All : false ,
586+ URIs : []string {
587+ "foo/" ,
588+ "bar/" ,
589+ "baz/" ,
590+ },
591+ }, al )
592+ })
593+
594+ t .Run ("error" , func (t * testing.T ) {
595+ al , err := parseAllowlistString ("" )
596+ assert .Error (t , err )
597+ assert .Equal (t , "SOPS_HC_VAULT_ALLOWLIST's entry 1 is empty" , err .Error ())
598+ assert .Equal (t , allowList {
599+ All : false ,
600+ URIs : nil ,
601+ }, al )
602+
603+ al , err = parseAllowlistString ("," )
604+ assert .Error (t , err )
605+ assert .Equal (t , "SOPS_HC_VAULT_ALLOWLIST's entry 1 is empty" , err .Error ())
606+ assert .Equal (t , allowList {
607+ All : false ,
608+ URIs : nil ,
609+ }, al )
610+
611+ al , err = parseAllowlistString (",a" )
612+ assert .Error (t , err )
613+ assert .Equal (t , "SOPS_HC_VAULT_ALLOWLIST's entry 1 is empty" , err .Error ())
614+ assert .Equal (t , allowList {
615+ All : false ,
616+ URIs : nil ,
617+ }, al )
618+
619+ al , err = parseAllowlistString ("a," )
620+ assert .Error (t , err )
621+ assert .Equal (t , "SOPS_HC_VAULT_ALLOWLIST's entry 2 is empty" , err .Error ())
622+ assert .Equal (t , allowList {
623+ All : false ,
624+ URIs : nil ,
625+ }, al )
626+ })
627+ }
628+
629+ func TestAllowlistAllow (t * testing.T ) {
630+ al , _ := parseAllowlistString ("all" )
631+ assert .Equal (t , al .Allows ("" ), true )
632+ assert .Equal (t , al .Allows ("foo" ), true )
633+ assert .Equal (t , al .Allows ("bar" ), true )
634+ assert .Equal (t , al .Allows ("http://example.com" ), true )
635+ assert .Equal (t , al .Allows ("http://example.com/" ), true )
636+ assert .Equal (t , al .Allows ("https://example.com/foo" ), true )
637+
638+ al , _ = parseAllowlistString ("none" )
639+ assert .Equal (t , al .Allows ("" ), false )
640+ assert .Equal (t , al .Allows ("foo" ), false )
641+ assert .Equal (t , al .Allows ("bar" ), false )
642+ assert .Equal (t , al .Allows ("http://example.com" ), false )
643+ assert .Equal (t , al .Allows ("http://example.com/" ), false )
644+ assert .Equal (t , al .Allows ("https://example.com/foo" ), false )
645+
646+ al , _ = parseAllowlistString ("http://example.com" )
647+ assert .Equal (t , al .Allows ("http://example.co" ), false )
648+ assert .Equal (t , al .Allows ("http://example.com" ), true )
649+ assert .Equal (t , al .Allows ("http://example.comm" ), false )
650+ assert .Equal (t , al .Allows ("http://example.com:80" ), false )
651+ assert .Equal (t , al .Allows ("http://example.com/" ), true )
652+ assert .Equal (t , al .Allows ("http://example.com/foo" ), true )
653+ assert .Equal (t , al .Allows ("http://fiz@example.com/" ), false )
654+ assert .Equal (t , al .Allows ("http://example.com:123/" ), false )
655+ assert .Equal (t , al .Allows ("https://example.com" ), false )
656+ assert .Equal (t , al .Allows ("https://example.com/" ), false )
657+ assert .Equal (t , al .Allows ("" ), false )
658+
659+ al , _ = parseAllowlistString ("http://example.com, https://example.org/bar/,http://foo:80" )
660+ assert .Equal (t , al .Allows ("http://example.com" ), true )
661+ assert .Equal (t , al .Allows ("http://example.com/" ), true )
662+ assert .Equal (t , al .Allows ("http://example.com/foo" ), true )
663+ assert .Equal (t , al .Allows ("http://fiz@example.com/" ), false )
664+ assert .Equal (t , al .Allows ("http://example.com:123/" ), false )
665+ assert .Equal (t , al .Allows ("https://example.com" ), false )
666+ assert .Equal (t , al .Allows ("https://example.com/" ), false )
667+ assert .Equal (t , al .Allows ("http://example.org" ), false )
668+ assert .Equal (t , al .Allows ("http://example.org/" ), false )
669+ assert .Equal (t , al .Allows ("http://example.org/foo" ), false )
670+ assert .Equal (t , al .Allows ("http://fiz@example.org/" ), false )
671+ assert .Equal (t , al .Allows ("http://example.org:123/" ), false )
672+ assert .Equal (t , al .Allows ("https://example.org" ), false )
673+ assert .Equal (t , al .Allows ("https://example.org/" ), false )
674+ assert .Equal (t , al .Allows ("https://example.org/bar" ), true )
675+ assert .Equal (t , al .Allows ("https://example.org/barr" ), false )
676+ assert .Equal (t , al .Allows ("https://example.org/bar/" ), true )
677+ assert .Equal (t , al .Allows ("https://example.org/bar/baz" ), true )
678+ assert .Equal (t , al .Allows ("http://foo" ), false )
679+ assert .Equal (t , al .Allows ("http://foo/" ), false )
680+ assert .Equal (t , al .Allows ("http://foo:80" ), true )
681+ assert .Equal (t , al .Allows ("http://foo:80/" ), true )
682+ assert .Equal (t , al .Allows ("http://foo:8080" ), false )
683+ assert .Equal (t , al .Allows ("http://foo:8080/" ), false )
684+ assert .Equal (t , al .Allows ("" ), false )
685+ }
0 commit comments