| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3f70b2f commit e096127
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -89,6 +89,14 @@ def get_agent_identity_certificate_path(): | |||
| 89 | 89 | if not cert_config_path and not has_well_known_dir: | |
| 90 | 90 | return None | |
| 91 | 91 | ||
| 92 | + # If ECP config path is specified but does not exist, and we are on a workstation, fail-fast immediately. | ||
| 93 | + if ( | ||
| 94 | + cert_config_path | ||
| 95 | + and not has_well_known_dir | ||
| 96 | + and not os.path.exists(cert_config_path) | ||
| 97 | + ): | ||
| 98 | + return None | ||
| 99 | + | ||
| 92 | 100 | has_logged_config_warning = False | |
| 93 | 101 | has_logged_cert_warning = False | |
| 94 | 102 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -165,29 +165,47 @@ def test_get_agent_identity_certificate_path_success(self, tmpdir, monkeypatch): | |||
| 165 | 165 | assert result == str(cert_path) | |
| 166 | 166 | ||
| 167 | 167 | @mock.patch("time.sleep") | |
| 168 | + @mock.patch("google.auth._agent_identity_utils.os.path.exists") | ||
| 168 | 169 | def test_get_agent_identity_certificate_path_retry( | |
| 169 | - self, mock_sleep, tmpdir, monkeypatch | ||
| 170 | + self, mock_exists, mock_sleep, tmpdir, monkeypatch | ||
| 170 | 171 | ): | |
| 171 | 172 | config_path = tmpdir.join("config.json") | |
| 172 | 173 | monkeypatch.setenv( | |
| 173 | 174 | environment_vars.GOOGLE_API_CERTIFICATE_CONFIG, str(config_path) | |
| 174 | 175 | ) | |
| 175 | 176 | ||
| 177 | + # Simulate workload env (well_known_dir exists) to avoid fail-fast | ||
| 178 | + def exists_side_effect(path): | ||
| 179 | + if path == "/var/run/secrets/workload-spiffe-credentials": | ||
| 180 | + return True | ||
| 181 | + return False | ||
| 182 | + | ||
| 183 | + mock_exists.side_effect = exists_side_effect | ||
| 184 | + | ||
| 176 | 185 | # File doesn't exist initially | |
| 177 | 186 | with pytest.raises(exceptions.RefreshError): | |
| 178 | 187 | _agent_identity_utils.get_agent_identity_certificate_path() | |
| 179 | 188 | ||
| 180 | 189 | assert mock_sleep.call_count == len(_agent_identity_utils._POLLING_INTERVALS) | |
| 181 | 190 | ||
| 182 | 191 | @mock.patch("time.sleep") | |
| 192 | + @mock.patch("google.auth._agent_identity_utils.os.path.exists") | ||
| 183 | 193 | def test_get_agent_identity_certificate_path_failure( | |
| 184 | - self, mock_sleep, tmpdir, monkeypatch | ||
| 194 | + self, mock_exists, mock_sleep, tmpdir, monkeypatch | ||
| 185 | 195 | ): | |
| 186 | 196 | config_path = tmpdir.join("non_existent_config.json") | |
| 187 | 197 | monkeypatch.setenv( | |
| 188 | 198 | environment_vars.GOOGLE_API_CERTIFICATE_CONFIG, str(config_path) | |
| 189 | 199 | ) | |
| 190 | 200 | ||
| 201 | + # Simulate workload env (well_known_dir exists) to avoid fail-fast | ||
| 202 | + def exists_side_effect(path): | ||
| 203 | + if path == "/var/run/secrets/workload-spiffe-credentials": | ||
| 204 | + return True | ||
| 205 | + return False | ||
| 206 | + | ||
| 207 | + mock_exists.side_effect = exists_side_effect | ||
| 208 | + | ||
| 191 | 209 | with pytest.raises(exceptions.RefreshError) as excinfo: | |
| 192 | 210 | _agent_identity_utils.get_agent_identity_certificate_path() | |
| 193 | 211 | ||
@@ -198,6 +216,19 @@ def test_get_agent_identity_certificate_path_failure( | |||
| 198 | 216 | ) | |
| 199 | 217 | assert mock_sleep.call_count == len(_agent_identity_utils._POLLING_INTERVALS) | |
| 200 | 218 | ||
| 219 | + def test_get_agent_identity_certificate_path_workstation_fail_fast( | ||
| 220 | + self, tmpdir, monkeypatch | ||
| 221 | + ): | ||
| 222 | + config_path = tmpdir.join("non_existent_config.json") | ||
| 223 | + monkeypatch.setenv( | ||
| 224 | + environment_vars.GOOGLE_API_CERTIFICATE_CONFIG, str(config_path) | ||
| 225 | + ) | ||
| 226 | + | ||
| 227 | + # On a workstation, well_known_dir does not exist, and config file is missing. | ||
| 228 | + # It should fail-fast and return None immediately. | ||
| 229 | + result = _agent_identity_utils.get_agent_identity_certificate_path() | ||
| 230 | + assert result is None | ||
| 231 | + | ||
| 201 | 232 | @mock.patch("time.sleep") | |
| 202 | 233 | @mock.patch("os.path.exists") | |
| 203 | 234 | def test_get_agent_identity_certificate_path_cert_not_found( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -154,8 +154,10 @@ def test_default_client_encrypted_cert_source( | |||
| 154 | 154 | # Test good callback. | |
| 155 | 155 | get_client_ssl_credentials.return_value = (True, b"cert", b"key", b"passphrase") | |
| 156 | 156 | callback = mtls.default_client_encrypted_cert_source("cert_path", "key_path") | |
| 157 | - with mock.patch("{}.open".format(__name__), return_value=mock.MagicMock()): | ||
| 157 | + with mock.patch("google.auth.transport.mtls.open", mock.mock_open()) as mock_file: | ||
| 158 | 158 | assert callback() == ("cert_path", "key_path", b"passphrase") | |
| 159 | + mock_file.assert_any_call("cert_path", "wb") | ||
| 160 | + mock_file.assert_any_call("key_path", "wb") | ||
| 159 | 161 | ||
| 160 | 162 | # Test bad callback which throws exception. | |
| 161 | 163 | get_client_ssl_credentials.side_effect = exceptions.ClientCertError() | |
| Back | FazBrowse Home | New Git URL |
0 commit comments