Пример #1
0
 def _assert_valid_variable_value(self, variable, value):
   if variable == CHECKSUM_NAME:
     try:
       d1_common.checksum.get_checksum_calculator_by_dataone_designator(value)
     except LookupError:
       raise cli_exceptions.InvalidArguments(
         'Invalid checksum algorithm: {}'.format(value)
       )
   elif variable == CN_URL_NAME:
     # TODO: Add warning if URL is not a known CN / environment
     pass
   elif variable == MN_URL_NAME:
     cn_base_url = self.get(CN_URL_NAME)
     if value not in [
         n[2] for n in self._nodes.get(cn_base_url) if n[0] == 'mn'
     ]:
       if not cli_util.confirm(
           '"{}" is not a known DataONE Member Node. Use anyway?'.
           format(value)
       ):
         raise cli_exceptions.InvalidArguments('Member Node update cancelled')
   elif variable == FORMAT_NAME:
     cn_base_url = self.get(CN_URL_NAME)
     if value not in self._format_ids.get(cn_base_url):
       raise cli_exceptions.InvalidArguments(
         'Invalid Object Format ID: {}'.format(value)
       )
Пример #2
0
 def _assert_valid_identifier_value(self, pid):
     if pid.count(' ') or pid.count('\t'):
         raise cli_exceptions.InvalidArguments(
             'Identifier cannot contain space or tab characters: {}'.format(
                 pid))
     if len(pid) > 800:
         raise cli_exceptions.InvalidArguments(
             'Identifier cannot be longer than 800 characters: {}'.format(
                 pid))
Пример #3
0
 def _assert_operation_has_valid_operation_type(self, operation):
     if 'operation' not in operation:
         raise cli_exceptions.InvalidArguments(
             'Operation is missing the operation type')
     if operation['operation'] not in ('create', 'update', 'create_package',
                                       'archive', 'update_access_policy',
                                       'update_replication_policy'):
         raise cli_exceptions.InvalidArguments(
             'Operation is of invalid type: {}'.format(
                 operation['operation']))
Пример #4
0
 def _assert_valid_access_control(self, operation):
     self._assert_value_type(operation, list, 'parameters', 'allow')
     for allow in operation['parameters']['allow']:
         if len(allow) != 2:
             raise cli_exceptions.InvalidArguments(
                 'Access control rule must be subject and permission: {}'.
                 format(', '.join(allow)))
         if allow[1] not in ('read', 'write', 'changePermission'):
             raise cli_exceptions.InvalidArguments(
                 'Access control permission must be read, write or changePermission: {}'
                 .format(allow[1]))
Пример #5
0
 def _assert_present(self, operation, *keys):
     operation_type = operation['operation']
     for key in keys:
         if key not in operation:
             raise cli_exceptions.InvalidArguments(
                 'Operation parameter "{}" must be present for {} operations'
                 .format(key, operation_type))
         if operation[key] is None:
             raise cli_exceptions.InvalidArguments(
                 'Operation parameter "{}" must be set for {} operations'.
                 format(key, operation_type))
         operation = operation[key]
Пример #6
0
 def _confirm_special_subject_write(self, subject, permission):
     if subject in ('public', 'authenticatedUser',
                    'verifiedUser') and permission != 'read':
         if not cli_util.confirm(
                 'It is not recommended to give {} access to {}. Continue?'.
                 format(permission, subject)):
             raise cli_exceptions.InvalidArguments('Cancelled')
Пример #7
0
 def set_with_conversion(self, variable, value_string):
   """Convert user supplied string to Python type. Lets user use values such as
   True, False and integers. All variables can be set to None, regardless of
   type. Handle the case where a string is typed by the user and is not quoted,
   as a string literal.
   """
   self._assert_valid_variable(variable)
   try:
     v = ast.literal_eval(value_string)
   except (ValueError, SyntaxError):
     v = value_string
   if v is None or v == 'none':
     self._variables[variable] = None
   else:
     try:
       type_converter = variable_type_map[variable]
       value_string = self._validate_variable_type(
         value_string, type_converter
       )
       value = type_converter(value_string)
       self._variables[variable] = value
     except ValueError:
       raise cli_exceptions.InvalidArguments(
         'Invalid value for {}: {}'.format(variable, value_string)
       )
Пример #8
0
 def _assert_authenticated_access(self, operation):
     self._assert_value_type(operation, bool, 'authentication', 'anonymous')
     auth = operation['authentication']
     if auth['anonymous']:
         raise cli_exceptions.InvalidArguments(
             'This operation cannot be performed without authentication')
     cli_util.assert_file_exists(operation['authentication']['cert-file'])
Пример #9
0
 def remove_allowed_subject(self, subject):
   try:
     del self.allow[subject]
   except KeyError:
     raise cli_exceptions.InvalidArguments(
       'Subject not in access control list: {}'.format(subject)
     )
Пример #10
0
 def _assert_valid_member_node_urn(self, operation, *keys):
     self._assert_value_type(operation, basestring, *keys)
     for key in keys:
         operation = operation[key]
     if not operation.startswith('urn:node'):
         raise cli_exceptions.InvalidArguments(
             'Invalid Member Node ID. Must start with "urn:node". parameter={}, value={}'
             .format(key, operation))
Пример #11
0
 def search(self, line):
     """CN search.
 """
     if self._session.get(session.QUERY_ENGINE_NAME) == u'solr':
         return self._search_solr(line)
     raise cli_exceptions.InvalidArguments(
         'Unsupported query engine: {}'.format(
             self._session.get(session.QUERY_ENGINE_NAME)))
Пример #12
0
 def _assert_valid_base_url(self, operation, *keys):
     self._assert_value_type(operation, basestring, *keys)
     for key in keys:
         operation = operation[key]
     o = urlparse.urlparse(operation)
     if o.scheme not in ('http', 'https'):
         raise cli_exceptions.InvalidArguments(
             'Invalid BaseURL. Must use HTTP or HTTPS protocol. parameter={}, value={}'
             .format(key, operation))
Пример #13
0
 def _assert_valid_auth_parameter_combination(self, operation):
     self._assert_value_type(operation, bool, 'authentication', 'anonymous')
     auth = operation['authentication']
     if not auth['anonymous']:
         if not self._is_value_type(operation, basestring, 'authentication',
                                    'cert-file'):
             raise cli_exceptions.InvalidArguments(
                 'Specified an authenticated connection without providing a certificate'
             )
         cli_util.assert_file_exists(
             operation['authentication']['cert-file'])
     if (self._is_value_type(operation, type(None), 'authentication',
                             'cert-file')
             and not self._is_value_type(operation, type(None),
                                         'authentication', 'key-file')):
         raise cli_exceptions.InvalidArguments(
             'Specified a certificate private key without specifying a certificate'
         )
Пример #14
0
 def _assert_valid_checksum_algorithm(self, operation):
     self._assert_value_type(operation, str, 'parameters', 'algorithm')
     algorithm = operation['parameters']['algorithm']
     try:
         d1_common.checksum.get_checksum_calculator_by_dataone_designator(
             algorithm)
     except LookupError:
         raise cli_exceptions.InvalidArguments(
             'Invalid checksum algorithm: {}'.format(algorithm))
Пример #15
0
 def execute(self):
     self._assert_queue_not_empty()
     self._print_operation_queue()
     if not cli_util.confirm(
             'You are about to perform {} queued write operations. Continue?'
             .format(len(self._operations)),
             default='yes'):
         raise cli_exceptions.InvalidArguments('Cancelled')
     while len(self._operations):
         self._execute_operation(self._operations[0])
         self._operations = self._operations[1:]
Пример #16
0
 def _split_args(self, line, n_required, n_optional, pad=True):
     # args = [a.decode('utf-8') for a in shlex.split(line.encode('utf-8'))]
     args = shlex.split(line)
     n_optional_max = 1000 if n_optional == -1 else n_optional
     if len(args) < n_required or len(args) > n_required + n_optional_max:
         msg = self._text_description_of_required_and_optional(
             n_required, n_optional)
         raise cli_exceptions.InvalidArguments(msg)
     if pad:
         # Pad the list_objects out with None for any optional parameters that were
         # not provided.
         args += [None] * (n_required + n_optional - len(args))
     #if len(args) == 1:
     #  return args[0]
     return args
Пример #17
0
 def _assert_valid_variable(self, variable):
   if variable not in self._variables:
     raise cli_exceptions.InvalidArguments(
       'Invalid session variable: {}'.format(variable)
     )
Пример #18
0
def assert_file_exists(path):
    if not os.path.isfile(os.path.expanduser(path)):
        msg = 'Invalid file: {}'.format(path)
        raise cli_exceptions.InvalidArguments(msg)
Пример #19
0
 def _assert_value_type(self, operation, type_, *keys):
     self._assert_present(operation, *keys)
     if not self._is_value_type(operation, type_, *keys):
         raise cli_exceptions.InvalidArguments(
             'Operation parameter "{}" must be a {}'.format(
                 keys[-1], self._type_map[type_]))
Пример #20
0
 def _assert_queue_not_empty(self):
     if not len(self._operations):
         raise cli_exceptions.InvalidArguments(
             'There are no operations in the queue')
Пример #21
0
 def _assert_valid_permission(self, permission):
   if permission not in self._get_valid_permissions():
     msg = 'Invalid permission: {}. Must be one of: {}'\
       .format(permission, ', '.join(self._get_valid_permissions()))
     raise cli_exceptions.InvalidArguments(msg)