Skip to content

Commit

Permalink
Open binary files in binary mode and do not encode them (#829)
Browse files Browse the repository at this point in the history
* open binary files in binary mode and do not encode them

* reveal base64 string if binary data, otherwise decode

* fix linting, use raw data if binary for all cases

* handle None MIME type as non-binary

* provide --binary command line parameter

Co-authored-by: Ricardo Amaro <[email protected]>
  • Loading branch information
Tymolc and ramaro authored Apr 12, 2022
1 parent 85fe799 commit 7f2d86e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
6 changes: 6 additions & 0 deletions kapitan/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,12 @@ def main():
action="store_true",
default=from_dot_kapitan("refs", "base64", False),
)
refs_parser.add_argument(
"--binary",
help="file content should be handled as binary data",
action="store_true",
default=from_dot_kapitan("refs", "binary", False),
)
refs_parser.add_argument(
"--reveal",
"-r",
Expand Down
6 changes: 5 additions & 1 deletion kapitan/refs/base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ def __init__(self, data, from_base64=False, **kwargs):

def reveal(self):
# TODO data should be bytes only
return base64.b64decode(self.data).decode()
decoded = base64.b64decode(self.data)
try:
return decoded.decode()
except:
return self.data

def compile_embedded(self):
dump = self.dump()
Expand Down
19 changes: 13 additions & 6 deletions kapitan/refs/cmd_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import os
import sys
import mimetypes

from kapitan.errors import KapitanError, RefHashMismatchError
from kapitan.refs.base import PlainRef, RefController, Revealer
Expand Down Expand Up @@ -37,6 +38,7 @@ def ref_write(args, ref_controller):
"Write ref to ref_controller based on cli args"
token_name = args.write
file_name = args.file
is_binary = args.binary
data = None

if file_name is None:
Expand All @@ -46,8 +48,13 @@ def ref_write(args, ref_controller):
for line in sys.stdin:
data += line
else:
with open(file_name) as fp:
data = fp.read()
mime_type = mimetypes.guess_type(file_name)
modifier = "rb" if is_binary else "r"
with open(file_name, modifier) as fp:
try:
data = fp.read()
except UnicodeDecodeError as e:
raise KapitanError("Could not read file. Please add '--binary' if the file contains binary data. ({})".format(e))

if token_name.startswith("gpg:"):
type_name, token_path = token_name.split(":")
Expand Down Expand Up @@ -167,7 +174,7 @@ def ref_write(args, ref_controller):

elif token_name.startswith("base64:"):
type_name, token_path = token_name.split(":")
_data = data.encode()
_data = data if is_binary else data.encode()
encoding = "original"
if args.base64:
_data = base64.b64encode(_data).decode()
Expand All @@ -179,7 +186,7 @@ def ref_write(args, ref_controller):

elif token_name.startswith("vaultkv:"):
type_name, token_path = token_name.split(":")
_data = data.encode()
_data = data if is_binary else data.encode()
vault_params = {}
encoding = "original"
if args.target_name:
Expand Down Expand Up @@ -213,7 +220,7 @@ def ref_write(args, ref_controller):

elif token_name.startswith("plain:"):
type_name, token_path = token_name.split(":")
_data = data.encode()
_data = data if is_binary else data.encode()
encoding = "original"
if args.base64:
_data = base64.b64encode(_data).decode()
Expand All @@ -225,7 +232,7 @@ def ref_write(args, ref_controller):

elif token_name.startswith("env:"):
type_name, token_path = token_name.split(":")
_data = data.encode()
_data = data if is_binary else data.encode()
encoding = "original"
if args.base64:
_data = base64.b64encode(_data).decode()
Expand Down

0 comments on commit 7f2d86e

Please sign in to comment.