Skip to content

Commit e56d755

Browse files
committed
Update mds mtls certificate well-known locations
1 parent 8226651 commit e56d755

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

google/auth/compute_engine/_mtls.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,46 @@
1616
#
1717
"""Mutual TLS for Google Compute Engine metadata server."""
1818

19-
from dataclasses import dataclass
19+
from dataclasses import dataclass, field
2020
import enum
2121
import os
22-
from pathlib import Path
2322
import ssl
2423

2524
import requests
2625
from requests.adapters import HTTPAdapter
2726

2827
from google.auth import environment_vars, exceptions
2928

29+
# MDS mTLS certificate paths based on OS.
30+
# Documentation to well known locations can be found at:
31+
# https://cloud.google.com/compute/docs/metadata/overview#https-mds-certificates
32+
33+
34+
def _get_mds_root_crt_path():
35+
if os.name == "nt":
36+
return os.path.join(
37+
"C:\\", "ProgramData", "Google", "ComputeEngine", "mds-mtls-root.crt"
38+
)
39+
else:
40+
return os.path.join("/", "run", "google-mds-mtls", "root.crt")
41+
42+
43+
def _get_mds_client_combined_cert_path():
44+
if os.name == "nt":
45+
return os.path.join(
46+
"C:\\", "ProgramData", "Google", "ComputeEngine", "mds-mtls-client.key"
47+
)
48+
else:
49+
return os.path.join("/", "run", "google-mds-mtls", "client.key")
50+
3051

3152
@dataclass
3253
class MdsMtlsConfig:
33-
ca_cert_path: str = os.path.join(
34-
Path.home(), "mtls_mds_certificates", "root.crt"
54+
ca_cert_path: str = field(
55+
default_factory=_get_mds_root_crt_path
3556
) # path to CA certificate
36-
client_combined_cert_path: str = os.path.join(
37-
Path.home(), "mtls_mds_certificates", "client_creds.key"
57+
client_combined_cert_path: str = field(
58+
default_factory=_get_mds_client_combined_cert_path
3859
) # path to file containing client certificate and key
3960

4061

tests/compute_engine/test__mtls.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# limitations under the License.
1616
#
1717

18+
import os
19+
1820
import mock
1921
import pytest # type: ignore
2022

@@ -29,6 +31,28 @@ def mock_mds_mtls_config():
2931
)
3032

3133

34+
@mock.patch("os.name", "nt")
35+
def test__MdsMtlsConfig_windows_defaults():
36+
config = _mtls.MdsMtlsConfig()
37+
assert config.ca_cert_path == os.path.join(
38+
"C:\\", "ProgramData", "Google", "ComputeEngine", "mds-mtls-root.crt"
39+
)
40+
assert config.client_combined_cert_path == os.path.join(
41+
"C:\\", "ProgramData", "Google", "ComputeEngine", "mds-mtls-client.key"
42+
)
43+
44+
45+
@mock.patch("os.name", "posix")
46+
def test__MdsMtlsConfig_non_windows_defaults():
47+
config = _mtls.MdsMtlsConfig()
48+
assert config.ca_cert_path == os.path.join(
49+
"/", "run", "google-mds-mtls", "root.crt"
50+
)
51+
assert config.client_combined_cert_path == os.path.join(
52+
"/", "run", "google-mds-mtls", "client.key"
53+
)
54+
55+
3256
def test__parse_mds_mode_default(monkeypatch):
3357
monkeypatch.delenv(environment_vars.GCE_METADATA_MTLS_MODE, raising=False)
3458
assert _mtls._parse_mds_mode() == _mtls.MdsMtlsMode.DEFAULT

0 commit comments

Comments
 (0)