|
| 1 | +from .. import case |
| 2 | +from .. import formats |
| 3 | + |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +class TestEndpointRenaming(case.TestCase): |
| 8 | + """Test cases for endpoint renaming (http.endpoint tag generation). |
| 9 | +
|
| 10 | + The endpoint renaming feature generates http.endpoint tags by analyzing |
| 11 | + URL paths and replacing dynamic segments with patterns like {param:int}, |
| 12 | + {param:hex}, etc. |
| 13 | + """ |
| 14 | + |
| 15 | + last_config = '' |
| 16 | + |
| 17 | + def replace_config(self, new_config): |
| 18 | + """Replace nginx configuration if different from last config.""" |
| 19 | + if new_config != TestEndpointRenaming.last_config: |
| 20 | + conf_path = Path(__file__).parent / "conf" / new_config |
| 21 | + conf_text = conf_path.read_text() |
| 22 | + status, log_lines = self.orch.nginx_replace_config( |
| 23 | + conf_text, conf_path.name) |
| 24 | + self.assertEqual(0, status, log_lines) |
| 25 | + TestEndpointRenaming.last_config = new_config |
| 26 | + |
| 27 | + def send_request_and_get_span(self, url, config_name): |
| 28 | + self.replace_config(config_name) |
| 29 | + self.orch.sync_service('agent') |
| 30 | + |
| 31 | + status, _, _ = self.orch.send_nginx_http_request(url) |
| 32 | + |
| 33 | + self.orch.reload_nginx() |
| 34 | + log_lines = self.orch.sync_service('agent') |
| 35 | + |
| 36 | + spans = formats.parse_spans(log_lines) |
| 37 | + nginx_spans = [s for s in spans if s.get('service') == 'test-service'] |
| 38 | + |
| 39 | + self.assertEquals(len(nginx_spans), 1, "Expected exactly one span") |
| 40 | + |
| 41 | + return status, nginx_spans[0] |
| 42 | + |
| 43 | + def test_endpoint_renaming_disabled_by_default(self): |
| 44 | + """Verify that endpoint renaming is disabled by default. |
| 45 | +
|
| 46 | + When disabled, no http.endpoint tag should be added to spans. |
| 47 | + """ |
| 48 | + status, span = self.send_request_and_get_span("/api/users/123", "disabled.conf") |
| 49 | + self.assertEqual(200, status) |
| 50 | + |
| 51 | + meta = span.get('meta', {}) |
| 52 | + self.assertNotIn('http.endpoint', meta, |
| 53 | + f"http.endpoint should not be present when disabled: {meta}") |
| 54 | + |
| 55 | + def test_endpoint_renaming_fallback_mode(self): |
| 56 | + """Verify endpoint renaming in fallback mode. |
| 57 | +
|
| 58 | + When enabled in fallback mode, http.endpoint should be added |
| 59 | + when http.route is not present. For this endpoint, it's not present |
| 60 | + """ |
| 61 | + status, span = self.send_request_and_get_span("/api/users/123", "fallback.conf") |
| 62 | + self.assertEqual(200, status) |
| 63 | + |
| 64 | + meta = span.get('meta', {}) |
| 65 | + self.assertIn('http.endpoint', meta, "http.endpoint should be present in fallback mode") |
| 66 | + self.assertEqual(meta['http.endpoint'], '/api/users/{param:int}', |
| 67 | + f"Expected /api/users/{{param:int}}, got {meta['http.endpoint']}") |
| 68 | + |
| 69 | + def test_fallback_mode_respects_http_route(self): |
| 70 | + """Verify fallback mode does NOT calculate endpoint when http.route is set.""" |
| 71 | + status, span = self.send_request_and_get_span("/api/orders/789", "fallback.conf") |
| 72 | + self.assertEqual(200, status) |
| 73 | + |
| 74 | + meta = span.get('meta', {}) |
| 75 | + # In fallback mode with http.route set, http.endpoint should NOT be present |
| 76 | + self.assertIn('http.route', meta, "http.route should be present") |
| 77 | + self.assertEqual(meta['http.route'], '/api/orders/:id') |
| 78 | + self.assertNotIn('http.endpoint', meta, |
| 79 | + "http.endpoint should NOT be set when http.route exists in fallback mode") |
| 80 | + |
| 81 | + |
| 82 | + def test_endpoint_renaming_always_mode(self): |
| 83 | + """Verify endpoint renaming in always mode. |
| 84 | +
|
| 85 | + When enabled in always mode, http.endpoint should always be calculated, |
| 86 | + even when http.route is present. |
| 87 | + """ |
| 88 | + status, span = self.send_request_and_get_span("/api/products/abc-123-def", "always.conf") |
| 89 | + self.assertEqual(200, status) |
| 90 | + |
| 91 | + meta = span.get('meta', {}) |
| 92 | + self.assertIn('http.endpoint', meta, "http.endpoint should be present in always mode") |
| 93 | + # Verify the pattern: /api/products/abc-123-def -> /api/products/{param:hex_id} |
| 94 | + self.assertEqual(meta['http.endpoint'], '/api/products/{param:hex_id}', |
| 95 | + f"Expected /api/products/{{param:hex_id}}, got {meta['http.endpoint']}") |
| 96 | + |
| 97 | + def test_always_mode_ignores_http_route(self): |
| 98 | + """Verify always mode calculates endpoint even when http.route is set.""" |
| 99 | + status, span = self.send_request_and_get_span("/api/orders/999", "always.conf") |
| 100 | + self.assertEqual(200, status) |
| 101 | + |
| 102 | + meta = span.get('meta', {}) |
| 103 | + # In always mode, both http.route and http.endpoint should be present |
| 104 | + self.assertIn('http.route', meta, "http.route should be present") |
| 105 | + self.assertIn('http.endpoint', meta, "http.endpoint should be present in always mode") |
| 106 | + self.assertEqual(meta['http.route'], '/api/orders/:id') |
| 107 | + self.assertEqual(meta['http.endpoint'], '/api/orders/{param:int}') |
| 108 | + |
| 109 | + def test_appsec_enables_fallback_mode_by_default(self): |
| 110 | + """Verify that when appsec is enabled, endpoint renaming defaults to fallback mode. |
| 111 | +
|
| 112 | + With appsec enabled and no explicit resource_renaming_enabled directive, |
| 113 | + the feature should be enabled in fallback mode: |
| 114 | + - http.endpoint calculated when http.route not present |
| 115 | + - http.endpoint NOT calculated when http.route is present |
| 116 | + """ |
| 117 | + if self.waf_disabled: |
| 118 | + self.skipTest("WAF is disabled - appsec test requires WAF support") |
| 119 | + |
| 120 | + # Test without http.route - should calculate endpoint |
| 121 | + status, span = self.send_request_and_get_span("/api/users/456", "appsec_enabled.conf") |
| 122 | + self.assertEqual(200, status) |
| 123 | + |
| 124 | + meta = span.get('meta', {}) |
| 125 | + self.assertIn('http.endpoint', meta, |
| 126 | + "http.endpoint should be present when appsec is enabled (fallback mode)") |
| 127 | + self.assertEqual(meta['http.endpoint'], '/api/users/{param:int}', |
| 128 | + f"Expected /api/users/{{param:int}}, got {meta['http.endpoint']}") |
| 129 | + |
| 130 | + # Test with http.route - should NOT calculate endpoint (fallback mode) |
| 131 | + status, span = self.send_request_and_get_span("/api/orders/555", "appsec_enabled.conf") |
| 132 | + self.assertEqual(200, status) |
| 133 | + |
| 134 | + meta = span.get('meta', {}) |
| 135 | + self.assertIn('http.route', meta, "http.route should be present") |
| 136 | + self.assertEqual(meta['http.route'], '/api/orders/:id') |
| 137 | + self.assertNotIn('http.endpoint', meta, |
| 138 | + "http.endpoint should NOT be set when http.route exists in fallback mode (appsec enabled)") |
0 commit comments