import re class ConnectionInfo: """Connection info class.""" def __init__(self, data, order_type='release'): """Init the connection info class. Args: data (dict): A dictionary with connection info data. order_type (str): Must be a valid order type. """ self.order_type = order_type self.supported_s3_signature_versions = ['s3', 'v2', 'v3https', 'v4'] self.supported_s3_regions = [ 'us-east-1', 'us-east-2', 'us-west-1', 'us-west-2', 'af-south-1', 'ap-east-1', 'ap-northeast-1', 'ap-northeast-2', 'ap-northeast-3', 'ap-south-1', 'ap-south-2', 'ap-southeast-1', 'ap-southeast-2', 'ap-southeast-3', 'ap-southeast-4', 'ap-southeast-5', 'ap-southeast-7', 'ca-central-1', 'ca-west-1', 'cn-north-1', 'eu-central-1', 'eu-central-2', 'eu-north-1', 'eu-south-1', 'eu-south-2', 'eu-west-1', 'eu-west-2', 'eu-west-3', 'il-central-1', 'me-south-1', 'me-central-1', 'mx-central-1', 'sa-east-1'] self.gcs_config_file = None # cloud storage properties self.udp_port = None self.cloud_domain_name = None self.cloud_user_name = None self.cloud_password = None self.cloud_protocol = None self.connection_type = data.get('connection_type') self.authenticate_type = data.get('authenticate_type') self.user_name = data.get('user_name') self._validate_required_fields(data) self._validate_non_hard_drive_fields(data) self._validate_cloud_fields(data) self.remote_initial_dir = data.get('remote_initial_dir', '') self.port = data.get('port', 0) self.domain_name = data.get('domain_name', '') self.password = data.get('password', '') self.priv_key = data.get('priv_key', '') self.pub_key = data.get('pub_key', '') self.md5_fingerprint = data.get('md5_fingerprint', '') self.ftp_pasv_mode = True if data.get('ftp_pasv_mode', '') == 'Y' else False # noqa self.file_encryption_passphrase = data.get( 'file_encryption_passphrase') self.pubkey_passphrase = data.get('pubkey_passphrase') self.aspera_token = data.get('aspera_token') self._sanitize_domain_name() self._set_cloud_storage_properties(data) self.s3_region = data.get('aws_s3_region', 'us-east-1') self.s3_signature_version = data.get('aws_s3_signature_version') self.sftp_disabled_algorithms = data.get('sftp_disabled_algorithms') def _validate_required_fields(self, data): if 'authenticate_type' not in data: raise ValueError('Authenticate Type is missing.') if 'connection_type' not in data: raise ValueError('Connection Type is missing.') if 'aws_s3_region' in data and \ data['aws_s3_region'] not in self.supported_s3_regions: raise ValueError('Invalid AWS S3 region.') if 'aws_s3_signature_version' in data and \ data[ 'aws_s3_signature_version'] not in self.supported_s3_signature_versions: # noqa raise ValueError('Invalid AWS S3 region.') def _validate_non_hard_drive_fields(self, data): if self.order_type != 'harddrive': if data.get('connection_type') != 'itp' and \ data.get('domain_name') is None: raise ValueError('Domain Name is missing.') if data.get('connection_type') == 'gcs': return if data.get('connection_type') not in ['itp', 's3', 'gcs'] and \ data.get('port') is None: raise ValueError('Port is missing.') if not data.get('user_name'): raise ValueError('User Name is missing.') if data.get('authenticate_type') == 'password' and \ data.get('password') is None: raise ValueError('Password is missing.') if data.get('authenticate_type') == 'public_key' and \ data.get('priv_key') is None: raise ValueError('Private Key is missing.') def _validate_cloud_fields(self, data): if self.connection_type == 'aspera_cloud': if not data.get('udp_port'): raise ValueError('UDP Port is missing.') if not data.get('cloud_domain_name'): raise ValueError('Cloud Domain Name is missing.') if not data.get('cloud_user_name'): raise ValueError('Cloud User Name is missing.') if not data.get('cloud_password'): raise ValueError('Cloud Password is missing.') if not data.get('cloud_protocol'): raise ValueError('Cloud Protocol is missing.') elif self.connection_type == 'gcs': if not data.get('gcs_config_file'): raise ValueError('GCS Config file is missing.') def _set_cloud_storage_properties(self, data): if self.connection_type == 'aspera_cloud': self.udp_port = data['udp_port'] self.cloud_domain_name = data['cloud_domain_name'] self.cloud_user_name = data['cloud_user_name'] self.cloud_password = data['cloud_password'] self.cloud_protocol = data['cloud_protocol'] elif self.connection_type == 'gcs': self.gcs_config_file = data['gcs_config_file'] def _sanitize_domain_name(self): self.domain_name = re.sub( r'^((s3)|(s?ftp)|(itp))(:\/\/)', '', self.domain_name)