Esempio n. 1
0
    def test_recording_processor_skip_body_on_unrecognized_content_type(self):
        location_header_template = (
            "https://graph.windows.net/{}/directoryObjects/"
            "f604c53a-aa21-44d5-a41f-c1ef0b5304bd/Microsoft.DirectoryServices.Application"
        )
        replaced_subscription_id = str(uuid.uuid4())
        rp = SubscriptionRecordingProcessor(replaced_subscription_id)

        mock_sub_id = str(uuid.uuid4())
        mock_response = dict({"body": {}})
        mock_response["body"]["string"] = mock_sub_id
        mock_response["headers"] = {
            "Location": [location_header_template.format(mock_sub_id)],
            "content-type": ["application/foo"],
        }

        # action
        rp.process_response(mock_response)

        # assert
        self.assertEqual(mock_response["body"]["string"],
                         mock_sub_id)  # body unchanged
        self.assertEqual(
            mock_response["headers"]["Location"],
            [location_header_template.format(replaced_subscription_id)
             ])  # header changed
    def test_subscription_recording_processor_for_response(self):
        replaced_subscription_id = str(uuid.uuid4())
        rp = SubscriptionRecordingProcessor(replaced_subscription_id)

        body_templates = ['https://management.azure.com/subscriptions/{}/providers/Microsoft.ContainerRegistry/'
                          'checkNameAvailability?api-version=2017-03-01',
                          'https://graph.Windows.net/{}/applications?api-version=1.6',
                          "{{'scope':'/subscriptions/{}', 'another_data':'/Microsoft.Something'}}",
                          'https://management.azure.com\\/subscriptions\\/{}\\/providers\\/'
                          'Microsoft.ContainerRegistry\\/checkNameAvailability?api-version=2017-03-01']

        location_header_template = 'https://graph.windows.net/{}/directoryObjects/' \
                                   'f604c53a-aa21-44d5-a41f-c1ef0b5304bd/Microsoft.DirectoryServices.Application'

        asyncoperation_header_template = 'https://management.azure.com/subscriptions/{}/resourceGroups/' \
                                         'clitest.rg000001/providers/Microsoft.Sql/servers/clitestserver000002/' \
                                         'databases/cliautomationdb01/azureAsyncOperation/' \
                                         '6ec6196b-fbaa-415f-8c1a-6cb634a96cb2?api-version=2014-04-01-Preview'

        for template in body_templates:
            mock_sub_id = str(uuid.uuid4())
            mock_response = dict({'body': {}})
            mock_response['body']['string'] = template.format(mock_sub_id)
            mock_response['headers'] = {'Location': [location_header_template.format(mock_sub_id)],
                                        'azure-asyncoperation': [asyncoperation_header_template.format(mock_sub_id)],
                                        'content-type': ['application/json']}
            rp.process_response(mock_response)
            self.assertEqual(mock_response['body']['string'], template.format(replaced_subscription_id))

            self.assertSequenceEqual(mock_response['headers']['Location'],
                                     [location_header_template.format(replaced_subscription_id)])
            self.assertSequenceEqual(mock_response['headers']['azure-asyncoperation'],
                                     [asyncoperation_header_template.format(replaced_subscription_id)])
Esempio n. 3
0
    def test_subscription_recording_processor_for_response(self):
        replaced_subscription_id = str(uuid.uuid4())
        rp = SubscriptionRecordingProcessor(replaced_subscription_id)

        body_templates = [
            "https://management.azure.com/subscriptions/{}/providers/Microsoft.ContainerRegistry/"
            "checkNameAvailability?api-version=2017-03-01",
            "https://graph.Windows.net/{}/applications?api-version=1.6",
            "{{'scope':'/subscriptions/{}', 'another_data':'/Microsoft.Something'}}",
        ]

        location_header_template = (
            "https://graph.windows.net/{}/directoryObjects/"
            "f604c53a-aa21-44d5-a41f-c1ef0b5304bd/Microsoft.DirectoryServices.Application"
        )

        asyncoperation_header_template = (
            "https://management.azure.com/subscriptions/{}/resourceGroups/"
            "clitest.rg000001/providers/Microsoft.Sql/servers/clitestserver000002/"
            "databases/cliautomationdb01/azureAsyncOperation/"
            "6ec6196b-fbaa-415f-8c1a-6cb634a96cb2?api-version=2014-04-01-Preview"
        )

        for template in body_templates:
            mock_sub_id = str(uuid.uuid4())
            mock_response = dict({"body": {}})
            mock_response["body"]["string"] = template.format(mock_sub_id)
            mock_response["headers"] = {
                "Location": [location_header_template.format(mock_sub_id)],
                "azure-asyncoperation":
                [asyncoperation_header_template.format(mock_sub_id)],
                "content-type": ["application/json"],
            }
            rp.process_response(mock_response)
            self.assertEqual(mock_response["body"]["string"],
                             template.format(replaced_subscription_id))

            self.assertSequenceEqual(
                mock_response["headers"]["Location"],
                [location_header_template.format(replaced_subscription_id)])
            self.assertSequenceEqual(
                mock_response["headers"]["azure-asyncoperation"],
                [
                    asyncoperation_header_template.format(
                        replaced_subscription_id)
                ],
            )
    def test_subscription_recording_processor_for_request(self):
        replaced_subscription_id = str(uuid.uuid4())
        rp = SubscriptionRecordingProcessor(replaced_subscription_id)

        uri_templates = ['https://management.azure.com/subscriptions/{}/providers/Microsoft.ContainerRegistry/'
                         'checkNameAvailability?api-version=2017-03-01',
                         'https://graph.windows.net/{}/applications?api-version=1.6']

        for template in uri_templates:
            mock_sub_id = str(uuid.uuid4())
            mock_request = mock.Mock()
            mock_request.uri = template.format(mock_sub_id)
            mock_request.body = self._mock_subscription_request_body(mock_sub_id)
            mock_request.headers = {'content-type': 'application/json'}

            rp.process_request(mock_request)
            self.assertEqual(mock_request.uri, template.format(replaced_subscription_id))
            self.assertEqual(mock_request.body,
                             self._mock_subscription_request_body(replaced_subscription_id))
    def test_recording_processor_skip_body_on_unrecognized_content_type(self):
        location_header_template = 'https://graph.windows.net/{}/directoryObjects/' \
                                   'f604c53a-aa21-44d5-a41f-c1ef0b5304bd/Microsoft.DirectoryServices.Application'
        replaced_subscription_id = str(uuid.uuid4())
        rp = SubscriptionRecordingProcessor(replaced_subscription_id)

        mock_sub_id = str(uuid.uuid4())
        mock_response = dict({'body': {}})
        mock_response['body']['string'] = mock_sub_id
        mock_response['headers'] = {
            'Location': [location_header_template.format(mock_sub_id)],
            'content-type': ['application/foo']
        }

        # action
        rp.process_response(mock_response)

        # assert
        self.assertEqual(mock_response['body']['string'], mock_sub_id)  # body unchanged
        self.assertEqual(mock_response['headers']['Location'],
                         [location_header_template.format(replaced_subscription_id)])  # header changed
Esempio n. 6
0
    def __init__(self, method_name):
        self._client_id = os.environ.get("AZURE_CLIENT_ID", ZERO_UID)
        self._client_secret = os.environ.get("AZURE_CLIENT_SECRET",
                                             PLACEHOLDER)
        self._tenant_id = os.environ.get("AZURE_TENANT_ID", TENANT_ID)
        self._resource_group = os.environ.get(
            "AZURE_QUANTUM_WORKSPACE_RG",
            os.environ.get("RESOURCE_GROUP", RESOURCE_GROUP))
        self._subscription_id = os.environ.get(
            "AZURE_QUANTUM_SUBSCRIPTION_ID",
            os.environ.get("SUBSCRIPTION_ID", ZERO_UID))
        self._workspace_name = os.environ.get("AZURE_QUANTUM_WORKSPACE_NAME")
        self._location = os.environ.get("AZURE_QUANTUM_WORKSPACE_LOCATION",
                                        os.environ.get("LOCATION", LOCATION))
        self._user_agent = os.environ.get("AZURE_QUANTUM_PYTHON_APPID")

        self._pause_recording_processor = PauseRecordingProcessor()
        regex_replacer = CustomRecordingProcessor()
        recording_processors = [
            self._pause_recording_processor, regex_replacer,
            AccessTokenReplacer(),
            InteractiveAccessTokenReplacer(),
            SubscriptionRecordingProcessor(ZERO_UID),
            AuthenticationMetadataFilter(),
            OAuthRequestResponsesFilter(),
            RequestUrlNormalizer()
        ]

        replay_processors = [
            AuthenticationMetadataFilter(),
            OAuthRequestResponsesFilter(),
            RequestUrlNormalizer(),
            OAuthResponsesFilter(),
        ]

        super(QuantumTestBase, self).__init__(
            method_name,
            recording_processors=recording_processors,
            replay_processors=replay_processors,
        )

        if self.is_playback:
            self._client_id = ZERO_UID
            self._client_secret = PLACEHOLDER
            self._tenant_id = TENANT_ID
            self._resource_group = RESOURCE_GROUP
            self._subscription_id = ZERO_UID
            self._workspace_name = WORKSPACE
            self._location = LOCATION

        regex_replacer.register_regex(self.client_id, ZERO_UID)
        regex_replacer.register_regex(self.client_secret, PLACEHOLDER)
        regex_replacer.register_regex(self.tenant_id, ZERO_UID)
        regex_replacer.register_regex(self.subscription_id, ZERO_UID)
        regex_replacer.register_regex(self.workspace_name, WORKSPACE)
        regex_replacer.register_regex(self.location, LOCATION)
        regex_replacer.register_regex(self.resource_group, RESOURCE_GROUP)
        regex_replacer.register_regex(
            r"/subscriptions/([a-f0-9]+[-]){4}[a-f0-9]+",
            "/subscriptions/" + ZERO_UID,
        )
        regex_replacer.register_regex(r"job-([a-f0-9]+[-]){4}[a-f0-9]+",
                                      "job-" + ZERO_UID)
        regex_replacer.register_regex(r"jobs/([a-f0-9]+[-]){4}[a-f0-9]+",
                                      "jobs/" + ZERO_UID)
        regex_replacer.register_regex(r"job-([a-f0-9]+[-]){4}[a-f0-9]+",
                                      "job-" + ZERO_UID)
        regex_replacer.register_regex(r"\d{8}-\d{6}", "20210101-000000")
        regex_replacer.register_regex(
            r'"id":\s*"([a-f0-9]+[-]){4}[a-f0-9]+"',
            '"id": "{}"'.format(ZERO_UID),
        )
        regex_replacer.register_regex(r"/resourceGroups/[a-z0-9-]+/",
                                      f'/resourceGroups/{RESOURCE_GROUP}/')
        regex_replacer.register_regex(r"/workspaces/[a-z0-9-]+/",
                                      f'/workspaces/{WORKSPACE}/')
        regex_replacer.register_regex(
            r"https://[^\.]+.blob.core.windows.net/",
            f'https://{STORAGE}.blob.core.windows.net/')
        regex_replacer.register_regex(
            r"https://[^\.]+.quantum.azure.com/",
            f'https://{LOCATION}.quantum.azure.com/')
        regex_replacer.register_regex(r"/workspaces/[a-z0-9-]+/",
                                      f'/workspaces/{WORKSPACE}/')

        regex_replacer.register_regex(r"sig=[^&]+\&", "sig=PLACEHOLDER&")
        regex_replacer.register_regex(r"sv=[^&]+\&", "sv=PLACEHOLDER&")
        regex_replacer.register_regex(r"se=[^&]+\&", "se=PLACEHOLDER&")
        regex_replacer.register_regex(r"client_id=[^&]+\&",
                                      "client_id=PLACEHOLDER&")
        regex_replacer.register_regex(r"claims=[^&]+\&", "claims=PLACEHOLDER&")
        regex_replacer.register_regex(r"code_verifier=[^&]+\&",
                                      "code_verifier=PLACEHOLDER&")
        regex_replacer.register_regex(r"code=[^&]+\&",
                                      "code_verifier=PLACEHOLDER&")
        regex_replacer.register_regex(r"code=[^&]+\&",
                                      "code_verifier=PLACEHOLDER&")