Skip to content

Commit

Permalink
Making the --uri require --header
Browse files Browse the repository at this point in the history
  • Loading branch information
silverdaz committed Jul 11, 2024
1 parent fcdc89c commit d9fd5a5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
3 changes: 3 additions & 0 deletions crypt4gh/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ def parse_args(argv=sys.argv[1:]):
if args['--range'] is not None and args['--uri'] is not None:
raise ValueError('Can not mix --range and --uri')

if args['--uri'] is not None and args['--header'] is None:
raise ValueError('--uri requires --header')

# print(args)
return args

Expand Down
7 changes: 3 additions & 4 deletions crypt4gh/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ def encrypt(keys, infile, outfile, headerfile=None, offset=None, span=None, time
LOG.debug('header length: %d', len(header_bytes))
headerfile.write(header_bytes)

if uri:
LOG.info('Encryption Successful via the URI: %s', uri)
return
if uri and headerfile is outfile:
raise ValueError('The header stream and output stream must be different')

# ...and cue music
LOG.debug("Streaming content")
Expand Down Expand Up @@ -405,7 +404,7 @@ def decrypt(keys, infile, outfile, sender_pubkey=None, offset=0, span=None):
# or we fetch the data portion from the URI.
if uri:
# replacing the infile with a fetcher
outfile = Fetcher(uri)
infile = Fetcher(uri)
# Note: the remainder of the infile might not be empty

# Generator to slice the output
Expand Down
14 changes: 8 additions & 6 deletions tests/expiration.bats
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ function teardown() {

# Bob encrypts the testfile for Alice
export C4GH_PASSPHRASE=${BOB_PASSPHRASE}
EXP_DATE=$(date -jf %s $(( $(date +%s) - 86400 * 2 )))
crypt4gh encrypt --sk ${BOB_SECKEY} --recipient_pk ${BOB_PUBKEY} --recipient_pk ${ALICE_PUBKEY} --expiration "${EXP_DATE}" < $TESTFILE > $TESTFILES/message.c4gh
EXP_DATE=$(date -Iseconds -jf %s $(( $(date +%s) - 86400 * 2 )))
crypt4gh encrypt --recipient_pk ${ALICE_PUBKEY} --expiration "${EXP_DATE}" < $TESTFILE > $TESTFILES/message.c4gh

# Alice decrypts it
export C4GH_PASSPHRASE=${ALICE_PASSPHRASE}
crypt4gh decrypt --sk ${ALICE_SECKEY} < $TESTFILES/message.c4gh > $TESTFILES/message.alice.received
[ "$status" -ne 0 ]
crypt4gh decrypt --sk ${ALICE_SECKEY} < $TESTFILES/message.c4gh 2>$TESTFILES/message.alice.error >$TESTFILES/message.alice.received

run grep -q 'Expired on' $TESTFILES/message.alice.error
[ "$status" -eq 0 ]

unset C4GH_PASSPHRASE
}
Expand All @@ -37,8 +39,8 @@ function teardown() {

# Bob encrypts the testfile for Alice
export C4GH_PASSPHRASE=${BOB_PASSPHRASE}
EXP_DATE=$(date -jf %s $(( $(date +%s) + 86400 * 2 )))
crypt4gh encrypt --sk ${BOB_SECKEY} --recipient_pk ${BOB_PUBKEY} --recipient_pk ${ALICE_PUBKEY} --expiration "${EXP_DATE}" < $TESTFILE > $TESTFILES/message.c4gh
EXP_DATE=$(date -Iseconds -jf %s $(( $(date +%s) + 86400 * 2 )))
crypt4gh encrypt --sk ${BOB_SECKEY} --recipient_pk ${ALICE_PUBKEY} --expiration "${EXP_DATE}" < $TESTFILE > $TESTFILES/message.c4gh

# Alice decrypts it
export C4GH_PASSPHRASE=${ALICE_PASSPHRASE}
Expand Down
33 changes: 33 additions & 0 deletions tests/uri.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bats

load _common/helpers

function setup() {

# Defining the TMP dir
TESTFILES=${BATS_TEST_FILENAME}.d
mkdir -p "$TESTFILES"

}

function teardown() {
rm -rf ${TESTFILES}
}

@test "Bob sends the testfile secretly to Alice over an URI" {

TESTFILE=${BATS_TEST_DIRNAME}/_common/testfile.abcd

# Bob encrypts the testfile for Alice
export C4GH_PASSPHRASE=${BOB_PASSPHRASE}
crypt4gh encrypt --recipient_pk ${ALICE_PUBKEY} --uri "file://$TESTFILES/message.c4gh.payload" --header "$TESTFILES/message.c4gh.header" < $TESTFILE > $TESTFILES/message.c4gh.payload

# Alice decrypts it
export C4GH_PASSPHRASE=${ALICE_PASSPHRASE}
crypt4gh decrypt --sk ${ALICE_SECKEY} < $TESTFILES/message.c4gh.header > $TESTFILES/message.alice.received

run diff $TESTFILE $TESTFILES/message.alice.received
[ "$status" -eq 0 ]

unset C4GH_PASSPHRASE
}

0 comments on commit d9fd5a5

Please sign in to comment.