Skip to content

Instantly share code, notes, and snippets.

@mbafford

mbafford/README Secret

Last active December 12, 2021 19:16
Show Gist options
  • Save mbafford/f14d9648b3694034c2a08d56855abdd7 to your computer and use it in GitHub Desktop.
Save mbafford/f14d9648b3694034c2a08d56855abdd7 to your computer and use it in GitHub Desktop.
Testing framework for ofxtools issues with Fidelity
https://github.com/csingley/ofxtools/issues/140
Horribly inefficient.
Relies on 1Password command-line tool to pull the username/password, but just change the first few lines of runtests.sh to hardcode your username/password.
**Sends the password to the remote server via SSH**
May be visible in process lists or logs, make sure you trust the server you're testing on.
#!/bin/bash
OP_ID="5yinxjxggnajnfqppztkxic7zy"
USER="$(op get item "$OP_ID" --fields username)"
PASS="$(op get item "$OP_ID" --fields password)"
testOfxTools() {
TYPE="$1"
IFS=":" read -r HOST PORT <<< "$2"
if [[ -z "$PORT" ]]; then
PORT="22"
fi
PYPATH="$3"
OFXVER="$4"
if [[ "$type" == "fidelity" ]]; then
OFX_URL='https://nbofx.fidelity.com/netbenefits/ofx/download'
OFX_ORG='nbofx.fidelity.com'
OFX_FID='8288'
elif [[ "$type" == "netbenefits" ]]; then
OFX_URL='https://ofx.fidelity.com/ftgw/OFX/clients/download'
OFX_ORG='fidelity.com'
OFX_FID='7776'
else
echo "Unknown bank type $type" > /dev/stderr
exit 1
fi
HASH=$(echo "$PYPATH:$OFXVER" | md5sum | awk '{print $1}')
BASEFILE="$TYPE-$HOST-$(echo "$PYPATH" | tr '/' '_')"
echo "Testing $TYPE on $HOST using $PYPATH installing $OFXVER" > /dev/stderr
ssh -q -T "$HOST" -p "$PORT" >& "$BASEFILE-setup.log" <<CMD
mkdir /tmp/ofxtoolstest/
mkdir /tmp/ofxtoolstest/$HASH/
$PYPATH -mvenv /tmp/ofxtoolstest/$HASH/.env
/tmp/ofxtoolstest/$HASH/.env/bin/pip install ofxtools==$OFXVER
CMD
SETUP=$?
scp -q -P "$PORT" "testofx.py" "$HOST:/tmp/ofxtoolstest/"
if [[ $SETUP != 0 ]]; then
SETUP="FAILED"
RESULT=""
else
SETUP="SUCCESS"
RESULT=$(
ssh -q "$HOST" -p "$PORT" -T <<CMD
export OFX_UNAME="$USER"
export OFX_PASS="$PASS"
export OFX_URL="$OFX_URL"
export OFX_ORG="$OFX_ORG"
export OFX_FID="$OFX_FID"
/tmp/ofxtoolstest/$HASH/.env/bin/python /tmp/ofxtoolstest/testofx.py 2>/dev/null
CMD
)
echo "$RESULT" > "$BASEFILE-$(echo "$PYVER" | tr -d ' ')-$OFXVER-result.json"
fi
}
for type in fidelity netbenefits; do
for ofxver in 0.6.3 0.7.0 0.8.0 0.8.22 0.9.0 0.9.4; do
for python in /usr/bin/python3 /usr/local/bin/python3 ~/.pyenv/versions/3*/bin/python3; do
testOfxTools "$type" "localhost" "$python" "$ofxver"
done
# docker
testOfxTools "$type" "localhost:2222" "python3" "$ofxver"
# ubuntu
testOfxTools "$type" "fridgenas" "python3" "$ofxver"
# raspbian
testOfxTools "$type" "10.10.70.51" "python3" "$ofxver"
done
done
(
echo -e "Type\tSuccess\tOFXTools\tOpenSSL\tPyVer\tPyImpl"
grep -h '{' *.json | jq '[.ofx_fid, .success, .ofxtools, .openssl, .pyver, .pyimpl] | @tsv' -r | sed 's/7776/netbenefits/; s/8288/fidelity/'
) | csvsort -t -c 6,5,3 | csvlook -I | pbcopy
#!/tmp/ofxtoolstest/.env/bin/python
import os
import sys
import ssl
import json
from inspect import getfullargspec
import pkg_resources
username = os.environ['OFX_UNAME']
password = os.environ['OFX_PASS']
url = os.environ['OFX_URL']
org = os.environ['OFX_ORG']
fid = os.environ['OFX_FID']
success = None
try:
from ofxtools.Client import OFXClient
try:
spec = getfullargspec(OFXClient)
if 'userid' in spec.args:
client = OFXClient(url, userid=username, org=org, fid=fid, version=220)
io = client.request_statements(password)
else:
client = OFXClient(url, org=org, fid=fid, version=220)
io = client.request_statements(username, password)
try:
success = 'SUCCESS' in io.read().decode('utf-8')
except:
try:
success = 'SUCCESS' in io.getvalue().decode('utf-8')
except:
success = 'SUCCESS' in io.content().decode('utf-8')
except Exception as ex:
success = f'EXCEPTION: {ex}'
except:
success = 'import failed'
print(json.dumps(dict(
ofx_org=org,
ofx_fid=fid,
success=str(success),
ofxtools=pkg_resources.get_distribution("ofxtools").version,
openssl=ssl.OPENSSL_VERSION,
pyver=sys.version.replace("\n"," "),
pyimpl=str(sys.implementation)
)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment