Skip to content

Commit

Permalink
Merge pull request #184 from lsst/tickets/DM-21015
Browse files Browse the repository at this point in the history
DM 21015: Fix AWS mock credentials.
  • Loading branch information
DinoBektesevic committed Aug 21, 2019
2 parents 9265809 + 49ed6bb commit b7fdf8b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
45 changes: 44 additions & 1 deletion python/lsst/daf/butler/core/s3utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

__all__ = ("s3CheckFileExists", "bucketExists")
__all__ = ("s3CheckFileExists", "bucketExists", "setAwsEnvCredentials",
"unsetAwsEnvCredentials")

import os

try:
import boto3
Expand Down Expand Up @@ -126,3 +129,43 @@ def bucketExists(bucketName, client=None):
return True
except s3.exceptions.NoSuchBucket:
return False


def setAwsEnvCredentials(accessKeyId='dummyAccessKeyId', secretAccessKey="dummySecretAccessKey"):
"""Set AWS credentials environmental variables AWS_ACCESS_KEY_ID and
AWS_SECRET_ACCESS_KEY.
Parameters
----------
accessKeyId : `str`
Value given to AWS_ACCESS_KEY_ID environmental variable. Defaults to
'dummyAccessKeyId'
secretAccessKey : `str`
Value given to AWS_SECRET_ACCESS_KEY environmental variable. Defaults
to 'dummySecretAccessKey'
Returns
-------
setEnvCredentials : `bool`
True when environmental variables were set, False otherwise.
Notes
-----
If either AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY are not set, both
values are overwritten.
"""
if "AWS_ACCESS_KEY_ID" not in os.environ or "AWS_SECRET_ACCESS_KEY" not in os.environ:
os.environ["AWS_ACCESS_KEY_ID"] = accessKeyId
os.environ["AWS_SECRET_ACCESS_KEY"] = secretAccessKey
return True
return False


def unsetAwsEnvCredentials():
"""Unsets AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environmental
variables.
"""
if "AWS_ACCESS_KEY_ID" in os.environ:
del os.environ["AWS_ACCESS_KEY_ID"]
if "AWS_SECRET_ACCESS_KEY" in os.environ:
del os.environ["AWS_SECRET_ACCESS_KEY"]
11 changes: 10 additions & 1 deletion tests/test_butler.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ def mock_s3(cls):
from examplePythonTypes import MetricsExample
from lsst.daf.butler.core.repoRelocation import BUTLER_ROOT_TAG
from lsst.daf.butler.core.location import ButlerURI
from lsst.daf.butler.core.s3utils import s3CheckFileExists
from lsst.daf.butler.core.s3utils import (s3CheckFileExists, setAwsEnvCredentials,
unsetAwsEnvCredentials)


TESTDIR = os.path.abspath(os.path.dirname(__file__))

Expand Down Expand Up @@ -619,6 +621,9 @@ def setUp(self):
uri = ButlerURI(config[".datastore.datastore.root"])
self.bucketName = uri.netloc

# set up some fake credentials if they do not exist
self.usingDummyCredentials = setAwsEnvCredentials()

if self.useTempRoot:
self.root = self.genRoot()
rooturi = f"s3://{self.bucketName}/{self.root}"
Expand Down Expand Up @@ -649,6 +654,10 @@ def tearDown(self):
bucket = s3.Bucket(self.bucketName)
bucket.delete()

# unset any potentially set dummy credentials
if self.usingDummyCredentials:
unsetAwsEnvCredentials()

def checkFileExists(self, root, relpath):
"""Checks if file exists at a given path (relative to root).
Expand Down
8 changes: 8 additions & 0 deletions tests/test_butlerFits.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def mock_s3(cls):
from lsst.daf.butler import DatasetType
from lsst.daf.butler.core.location import ButlerURI
from datasetsHelper import FitsCatalogDatasetsHelper, DatasetTestHelper
from lsst.daf.butler.core.s3utils import setAwsEnvCredentials, unsetAwsEnvCredentials

try:
import lsst.afw.image
Expand Down Expand Up @@ -197,6 +198,9 @@ def setUp(self):
rooturi = f"s3://{self.bucketName}/{self.root}"
config.update({"datastore": {"datastore": {"root": rooturi}}})

# set up some fake credentials if they do not exist
self.usingDummyCredentials = setAwsEnvCredentials()

# MOTO needs to know that we expect Bucket bucketname to exist
# (this used to be the class attribute bucketName)
s3 = boto3.resource("s3")
Expand All @@ -223,6 +227,10 @@ def tearDown(self):
bucket = s3.Bucket(self.bucketName)
bucket.delete()

# unset any potentially set dummy credentials
if self.usingDummyCredentials:
unsetAwsEnvCredentials()


if __name__ == "__main__":
unittest.main()
10 changes: 9 additions & 1 deletion tests/test_s3utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def mock_s3(cls):
"""
return cls

from lsst.daf.butler.core.s3utils import bucketExists, s3CheckFileExists
from lsst.daf.butler.core.s3utils import (bucketExists, s3CheckFileExists,
setAwsEnvCredentials, unsetAwsEnvCredentials)
from lsst.daf.butler.core.location import Location, ButlerURI


Expand All @@ -46,6 +47,9 @@ class S3UtilsTestCase(unittest.TestCase):
fileName = "testFileName"

def setUp(self):
# set up some fake credentials if they do not exist
self.usingDummyCredentials = setAwsEnvCredentials()

s3 = boto3.client("s3")
try:
s3.create_bucket(Bucket=self.bucketName)
Expand All @@ -70,6 +74,10 @@ def tearDown(self):
bucket = s3.Bucket(self.bucketName)
bucket.delete()

# unset any potentially set dummy credentials
if self.usingDummyCredentials:
unsetAwsEnvCredentials()

def testBucketExists(self):
self.assertTrue(bucketExists(f"{self.bucketName}"))
self.assertFalse(bucketExists(f"{self.bucketName}_NO_EXIST"))
Expand Down

0 comments on commit b7fdf8b

Please sign in to comment.