|
| 1 | +"""" |
| 2 | +Copyright 2022 Google LLC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | +# file containing all mocks used for Cloud SQL Python Connector unit tests |
| 17 | + |
| 18 | +import json |
| 19 | +import ssl |
| 20 | +from tempfile import TemporaryDirectory |
| 21 | +from typing import Any, Dict, Tuple, Optional |
| 22 | +from google.cloud.sql.connector import IPTypes |
| 23 | +from google.cloud.sql.connector.instance import InstanceMetadata |
| 24 | +from google.cloud.sql.connector.utils import write_to_file, generate_keys |
| 25 | +import datetime |
| 26 | +from cryptography.hazmat.backends import default_backend |
| 27 | +from cryptography.hazmat.primitives import serialization, hashes |
| 28 | +from cryptography.hazmat.primitives.asymmetric import rsa |
| 29 | +from cryptography import x509 |
| 30 | +from cryptography.x509.oid import NameOID |
| 31 | + |
| 32 | + |
| 33 | +class MockInstance: |
| 34 | + _enable_iam_auth: bool |
| 35 | + |
| 36 | + def __init__( |
| 37 | + self, |
| 38 | + enable_iam_auth: bool = False, |
| 39 | + ) -> None: |
| 40 | + self._enable_iam_auth = enable_iam_auth |
| 41 | + |
| 42 | + # mock connect_info |
| 43 | + async def connect_info( |
| 44 | + self, |
| 45 | + driver: str, |
| 46 | + ip_type: IPTypes, |
| 47 | + **kwargs: Any, |
| 48 | + ) -> Any: |
| 49 | + return True |
| 50 | + |
| 51 | + |
| 52 | +class BadRefresh(Exception): |
| 53 | + pass |
| 54 | + |
| 55 | + |
| 56 | +class MockMetadata(InstanceMetadata): |
| 57 | + """Mock class for InstanceMetadata""" |
| 58 | + |
| 59 | + def __init__( |
| 60 | + self, expiration: datetime.datetime, ip_addrs: Dict = {"PRIMARY": "0.0.0.0"} |
| 61 | + ) -> None: |
| 62 | + self.expiration = expiration |
| 63 | + self.ip_addrs = ip_addrs |
| 64 | + |
| 65 | + |
| 66 | +async def instance_metadata_success(*args: Any, **kwargs: Any) -> MockMetadata: |
| 67 | + return MockMetadata(datetime.datetime.now() + datetime.timedelta(minutes=10)) |
| 68 | + |
| 69 | + |
| 70 | +async def instance_metadata_expired(*args: Any, **kwargs: Any) -> MockMetadata: |
| 71 | + return MockMetadata(datetime.datetime.now() - datetime.timedelta(minutes=10)) |
| 72 | + |
| 73 | + |
| 74 | +async def instance_metadata_error(*args: Any, **kwargs: Any) -> None: |
| 75 | + raise BadRefresh("something went wrong...") |
| 76 | + |
| 77 | + |
| 78 | +def generate_cert( |
| 79 | + project: str, name: str |
| 80 | +) -> Tuple[x509.CertificateBuilder, rsa.RSAPrivateKey]: |
| 81 | + """ |
| 82 | + Generate a private key and cert object to be used in testing. |
| 83 | + """ |
| 84 | + # generate private key |
| 85 | + key = rsa.generate_private_key(public_exponent=65537, key_size=2048) |
| 86 | + common_name = f"{project}:{name}" |
| 87 | + # configure cert subject |
| 88 | + subject = issuer = x509.Name( |
| 89 | + [ |
| 90 | + x509.NameAttribute(NameOID.COUNTRY_NAME, "US"), |
| 91 | + x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "California"), |
| 92 | + x509.NameAttribute(NameOID.LOCALITY_NAME, "Mountain View"), |
| 93 | + x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Google Inc"), |
| 94 | + x509.NameAttribute(NameOID.COMMON_NAME, "{}".format(common_name)), |
| 95 | + ] |
| 96 | + ) |
| 97 | + # build cert |
| 98 | + cert = ( |
| 99 | + x509.CertificateBuilder() |
| 100 | + .subject_name(subject) |
| 101 | + .issuer_name(issuer) |
| 102 | + .public_key(key.public_key()) |
| 103 | + .serial_number(x509.random_serial_number()) |
| 104 | + .not_valid_before(datetime.datetime.utcnow()) |
| 105 | + .not_valid_after( |
| 106 | + # cert valid for 10 mins |
| 107 | + datetime.datetime.utcnow() |
| 108 | + + datetime.timedelta(minutes=60) |
| 109 | + ) |
| 110 | + ) |
| 111 | + return cert, key |
| 112 | + |
| 113 | + |
| 114 | +def self_signed_cert(cert: x509.CertificateBuilder, key: rsa.RSAPrivateKey) -> str: |
| 115 | + """ |
| 116 | + Create a PEM encoded certificate that is self-signed. |
| 117 | + """ |
| 118 | + return ( |
| 119 | + cert.sign(key, hashes.SHA256(), default_backend()) |
| 120 | + .public_bytes(encoding=serialization.Encoding.PEM) |
| 121 | + .decode("UTF-8") |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +def client_key_signed_cert( |
| 126 | + cert: x509.CertificateBuilder, |
| 127 | + priv_key: rsa.RSAPrivateKey, |
| 128 | + client_key: rsa.RSAPublicKey, |
| 129 | +) -> str: |
| 130 | + """ |
| 131 | + Create a PEM encoded certificate that is signed by given public key. |
| 132 | + """ |
| 133 | + # configure cert subject |
| 134 | + subject = issuer = x509.Name( |
| 135 | + [ |
| 136 | + x509.NameAttribute(NameOID.COUNTRY_NAME, "US"), |
| 137 | + x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Google Inc"), |
| 138 | + x509.NameAttribute(NameOID.COMMON_NAME, "Google Cloud SQL Client"), |
| 139 | + ] |
| 140 | + ) |
| 141 | + # build cert |
| 142 | + cert = ( |
| 143 | + x509.CertificateBuilder() |
| 144 | + .subject_name(subject) |
| 145 | + .issuer_name(issuer) |
| 146 | + .public_key(client_key) |
| 147 | + .serial_number(x509.random_serial_number()) |
| 148 | + .not_valid_before(datetime.datetime.utcnow()) |
| 149 | + .not_valid_after(cert._not_valid_after) # type: ignore |
| 150 | + ) |
| 151 | + return ( |
| 152 | + cert.sign(priv_key, hashes.SHA256(), default_backend()) |
| 153 | + .public_bytes(encoding=serialization.Encoding.PEM) |
| 154 | + .decode("UTF-8") |
| 155 | + ) |
| 156 | + |
| 157 | + |
| 158 | +async def create_ssl_context() -> ssl.SSLContext: |
| 159 | + """Helper method to build an ssl.SSLContext for tests""" |
| 160 | + # generate keys and certs for test |
| 161 | + cert, private_key = generate_cert("my-project", "my-instance") |
| 162 | + server_ca_cert = self_signed_cert(cert, private_key) |
| 163 | + client_private, client_bytes = await generate_keys() |
| 164 | + client_key: rsa.RSAPublicKey = serialization.load_pem_public_key( |
| 165 | + client_bytes.encode("UTF-8"), default_backend() |
| 166 | + ) # type: ignore |
| 167 | + ephemeral_cert = client_key_signed_cert(cert, private_key, client_key) |
| 168 | + # build default ssl.SSLContext |
| 169 | + context = ssl.create_default_context() |
| 170 | + # load ssl.SSLContext with certs |
| 171 | + with TemporaryDirectory() as tmpdir: |
| 172 | + ca_filename, cert_filename, key_filename = write_to_file( |
| 173 | + tmpdir, server_ca_cert, ephemeral_cert, client_private |
| 174 | + ) |
| 175 | + context.load_cert_chain(cert_filename, keyfile=key_filename) |
| 176 | + context.load_verify_locations(cafile=ca_filename) |
| 177 | + return context |
| 178 | + |
| 179 | + |
| 180 | +class FakeCSQLInstance: |
| 181 | + def __init__(self, project: str, region: str, name: str) -> None: |
| 182 | + self.project = project |
| 183 | + self.region = region |
| 184 | + self.name = name |
| 185 | + self.db_version = "POSTGRES_14" # arbitrary value |
| 186 | + self.ip_addrs = {"PRIMARY": "0.0.0.0", "PRIVATE": "1.1.1.1"} |
| 187 | + self.backend_type = "SECOND_GEN" |
| 188 | + |
| 189 | + # generate server private key and cert |
| 190 | + cert, key = generate_cert(project, name) |
| 191 | + self.key = key |
| 192 | + self.cert = cert |
| 193 | + |
| 194 | + def connect_settings(self, ip_addrs: Optional[Dict] = None) -> str: |
| 195 | + """ |
| 196 | + Mock data for the following API: |
| 197 | + https://sqladmin.googleapis.com/sql/v1beta4/projects/{project}/instances/{instance}/connectSettings |
| 198 | + """ |
| 199 | + server_ca_cert = self_signed_cert(self.cert, self.key) |
| 200 | + ip_addrs = ip_addrs if ip_addrs else self.ip_addrs |
| 201 | + ip_addresses = [ |
| 202 | + {"type": key, "ipAddress": value} for key, value in ip_addrs.items() |
| 203 | + ] |
| 204 | + return json.dumps( |
| 205 | + { |
| 206 | + "kind": "sql#connectSettings", |
| 207 | + "serverCaCert": { |
| 208 | + "cert": server_ca_cert, |
| 209 | + "instance": self.name, |
| 210 | + "expirationTime": str( |
| 211 | + datetime.datetime.utcnow() + datetime.timedelta(minutes=10) |
| 212 | + ), |
| 213 | + }, |
| 214 | + "ipAddresses": ip_addresses, |
| 215 | + "region": self.region, |
| 216 | + "databaseVersion": self.db_version, |
| 217 | + "backendType": self.backend_type, |
| 218 | + } |
| 219 | + ) |
| 220 | + |
| 221 | + def generate_ephemeral(self, client_bytes: str) -> str: |
| 222 | + """ |
| 223 | + Mock data for the following API: |
| 224 | + https://sqladmin.googleapis.com/sql/v1beta4/projects/{project}/instances/{instance}:generateEphemeralCert |
| 225 | + """ |
| 226 | + client_key: rsa.RSAPublicKey = serialization.load_pem_public_key( |
| 227 | + client_bytes.encode("UTF-8"), default_backend() |
| 228 | + ) # type: ignore |
| 229 | + ephemeral_cert = client_key_signed_cert(self.cert, self.key, client_key) |
| 230 | + return json.dumps( |
| 231 | + { |
| 232 | + "ephemeralCert": { |
| 233 | + "kind": "sql#sslCert", |
| 234 | + "cert": ephemeral_cert, |
| 235 | + "expirationTime": str( |
| 236 | + datetime.datetime.utcnow() + datetime.timedelta(minutes=10) |
| 237 | + ), |
| 238 | + } |
| 239 | + } |
| 240 | + ) |
0 commit comments