Ejemplo n.º 1
0
 def fetcher(self, file_cache):
     return TCPFetcher(
         file_cache,
         cluster=False,
         family=socket.AF_INET,
         address=("1.2.3.4", 6556),
         timeout=0.1,
         encryption_settings={"encryption": "settings"},
         use_only_cache=False,
     )
Ejemplo n.º 2
0
 def fetcher(self, monkeypatch, file_cache):
     # We use the TCPFetcher to test a general feature of the fetchers.
     return TCPFetcher(
         StubFileCache.from_json(file_cache.to_json()),
         family=socket.AF_INET,
         address=("1.2.3.4", 0),
         timeout=0.0,
         encryption_settings={},
         use_only_cache=False,
     )
Ejemplo n.º 3
0
 def fetcher(self, file_cache: DefaultAgentFileCache) -> TCPFetcher:
     return TCPFetcher(
         file_cache,
         family=socket.AF_INET,
         address=("1.2.3.4", 6556),
         host_name=HostName("irrelevant_for_this_test"),
         timeout=0.1,
         encryption_settings={"use_regular": "allow"},
         use_only_cache=False,
     )
Ejemplo n.º 4
0
 def fetcher(self, monkeypatch: MonkeyPatch, file_cache: DefaultAgentFileCache) -> TCPFetcher:
     # We use the TCPFetcher to test a general feature of the fetchers.
     return TCPFetcher(
         StubFileCache.from_json(file_cache.to_json()),
         family=socket.AF_INET,
         address=("1.2.3.4", 0),
         timeout=0.0,
         host_name=HostName("irrelevant_for_this_test"),
         encryption_settings={},
         use_only_cache=False,
     )
Ejemplo n.º 5
0
 def test_validate_protocol_tls_allways_ok(self, file_cache: DefaultAgentFileCache) -> None:
     for setting in ("tls", "enforce", "enable", "disable"):
         TCPFetcher(
             file_cache,
             family=socket.AF_INET,
             address=("1.2.3.4", 0),
             host_name=HostName("irrelevant_for_this_test"),
             timeout=0.0,
             encryption_settings={"use_regular": setting},
             use_only_cache=False,
         )._validate_protocol(TransportProtocol.TLS)
Ejemplo n.º 6
0
    def test_get_agent_data_with_tls(self, monkeypatch: MonkeyPatch,
                                     fetcher: TCPFetcher) -> None:
        mock_data = b"<<<section:sep(0)>>>\nbody\n"
        mock_sock = _MockSock(b"16%b%b%b" % (
            bytes(Version.V1),
            bytes(HeaderV1(CompressionType.ZLIB)),
            compress(mock_data),
        ))
        monkeypatch.setattr(fetcher, "_opt_socket", mock_sock)
        monkeypatch.setattr(fetcher, "_wrap_tls", lambda: mock_sock)

        agent_data, protocol = fetcher._get_agent_data()
        assert agent_data == mock_data[2:]
        assert protocol == TransportProtocol.PLAIN
Ejemplo n.º 7
0
    def test_open_exception_becomes_fetcher_rerror(
            self, file_cache: AgentFileCache,
            monkeypatch: MonkeyPatch) -> None:
        with TCPFetcher(
                file_cache,
                family=socket.AF_INET,
                address=("This is not an IP address. Connecting fails.", 6556),
                host_name=HostName("irrelevant_for_this_test"),
                timeout=0.1,
                encryption_settings={"use_regular": "allow"},
        ) as fetcher:
            fetched = fetcher.fetch(Mode.CHECKING)

        assert isinstance(fetched.error, MKFetcherError)
Ejemplo n.º 8
0
    def test_with_cached_does_not_open(self, file_cache: AgentFileCache,
                                       monkeypatch: MonkeyPatch) -> None:
        file_cache.write(AgentRawData(b"<<<whatever>>>"), Mode.CHECKING)
        with TCPFetcher(
                file_cache,
                family=socket.AF_INET,
                address=("This is not an IP address. Connecting would fail.",
                         6556),
                host_name=HostName("irrelevant_for_this_test"),
                timeout=0.1,
                encryption_settings={"use_regular": "allow"},
        ) as fetcher:
            fetched = fetcher.fetch(Mode.CHECKING)

        assert fetched.is_ok()
Ejemplo n.º 9
0
 def test_validate_protocol_tls_always_ok(
         self, file_cache: AgentFileCache) -> None:
     for setting, is_registered in cartesian_product(
         ("tls", "enforce", "enable", "disable"),
         (True, False),
     ):
         TCPFetcher(
             file_cache,
             family=socket.AF_INET,
             address=("1.2.3.4", 0),
             host_name=HostName("irrelevant_for_this_test"),
             timeout=0.0,
             encryption_settings={
                 "use_regular": setting
             },
         )._validate_protocol(TransportProtocol.TLS,
                              is_registered=is_registered)
Ejemplo n.º 10
0
 def test_fetching_without_cache_raises_in_non_checking_mode(self) -> None:
     with TCPFetcher(
             StubFileCache(
                 HostName("hostname"),
                 base_path=Path(os.devnull),
                 max_age=MaxAge.none(),
                 disabled=False,
                 use_outdated=True,
                 simulation=False,
                 use_only_cache=False,
             ),
             family=socket.AF_INET,
             address=("127.0.0.1", 6556),
             host_name=HostName("irrelevant_for_this_test"),
             timeout=0.1,
             encryption_settings={"use_regular": "allow"},
     ) as fetcher:
         for mode in Mode:
             if mode is Mode.CHECKING:
                 continue
             fetched = fetcher.fetch(mode)
             assert isinstance(fetched.error, MKFetcherError)
Ejemplo n.º 11
0
 def test_detect_transport_protocol_empty_error(self, fetcher: TCPFetcher) -> None:
     with pytest.raises(MKFetcherError, match="Passed error message"):
         fetcher._detect_transport_protocol(b"", "Passed error message")
Ejemplo n.º 12
0
 def test_detect_transport_protocol_error(self, fetcher: TCPFetcher) -> None:
     with pytest.raises(MKFetcherError, match="Unknown transport protocol: b'abc'"):
         fetcher._detect_transport_protocol(b"abc", "unused")
Ejemplo n.º 13
0
 def test_detect_transport_protocol(self, fetcher: TCPFetcher) -> None:
     assert fetcher._detect_transport_protocol(b"02", "Unused") == TransportProtocol.SHA256
Ejemplo n.º 14
0
 def test_fetch_reading_cache_in_inventory_mode(self, fetcher: TCPFetcher) -> None:
     assert isinstance(fetcher.file_cache, StubFileCache)  # for mypy
     assert fetcher.file_cache.cache == b"cached_section"
     assert fetcher.fetch(Mode.INVENTORY) == result.OK(b"cached_section")
     assert fetcher.file_cache.cache == b"cached_section"