Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM 21015: Fix AWS mock credentials. #184

Merged
merged 4 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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:
DinoBektesevic marked this conversation as resolved.
Show resolved Hide resolved
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