1- import itertools
2-
31import pytest
42
5- from vault_cli import client , exceptions , testing
3+ from vault_cli import client , exceptions
64
75
86def test_get_client (mocker ):
@@ -476,44 +474,6 @@ def test_vault_client_base_render_template_path_not_found(vault, template):
476474 ({"a" : {"value" : "b" }}, {"value" : "b" }),
477475 # Secret not a string
478476 ({"a" : {"value" : ["yay" ]}}, {"value" : ["yay" ]}),
479- # Secret is a template without variable expansion
480- ({"a" : {"value" : "!template!b" }, "b" : {"value" : "c" }}, {"value" : "b" }),
481- # Secret is a template
482- (
483- {"a" : {"value" : "!template!{{ vault('b').value }}" }, "b" : {"value" : "c" }},
484- {"value" : "c" },
485- ),
486- # Secret is a dict with containing a template
487- (
488- {
489- "a" : {"x" : "!template!{{ vault('b').value }}" , "y" : "yay" },
490- "b" : {"value" : "c" },
491- },
492- {"x" : "c" , "y" : "yay" },
493- ),
494- # Finite recursion
495- (
496- {
497- "a" : {"value" : "!template!{{ vault('b').value }}" },
498- "b" : {"value" : "!template!{{ vault('c').value }}" },
499- "c" : {"value" : "d" },
500- },
501- {"value" : "d" },
502- ),
503- # Infinite Recursion
504- (
505- {
506- "a" : {"value" : "!template!{{ vault('b').value }}" },
507- "b" : {"value" : "!template!{{ vault('c').value }}" },
508- "c" : {"value" : "!template!{{ vault('a').value }}" },
509- },
510- {"value" : '<recursive value "a">' },
511- ),
512- # Direct Recursion
513- (
514- {"a" : {"value" : "!template!{{ vault('a').value }}" }},
515- {"value" : '<recursive value "a">' },
516- ),
517477 ],
518478)
519479def test_vault_client_base_get_secret (vault , vault_contents , expected ):
@@ -522,23 +482,6 @@ def test_vault_client_base_get_secret(vault, vault_contents, expected):
522482 assert vault .get_secret ("a" ) == expected
523483
524484
525- def test_vault_client_base_get_secret_deprecation_warning (vault , caplog ):
526- vault .db = {"a" : {"value" : "!template!b" }}
527- caplog .set_level ("WARNING" )
528-
529- vault .get_secret ("a" )
530- assert "Templated values are deprecated" in caplog .records [0 ].message
531-
532-
533- def test_vault_client_base_get_secret_template_root (vault ):
534- vault .base_path = "base"
535- vault .db = {"/base/a" : {"value" : '!template!{{ vault("a").value }} yay' }}
536-
537- # In case of erroneous caching, e.g. a different cache entry
538- # for /base/a and base/a, we would find '<recursive value "a"> yay yay'
539- assert vault .get_secret ("/base/a" ) == {"value" : '<recursive value "a"> yay' }
540-
541-
542485def test_vault_client_base_get_secret_multiple_keys (vault ):
543486 vault .db = {"rabbitmq/creds/role" : {"username" : "foo" , "password" : "bar" }}
544487 assert vault .get_secret ("rabbitmq/creds/role" ) == {
@@ -547,22 +490,11 @@ def test_vault_client_base_get_secret_multiple_keys(vault):
547490 }
548491
549492
550- def test_vault_client_base_get_secret_with_dict (vault ):
551- vault .db = {
552- "credentials" : {"value" : {"username" : "foo" , "password" : "bar" }},
553- "dsn" : {
554- "value" : "!template!proto://{{ vault('credentials')['value']['username'] }}:{{ vault('credentials').value.password }}@host"
555- },
556- }
557-
558- assert vault .get_secret ("dsn" ) == {"value" : "proto://foo:bar@host" }
559-
560-
561493def test_vault_client_base_get_secret_not_found (vault ):
562494 vault .db = {}
563495
564496 with pytest .raises (exceptions .VaultSecretNotFound ):
565- vault .get_secret ("not-exiting " )
497+ vault .get_secret ("not-existing " )
566498
567499
568500def test_vault_client_base_get_secret_missing_key (vault ):
@@ -572,20 +504,6 @@ def test_vault_client_base_get_secret_missing_key(vault):
572504 vault .get_secret ("a" , key = "username" )
573505
574506
575- def test_vault_client_base_get_secret_template_error (vault , caplog ):
576- vault .db = {"a" : {"key" : "!template!{{" }}
577-
578- with pytest .raises (exceptions .VaultRenderTemplateError ) as exc_info :
579- vault .get_secret ("a" )
580-
581- assert str (exc_info .value ) == 'Error while rendering secret at path "a"'
582- assert (
583- str (exc_info .value .__cause__ )
584- == 'Error while rendering secret value for key "key"'
585- )
586- assert str (exc_info .value .__cause__ .__cause__ ) == "Jinja2 template syntax error"
587-
588-
589507def test_vault_client_base_lookup_token (vault ):
590508 assert vault .lookup_token () == {"data" : {"expire_time" : "2100-01-01T00:00:00" }}
591509
@@ -672,56 +590,6 @@ def test_vault_client_base_get_secret_implicit_cache(vault):
672590 assert vault .get_secret ("a" ) == {"value" : "b" }
673591
674592
675- class RaceConditionTestVaultClient (testing .TestVaultClient ):
676- def __init__ (self , * args , ** kwargs ):
677- super ().__init__ (* args , ** kwargs )
678- self .counter = itertools .count ()
679-
680- def _get_secret (self , path ):
681- if path == "a" :
682- val = next (self .counter )
683- return {"b" : f"b{ val } " , "c" : f"c{ val } " }
684- return super ()._get_secret (path )
685-
686-
687- def test_vault_client_base_get_secret_implicit_cache_no_race_condition ():
688- # In this test we check that if a value is read several times by
689- # a template, implicit caching makes sure we have the same value
690- # every time.
691-
692- # Values returned by this client keep changing
693-
694- vault = RaceConditionTestVaultClient ()
695-
696- with vault :
697- assert vault .get_secret ("a" ) == {"b" : "b0" , "c" : "c0" }
698- with vault :
699- assert vault .get_secret ("a" ) == {"b" : "b1" , "c" : "c1" }
700-
701- vault .db = {"d" : {"value" : """!template!{{ vault("a").b }}-{{ vault("a").c }}""" }}
702-
703- # b2-c3 would be the value if caching didn't work.
704- with vault :
705- assert vault .get_secret ("d" ) == {"value" : "b2-c2" }
706-
707-
708- def test_vault_client_base_get_secrets_implicit_cache_no_race_condition ():
709- # In this test, the same value is read twice by get-all and template
710- # We check that 2 values are consistent
711-
712- vault = RaceConditionTestVaultClient ()
713-
714- vault .db = {
715- "a" : {},
716- "d" : {"value" : """!template!{{ vault("a").b }}-{{ vault("a").c }}""" },
717- }
718-
719- assert vault .get_secrets ("" ) == {
720- "a" : {"b" : "b0" , "c" : "c0" },
721- "d" : {"value" : "b0-c0" },
722- }
723-
724-
725593def test_vault_client_base_get_secret_explicit_cache (vault ):
726594 vault .db = {"a" : {"value" : "b" }}
727595 with vault :
0 commit comments