Skip to content

Commit 5595d58

Browse files
authored
[Hack session] Create protected s3 endpoint for secured assets (#311)
* Create s3 data script to fetch protected resources * Change safety.txt load path * Add fallback for s3 read with boto3 download using access tokens * Fix bug in s3 download_file option * Download protected resources in download script * Add protected resources path to gitingore * Update safety words unit test * Add boto3 to requirements * Format schema files and add OTHER and ALL to location and selector * Fix NSP initialization in safety unit test * Fix safety words loading bug * Update unit test to check safety * Fix merge conflict * Remove safety check from datasets * Correct gitignore paths * Use pkg_resources to fetch resource location
1 parent a388126 commit 5595d58

8 files changed

Lines changed: 56 additions & 6 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ __pycache__
1616
*.sql
1717
!memory_schema.sql
1818
*~
19+
base_agent/documents/internal/*
1920
bin/*
2021
writeups/*.pdf
2122
writeups/*.aux

base_agent/droidlet_nsp_model_wrapper.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ def read_datasets(self, opts):
6969
"""
7070
# Extract the set of safety words from safety file
7171
self.safety_words = set()
72-
safety_words_path = opts.ground_truth_data_dir + "safety.txt"
72+
safety_words_path = "{}/{}".format(
73+
pkg_resources.resource_filename("base_agent.documents", "internal"),
74+
"safety.txt",
75+
)
7376
if os.path.isfile(safety_words_path):
7477
"""Read a set of safety words to prevent abuse."""
7578
with open(safety_words_path) as f:

craftassist/agent/craftassist_agent.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,11 @@ def add_self_memory_node(self):
325325
logger.addHandler(sh)
326326
logging.info("LOG LEVEL: {}".format(logger.level))
327327

328-
# Check that models and datasets are up to date
328+
# Check that models and datasets are up to date and download latest resources.
329+
# Also fetches additional resources for internal users.
329330
if not opts.dev:
330331
rc = subprocess.call([opts.verify_hash_script_path, "craftassist"])
331-
332+
332333
set_start_method("spawn", force=True)
333334

334335
sa = CraftAssistAgent(opts)

craftassist/test/test_safety.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from base_craftassist_test_case import BaseCraftassistTestCase
99
from fake_agent import MockOpt
1010

11-
GROUND_TRUTH_DATA_DIR = os.path.join(os.path.dirname(__file__), "../agent/datasets/ground_truth/")
11+
TTAD_BERT_DATA_DIR = os.path.join(os.path.dirname(__file__), "../agent/datasets/annotated_data/")
1212

1313
"""This class tests safety checks using a preset list of blacklisted words.
1414
"""
@@ -17,10 +17,11 @@
1717
class SafetyTest(BaseCraftassistTestCase):
1818
def setUp(self):
1919
opts = MockOpt()
20-
opts.ground_truth_data_dir = GROUND_TRUTH_DATA_DIR
20+
opts.nsp_data_dir = TTAD_BERT_DATA_DIR
2121
opts.no_ground_truth = False
2222
super().setUp(agent_opts=opts)
2323

24+
2425
def test_unsafe_word(self):
2526
is_safe = self.agent.dialogue_manager.semantic_parsing_model_wrapper.is_safe("bad Clinton")
2627
self.assertFalse(is_safe)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
awscli>=1.18.0
22
black>=18.9b0
3+
boto3>=1.17.32
34
botocore>=1.15.0
45
cython>=0.29.14
56
decorator>=4.3.0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
95ad845207f5d67383535a1c97ef1e94e1f687c9
1+
95ad845207f5d67383535a1c97ef1e94e1f687c9
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import boto3
2+
import subprocess
3+
import os
4+
import sys
5+
6+
7+
def fetch_safety_words():
8+
"""
9+
Fetch secure s3 resources for internal users and production systems, eg. safety keyword list.
10+
11+
Currently needs to be called with sys arg passing in output directory.
12+
"""
13+
try:
14+
s3 = boto3.client('s3')
15+
response = s3.head_bucket(Bucket='droidlet-internal')
16+
if response["ResponseMetadata"]["HTTPStatusCode"] == 200:
17+
print("Authenticated user, fetching safety words list.")
18+
path_to_root = os.path.realpath(sys.argv[1])
19+
return subprocess.run(
20+
"aws s3 cp s3://droidlet-internal/safety.txt {}".format(path_to_root), shell=True
21+
)
22+
except Exception as e:
23+
print(e)
24+
pass
25+
# If awscli is not setup, eg. in one time use containers, read access tokens from environment
26+
try:
27+
s3 = boto3.client('s3',
28+
aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
29+
aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY")
30+
)
31+
return s3.download_file(
32+
'droidlet-internal',
33+
'safety.txt',
34+
os.path.realpath(sys.argv[1])
35+
)
36+
except Exception as e:
37+
print(e)
38+
39+
if __name__ == "__main__":
40+
fetch_safety_words()

tools/data_scripts/try_download.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ function pyabspath() {
1111
ROOTDIR=$(pyabspath $(dirname "$0")/../../)
1212
echo "Rootdir ${ROOTDIR}"
1313

14+
# Optionally fetch secure resources for internal users and prod systems
15+
python ${ROOTDIR}/tools/data_scripts/fetch_internal_resources.py ${ROOTDIR}/base_agent/documents/internal/
16+
1417
. ${ROOTDIR}/tools/data_scripts/checksum_fn.sh --source-only # import checksum function
1518

1619
if [ -z $1 ]

0 commit comments

Comments
 (0)