def _get_hs_obj(hs_user_name, hs_user_pwd, hs_host_url): """ init hs_restclient obj using global vars :return: hs_obj """ auth = HydroShareAuthBasic(username=hs_user_name, password=hs_user_pwd) if "hydroshare.org" in hs_host_url or "cuahsi.org" in hs_host_url: hs = HydroShare(auth=auth, hostname=hs_host_url) else: hs = HydroShare(auth=auth, hostname=hs_host_url, port=8000, use_https=False, verify=False) return hs
def __init__(self, username=None, password=None, cache=True): self.hs = None self.content = {} # load the HS environment variables # todo: this should be set as a path variable somehow. # possibly add JPY_TMP to Dockerfile self.cache = cache if cache: utilities.load_environment() self.auth_path = '/home/jovyan/.auth' if password is None: # try for secure connection auth = self.getSecureConnection(username) else: print('WARNING: THIS IS NOT A SECURE METHOD OF CONNECTING TO ' 'HYDROSHARE...AVOID TYPING CREDENTIALS AS PLAIN TEXT') auth = HydroShareAuthBasic(username=username, password=password) try: self.hs = HydroShare(auth=auth) self.hs.getUserInfo() print('Successfully established a connection with HydroShare') except HydroShareHTTPException as e: print('Failed to establish a connection with HydroShare.\n ' 'Please check that you provided the correct credentials.\n' '%s' % e) # remove the cached authentication if os.path.exists(self.auth_path): os.remove(self.auth_path)
def get_epanet_model_metadata(request): return_obj = { 'success': False, 'message': None, 'results': "", } if request.is_ajax() and request.method == 'GET': if not request.GET.get('model_id'): return_obj['message'] = message_template_param_unfilled.format( param='model_id') else: model_id = request.GET['model_id'] try: hs = get_oauth_hs(request) except: hs = HydroShare() metadata_json = hs.getScienceMetadata(model_id) return_obj['results'] = metadata_json return_obj['success'] = True else: return_obj['message'] = message_template_wrong_req_method.format( method="GET") return JsonResponse(return_obj)
def download_epanet_model(request): return_obj = {'success': False, 'message': None, 'results': "", 'name': ""} if request.is_ajax() and request.method == 'GET': if not request.GET.get('model_id'): return_obj['message'] = message_template_param_unfilled.format( param='model_id') else: model_id = request.GET['model_id'] try: hs = get_oauth_hs(request) except: hs = HydroShare() for model_file in hs.getResourceFileList(model_id): model_url = model_file['url'] model_name = model_url[model_url.find('contents/') + 9:] model = "" for line in hs.getResourceFile(model_id, model_name): model += line.decode("utf-8") return_obj['name'] = model_name return_obj['results'] = model return_obj['success'] = True else: return_obj['message'] = message_template_wrong_req_method.format( method="POST") return JsonResponse(return_obj)
def authenticate(self, username, password, client_id=None, client_secret=None): """ Authenticates access to allow read/write access to privileged resource_cache :param username: username for HydroShare.org :param password: password associated with username :param client_id: Client ID obtained from HydroShare :param client_secret: Client Secret provided by HydroShare :return: Returns true if authentication was successful, false otherwise """ if not all([username, password]): self.auth = None return False if client_id is not None and client_secret is not None: self.auth = HydroShareAuthOAuth2(client_id, client_secret, username=username, password=password) else: self.auth = HydroShareAuthBasic(username, password) try: self.client = HydroShare(auth=self.auth) # , verify=False) self.user_info = self.client.getUserInfo() return True except HydroShareException as e: # for incorrect username/password combinations print('Authentication failed: {}'.format(e)) except InvalidGrantError as e: # for failures when attempting to use OAuth2 print('Credentials could not be validated: {}'.format(e)) except InvalidClientError as e: print('Invalid client ID and/or client secret: {}'.format(e)) except Exception as e: print(e) self.auth = None return False
def __init__(self, username=None): self.hs = None self.content = {} # load the HS environment variables # self.load_environment() uname = username if uname is None: uname = os.environ['HS_USR_NAME'] # get a secure connection to hydroshare auth = self.getSecureConnection(uname) try: self.hs = HydroShare(auth=auth) self.hs.getUserInfo() display(HTML('<b style="color:green;">Successfully established a connection with HydroShare</b>')) except HydroShareHTTPException as e: display(HTML( '<p style="color:red;"><b>Failed to establish a connection with HydroShare. Please check that you provided the correct credentials</b><br>%s </p>' % e)) # remove the cached authentication auth_path = os.path.join(os.path.dirname(__file__), '../../../.auth') os.remove(auth_path) return None
def test_get_resource_list_filter_date(self): hs = HydroShare(hostname=self.url) from_date = date(2015, 5, 20) res_list = hs.getResourceList(from_date=from_date) for (i, r) in enumerate(res_list): self.assertTrue( datetime.strptime(r['date_created'], '%m-%d-%Y').date() >= from_date) to_date = date(2015, 5, 21) # up to and including 5/21/2015 res_list = hs.getResourceList(to_date=to_date) for (i, r) in enumerate(res_list): self.assertTrue( datetime.strptime(r['date_created'], '%m-%d-%Y').date() < to_date) from_date = date(2015, 5, 19) to_date = date(2015, 5, 22) # up to and including 5/21/2015 res_list = hs.getResourceList(from_date=from_date, to_date=to_date) for (i, r) in enumerate(res_list): self.assertTrue( datetime.strptime(r['date_created'], '%m-%d-%Y').date() >= from_date) self.assertTrue( datetime.strptime(r['date_created'], '%m-%d-%Y').date() < to_date)
def test_get_resource_list(self): hs = HydroShare(prompt_auth=False) res_list = hs.getResourceList() for (i, r) in enumerate(res_list): self.assertEqual(r['resource_title'], self.resource_titles[i]) self.assertEqual(r['resource_id'], self.resource_ids[i])
def get_hs_oauth2_token(self): hs = None # Get token ourselves (i.e. without using oauthlib, etc.) try: if self.use_https: scheme = 'https' else: scheme = 'http' data = {'grant_type': 'password', 'username': self.creator, 'password': self.creatorPassword} if self.port: token_url = self._TOKEN_URL_PROTO_WITH_PORT.format(scheme=scheme, hostname=self.url, port=self.port) else: token_url = self._TOKEN_URL_PROTO_WITHOUT_PORT.format(scheme=scheme, hostname=self.url) r = requests.post(token_url, data=data, auth=(self.client_id, self.client_secret), verify=self.verify) token = json.loads(r.text) oauth_token = HydroShareAuthOAuth2(self.client_id, self.client_secret, self.url, use_https=self.use_https, port=self.port, token=token) hs = HydroShare(hostname=self.url, auth=oauth_token, use_https=self.use_https, verify=self.verify, port=self.port) except: self.fail("OAuth2 Connection Failed" + str(sys.exc_info()[0])) return hs
def test_get_resourcemap(self): hs = HydroShare(prompt_auth=False) resourcemap = hs.getResourceMap('6dbb0dfb8f3a498881e4de428cb1587c') self.assertTrue( resourcemap.find( """<rdf:Description rdf:about="http://www.hydroshare.org/resource/6dbb0dfb8f3a498881e4de428cb1587c">""" ) != -1)
def get_o_auth_hs(request): """ Gets HydroShare Open Authorization. Arguments: [request] Returns: [hs] Referenced By: [controllers_ajax.chart_data, controllers_ajax.create_layer] References: [] Libraries: [HydroShareAuthOAuth2, HydroShare] """ if use_hs_client_helper: hs = get_oauth_hs(request) else: hs_instance_name = "www" client_id = getattr(settings, "SOCIAL_AUTH_HYDROSHARE_KEY", None) client_secret = getattr(settings, "SOCIAL_AUTH_HYDROSHARE_SECRET", None) # this line will throw out from django.core.exceptions.ObjectDoesNotExist\ # if current user is not signed in via HydroShare OAuth token = request.user.social_auth.get(provider='hydroshare').extra_data['token_dict'] hs_hostname = "{0}.hydroshare.org".format(hs_instance_name) auth = HydroShareAuthOAuth2(client_id, client_secret, token=token) hs = HydroShare(auth=auth, hostname=hs_hostname) return hs
def _get_hs(self): try: return HydroShare(auth=self.hs_auth, hostname=self.hs_url, port=self.port, use_https=self.use_https, verify=self.verify_https) except Exception as ex: logging.error(ex)
def test_update_scimeta(self): hs = HydroShare(prompt_auth=False) metadata_to_update = {'title': 'Updated resource title'} scimeta = hs.updateScienceMetadata('511debf8858a4ea081f78d66870da76c', metadata=metadata_to_update) self.assertEqual(scimeta['title'], 'Updated resource title') self.assertEqual(len(scimeta['creators']), 2) self.assertEqual(len(scimeta['contributors']), 1) self.assertEqual(len(scimeta['coverages']), 1) self.assertEqual(len(scimeta['dates']), 2) self.assertEqual( scimeta['description'], 'Time series of level, area and volume in the Great Salt Lake. Volume and area of the Great Salt Lake are derived from recorded levels' ) self.assertEqual(scimeta['formats'][0]['value'], 'image/tiff') self.assertEqual(len(scimeta['funding_agencies']), 1) self.assertEqual(len(scimeta['identifiers']), 1) self.assertEqual(scimeta['language'], 'eng') self.assertEqual( scimeta['rights'], 'This resource is shared under the Creative Commons Attribution CC BY. http://creativecommons.org/licenses/by/4.0/' ) self.assertEqual(scimeta['type'], 'http://www.hydroshare.org/terms/GenericResource') self.assertEqual(scimeta['publisher'], None) self.assertEqual(len(scimeta['sources']), 0) self.assertEqual(len(scimeta['relations']), 0) self.assertEqual(len(scimeta['subjects']), 2)
def test_get_scimeta_xml(self): hs = HydroShare(prompt_auth=False) scimeta = hs.getScienceMetadataRDF('6dbb0dfb8f3a498881e4de428cb1587c') self.assertTrue( scimeta.find( """<rdf:Description rdf:about="http://www.hydroshare.org/resource/6dbb0dfb8f3a498881e4de428cb1587c">""" ) != -1)
def test_auth(self): self.assertIsNotNone(self.auth, "Auth not provided") hs = None try: hs = HydroShare(hostname=self.url, auth=self.auth) except: self.fail("Authorized Connection Failed" + sys.exc_info()[0]) return hs
def get_hs_oauth2(self): hs = None try: hs = HydroShare(hostname=self.url, auth=self.oauth, use_https=self.use_https, verify=self.verify, port=self.port) except: self.fail("OAuth2 Connection Failed" + str(sys.exc_info()[0])) return hs
def test_resource_scimeta_custom(self): hs = HydroShare(prompt_auth=False) response = hs.resource( '511debf8858a4ea081f78d66870da76c').scimeta.custom({ "foo": "bar", "foo2": "bar2" }) self.assertEqual(response.status_code, 200)
def test_folder_delete(self): hs = HydroShare(prompt_auth=False) response = hs.deleteResourceFolder('511debf8858a4ea081f78d66870da76c', pathname='model/initial/') self.assertEqual(response['resource_id'], '511debf8858a4ea081f78d66870da76c') self.assertEqual(response['path'], 'model/initial')
def get_hs(self, auth=None, require_valid_auth=False): try: auth = auth or self.auth hs = HydroShare(auth=auth, **self.connection_info) list(hs.resources(count=1)) return hs except Exception as e: if require_valid_auth: raise e try: hs = HydroShare() list(hs.resources(count=1)) except: raise ValueError("Cannot connect to the HydroShare.") return hs
def test_get_resource_file_list(self): hs = HydroShare(prompt_auth=False) res_list = hs.getResourceFileList('511debf8858a4ea081f78d66870da76c') for (i, r) in enumerate(res_list): self.assertEqual(r['url'], self.urls[i]) self.assertEqual(r['size'], self.sizes[i]) self.assertEqual(r['content_type'], self.content_types[i])
def get_hs_noauth(self): hs = None try: hs = HydroShare(hostname=self.url, use_https=self.use_https, verify=self.verify, port=self.port) except: self.fail("Anonymous Connection Failed" + sys.exc_info()[0]) return hs
def test_get_user_info(self): hs = HydroShare(prompt_auth=False) user_info = hs.getUserInfo() self.assertEqual(user_info['username'], 'username') self.assertEqual(user_info['first_name'], 'First') self.assertEqual(user_info['last_name'], 'Last') self.assertEqual(user_info['email'], '*****@*****.**')
def runSCI(id): if request.method == 'POST': auth = HydroShareAuthBasic(username=request.form['username'], password=request.form['password']) hs = HydroShare(auth=auth) hs.getResource('e987ddcf73a94376a4a70e5db0fb7646', destination='/home/ubuntu/hydroshareLink/', unzip=True) subprocess.Popen( 'sciunit open /home/ubuntu/hydroshareLink/e987ddcf73a94376a4a70e5db0fb7646/e987ddcf73a94376a4a70e5db0fb7646/data/contents/modflow.zip', stdout=subprocess.PIPE, shell=True) os.chdir("/home/ubuntu/test/") hs.getResource(id, destination='/home/ubuntu/test/', unzip=True) proc = subprocess.Popen('sciunit repeat e2 ' + str(id), stdout=subprocess.PIPE, shell=True) output = proc.stdout.read() abstract = '' title = 'MODFLOW_NWT_SCIUNIT_OUTPUT' keywords = ('my keyword 1', 'my keyword 2') rtype = 'MODFLOWModelInstanceResource' output_id = hs.createResource(rtype, title, abstract=abstract, keywords=keywords) for file in os.listdir("/home/ubuntu/test/MODFLOW/"): if file != "mfnwt": hs.addResourceFile(output_id, "/home/ubuntu/test/MODFLOW/" + file) title = 'ModflowNwtCollection' keywords = ('MODFLOW-NWT Input data', 'MODFLOW-NWT Output data', 'MODFLOW-NWT') rtype = 'CollectionResource' resource_id = hs.createResource(rtype, title, abstract=abstract, keywords=keywords) metaData = {'relations': []} newObject = {} newObject['type'] = 'hasPart' newObject['value'] = 'http://www.hydroshare.org/resource/' + str( id) + '/' metaData['relations'].append(newObject) newObject = {} newObject['type'] = 'hasPart' newObject['value'] = 'http://www.hydroshare.org/resource/' + str( output_id) + '/' metaData['relations'].append(newObject) hs.updateScienceMetadata(resource_id, metadata=metaData) return output return render_template('login.html', id=str(id))
def get_hs_auth(self): self.assertIsNotNone(self.auth, "Auth not provided") hs = None try: hs = HydroShare(hostname=self.url, auth=self.auth, use_https=self.use_https, verify=self.verify, port=self.port) except: self.fail("Authorized Connection Failed" + sys.exc_info()[0]) return hs
def test_set_file_type(self): hs = HydroShare(prompt_auth=False) response = hs.resource( '511debf8858a4ea081f78d66870da76c').functions.set_file_type({ "file_path": "file_path", "hs_file_type": "NetCDF" }) self.assertEqual(response.status_code, 202)
def test_get_resource_types(self): hs = HydroShare(prompt_auth=False) res_type_proto = { 'GenericResource', 'ModelInstanceResource', 'ModelProgramResource', 'NetcdfResource', 'RasterResource', 'RefTimeSeries', 'SWATModelInstanceResource', 'TimeSeriesResource', 'ToolResource' } res_types = hs.getResourceTypes() self.assertSetEqual(res_type_proto, res_types)
def test_get_folder_contents(self): hs = HydroShare(prompt_auth=False) folder_contents = hs.getResourceFolderContents( '511debf8858a4ea081f78d66870da76c', pathname='model/initial/') self.assertEqual(folder_contents['resource_id'], '511debf8858a4ea081f78d66870da76c') self.assertEqual(folder_contents['path'], 'model/initial') self.assertEqual(folder_contents['files'], ["model.exe", "param.txt"]) self.assertEqual(folder_contents['folders'], ["run/1", "run/2"])
def test_resource_move_or_rename(self): hs = HydroShare(prompt_auth=False) response = hs.resource( '511debf8858a4ea081f78d66870da76c').functions.move_or_rename({ "source_path": "/source/path", "target_path": "/target/path" }) self.assertEqual(response.status_code, 200)
def test_resource_list_by_bounding_box(self): hs = HydroShare(prompt_auth=False) res_list = hs.resources(coverage_type="box", north="50", south="30", east="40", west="20") for (i, r) in enumerate(res_list): self.assertEquals(True, True)
def test_referenced_update(self): hs = HydroShare(prompt_auth=False) response = hs.updateReferencedFile( pid='511debf8858a4ea081f78d66870da76c', path='data/contents', name='file.url', ref_url='https://www.cuahsi.org') self.assertEqual(response['status'], 'success')