|
14 | 14 | APPLICATION_INCLUDE, |
15 | 15 | CLUSTER_INCLUDE, |
16 | 16 | GENRESOURCES_API_UP_METRIC, |
| 17 | + REPOSITORY_INCLUDE, |
17 | 18 | ) |
18 | 19 | from datadog_checks.dev.http import MockResponse |
19 | 20 |
|
@@ -124,6 +125,115 @@ def test_application_include_contains_kubernetes_resource_identity_for_automatic |
124 | 125 | } <= set(APPLICATION_INCLUDE["paths"]) |
125 | 126 |
|
126 | 127 |
|
| 128 | +def test_application_include_adds_pilot_metadata_and_state_fields(): |
| 129 | + assert { |
| 130 | + "metadata.uid", |
| 131 | + "metadata.creationTimestamp", |
| 132 | + "status.health.lastTransitionTime", |
| 133 | + "status.operationState.retryCount", |
| 134 | + } <= set(APPLICATION_INCLUDE["paths"]) |
| 135 | + |
| 136 | + |
| 137 | +def test_application_include_contains_deployment_history_and_operation_fields(): |
| 138 | + assert { |
| 139 | + "status.operationState.message", |
| 140 | + "status.summary.externalURLs[*]", |
| 141 | + "status.history[*].source.repoURL", |
| 142 | + "status.history[*].source.path", |
| 143 | + "status.history[*].sources[*].repoURL", |
| 144 | + "status.history[*].revisions[*]", |
| 145 | + } <= set(APPLICATION_INCLUDE["paths"]) |
| 146 | + |
| 147 | + |
| 148 | +def test_collect_scrubs_credentials_from_operation_state_message(mock_http_response_per_endpoint): |
| 149 | + app = _application("broken") |
| 150 | + app["status"]["operationState"] = { |
| 151 | + "phase": "Failed", |
| 152 | + "message": "sync failed: https://oauth2:t0ken@github.com/org/repo: auth required", |
| 153 | + } |
| 154 | + mock_http_response_per_endpoint( |
| 155 | + { |
| 156 | + APPLICATIONS_URL: [_items_response([app])], |
| 157 | + CLUSTERS_URL: [_items_response([])], |
| 158 | + REPOSITORIES_URL: [_items_response([])], |
| 159 | + } |
| 160 | + ) |
| 161 | + check = _check() |
| 162 | + |
| 163 | + with patch.object(check, "submit_generic_resource") as submit: |
| 164 | + check._resource_collector.collect() |
| 165 | + |
| 166 | + app_call = next(c for c in submit.call_args_list if c.kwargs["type"] == "argocd_application") |
| 167 | + message = app_call.kwargs["fields"]["status"]["operationState"]["message"] |
| 168 | + assert "t0ken" not in message |
| 169 | + assert "https://github.com/org/repo" in message |
| 170 | + |
| 171 | + |
| 172 | +def test_collect_strips_credentials_from_external_urls(mock_http_response_per_endpoint): |
| 173 | + app = _application("web") |
| 174 | + app["status"]["summary"] = {"externalURLs": ["https://user:t0ken@app.example.com"]} |
| 175 | + mock_http_response_per_endpoint( |
| 176 | + { |
| 177 | + APPLICATIONS_URL: [_items_response([app])], |
| 178 | + CLUSTERS_URL: [_items_response([])], |
| 179 | + REPOSITORIES_URL: [_items_response([])], |
| 180 | + } |
| 181 | + ) |
| 182 | + check = _check() |
| 183 | + |
| 184 | + with patch.object(check, "submit_generic_resource") as submit: |
| 185 | + check._resource_collector.collect() |
| 186 | + |
| 187 | + app_call = next(c for c in submit.call_args_list if c.kwargs["type"] == "argocd_application") |
| 188 | + assert app_call.kwargs["fields"]["status"]["summary"]["externalURLs"] == ["https://app.example.com"] |
| 189 | + |
| 190 | + |
| 191 | +def test_real_helper_ships_multisource_history_and_scrubs_its_repo_urls(aggregator, mock_http_response_per_endpoint): |
| 192 | + # Runs the real helper: proves nested [*] (history[*].sources[*]) projects AND history repoURLs are scrubbed. |
| 193 | + app = _application("checkout") |
| 194 | + app["status"]["history"] = [ |
| 195 | + { |
| 196 | + "id": 1, |
| 197 | + "source": {"repoURL": "https://oauth2:t0ken@github.com/org/repo", "path": "guestbook"}, |
| 198 | + "sources": [{"repoURL": "https://oauth2:t0ken@github.com/org/multi", "path": "base"}], |
| 199 | + } |
| 200 | + ] |
| 201 | + mock_http_response_per_endpoint( |
| 202 | + { |
| 203 | + APPLICATIONS_URL: [_items_response([app])], |
| 204 | + CLUSTERS_URL: [_items_response([])], |
| 205 | + REPOSITORIES_URL: [_items_response([])], |
| 206 | + } |
| 207 | + ) |
| 208 | + check = _check() |
| 209 | + |
| 210 | + check._resource_collector.collect() |
| 211 | + |
| 212 | + payloads = aggregator.get_event_platform_events("genresources", parse_json=False) |
| 213 | + blob = b"".join(p if isinstance(p, bytes) else p.encode() for p in payloads) |
| 214 | + assert b"guestbook" in blob # single-source history field projected |
| 215 | + assert b"base" in blob # multi-source history field projected (nested [*] works) |
| 216 | + assert b"t0ken" not in blob # both history repoURLs scrubbed before ship |
| 217 | + |
| 218 | + |
| 219 | +def test_cluster_include_contains_connection_and_shard_fields(): |
| 220 | + assert { |
| 221 | + "connectionState.attemptedAt", |
| 222 | + "info.connectionState.status", |
| 223 | + "shard", |
| 224 | + } <= set(CLUSTER_INCLUDE["paths"]) |
| 225 | + |
| 226 | + |
| 227 | +def test_repository_include_contains_connection_and_capability_flags(): |
| 228 | + assert { |
| 229 | + "connectionState.attemptedAt", |
| 230 | + "insecure", |
| 231 | + "enableLfs", |
| 232 | + "enableOCI", |
| 233 | + "forceHttpBasicAuth", |
| 234 | + } <= set(REPOSITORY_INCLUDE["paths"]) |
| 235 | + |
| 236 | + |
127 | 237 | def test_application_key_uses_app_identity_not_destination(mock_http_response_per_endpoint): |
128 | 238 | apps = [ |
129 | 239 | _application("web", namespace="team-a", cluster="https://remote", dest_namespace="prod"), |
|
0 commit comments