|
| 1 | +/* |
| 2 | + * Copyright 2025 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 | + * http://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 | + |
| 17 | +package com.google.cloud.sql.core; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertThrows; |
| 20 | + |
| 21 | +import java.security.cert.Certificate; |
| 22 | +import java.security.cert.CertificateException; |
| 23 | +import java.security.cert.X509Certificate; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.List; |
| 28 | +import org.junit.BeforeClass; |
| 29 | +import org.junit.Test; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | +import org.junit.runners.Parameterized; |
| 32 | +import org.junit.runners.Parameterized.Parameters; |
| 33 | + |
| 34 | +@RunWith(Parameterized.class) |
| 35 | +public class InstanceCheckingTrustManagerFactoryTest { |
| 36 | + |
| 37 | + private static TestCertificateGenerator generator; |
| 38 | + private final TestCase tc; |
| 39 | + |
| 40 | + @BeforeClass |
| 41 | + public static void beforeClass() { |
| 42 | + generator = new TestCertificateGenerator(); |
| 43 | + } |
| 44 | + |
| 45 | + public InstanceCheckingTrustManagerFactoryTest(TestCase tc) { |
| 46 | + this.tc = tc; |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testValidateCertificate() throws Exception { |
| 51 | + |
| 52 | + List<Certificate> caCerts; |
| 53 | + X509Certificate[] serverCert; |
| 54 | + if (tc.cas) { |
| 55 | + caCerts = Arrays.asList(generator.getCasServerCertificateChain()); |
| 56 | + caCerts = caCerts.subList(1, caCerts.size()); |
| 57 | + serverCert = generator.createServerCertificate(tc.cn, tc.san, true); |
| 58 | + } else { |
| 59 | + caCerts = Collections.singletonList(generator.getServerCaCert()); |
| 60 | + serverCert = generator.createServerCertificate(tc.cn, tc.san, false); |
| 61 | + } |
| 62 | + |
| 63 | + InstanceMetadata instanceMetadata = |
| 64 | + new InstanceMetadata( |
| 65 | + new CloudSqlInstanceName(tc.icn, tc.serverName), |
| 66 | + Collections.emptyMap(), |
| 67 | + caCerts, |
| 68 | + false, |
| 69 | + null, |
| 70 | + false); |
| 71 | + |
| 72 | + InstanceCheckingTrustManagerFactory f = |
| 73 | + InstanceCheckingTrustManagerFactory.newInstance(instanceMetadata); |
| 74 | + InstanceCheckingTrustManger tm = (InstanceCheckingTrustManger) f.getTrustManagers()[0]; |
| 75 | + |
| 76 | + if (tc.valid) { |
| 77 | + tm.checkServerTrusted(serverCert, "UNKNOWN"); |
| 78 | + } else { |
| 79 | + assertThrows( |
| 80 | + CertificateException.class, |
| 81 | + () -> { |
| 82 | + tm.checkServerTrusted(serverCert, "UNKNOWN"); |
| 83 | + }); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Parameters(name = "{index}: {0}") |
| 88 | + public static List<TestCase> testCases() { |
| 89 | + List<TestCase> cases = |
| 90 | + Arrays.asList( |
| 91 | + new TestCase( |
| 92 | + "cn match", |
| 93 | + null, |
| 94 | + "myProject:myRegion:myInstance", |
| 95 | + "myProject:myInstance", |
| 96 | + null, |
| 97 | + true), |
| 98 | + new TestCase( |
| 99 | + "cn no match", |
| 100 | + null, |
| 101 | + "myProject:myRegion:badInstance", |
| 102 | + "myProject:myInstance", |
| 103 | + null, |
| 104 | + false), |
| 105 | + new TestCase( |
| 106 | + "cn empty", null, "myProject:myRegion:myInstance", "db.example.com", null, false), |
| 107 | + new TestCase( |
| 108 | + "san match", |
| 109 | + "db.example.com", |
| 110 | + "myProject:myRegion:myInstance", |
| 111 | + null, |
| 112 | + "db.example.com", |
| 113 | + true), |
| 114 | + new TestCase( |
| 115 | + "san no match", |
| 116 | + "bad.example.com", |
| 117 | + "myProject:myRegion:myInstance", |
| 118 | + null, |
| 119 | + "db.example.com", |
| 120 | + false), |
| 121 | + new TestCase( |
| 122 | + "san empty match", |
| 123 | + "empty.example.com", |
| 124 | + "myProject:myRegion:myInstance", |
| 125 | + "", |
| 126 | + null, |
| 127 | + false), |
| 128 | + new TestCase( |
| 129 | + "san match with cn present", |
| 130 | + "db.example.com", |
| 131 | + "myProject:myRegion:myInstance", |
| 132 | + "myProject:myInstance", |
| 133 | + "db.example.com", |
| 134 | + true), |
| 135 | + new TestCase( |
| 136 | + "san no match fallback to cn", |
| 137 | + "db.example.com", |
| 138 | + "myProject:myRegion:myInstance", |
| 139 | + "myProject:myInstance", |
| 140 | + "other.example.com", |
| 141 | + true), |
| 142 | + new TestCase( |
| 143 | + "san empty match fallback to cn", |
| 144 | + "db.example.com", |
| 145 | + "myProject:myRegion:myInstance", |
| 146 | + "myProject:myInstance", |
| 147 | + null, |
| 148 | + true), |
| 149 | + new TestCase( |
| 150 | + "san no match fallback to cn and fail", |
| 151 | + "db.example.com", |
| 152 | + "myProject:myRegion:badInstance", |
| 153 | + "other.example.com", |
| 154 | + "myProject:myInstance", |
| 155 | + false)); |
| 156 | + List<TestCase> casesWithCas = new ArrayList<>(cases); |
| 157 | + for (TestCase tc : cases) { |
| 158 | + casesWithCas.add(tc.withCas(true)); |
| 159 | + } |
| 160 | + return casesWithCas; |
| 161 | + } |
| 162 | + |
| 163 | + private static class TestCase { |
| 164 | + /** Testcase description. */ |
| 165 | + private final String desc; |
| 166 | + /** connector configuration domain name. */ |
| 167 | + private final String serverName; |
| 168 | + /** connector configuration instance name. */ |
| 169 | + private final String icn; |
| 170 | + /** server cert CN field value. */ |
| 171 | + private final String cn; |
| 172 | + /** server cert SAN field value. */ |
| 173 | + private final String san; |
| 174 | + /** wants validation to succeed. */ |
| 175 | + private final boolean valid; |
| 176 | + |
| 177 | + private final boolean cas; |
| 178 | + |
| 179 | + public TestCase( |
| 180 | + String desc, String serverName, String icn, String cn, String san, boolean valid) { |
| 181 | + this(desc, serverName, icn, cn, san, valid, false); |
| 182 | + } |
| 183 | + |
| 184 | + public TestCase( |
| 185 | + String desc, |
| 186 | + String serverName, |
| 187 | + String icn, |
| 188 | + String cn, |
| 189 | + String san, |
| 190 | + boolean valid, |
| 191 | + boolean cas) { |
| 192 | + this.desc = desc; |
| 193 | + this.serverName = serverName; |
| 194 | + this.icn = icn; |
| 195 | + this.cn = cn; |
| 196 | + this.san = san; |
| 197 | + this.valid = valid; |
| 198 | + this.cas = cas; |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public String toString() { |
| 203 | + return desc; |
| 204 | + } |
| 205 | + |
| 206 | + private TestCase withCas(boolean cas) { |
| 207 | + return new TestCase(this.desc, this.serverName, this.icn, this.cn, this.san, this.valid, cas); |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments