|
| 1 | +/* |
| 2 | + * Copyright 2023 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.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.airbyte; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static com.google.tsunami.common.data.NetworkEndpointUtils.forHostnameAndPort; |
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | + |
| 25 | +import com.google.common.collect.ImmutableList; |
| 26 | +import com.google.inject.Guice; |
| 27 | +import com.google.tsunami.common.net.db.ConnectionProviderInterface; |
| 28 | +import com.google.tsunami.common.net.http.HttpClientModule; |
| 29 | +import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.provider.TestCredential; |
| 30 | +import com.google.tsunami.proto.NetworkService; |
| 31 | +import java.io.IOException; |
| 32 | +import java.sql.Connection; |
| 33 | +import java.util.Objects; |
| 34 | +import java.util.Optional; |
| 35 | +import javax.inject.Inject; |
| 36 | +import okhttp3.mockwebserver.Dispatcher; |
| 37 | +import okhttp3.mockwebserver.MockResponse; |
| 38 | +import okhttp3.mockwebserver.MockWebServer; |
| 39 | +import okhttp3.mockwebserver.RecordedRequest; |
| 40 | +import org.junit.Before; |
| 41 | +import org.junit.Rule; |
| 42 | +import org.junit.Test; |
| 43 | +import org.junit.runner.RunWith; |
| 44 | +import org.junit.runners.JUnit4; |
| 45 | +import org.mockito.Mock; |
| 46 | +import org.mockito.junit.MockitoJUnit; |
| 47 | +import org.mockito.junit.MockitoRule; |
| 48 | + |
| 49 | +/** Tests for {@link AirbyteCredentialTester}. */ |
| 50 | +@RunWith(JUnit4.class) |
| 51 | +public class AirbyteCredentialTesterTest { |
| 52 | + @Rule public MockitoRule rule = MockitoJUnit.rule(); |
| 53 | + @Mock private ConnectionProviderInterface mockConnectionProvider; |
| 54 | + @Mock private Connection mockConnection; |
| 55 | + @Inject private AirbyteCredentialTester tester; |
| 56 | + private MockWebServer mockWebServer; |
| 57 | + private static final TestCredential WEAK_CRED_1 = |
| 58 | + TestCredential.create("user@company.example", Optional.of("new_password")); |
| 59 | + private static final TestCredential WRONG_CRED_1 = |
| 60 | + TestCredential.create("wrong", Optional.of("wrong")); |
| 61 | + |
| 62 | + @Before |
| 63 | + public void setup() { |
| 64 | + mockWebServer = new MockWebServer(); |
| 65 | + Guice.createInjector(new HttpClientModule.Builder().build()).injectMembers(this); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void detect_weakCredentialsExists_returnsWeakCredentials() throws Exception { |
| 70 | + startMockWebServer(); |
| 71 | + NetworkService targetNetworkService = |
| 72 | + NetworkService.newBuilder() |
| 73 | + .setNetworkEndpoint( |
| 74 | + forHostnameAndPort(mockWebServer.getHostName(), mockWebServer.getPort())) |
| 75 | + .setServiceName("airbyte") |
| 76 | + .build(); |
| 77 | + |
| 78 | + assertThat(tester.testValidCredentials(targetNetworkService, ImmutableList.of(WEAK_CRED_1))) |
| 79 | + .containsExactly(WEAK_CRED_1); |
| 80 | + mockWebServer.shutdown(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void detect_weakCredentialsExist_returnsFirstWeakCredentials() throws Exception { |
| 85 | + startMockWebServer(); |
| 86 | + NetworkService targetNetworkService = |
| 87 | + NetworkService.newBuilder() |
| 88 | + .setNetworkEndpoint( |
| 89 | + forHostnameAndPort(mockWebServer.getHostName(), mockWebServer.getPort())) |
| 90 | + .setServiceName("airbyte") |
| 91 | + .build(); |
| 92 | + |
| 93 | + assertThat(tester.testValidCredentials(targetNetworkService, ImmutableList.of(WEAK_CRED_1))) |
| 94 | + .containsExactly(WEAK_CRED_1); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void detect_airbyteService_canAccept() throws Exception { |
| 99 | + startMockWebServer(); |
| 100 | + NetworkService targetNetworkService = |
| 101 | + NetworkService.newBuilder() |
| 102 | + .setNetworkEndpoint( |
| 103 | + forHostnameAndPort(mockWebServer.getHostName(), mockWebServer.getPort())) |
| 104 | + .setServiceName("airbyte") |
| 105 | + .build(); |
| 106 | + |
| 107 | + assertThat(tester.canAccept(targetNetworkService)).isTrue(); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void detect_weakCredentialsExistAndAirbyteInForeignLanguage_returnsFirstWeakCredentials() |
| 112 | + throws Exception { |
| 113 | + startMockWebServer(); |
| 114 | + NetworkService targetNetworkService = |
| 115 | + NetworkService.newBuilder() |
| 116 | + .setNetworkEndpoint( |
| 117 | + forHostnameAndPort(mockWebServer.getHostName(), mockWebServer.getPort())) |
| 118 | + .setServiceName("airbyte") |
| 119 | + .build(); |
| 120 | + |
| 121 | + assertThat(tester.testValidCredentials(targetNetworkService, ImmutableList.of(WEAK_CRED_1))) |
| 122 | + .containsExactly(WEAK_CRED_1); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void detect_noWeakCredentials_returnsNoCredentials() throws Exception { |
| 127 | + startMockWebServer(); |
| 128 | + NetworkService targetNetworkService = |
| 129 | + NetworkService.newBuilder() |
| 130 | + .setNetworkEndpoint( |
| 131 | + forHostnameAndPort(mockWebServer.getHostName(), mockWebServer.getPort())) |
| 132 | + .setServiceName("airbyte") |
| 133 | + .build(); |
| 134 | + assertThat(tester.testValidCredentials(targetNetworkService, ImmutableList.of(WRONG_CRED_1))) |
| 135 | + .isEmpty(); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void detect_nonAirbyteService_skips() throws Exception { |
| 140 | + when(mockConnectionProvider.getConnection(any(), any(), any())).thenReturn(mockConnection); |
| 141 | + NetworkService targetNetworkService = |
| 142 | + NetworkService.newBuilder() |
| 143 | + .setNetworkEndpoint(forHostnameAndPort("example.com", 8080)) |
| 144 | + .setServiceName("http") |
| 145 | + .build(); |
| 146 | + |
| 147 | + assertThat(tester.testValidCredentials(targetNetworkService, ImmutableList.of(WEAK_CRED_1))) |
| 148 | + .isEmpty(); |
| 149 | + verifyNoInteractions(mockConnectionProvider); |
| 150 | + } |
| 151 | + |
| 152 | + private void startMockWebServer() throws IOException { |
| 153 | + final Dispatcher dispatcher = |
| 154 | + new Dispatcher() { |
| 155 | + @Override |
| 156 | + public MockResponse dispatch(RecordedRequest request) { |
| 157 | + if (request.getPath().equals("/api/login") |
| 158 | + && Objects.equals(request.getMethod(), "POST") |
| 159 | + && Objects.equals(request.getHeader("content-type"), "application/json") |
| 160 | + && request |
| 161 | + .getBody() |
| 162 | + .readUtf8() |
| 163 | + .equals( |
| 164 | + "{\"username\":\"user@company.example\",\"password\":\"new_password\"}")) { |
| 165 | + return new MockResponse().setResponseCode(200).setHeader("set-cookie", "jwt=12345"); |
| 166 | + } else { |
| 167 | + return new MockResponse().setResponseCode(401); |
| 168 | + } |
| 169 | + } |
| 170 | + }; |
| 171 | + mockWebServer.setDispatcher(dispatcher); |
| 172 | + mockWebServer.start(); |
| 173 | + mockWebServer.url("/"); |
| 174 | + } |
| 175 | +} |
0 commit comments