Repository: peopledoc/vault-cli

Issue:
vault get-all is all funky when path ends with /

Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/peopledoc/vault-cli/pull/140?src=pr&el=h1) Report
> Merging [#140](https://codecov.io/gh/peopledoc/vault-cli/pull/140?src=pr&el=desc) into [master](https://codecov.io/gh/peopledoc/vault-cli/commit/35c185d17c1ca70339900f7236746d000273e783?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/peopledoc/vault-cli/pull/140/graphs/tree.svg?width=650&token=fjUxXKxpOh&height=150&src=pr)](https://codecov.io/gh/peopledoc/vault-cli/pull/140?src=pr&el=tree)

```diff
@@          Coverage Diff          @@
##           master   #140   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          12     12           
  Lines         800    804    +4     
  Branches      103    103           
=====================================
+ Hits          800    804    +4
```

| Flag | Coverage Δ | |
|---|---|---|
| #integration | `67.41% <50%> (-0.09%)` | :arrow_down: |
| #unit | `100% <100%> (ø)` | :arrow_up: |

| [Impacted Files](https://codecov.io/gh/peopledoc/vault-cli/pull/140?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [vault\_cli/client.py](https://codecov.io/gh/peopledoc/vault-cli/pull/140/diff?src=pr&el=tree#diff-dmF1bHRfY2xpL2NsaWVudC5weQ==) | `100% <100%> (ø)` | :arrow_up: |
| [vault\_cli/testing.py](https://codecov.io/gh/peopledoc/vault-cli/pull/140/diff?src=pr&el=tree#diff-dmF1bHRfY2xpL3Rlc3RpbmcucHk=) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/peopledoc/vault-cli/pull/140?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/peopledoc/vault-cli/pull/140?src=pr&el=footer). Last update [35c185d...2f3e47a](https://codecov.io/gh/peopledoc/vault-cli/pull/140?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

SdgJlbl: Problem also impacts the library, so fixing in `client.py` seems more relevant.

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4080144..d4604f9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@ CHANGELOG
 - Omit secrets in `vault env` when path contains forbidden chars (#138)
 - `vault env` fails properly when program name is not supplied
 - `vault env` now supports `-` and ` ` (spaces) in variable names, changing them into `_`
+- Fix trailing slash in path issue in `get-all` and `delete-all` (#135)
 
 1.1.0 (2020-02-28)
 -------------------
diff --git a/vault_cli/client.py b/vault_cli/client.py
index d849d72..a5fc3d5 100644
--- a/vault_cli/client.py
+++ b/vault_cli/client.py
@@ -265,6 +265,7 @@ class VaultClientBase:
         types.JSONDict
             {"folder/subfolder": {"secret_key": "secret_value"}}
         """
+        path = path.rstrip("/")
         try:
             secrets_paths = list(
                 self._browse_recursive_secrets(path=path, render=render)
@@ -400,6 +401,7 @@ class VaultClientBase:
 
     def delete_all_secrets_iter(self, *paths: str) -> Iterable[str]:
         for path in paths:
+            path = path.rstrip("/")
             secrets_paths = self._browse_recursive_secrets(path=path, render=False)
             for secret_path in secrets_paths:
                 yield secret_path


Test patch:
diff --git a/tests/unit/test_client_base.py b/tests/unit/test_client_base.py
index 7e963db..93b8176 100644
--- a/tests/unit/test_client_base.py
+++ b/tests/unit/test_client_base.py
@@ -137,6 +137,10 @@ def test_vault_client_base_get_all_secrets(vault):
 
     assert result == {"a": {"c": {"value": "secret-ac"}}}
 
+    result = vault.get_all_secrets("a/")
+
+    assert result == {"a": {"c": {"value": "secret-ac"}}}
+
 
 def test_vault_client_base_get_all_secrets_flat(vault):
     vault.db = {"a/c": {"value": "secret-ac"}, "b": {"value": "secret-b"}}
@@ -152,13 +156,16 @@ def test_vault_client_base_get_all_secrets_flat(vault):
 
 @pytest.mark.parametrize(
     "input, expected",
-    [("a", {"a/c": {"value": "secret-ac"}}), ("b", {"b": {"value": "secret-b"}})],
+    [
+        ("a", {"a/c": {"value": "secret-ac"}}),
+        ("b", {"b": {"value": "secret-b"}}),
+        ("a/", {"a/c": {"value": "secret-ac"}}),
+    ],
 )
 def test_vault_client_base_get_secrets(vault, input, expected):
     vault.db = {"a/c": {"value": "secret-ac"}, "b": {"value": "secret-b"}}
 
     result = vault.get_secrets(input)
-
     assert result == expected
 
 
@@ -203,6 +210,14 @@ def test_vault_client_base_delete_all_secrets_no_generator(vault):
     assert vault.db == {}
 
 
+def test_vault_client_base_delete_all_secrets_trailing_slash(vault):
+    vault.db = {"a/c": {"value": "secret-ac"}}
+
+    assert vault.delete_all_secrets("a/") == ["a/c"]
+
+    assert vault.db == {}
+
+
 def test_vault_client_base_context_manager(vault):
 
     with vault as c:
diff --git a/vault_cli/testing.py b/vault_cli/testing.py
index 396feae..2d6c758 100644
--- a/vault_cli/testing.py
+++ b/vault_cli/testing.py
@@ -23,6 +23,7 @@ class TestVaultClient(client.VaultClientBase):
         pass
 
     def _get_secret(self, path):
+        path = path.rstrip("/")
         if path in self.forbidden_get_paths:
             raise exceptions.VaultForbidden()
         try:
@@ -31,6 +32,7 @@ class TestVaultClient(client.VaultClientBase):
             raise exceptions.VaultSecretNotFound()
 
     def _list_secrets(self, path):
+        path = path.rstrip("/")
         if path in self.forbidden_list_paths:
             raise exceptions.VaultForbidden()
         # Just reproducing in memory the behaviour of the real list_secrets