Esempio n. 1
0
    def connect_client(self, force_no_credentials=False, disable_logs=False):

        if getattr(self, '_handle_client', None) is None:

            if disable_logs:
                import logging
                logging.getLogger('b2handle').setLevel(logging.WARNING)

            # With credentials
            if force_no_credentials:
                self._handle_client = b2handle.instantiate_for_read_access()
                log.debug("HANDLE client connected [w/out credentials]")
            else:
                found = False
                file = os.environ.get('HANDLE_CREDENTIALS', None)
                if file is not None:
                    from utilities import path
                    credentials_path = path.build(file)
                    found = path.file_exists_and_nonzero(credentials_path)
                    if not found:
                        log.warning("B2HANDLE credentials file not found %s",
                                    file)

                if found:
                    self._handle_client = \
                        b2handle.instantiate_with_credentials(
                            credentials.load_from_JSON(file)
                        )
                    log.debug("HANDLE client connected [w/ credentials]")
                    return self._handle_client, True

        return self._handle_client, False
 def __init__(self,
              db_host,
              db_user,
              db_password,
              database_name,
              admin_handle,
              output_batch_filename,
              queried_prefixes,
              fixed_content,
              input_batch_file=None,
              handle_key_file=None,
              handle_secret_key=None,
              handle_server_url=None):
     super(MigrationTool, self).__init__()
     self.db_host = db_host
     self.db_user = db_user
     self.db_password = db_password
     self.database_name = database_name
     self.admin_handle = admin_handle
     self.key_file = handle_key_file
     self.secret_key = handle_secret_key
     self.input_batch_filename = input_batch_file
     self.output_batch_filename = output_batch_filename
     self.queried_prefixes = queried_prefixes
     if fixed_content:
         self.fixed_content = "TRUE"
     else:
         self.fixed_content = "FALSE"
     self.all_handles = []
     self.handle_server_url = handle_server_url
     self.b2handleclient = EUDATHandleClient.instantiate_for_read_access()
     self.b2handleclient = EUDATHandleClient.instantiate_for_read_access(
         handle_server_url)
    def __init__(self, handle_server_url, username, password, prefix=None, handleowner=None, **config):
        '''
        Initialize client credentials instance with Handle server url,
            username and password.

        :param handle_server_url: URL to your handle server
        :param username: User information in the format "index:prefix/suffix"
        :param password: Password.
        :param prefix: Prefix.
        :param config: Any key-value pairs added are stored as config.
        :raises: HandleSyntaxError
        '''
        EUDATHandleClient.check_handle_syntax_with_index(username)
        self.__handle_server_url = handle_server_url
        self.__username = username
        self.__password = password
        self.__prefix = prefix
        self.__handleowner = handleowner
        self.__config = None
        if len(config) > 0:
            self.__config = config

        if handleowner is not None:
            EUDATHandleClient.check_handle_syntax_with_index(handleowner)
            self.__handleowner = handleowner
Esempio n. 4
0
    def test_global_resolve(self):
        """Testing if instantiating with default handle server'works
        and if a handle is correctly retrieved. """

        # Create instance with default server url:
        inst = EUDATHandleClient(HTTPS_verify=self.https_verify)
        rec = inst.retrieve_handle_record_json(self.handle_global)

        self.assertIn('handle', rec, 'Response lacks "handle".')
        self.assertIn('responseCode', rec, 'Response lacks "responseCode".')
    def test_global_resolve(self):
        """Testing if instantiating with default handle server'works
        and if a handle is correctly retrieved. """

        # Create instance with default server url:
        inst = EUDATHandleClient(HTTPS_verify=self.https_verify)
        rec = inst.retrieve_handle_record_json(self.handle_global)

        self.assertIn("handle", rec, 'Response lacks "handle".')
        self.assertIn("responseCode", rec, 'Response lacks "responseCode".')
    def test_modify_handle_value_without_authentication(self):
        """Test if exception when not authenticated."""
        log_new_test_case("test_modify_handle_value_without_authentication")

        # Test variables
        testhandle = self.handle
        inst_readonly = EUDATHandleClient(self.url, HTTPS_verify=self.https_verify)

        # Run code to be tested + check exception:
        log_start_test_code()
        with self.assertRaises(HandleAuthenticationError):
            inst_readonly.modify_handle_value(testhandle, foo='bar')
        log_end_test_code()
Esempio n. 7
0
    def test_modify_handle_value_without_authentication(self):
        """Test if exception when not authenticated."""
        log_new_case("test_modify_handle_value_without_authentication")

        # Test variables
        testhandle = self.handle
        inst_readonly = EUDATHandleClient(self.url,
                                          HTTPS_verify=self.https_verify)

        # Run code to be tested + check exception:
        log_start_test_code()
        with self.assertRaises(HandleAuthenticationError):
            inst_readonly.modify_handle_value(testhandle, foo='bar')
        log_end_test_code()
Esempio n. 8
0
 def register_handle(self, location):
     """Register a handle."""
     pid = ''
     try:
         credential = PIDClientCredentials.load_from_JSON(
             self.credential_path)
         client = EUDATHandleClient.instantiate_with_credentials(credential)
         pid = credential.get_prefix() + '/' \
             + "{:010d}".format(int(location.split('/records/')[1]))
         handle = client.register_handle(pid, location)
         current_app.logger.info(
             'Registered successfully handle {}'.format(pid))
         return handle
     except (FileNotFoundError, CredentialsFormatError,
             HandleAuthenticationError, GenericHandleError) as e:
         current_app.logger.error('Registration failed of handle {}. {} in '
                                  'HandleClient.register_handle'.format(
                                      pid, e))
         return None
     except AttributeError:
         current_app.logger.error('Missing Private Key!')
         return None
     except Exception as e:
         current_app.logger.error(e)
         return None
    def test_instantiate_wrong_search_url(self):

        inst = EUDATHandleClient.instantiate_for_read_and_search(
            "someurl", "someuser", "somepassword", reverselookup_baseuri="http://something_random_foo_bar"
        )

        self.assertIsInstance(inst, EUDATHandleClient)
Esempio n. 10
0
def _check_handle_status(dataset_id):
    """Checks handle exists or not.

    :returns: Flag indicating whether handle status is such that it requires fiurther processing
    :rtype: bool

    """
    # Get handle information.
    handle_string = resolve_input(dataset_id)
    handle_client = EUDATHandleClient.instantiate_for_read_access()
    encoded_dict = handle_client.retrieve_handle_record(handle_string)

    # Error if not found.
    if encoded_dict is None:
        raise exceptions.HandleMismatch('Dataset {} has no published pid handle'.format(dataset_id))

    # Reformat handle information.
    handle_record = {k.decode('utf8'): v.decode('utf8') for k, v in encoded_dict.items()}

    # Error if handle has no test value.
    if '_TEST' not in handle_record.keys():
        logger.log_pid('Dataset handle does not have test value, assuming not test...')
        # raise exceptions.HandleMismatch('TEST VALUE WAS NOT FOUND IN HANDLE, ABORTING....')
    else:
        # Error if handle record value.
        if handle_record['_TEST'].lower() != str(config.pid.is_test).lower():
            raise exceptions.HandleMismatch('Dataset {} has mismatched test status [{}] with pid connector'.format(dataset_id, handle_record['_TEST']))
Esempio n. 11
0
    def test_instantiate_for_read_an_search(self):
        """Testing if instantiating with default handle server works. """

        # Try to create client instance for search without a search URL:
        with self.assertRaises(TypeError):
            inst = EUDATHandleClient.instantiate_for_read_and_search(
                None, 'johndoe', 'passywordy')
    def test_instantiate_with_credentials_config_override(self):
        """Test instantiation of client: No exception if password wrong."""

        # Test variables
        credentials = MagicMock()
        config_from_cred = {}
        valuefoo = 'foo/foo/foo/'
        config_from_cred['REST_API_url_extension'] = valuefoo
        credentials.get_config = MagicMock(return_value=config_from_cred)
        credentials.get_username = MagicMock(return_value=self.user)
        credentials.get_password = MagicMock(return_value=self.randompassword)
        credentials.get_server_URL = MagicMock(return_value=self.url)

        self.assertEqual(credentials.get_config()['REST_API_url_extension'],
                         valuefoo, 'Config: ' + str(credentials.get_config()))

        # Run code to be tested
        # Create instance with credentials
        inst = EUDATHandleClient.instantiate_with_credentials(
            credentials,
            HTTPS_verify=self.https_verify,
            REST_API_url_extension='api/handles')

        # If this raises an exception, it is because /foo/foo from the
        # credentials config was used as path. /foo/foo should be overridden
        # by the standard stuff.

        # Check desired outcomes
        self.assertIsInstance(inst, EUDATHandleClient)
        val = self.inst.get_value_from_handle(self.handle, 'test1')
        self.assertEqual(
            val, 'val1',
            'Retrieving "test1" should lead to "val1", but it lead to: ' +
            str(val))
Esempio n. 13
0
    def test_instantiate_with_username_and_password_noindex(self):

        # Try to ceate client instance with username and password

        with self.assertRaises(HandleSyntaxError):
            inst = EUDATHandleClient.instantiate_with_username_and_password(
                'someurl', 'johndoe', 'passywordy')
Esempio n. 14
0
def create(args):
    """perform create action"""

    try:
        # load credentials
        credentials = PIDClientCredentials.load_from_JSON(args.credpath)
    except CredentialsFormatError:
        sys.stdout.write('error')
        return
    except HandleSyntaxError:
        sys.stdout.write('error')
        return

    # retrieve and set extra values
    extra_config = {}

    # create a handle to put. Concate the prefix with a new generated suffix
    prefix = str(credentials.get_prefix())
    uid = uuid.uuid1()
    suffix = str(uid)
    handle = prefix+"/"+suffix

    try:
        # setup connection to handle server
        client = EUDATHandleClient.instantiate_with_credentials(
            credentials,
            **extra_config)
    except HandleNotFoundException:
        sys.stdout.write('error')
        return

    overwrite=False
    result = create_execution(client, handle, args.location, overwrite, args.checksum, args.loc10320, args.extratype)

    sys.stdout.write(result)
Esempio n. 15
0
def download_dataset(pid, destination):
    #Instantiate client for reading --> credentials necessary
    ec = EUDATHandleClient.instantiate_for_read_access(
        'https://hdl.handle.net')
    record = ec.retrieve_handle_record(pid)

    assert record != None

    assert 'URL' in record
    assert 'PROTOCOL' in record
    assert 'SITE' in record

    protocol = record['PROTOCOL']
    site = record['SITE']
    source = record['URL']

    print GREEN, "DEBUG", DEFAULT, \
            "PID", pid, "resolves to", protocol+"://"+site+source

    exit_code = subprocess.call(["grid-proxy-init"])

    print GREEN, "DEBUG downloading:"
    exit_code = subprocess.call(
        ["globus-url-copy", "-list", protocol + "://" + site + source])
    print "Destination", destination
    print DEFAULT

    exit_code = subprocess.call([
        "globus-url-copy", "-cd", "-r", protocol + "://" + site + source,
        destination
    ])

    return exit_code
    def test_instantiate_for_read_an_search(self):
        """Testing if instantiating with default handle server works. """

        # Try to create client instance for search without a search URL:
        with self.assertRaises(TypeError):
            inst = EUDATHandleClient.instantiate_for_read_and_search(
                None, 'johndoe', 'passywordy')
    def test_instantiate_with_credentials_config(self):
        """Test instantiation of client: No exception if password wrong."""

        # Test variables
        credentials = MagicMock()
        config_from_cred = {}
        valuefoo = "foo/foo/foo/"
        config_from_cred["REST_API_url_extension"] = valuefoo
        credentials.get_config = MagicMock(return_value=config_from_cred)
        credentials.get_username = MagicMock(return_value=self.user)
        credentials.get_password = MagicMock(return_value=self.randompassword)
        credentials.get_server_URL = MagicMock(return_value=self.url)
        credentials.get_handleowner = MagicMock(return_value=None)
        credentials.get_path_to_private_key = MagicMock(return_value=None)
        credentials.get_path_to_file_certificate_only = MagicMock(return_value=None)
        credentials.get_path_to_file_certificate_and_key = MagicMock(return_value=None)

        self.assertEqual(
            credentials.get_config()["REST_API_url_extension"], valuefoo, "Config: " + str(credentials.get_config())
        )

        # Run code to be tested
        # Create instance with credentials
        with self.assertRaises(GenericHandleError):
            inst = EUDATHandleClient.instantiate_with_credentials(credentials, HTTPS_verify=self.https_verify)
    def test_instantiate_with_username_and_password_noindex(self):

        # Try to ceate client instance with username and password

        with self.assertRaises(HandleSyntaxError):
            inst = EUDATHandleClient.instantiate_with_username_and_password(
                'someurl', 'johndoe', 'passywordy')
Esempio n. 19
0
    def setUp(self):
        """ For most test, provide a client instance with the user-specified
        handle server url."""

        self.inst = EUDATHandleClient(HTTPS_verify=self.https_verify,
                                      handle_server_url=self.url,
                                      url_extension_REST_API=self.path_to_api)

        # Before being able to run these tests without write access,
        # the handle that we use for testing must exist. With this code,
        # you can create it. You only need to create it once and leave it
        # on the server, it will not be modified and can be used eternally.
        if False:
            # This should always be false!!! Except for creating the
            # required handle once!
            self.create_required_test_handles()
Esempio n. 20
0
    def test_instantiate_with_credentials_config(self):
        """Test instantiation of client: No exception if password wrong."""

        # Test variables
        credentials = mock.MagicMock()
        config_from_cred = {}
        valuefoo = 'foo/foo/foo/'
        config_from_cred['REST_API_url_extension'] = valuefoo
        credentials = b2handle.clientcredentials.PIDClientCredentials(
            handle_server_url=self.url,
            username=self.user,
            password=self.randompassword,
            handleowner=self.user,
            REST_API_url_extension=valuefoo)

        self.assertEqual(credentials.get_config()['REST_API_url_extension'],
                         valuefoo, 'Config: ' + str(credentials.get_config()))

        # foo/foo/ from the credentials should override default api/handles/

        # Run code to be tested - we expect an exception, as it will try to do a GET on the bogus rest api:
        with self.assertRaises(GenericHandleError):
            inst = EUDATHandleClient.instantiate_with_credentials(
                credentials, HTTPS_verify=self.https_verify)

            # So this code can only be reached if something went wrong:
            self.assertIsInstance(inst, EUDATHandleClient)
            # Check if foo/foo instead of api/handles was stored as path!
            serverconn = inst._EUDATHandleClient__handlesystemconnector
            self.assertIn(
                '/foo/',
                serverconn._HandleSystemConnector__REST_API_url_extension)
            self.assertEquals(
                serverconn._HandleSystemConnector__REST_API_url_extension,
                valuefoo)
    def test_instantiate_with_credentials_config_override(self):
        """Test instantiation of client: No exception if password wrong."""

        # Test variables
        credentials = MagicMock()
        config_from_cred = {}
        valuefoo = 'foo/foo/foo/'
        config_from_cred['REST_API_url_extension'] = valuefoo
        credentials.get_config = MagicMock(return_value=config_from_cred)
        credentials.get_username = MagicMock(return_value=self.user)
        credentials.get_password = MagicMock(return_value=self.randompassword)
        credentials.get_server_URL = MagicMock(return_value=self.url)

        self.assertEqual(credentials.get_config()['REST_API_url_extension'],valuefoo,
            'Config: '+str(credentials.get_config()))

        # Run code to be tested
        # Create instance with credentials
        inst = EUDATHandleClient.instantiate_with_credentials(
            credentials,
            HTTPS_verify=self.https_verify,
            REST_API_url_extension='api/handles')

        # If this raises an exception, it is because /foo/foo from the
        # credentials config was used as path. /foo/foo should be overridden
        # by the standard stuff.

        # Check desired outcomes
        self.assertIsInstance(inst, EUDATHandleClient)
        val = self.inst.get_value_from_handle(self.handle, 'test1')
        self.assertEqual(val, 'val1',
            'Retrieving "test1" should lead to "val1", but it lead to: '+str(val))
Esempio n. 22
0
def read(args):
    """perform read action"""

    try:
        # load credentials
        credentials = PIDClientCredentials.load_from_JSON(args.credpath)
    except CredentialsFormatError:
        sys.stdout.write('error')
        return
    except HandleSyntaxError:
        sys.stdout.write('error')
        return

    # retrieve and set extra values
    extra_config = {}

    try:
        # setup connection to handle server
        client = EUDATHandleClient.instantiate_with_credentials(
            credentials,
            **extra_config)
    except HandleNotFoundException:
        sys.stdout.write('error')
        return

    if args.key is None:
        result =  read_execution(client, args.handle)
    else:
        result =  read_execution(client, args.handle, args.key)

    sys.stdout.write(result)
Esempio n. 23
0
def search(args):
    """perform search action"""

    # load credentials
    credentials = PIDClientCredentials.load_from_JSON(args.credpath)

    # set username and password for search
    if 'reverse_username' in credentials.get_config():
        reverselookup_username = credentials.get_config()['reverse_username']
    else:
        reverselookup_username = credentials.get_prefix()

    if 'reverse_password' in credentials.get_config():
        reverselookup_password = credentials.get_config()['reverse_password']
    else:
        reverselookup_password = credentials.get_password()

    # retrieve and set extra values
    extra_config = {}

    # setup connection to handle server
    client = EUDATHandleClient.instantiate_for_read_and_search(
        credentials.get_server_URL(), reverselookup_username,
        reverselookup_password, **extra_config)

    kvpairs = dict([(args.key, str(''.join(args.value)))])

    # search for handle
    result = client.search_handle(**kvpairs)
    json_result = str(json.dumps(result))

    if json_result == '[]':
        json_result = 'empty'

    sys.stdout.write(json_result)
Esempio n. 24
0
def modify(args):
    """perform modify action"""

    # load credentials
    credentials = PIDClientCredentials.load_from_JSON(args.credpath)

    # retrieve and set extra values
    extra_config = {}

    # setup connection to handle server
    client = EUDATHandleClient.instantiate_with_credentials(
        credentials, **extra_config)

    kvpairs = dict([(args.key, args.value)])

    # modify key/value pairs
    result = client.modify_handle_value(args.handle,
                                        ttl=None,
                                        add_if_not_exist=True,
                                        **kvpairs)

    output_result = str(result)

    if output_result == 'None':
        output_result = 'True'

    sys.stdout.write(output_result)
Esempio n. 25
0
def read(args):
    """perform read action"""

    # load credentials
    credentials = PIDClientCredentials.load_from_JSON(args.credpath)

    # retrieve and set extra values
    extra_config = {}

    # setup connection to handle server
    client = EUDATHandleClient.instantiate_for_read_access(
        credentials.get_server_URL(), **extra_config)

    # set default return value
    json_result = "None"

    if args.key is None:
        # retrieve whole handle
        result = client.retrieve_handle_record_json(args.handle)
        if result is not None:
            json_result = json.dumps(result["values"])
    else:
        # retrieve single value from a handle
        result = client.get_value_from_handle(args.handle, args.key)
        if result is not None:
            json_result = json.dumps(result)
            # remove starting and finishing quotes.
            json_result = json_result.lstrip('"')
            json_result = json_result.rstrip('"')

    sys.stdout.write(json_result)
Esempio n. 26
0
def create(args):
    """perform create action"""

    try:
        # load credentials
        credentials = PIDClientCredentials.load_from_JSON(args.credpath)
    except CredentialsFormatError:
        sys.stdout.write('error')
        return
    except HandleSyntaxError:
        sys.stdout.write('error')
        return

    # retrieve and set extra values
    extra_config = {}

    # create a handle to put. Concate the prefix with a new generated suffix
    prefix = str(credentials.get_prefix())
    uid = uuid.uuid1()
    suffix = str(uid)
    handle = prefix + "/" + suffix

    try:
        # setup connection to handle server
        client = EUDATHandleClient.instantiate_with_credentials(
            credentials, **extra_config)
    except HandleNotFoundException:
        sys.stdout.write('error')
        return

    overwrite = False
    result = create_execution(client, handle, args.location, overwrite,
                              args.checksum, args.loc10320, args.extratype)

    sys.stdout.write(result)
Esempio n. 27
0
def read(args):
    """perform read action"""

    try:
        # load credentials
        credentials = PIDClientCredentials.load_from_JSON(args.credpath)
    except CredentialsFormatError:
        sys.stdout.write('error')
        return
    except HandleSyntaxError:
        sys.stdout.write('error')
        return

    # retrieve and set extra values
    extra_config = {}

    try:
        # setup connection to handle server
        client = EUDATHandleClient.instantiate_with_credentials(
            credentials, **extra_config)
    except HandleNotFoundException:
        sys.stdout.write('error')
        return

    if args.key is None:
        result = read_execution(client, args.handle)
    else:
        result = read_execution(client, args.handle, args.key)

    sys.stdout.write(result)
Esempio n. 28
0
 def check_issue_pid(self):
     # Checking PID HANDLE
     print('checkin PIDs')
     sleep(5)
     handle_client = EUDATHandleClient.instantiate_for_read_access()
     self.uid = self.issue['uid']
     for line in self.dsets:
         print('CHECKING {} ERRATA IDS'.format(line))
         exists = False
         dataset = line.split('#')
         dset_id = dataset[0]
         dset_version = dataset[1]
         hash_basis = dset_id+'.v'+dset_version
         hash_basis_utf8 = hash_basis.encode('utf-8')
         handle_string = uuid.uuid3(uuid.NAMESPACE_URL, hash_basis_utf8)
         encoded_dict = handle_client.retrieve_handle_record(prefix + str(handle_string))
         if encoded_dict is not None:
                 handle_record = {k.decode('utf8'): v.decode('utf8') for k, v in encoded_dict.items()}
                 if 'ERRATA_IDS' in handle_record.keys():
                     for uid in str(handle_record['ERRATA_IDS']).split(';'):
                         if uid == self.uid:
                             exists = True
                             break
         if not exists:
             print('An error occurred updating handle.')
             return exists
    def test_instantiate_with_username_and_wrong_password(self):
        """Test instantiation of client: No exception if password wrong."""

        # Create client instance with username and password
        inst = EUDATHandleClient.instantiate_with_username_and_password(
            self.url, self.user, self.randompassword, HTTPS_verify=self.https_verify
        )
        self.assertIsInstance(inst, EUDATHandleClient)
    def setUp(self):

        REQUESTLOGGER.info("\n"+60*"*"+"\nsetUp of EUDATHandleClientWriteaccessTestCase")

        self.inst = EUDATHandleClient.instantiate_with_username_and_password(
            self.url,
            self.user,
            self.password,
            HTTPS_verify=self.https_verify)

        authstring = b2handle.utilhandle.create_authentication_string(self.user, self.password)
        self.headers = {
            'Content-Type': 'application/json',
            'Authorization': 'Basic '+authstring
        }

        list_of_all_entries = [
            {
                "index":111,
                "type": "TEST1",
                "data":{
                    "format":"string",
                    "value":"val1"
                }
            },
            {
                "index":2222,
                "type": "TEST2",
                "data":{
                    "format":"string",
                    "value":"val2"
                }
            },
            {
                "index":333,
                "type": "TEST3",
                "data":{
                    "format":"string",
                    "value":"val3"
                }
            },
            {
                "index":4,
                "type": "TEST4",
                "data":{
                    "format":"string",
                    "value":"val4"
                }
            },
        ]

        testhandle = self.handle
        url = self.connector.make_handle_URL(testhandle)
        veri = self.https_verify
        head = self.headers
        data = json.dumps({'values':list_of_all_entries})
        resp = requests.put(url, data=data, headers=head, verify=veri)
        log_request_response_to_file('PUT', self.handle, url, head, veri, resp)
    def test_instantiate_wrong_search_url(self):

        inst = EUDATHandleClient.instantiate_for_read_and_search(
            'someurl',
            'someuser',
            'somepassword',
            reverselookup_baseuri='http://something_random_foo_bar')

        self.assertIsInstance(inst, EUDATHandleClient)
Esempio n. 32
0
    def setUp(self):

        REQUESTLOGGER.info("\n" + 60 * "*" +
                           "\nsetUp of EUDATHandleClientWriteaccessTestCase")

        self.inst = EUDATHandleClient.instantiate_with_username_and_password(
            self.url, self.user, self.password, HTTPS_verify=self.https_verify)

        authstring = self.inst.create_authentication_string(
            self.user, self.password)
        self.headers = {
            'Content-Type': 'application/json',
            'Authorization': 'Basic ' + authstring
        }

        list_of_all_entries = [
            {
                "index": 111,
                "type": "test1",
                "data": {
                    "format": "string",
                    "value": "val1"
                }
            },
            {
                "index": 2222,
                "type": "test2",
                "data": {
                    "format": "string",
                    "value": "val2"
                }
            },
            {
                "index": 333,
                "type": "test3",
                "data": {
                    "format": "string",
                    "value": "val3"
                }
            },
            {
                "index": 4,
                "type": "test4",
                "data": {
                    "format": "string",
                    "value": "val4"
                }
            },
        ]

        testhandle = self.handle
        url = self.inst.make_handle_URL(testhandle)
        veri = self.https_verify
        head = self.headers
        data = json.dumps({'values': list_of_all_entries})
        resp = requests.put(url, data=data, headers=head, verify=veri)
        log_request_response_to_file('PUT', self.handle, url, head, veri, resp)
    def test_instantiate_with_username_and_password_inexistentuser(self, getpatch):
        
        # Define the replacement for the patched method:
        mock_response = MockResponse(notfound=True)
        getpatch.return_value = mock_response

        with self.assertRaises(HandleNotFoundException):
            inst = EUDATHandleClient.instantiate_with_username_and_password(
                'http://someurl', '100:john/doe', 'passywordy')
Esempio n. 34
0
    def test_instantiate_with_username_and_password_wrongpw(self, getpatch):

        # Define the replacement for the patched method:
        mock_response = MockResponse(success=True)
        getpatch.return_value = mock_response

        inst = EUDATHandleClient.instantiate_with_username_and_password(
            'http://someurl', '100:my/testhandle', 'passywordy')

        self.assertIsInstance(inst, EUDATHandleClient)
Esempio n. 35
0
def conn_handle(credentials='cred_21.T12995.json'):
    cred = PIDClientCredentials.load_from_JSON(credentials)
    print GREEN, "DEBUG"
    print('PID prefix ' + cred.get_prefix())
    print('Server ' + cred.get_server_URL())
    print DEFAULT

    ec = EUDATHandleClient.instantiate_with_credentials(cred)

    return ec, cred
Esempio n. 36
0
    def test_instantiate_with_username_and_password_inexistentuser(
            self, getpatch):

        # Define the replacement for the patched method:
        mock_response = MockResponse(notfound=True)
        getpatch.return_value = mock_response

        with self.assertRaises(HandleNotFoundException):
            inst = EUDATHandleClient.instantiate_with_username_and_password(
                'http://someurl', '100:john/doe', 'passywordy')
Esempio n. 37
0
    def test_instantiate_with_username_and_wrong_password(self):
        """Test instantiation of client: No exception if password wrong."""

        # Create client instance with username and password
        inst = EUDATHandleClient.instantiate_with_username_and_password(
            self.url,
            self.user,
            self.randompassword,
            HTTPS_verify=self.https_verify)
        self.assertIsInstance(inst, EUDATHandleClient)
    def test_instantiate_for_read_access(self):
        """Testing if instantiating with default handle server works
        and if a handle is correctly retrieved. """

        # Create client instance with username and password
        inst = EUDATHandleClient.instantiate_for_read_access(HTTPS_verify=self.https_verify)
        rec = self.inst.retrieve_handle_record_json(self.handle)
        self.assertIsInstance(inst, EUDATHandleClient)
        self.assertIn("handle", rec, 'Response lacks "handle".')
        self.assertIn("responseCode", rec, 'Response lacks "responseCode".')
    def test_instantiate_with_username_and_password_wrongpw(self, getpatch):
        

        # Define the replacement for the patched method:
        mock_response = MockResponse(success=True)
        getpatch.return_value = mock_response

        inst = EUDATHandleClient.instantiate_with_username_and_password(
                'http://someurl', '100:my/testhandle', 'passywordy')

        self.assertIsInstance(inst, EUDATHandleClient)
    def test_instantiate_with_username_without_index_and_password(self):
        """Test instantiation of client: Exception if username has no index."""
        testusername_without_index = self.user.split(":")[1]

        # Run code to be tested + check exception:
        with self.assertRaises(HandleSyntaxError):

            # Create client instance with username and password
            inst = EUDATHandleClient.instantiate_with_username_and_password(
                self.url, testusername_without_index, self.randompassword, HTTPS_verify=self.https_verify
            )
Esempio n. 41
0
    def test_instantiate_for_read_access(self):
        """Testing if instantiating with default handle server works
        and if a handle is correctly retrieved. """

        # Create client instance with username and password
        inst = EUDATHandleClient.instantiate_for_read_access(
            HTTPS_verify=self.https_verify)
        rec = self.inst.retrieve_handle_record_json(self.handle)
        self.assertIsInstance(inst, EUDATHandleClient)
        self.assertIn('handle', rec, 'Response lacks "handle".')
        self.assertIn('responseCode', rec, 'Response lacks "responseCode".')
    def test_instantiate_with_nonexistent_username_and_password(self):
        """Test instantiation of client: Exception if username does not exist."""
        testusername_inexistent = "100:" + self.inexistent_handle

        # Run code to be tested + check exception:
        with self.assertRaises(HandleNotFoundException):

            # Create client instance with username and password
            inst = EUDATHandleClient.instantiate_with_username_and_password(
                self.url, testusername_inexistent, self.randompassword, HTTPS_verify=self.https_verify
            )
Esempio n. 43
0
    def create_required_test_handles(self):

        # Creating an instance that knows how to write:
        pw = self.testvalues['password']
        inst = EUDATHandleClient.instantiate_with_username_and_password(
            self.testvalues['handle_server_url_write'],
            self.user,
            pw,
            HTTPS_verify=self.https_verify)

        authstring = b2handle.utilhandle.create_authentication_string(
            self.user, pw)
        headers = {
            'Content-Type': 'application/json',
            'Authorization': 'Basic ' + authstring
        }

        list_of_all_entries = [{
            "index": 100,
            "type": "HS_ADMIN",
            "data": {
                "format": "admin",
                "value": {
                    "handle": "21.T14999/B2HANDLE_INTEGRATION_TESTS",
                    "index": 300,
                    "permissions": "011111110011"
                }
            }
        }, {
            "index": 111,
            "type": "TEST1",
            "data": "val1"
        }, {
            "index": 2222,
            "type": "TEST2",
            "data": "val2"
        }, {
            "index": 333,
            "type": "TEST3",
            "data": "val3"
        }, {
            "index": 4,
            "type": "TEST4",
            "data": "val4"
        }]

        testhandle = self.handle
        url = self.testvalues['handle_server_url_write'] + self.testvalues[
            'url_extension_REST_API'] + testhandle
        veri = self.https_verify
        head = headers
        data = json.dumps({'values': list_of_all_entries})
        resp = requests.put(url, data=data, headers=head, verify=veri)
Esempio n. 44
0
 def retrieve_handle(self, handle):
     """Retrieve a handle."""
     try:
         credential = PIDClientCredentials.load_from_JSON(
             self.credential_path)
         client = EUDATHandleClient.instantiate_with_credentials(credential)
         handle_record_json = client.retrieve_handle_record_json(handle)
         return jsonify(handle_record_json)
     except (CredentialsFormatError, FileNotFoundError) as e:
         current_app.logger.error(
             '{} in HandleClient.retrieve_handle_record_json({})'.format(
                 e, handle))
Esempio n. 45
0
    def __init__(self, credentials=None):
        """Constructor.

        :param credentials: B2Handle credentials.
        """
        self.credentials = credentials
        self.handle_prefix = None
        self.handle_client = None
        if credentials:
            self.handle_prefix = credentials.get('prefix')
            assert self.handle_prefix
            self.handle_client = EUDATHandleClient(**credentials)
Esempio n. 46
0
def main():
    """ Main function to test the script """
    client = EUDATHandleClient.instantiate_for_read_access()
    value = client.get_value_from_handle("11100/33ac01fc-6850-11e5-b66e-e41f13eb32b2", "URL")
    print value

    result = client.search_handle("irods://data.repo.cineca.it:1247/CINECA01/home/cin_staff/rmucci00/DSI_Test/test.txt")
    print result



    get_pid_info(pid='11100/0beb6af8-cbe5-11e3-a9da-e41f13eb41b2')
    def setUp(self):

        REQUESTLOGGER.info("\n"+60*"*"+"\nsetUp of EUDATHandleClientWriteaccess10320LOCTestCase")

        self.inst = EUDATHandleClient.instantiate_with_username_and_password(
            self.url,
            self.user,
            self.password,
            HTTPS_verify=self.https_verify)
        
        list_of_all_entries_with = [
            {
                "index":1,
                "type":"URL",
                "data":"www.url.foo"
            },
            {
                "index":2,
                "type":"10320/LOC",
                "data":{
                    "format":"string",
                    "value":"<locations><location href = 'http://first.foo' /><location href = 'http://second.foo' /></locations> "
                }
            }
        ]

        list_of_all_entries_without = [
            {
                "index":1,
                "type":"URL",
                "data":"www.url.foo"
            }
        ]

        authstring = b2handle.utilhandle.create_authentication_string(self.user, self.password)
        head = {
            'Content-Type': 'application/json',
            'Authorization': 'Basic '+authstring
        }
        veri = self.https_verify

        testhandle = self.handle_withloc
        url = self.connector.make_handle_URL(testhandle)
        data = json.dumps({'values':list_of_all_entries_with})
        resp = requests.put(url, data=data, headers=head, verify=veri)
        log_request_response_to_file('PUT', testhandle, url, head, veri, resp)

        testhandle = self.handle_withoutloc
        url = self.connector.make_handle_URL(testhandle)
        data = json.dumps({'values':list_of_all_entries_without})
        requests.put(url, data=data, headers=head, verify=veri)
        log_request_response_to_file('PUT', testhandle, url, head, veri, resp)
Esempio n. 48
0
    def setUp(self):

        REQUESTLOGGER.info("\n"+60*"*"+"\nsetUp of EUDATHandleClientWriteaccess10320LOCTestCase")

        self.inst = EUDATHandleClient.instantiate_with_username_and_password(
            self.url,
            self.user,
            self.password,
            HTTPS_verify=self.https_verify)
        
        list_of_all_entries_with = [
            {
                "index":1,
                "type":"URL",
                "data":"www.url.foo"
            },
            {
                "index":2,
                "type":"10320/LOC",
                "data":{
                    "format":"string",
                    "value":"<locations><location href = 'http://first.foo' /><location href = 'http://second.foo' /></locations> "
                }
            }
        ]

        list_of_all_entries_without = [
            {
                "index":1,
                "type":"URL",
                "data":"www.url.foo"
            }
        ]

        authstring = self.inst.create_authentication_string(self.user, self.password)
        head = {
            'Content-Type': 'application/json',
            'Authorization': 'Basic '+authstring
        }
        veri = self.https_verify

        testhandle = self.handle_withloc
        url = self.inst.make_handle_URL(testhandle)
        data = json.dumps({'values':list_of_all_entries_with})
        resp = requests.put(url, data=data, headers=head, verify=veri)
        log_request_response_to_file('PUT', testhandle, url, head, veri, resp)

        testhandle = self.handle_withoutloc
        url = self.inst.make_handle_URL(testhandle)
        data = json.dumps({'values':list_of_all_entries_without})
        requests.put(url, data=data, headers=head, verify=veri)
        log_request_response_to_file('PUT', testhandle, url, head, veri, resp)
Esempio n. 49
0
    def test_instantiate_with_nonexistent_username_and_password(self):
        """Test instantiation of client: Exception if username does not exist."""
        testusername_inexistent = '100:' + self.inexistent_handle

        # Run code to be tested + check exception:
        with self.assertRaises(HandleNotFoundException):

            # Create client instance with username and password
            inst = EUDATHandleClient.instantiate_with_username_and_password(
                self.url,
                testusername_inexistent,
                self.randompassword,
                HTTPS_verify=self.https_verify)
    def test_instantiate_with_credentials_inexistentuser(self):
        """Test instantiation of client: Exception if username does not exist."""

        # Test variables
        testusername_inexistent = "100:" + self.inexistent_handle
        credentials = b2handle.clientcredentials.PIDClientCredentials(
            handle_server_url=self.url, username=testusername_inexistent, password=self.randompassword
        )

        # Run code to be tested + check exception:
        # Create instance with credentials
        with self.assertRaises(HandleNotFoundException):
            inst = EUDATHandleClient.instantiate_with_credentials(credentials, HTTPS_verify=self.https_verify)
Esempio n. 51
0
def read(args):
    """perform read action"""

    try:
        # load credentials
        credentials = PIDClientCredentials.load_from_JSON(args.credpath)
    except CredentialsFormatError:
        sys.stdout.write('error')
        return
    except HandleSyntaxError:
        sys.stdout.write('error')
        return

    # retrieve and set extra values
    extra_config = {}

    try:
        # setup connection to handle server
        client = EUDATHandleClient.instantiate_with_credentials(
            credentials,
            **extra_config)
    except HandleNotFoundException:
        sys.stdout.write('error')
        return

    # set default return value
    json_result = "None"

    if args.key is None:
        try:
            # retrieve whole handle
            result = client.retrieve_handle_record_json(args.handle)
        except HandleSyntaxError:
            json_result = 'error'

        if result is not None:
            json_result = json.dumps(result["values"])
    else:
        try:
            # retrieve single value from a handle
            result = client.get_value_from_handle(args.handle, args.key)
        except HandleSyntaxError:
            json_result = 'error'

        if result is not None:
            json_result = json.dumps(result)
            # remove starting and finishing quotes.
            json_result = json_result.lstrip('"')
            json_result = json_result.rstrip('"')

    sys.stdout.write(json_result)
    def test_instantiate_with_credentials(self):
        """Test instantiation of client: No exception if password wrong."""

        # Test variables
        credentials = b2handle.clientcredentials.PIDClientCredentials(
            handle_server_url=self.url, username=self.user, password=self.randompassword
        )

        # Run code to be tested
        # Create instance with credentials
        inst = EUDATHandleClient.instantiate_with_credentials(credentials, HTTPS_verify=self.https_verify)

        # Check desired outcomes
        self.assertIsInstance(inst, EUDATHandleClient)
    def setUp(self):
        '''Providing a client instance that has read and search access
        to the servers specified in the test resources JSON file.
        '''

        REQUESTLOGGER.info("\n"+60*"*"+"\nsetUp of EUDATHandleClientSearchTestCase")

        self.inst = EUDATHandleClient.instantiate_for_read_and_search(
            self.url,
            self.searchuser,
            self.searchpassword,
            reverselookup_baseuri=self.searchurl,
            reverselookup_url_extension=self.searchpath,
            HTTPS_verify=self.https_verify)
Esempio n. 54
0
def delete(args):
    """perform delete action"""

    try:
        # load credentials
        credentials = PIDClientCredentials.load_from_JSON(args.credpath)
    except CredentialsFormatError:
        sys.stdout.write('error')
        return
    except HandleSyntaxError:
        sys.stdout.write('error')
        return

    # retrieve and set extra values
    extra_config = {}

    try:
        # setup connection to handle server
        client = EUDATHandleClient.instantiate_with_credentials(
            credentials,
            **extra_config)
    except HandleNotFoundException:
        sys.stdout.write('error')
        return

    result = 'True'

    if args.key is None:
        # delete whole handle
        try:
            client.delete_handle(args.handle)
        except HandleAuthenticationError:
            result = 'error'
        except HandleNotFoundException:
            result = 'False'
        except HandleSyntaxError:
            result = 'error'
    else:
        # delete value
        try:
            client.delete_handle_value(args.handle, args.key)
        except HandleAuthenticationError:
            result = 'error'
        except HandleNotFoundException:
            result = 'False'
        except HandleSyntaxError:
            result = 'error'

    sys.stdout.write(result)
    def setUp(self):
        """ For most test, provide a client instance with the user-specified
        handle server url."""

        self.inst = EUDATHandleClient(
            HTTPS_verify=self.https_verify, handle_server_url=self.url, url_extension_REST_API=self.path_to_api
        )

        # Before being able to run these tests without write access,
        # the handle that we use for testing must exist. With this code,
        # you can create it. You only need to create it once and leave it
        # on the server, it will not be modified and can be used eternally.
        if False:
            # This should always be false!!! Except for creating the
            # required handle once!
            self.create_required_test_handles()
    def test_instantiate_with_credentials_config(self, checkpatch):
        """Test instantiation of client: No exception if password wrong."""

        # Define the replacement for the patched method:
        mock_response = MockResponse(success=True)
        checkpatch.return_value = mock_response

        # Test variables
        credentials = MockCredentials(restapi='foobar')

        self.assertEqual(credentials.get_config()['REST_API_url_extension'],'foobar',
            'Config: '+str(credentials.get_config()))

        # Run code to be tested
        # Create instance with credentials
        inst = EUDATHandleClient.instantiate_with_credentials(credentials)
        self.assertIsInstance(inst, EUDATHandleClient)
    def test_instantiate_with_credentials_inexistentuser(self, getpatch):
        """Test instantiation of client: Exception if username does not exist."""

        # Define the replacement for the patched method:
        mock_response = MockResponse(notfound=True)
        getpatch.return_value = mock_response

        # Test variables
        testusername_inexistent = "100:john/doe"
        credentials = b2handle.clientcredentials.PIDClientCredentials(
            "some/url", testusername_inexistent, "some_password"
        )

        # Run code to be tested + check exception:
        # Create instance with credentials
        with self.assertRaises(HandleNotFoundException):
            inst = EUDATHandleClient.instantiate_with_credentials(credentials)
    def create_required_test_handles(self):

        # Creating an instance that knows how to write:
        pw = self.testvalues['password']
        inst = EUDATHandleClient.instantiate_with_username_and_password(
            self.testvalues['handle_server_url_write'],
            self.user,
            pw,
            HTTPS_verify=self.https_verify)

        authstring = self.inst.create_authentication_string(self.user, pw)
        headers = {
            'Content-Type': 'application/json',
            'Authorization': 'Basic '+authstring
        }

        list_of_all_entries = [
            {
                "index":111,
                "type":"test1",
                "data":"val1"
            },
            {
                "index":2222,
                "type":"test2",
                "data":"val2"
            },
            {
                "index":333,
                "type":"test3",
                "data":"val3"
            },
            {
                "index":4,
                "type":"test4",
                "data":"val4"
            }
        ]

        testhandle = self.handle
        url = inst.make_handle_URL(testhandle)
        veri = self.https_verify
        head = headers
        data = json.dumps({'values':list_of_all_entries})
        resp = requests.put(url, data=data, headers=head, verify=veri)