Skip to content

Commit

Permalink
KSM-496 - added upload file option (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
idimov-keeper authored Apr 12, 2024
1 parent a3c5fa9 commit 77c4bcb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class AliasedGroup(HelpColorsGroup):
"secret",
"totp",
"download",
"upload",
"get",
"list",
"notation",
Expand Down Expand Up @@ -635,6 +636,20 @@ def secret_update_command(ctx, uid, field, custom_field, field_json, custom_fiel
)


@click.command(
name='upload',
cls=HelpColorsCommand,
help_options_color='blue'
)
@click.option('--uid', '-u', required=True, type=str, help='UID of the secret.')
@click.option('--file', '-f', required=True, type=str, help='Path to the file to upload.')
@click.option('--title', '-t', type=str, help='File title (defaults to the filename if none provided).')
@click.pass_context
def secret_upload_command(ctx, uid, file, title):
"""Upload a file to a secret record"""
ctx.obj["secret"].upload(uid=uid, file=file, title=title)


@click.command(
name='download',
cls=HelpColorsCommand,
Expand Down Expand Up @@ -897,6 +912,7 @@ def secret_add_field_command(ctx, shared_folder_uid, record_type, title, passwor
secret_command.add_command(secret_notation_command)
secret_command.add_command(secret_update_command)
secret_command.add_command(secret_add_command)
secret_command.add_command(secret_upload_command)
secret_command.add_command(secret_download_command)
secret_command.add_command(secret_totp_command)
secret_command.add_command(secret_password_command)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
from jsonpath_rw_ext import parse
import sys
from colorama import Fore, Style
from pathlib import Path
from keeper_secrets_manager_cli.exception import KsmCliException
from keeper_secrets_manager_cli.common import launch_editor
from keeper_secrets_manager_core.core import SecretsManager
from keeper_secrets_manager_core.core import SecretsManager, KeeperFileUpload
from keeper_secrets_manager_core.utils import get_totp_code, generate_password as sdk_generate_password
from keeper_secrets_manager_helper.record import Record
from keeper_secrets_manager_helper.field_type import FieldType
Expand Down Expand Up @@ -448,6 +449,25 @@ def secret_list(self, uids=None, output_format='json', use_color=None):
for x in record_dict]
self.cli.output(json.dumps(records, indent=4))

def upload(self, uid, file, title):

# check if file exists and is readable by current user
if not (os.path.isfile(file) and os.access(file, os.R_OK)):
raise KsmCliException(f"File {file} doesn't exist or isn't readable.")

records = self.cli.client.get_secrets(uids=[uid])
if len(records) == 0:
raise KsmCliException(f"Cannot find a record for UID {uid}. Cannot upload {file}")

record = records[0]
fname = Path(file).name
title = title if title else fname
ksm_file = KeeperFileUpload.from_file(file, fname, title)
file_uid = self.cli.client.upload_file(record, file=ksm_file)

print("The following is the new file UID ...", file=sys.stderr)
return self.cli.output(file_uid)

def download(self, uid, name, file_uid, file_output, create_folders=False):

record = self.cli.client.get_secrets(uids=[uid])
Expand All @@ -466,7 +486,7 @@ def download(self, uid, name, file_uid, file_output, create_folders=False):
raise KsmCliException("Cannot find a file named {} for UID {}. Cannot download file".format(name, uid))

if file_output == 'stdout':
sys.stderr.buffer.write(file.get_file_data())
sys.stdout.buffer.write(file.get_file_data())
elif file_output == 'stderr':
sys.stderr.buffer.write(file.get_file_data())
elif type(file_output) is str:
Expand Down

0 comments on commit 77c4bcb

Please sign in to comment.