Skip to content

Commit 8a728fe

Browse files
authored
Merge pull request #29 from yaleman/yaleman-28
That fix
2 parents 152e205 + 0c42cdd commit 8a728fe

3 files changed

Lines changed: 53 additions & 3 deletions

File tree

bin/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# this is just here to trick pytest into thinking bin is a package so we can do imports

bin/decrypt.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,27 @@
1515
)
1616
import decryptlib
1717

18+
FAILURE_FIELD = ".decrypt_failure__"
19+
1820

1921
@Configuration()
2022
class DecryptCommand(StreamingCommand):
2123
field = Option(require=False, default="_raw", validate=validators.Fieldname())
2224

25+
def prepare(self) -> None:
26+
"""Prepare for execution.
27+
28+
This method should be overridden in search command classes that wish to examine and update their configuration
29+
or option settings prior to execution. It is called during the getinfo exchange before command metadata is sent
30+
to splunkd.
31+
"""
32+
# this forces the field to exist at the end
33+
self.configuration.required_fields = [self.field]
34+
# this means we're only sent the field the user asked for
35+
self.configuration.clear_required_fields = True
36+
2337
def stream(self, records):
2438
stmt = " ".join(self.fieldnames)
25-
2639
try:
2740
for record in records:
2841
try:
@@ -36,8 +49,7 @@ def stream(self, records):
3649
result = fn(result, args)
3750

3851
except Exception as e:
39-
exception_string = str(e)
40-
record[".decrypt_failure__"] = exception_string
52+
record[FAILURE_FIELD] = str(e)
4153

4254
yield record
4355
except csv.Error:

tests/test_ssc.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import os
2+
import sys
3+
from collections import OrderedDict
4+
5+
# add the bin dir to the path
6+
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
7+
8+
9+
def test_decrypt_hexed() -> None:
10+
"""this checks the stream function not the rest"""
11+
import bin.decrypt as decrypt
12+
13+
streamer = decrypt.DecryptCommand()
14+
""" equivalent to `| decrypt field=testfield hex() emit('hexed')` """
15+
streamer.field = "testfield"
16+
streamer.fieldnames = ["hex()", "emit('hexed')"]
17+
18+
records = [
19+
OrderedDict([("testfield", "hello"), ("_chunked_idx", "0")]),
20+
OrderedDict([("testfield", "world"), ("_chunked_idx", "1")]),
21+
OrderedDict([("_chunked_idx", "2")]),
22+
]
23+
24+
output = list(streamer.stream(records))
25+
assert output[0]["testfield"] == "hello"
26+
assert output[0]["hexed"] == "68656c6c6f"
27+
print(output[0])
28+
29+
assert output[1]["testfield"] == "world"
30+
assert output[1]["hexed"] == "776f726c64"
31+
print(output[1])
32+
33+
assert output[2] == {"_chunked_idx": "2"}
34+
print(output[2])
35+
assert len(output) == 3
36+
# ensure there's no errors
37+
assert not any(decrypt.FAILURE_FIELD in rec for rec in output)

0 commit comments

Comments
 (0)