Skip to content

Commit 4b9c986

Browse files
authored
Merge pull request #3 from tchlr/bugfix/eof
fix: rekey of encrypted values at the end of the file
2 parents 5c82e3b + 277fb02 commit 4b9c986

7 files changed

Lines changed: 35 additions & 3 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
test: !vault |
2+
$ANSIBLE_VAULT;1.1;AES256
3+
34636530313034373261383234633232653732316262383339653836323862306263613432623935
4+
6536646366356261386539343166333065356432663264650a313566316439356364663032346639
5+
64396563353261333239643163303933343265666433666632333535336565313331613863383936
6+
6662356434666238370a346334643536653462333164643464383233623830393766333561316538
7+
3333

ansible_vault_rotate/match/find.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re
22
import typing
33

4-
ANSIBLE_VAULT_REGEX = re.compile(r'(^(\s*)\$ANSIBLE_VAULT;(\S*)\n(\s*\w+\n)*)', re.MULTILINE)
4+
ANSIBLE_VAULT_REGEX = re.compile(r'(^(\s*)\$ANSIBLE_VAULT;(\S*)\n(\s*\w+$)*)', re.MULTILINE)
55

66

77
class FindVaultStringResult(typing.TypedDict):

ansible_vault_rotate/match/test_find.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ def test_find_single(self):
1717
result = results[0]
1818
self.assertIsNone(result['label'])
1919
self.assertEqual(result['indent'], ' ')
20+
21+
def test_find_single_eof(self):
22+
results = self.load_results("single_vaulted_eof")
23+
self.assertEqual(len(results), 1)
24+
25+
result = results[0]
26+
self.assertIsNone(result['label'])
27+
self.assertEqual(result['indent'], ' ')
2028

2129
def test_find_labeled(self):
2230
results = self.load_results("label_vaulted")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
test: !vault |
2+
$ANSIBLE_VAULT;1.1;AES256
3+
31663031633532633662666465396235383461646561373762303432373866313466376637303764
4+
3734363362623037613935326332623039636434373562300a336639313131326135323833346634
5+
39343737336437333161656434613064376633366435383663643836316135393336393932386530
6+
3965643937663235320a353463623430373333373337636339313238633931343932313166363663
7+
6161

ansible_vault_rotate/vault/file_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ def test_single_secret(self):
2929
self.assertEqual(doc['regular_key'], "goes here")
3030
self.assertEqual(doc['test'], 'test')
3131

32+
def test_single_secret_eof(self):
33+
with NamedTemporaryFile("r", delete=False) as f:
34+
rekey_file(self.fixture_name("single-secret-eof.yml"), "test", "test123", f.name)
35+
36+
self.assertLineCount(f, 7)
37+
38+
os.chdir("/tmp") # work around for ansible path resolve issues
39+
doc = load_with_vault(f.name, "default", "test123")
40+
self.assertEqual(doc['test'], 'test')
41+
3242
def test_multiple_secret(self):
3343
with NamedTemporaryFile("r", delete=False) as f:
3444
rekey_file(self.fixture_name("multiple-secret.yml"), "test", "test123", f.name)

ansible_vault_rotate/vault/string.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def vault_string(vault_string_search_result: FindVaultStringResult, old_passphra
3535

3636
# read content and add indentation again
3737
with open(f.name, "r") as f:
38-
new_vault = indent + indent.join(f.readlines())
38+
new_vault = indent + indent.join(f.readlines()).rstrip()
3939
content = vaulted_string.replace(vaulted_string, new_vault)
4040

4141
# delete temp file and return ready to use string

ansible_vault_rotate/vault/string_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def verify_indent(self, result, indent):
1010
if line == "":
1111
continue
1212
self.assertTrue(line.startswith(indent), "line '%s' is not indented" % line)
13-
self.assertEqual(len(lines), 7)
13+
self.assertEqual(len(lines), 6)
1414

1515
def test_rekey_unlabeled(self):
1616
search_result = FindVaultStringResult(

0 commit comments

Comments
 (0)