Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Argo CD weak credential tester #502

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.grafana.GrafanaCredentialTester;
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.hydra.HydraCredentialTester;
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.jenkins.JenkinsCredentialTester;
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.argocd.ArgoCdCredentialTester;
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.mlflow.MlFlowCredentialTester;
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.mysql.MysqlCredentialTester;
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.ncrack.NcrackCredentialTester;
Expand Down Expand Up @@ -67,6 +68,7 @@ protected void configurePlugin() {

Multibinder<CredentialTester> credentialTesterBinder =
Multibinder.newSetBinder(binder(), CredentialTester.class);
credentialTesterBinder.addBinding().to(ArgoCdCredentialTester.class);
credentialTesterBinder.addBinding().to(JenkinsCredentialTester.class);
credentialTesterBinder.addBinding().to(MlFlowCredentialTester.class);
credentialTesterBinder.addBinding().to(MysqlCredentialTester.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.argocd;
JamesFoxxx marked this conversation as resolved.
Show resolved Hide resolved

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.tsunami.common.net.http.HttpRequest.post;
import static com.google.tsunami.common.net.http.HttpStatus.TEMPORARY_REDIRECT;

import com.google.common.collect.ImmutableList;
import com.google.common.flogger.GoogleLogger;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.protobuf.ByteString;
import com.google.tsunami.common.data.NetworkEndpointUtils;
import com.google.tsunami.common.data.NetworkServiceUtils;
import com.google.tsunami.common.net.http.HttpClient;
import com.google.tsunami.common.net.http.HttpHeaders;
import com.google.tsunami.common.net.http.HttpResponse;
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.provider.TestCredential;
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.tester.CredentialTester;
import com.google.tsunami.proto.NetworkService;
import java.io.IOException;
import java.util.List;
import javax.inject.Inject;

/** Credential tester specifically for argocd. */
public final class ArgoCdCredentialTester extends CredentialTester {
private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();
private final HttpClient httpClient;

private static final String ARGOCD_SERVICE = "argocd";

@Inject
ArgoCdCredentialTester(HttpClient httpClient) {
this.httpClient = checkNotNull(httpClient);
}

@Override
public String name() {
return "ArgoCdCredentialTester";
}

@Override
public String description() {
return "ArgoCd credential tester.";
}

@Override
public boolean canAccept(NetworkService networkService) {
return NetworkServiceUtils.getWebServiceName(networkService).equals(ARGOCD_SERVICE);
}

@Override
public boolean batched() {
return true;
}

@Override
public ImmutableList<TestCredential> testValidCredentials(
NetworkService networkService, List<TestCredential> credentials) {
// Always return 1st weak credential to gracefully handle no auth configured case, where we
// return empty credential instead of all the weak credentials
return credentials.stream()
.filter(cred -> isArgoCdAccessible(networkService, cred))
.findFirst()
.map(ImmutableList::of)
.orElseGet(ImmutableList::of);
}

private boolean isArgoCdAccessible(NetworkService networkService, TestCredential credential) {
var uriAuthority = NetworkEndpointUtils.toUriAuthority(networkService.getNetworkEndpoint());
var url = String.format("http://%s/%s", uriAuthority, "api/v1/session");
try {
logger.atInfo().log(
"url: %s, username: %s, password: %s",
url, credential.username(), credential.password().orElse(""));
ByteString loginReqBody =
ByteString.copyFromUtf8(
String.format(
"{\"username\":\"%s\",\"password\":\"%s\"}",
credential.username(), credential.password().get()));
HttpHeaders loginHeaders =
HttpHeaders.builder().addHeader("Content-Type", "application/json").build();
HttpResponse loginResponse =
httpClient.send(post(url).setHeaders(loginHeaders).setRequestBody(loginReqBody).build());
if (loginResponse.status() == TEMPORARY_REDIRECT) {
url = String.format("https://%s/%s", uriAuthority, "api/v1/session");
loginResponse =
httpClient.send(
post(url).setHeaders(loginHeaders).setRequestBody(loginReqBody).build());
}
return loginResponse.status().isSuccess()
&& loginResponse.bodyString().isPresent()
&& bodyContainsToken(loginResponse.bodyString().get());
} catch (IOException e) {
logger.atWarning().withCause(e).log("Unable to query '%s'.", url);
return false;
}
}

private static boolean bodyContainsToken(String responseBody) {
try {
return JsonParser.parseString(responseBody).getAsJsonObject().has("token");
} catch (IllegalStateException | JsonSyntaxException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,11 @@ service_default_credentials {
default_usernames: "default"
default_passwords: ""
}

service_default_credentials {
service_name: "argocd"
default_usernames: "admin"
default_passwords: "Password1!"
default_passwords: "password"
default_passwords: "YOUR-PASSWORD-HERE"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.argocd;

import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
import static com.google.common.truth.Truth.assertThat;
import static com.google.tsunami.common.data.NetworkEndpointUtils.forHostnameAndPort;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableList;
import com.google.inject.Guice;
import com.google.tsunami.common.net.db.ConnectionProviderInterface;
import com.google.tsunami.common.net.http.HttpClientModule;
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.provider.TestCredential;
import com.google.tsunami.proto.NetworkService;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.util.Objects;
import java.util.Optional;
import javax.inject.Inject;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

/** Tests for {@link ArgoCdCredentialTester}. */
@RunWith(JUnit4.class)
public class ArgoCdCredentialTesterTest {
@Rule public MockitoRule rule = MockitoJUnit.rule();
@Mock private ConnectionProviderInterface mockConnectionProvider;
@Mock private Connection mockConnection;
@Inject private ArgoCdCredentialTester tester;
private MockWebServer mockWebServer;
private static final TestCredential WEAK_CRED_1 =
TestCredential.create("admin", Optional.of("password"));
private static final TestCredential WEAK_CRED_2 =
TestCredential.create("admin", Optional.of("Password1!"));
private static final TestCredential WEAK_CRED_3 =
TestCredential.create("admin", Optional.of("YOUR-PASSWORD-HERE"));
private static final TestCredential WRONG_CRED_1 =
TestCredential.create("wrong", Optional.of("wrong"));

@Before
public void setup() {
mockWebServer = new MockWebServer();
Guice.createInjector(new HttpClientModule.Builder().build()).injectMembers(this);
}

@Test
public void detect_weakCredentialsExists_returnsWeakCredentials() throws Exception {
startMockWebServer();
NetworkService targetNetworkService =
NetworkService.newBuilder()
.setNetworkEndpoint(
forHostnameAndPort(mockWebServer.getHostName(), mockWebServer.getPort()))
.setServiceName("argocd")
.build();

assertThat(tester.testValidCredentials(targetNetworkService, ImmutableList.of(WEAK_CRED_1)))
.containsExactly(WEAK_CRED_1);
mockWebServer.shutdown();
}

@Test
public void detect_weakCredentialsExist_returnsFirstWeakCredentials() throws Exception {
startMockWebServer();
NetworkService targetNetworkService =
NetworkService.newBuilder()
.setNetworkEndpoint(
forHostnameAndPort(mockWebServer.getHostName(), mockWebServer.getPort()))
.setServiceName("argocd")
.build();

assertThat(
tester.testValidCredentials(
targetNetworkService, ImmutableList.of(WEAK_CRED_1, WEAK_CRED_2, WEAK_CRED_3)))
.containsExactly(WEAK_CRED_1);
}

@Test
public void detect_argocdService_canAccept() throws Exception {
startMockWebServer();
NetworkService targetNetworkService =
NetworkService.newBuilder()
.setNetworkEndpoint(
forHostnameAndPort(mockWebServer.getHostName(), mockWebServer.getPort()))
.setServiceName("argocd")
.build();

assertThat(tester.canAccept(targetNetworkService)).isTrue();
}

@Test
public void detect_noWeakCredentials_returnsNoCredentials() throws Exception {
startMockWebServer();
NetworkService targetNetworkService =
NetworkService.newBuilder()
.setNetworkEndpoint(
forHostnameAndPort(mockWebServer.getHostName(), mockWebServer.getPort()))
.setServiceName("argocd")
.build();
assertThat(tester.testValidCredentials(targetNetworkService, ImmutableList.of(WRONG_CRED_1)))
.isEmpty();
}

@Test
public void detect_nonArgoCdService_skips() throws Exception {
when(mockConnectionProvider.getConnection(any(), any(), any())).thenReturn(mockConnection);
NetworkService targetNetworkService =
NetworkService.newBuilder()
.setNetworkEndpoint(forHostnameAndPort("example.com", 8080))
.setServiceName("http")
.build();

assertThat(tester.testValidCredentials(targetNetworkService, ImmutableList.of(WEAK_CRED_1)))
.isEmpty();
verifyNoInteractions(mockConnectionProvider);
}

private void startMockWebServer() throws IOException {
final Dispatcher dispatcher =
new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) {
String authorizationRequestBody = request.getBody().readString(StandardCharsets.UTF_8);
if (request.getPath().equals("/api/v1/session")
&& Objects.equals(request.getMethod(), "POST")
&& Objects.equals(request.getHeader(CONTENT_TYPE), "application/json")) {
boolean isDefaultCredentials =
authorizationRequestBody.equals(
"{\"username\":\"admin\",\"password\":\"Password1!\"}")
|| authorizationRequestBody.equals(
"{\"username\":\"admin\",\"password\":\"password\"}")
|| authorizationRequestBody.equals(
"{\"username\":\"admin\",\"password\":\"YOUR-PASSWORD-HERE\"}");
if (isDefaultCredentials) {
return new MockResponse()
.setResponseCode(200)
.setBody("{\"token\": \"AToken\"\n" + "}");
} else {
return new MockResponse().setResponseCode(401);
}
}
return new MockResponse().setResponseCode(404);
}
};
mockWebServer.setDispatcher(dispatcher);
mockWebServer.start();
mockWebServer.url("/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.tsunami.common.net.http.HttpRequest.get;
import static com.google.tsunami.common.net.http.HttpRequest.post;
import static com.google.tsunami.common.net.http.HttpStatus.TEMPORARY_REDIRECT;
import static java.util.stream.Collectors.joining;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.flogger.GoogleLogger;
import com.google.tsunami.common.data.NetworkEndpointUtils;
import com.google.protobuf.ByteString;
import com.google.tsunami.common.data.NetworkServiceUtils;
import com.google.tsunami.common.net.http.HttpClient;
Expand Down Expand Up @@ -280,6 +282,7 @@ private ImmutableSet<DetectedSoftware> detectSoftwareByCustomHeuristics(

checkForMlflow(detectedSoftware, networkService, startingUrl);
checkForZenMl(detectedSoftware, networkService, startingUrl);
checkForArgoCd(detectedSoftware, networkService, startingUrl);
return ImmutableSet.copyOf(detectedSoftware);
}

Expand Down Expand Up @@ -372,4 +375,44 @@ private void checkForZenMl(
logger.atWarning().withCause(e).log("Unable to query '%s'.", loginUrl);
}
}

private void checkForArgoCd(
Set<DetectedSoftware> software, NetworkService networkService, String startingUrl) {
logger.atInfo().log("probing Argo CD - custom fingerprint phase");

var uriAuthority = NetworkEndpointUtils.toUriAuthority(networkService.getNetworkEndpoint());
var applicationsApiUrl = String.format("http://%s/%s", uriAuthority, "api/v1/applications");
try {
HttpHeaders apiApplicationsReqHeaders =
HttpHeaders.builder().addHeader("Content-Type", "application/json").build();
HttpResponse apiApplicationsResponse =
httpClient.send(post(applicationsApiUrl).setHeaders(apiApplicationsReqHeaders).build());
if (apiApplicationsResponse.status() == TEMPORARY_REDIRECT) {
applicationsApiUrl = String.format("https://%s/%s", uriAuthority, "api/v1/applications");
apiApplicationsResponse =
httpClient.send(post(applicationsApiUrl).setHeaders(apiApplicationsReqHeaders).build());
}
if (apiApplicationsResponse.status() != HttpStatus.INTERNAL_SERVER_ERROR
|| apiApplicationsResponse.bodyString().isEmpty()) {
return;
}

if (apiApplicationsResponse
.bodyString()
.get()
.contains(
"{\"error\":\"grpc: error while marshaling: proto: required field \\\"application\\\""
+ " not set\",\"code\":13,\"message\":\"grpc: error while marshaling: "
+ "proto: required field \\\"application\\\" not set\"}")) {
software.add(
DetectedSoftware.builder()
.setSoftwareIdentity(SoftwareIdentity.newBuilder().setSoftware("argocd").build())
.setRootPath(startingUrl)
.setContentHashes(ImmutableMap.of())
.build());
}
} catch (IOException e) {
logger.atWarning().withCause(e).log("Unable to query '%s'.", applicationsApiUrl);
}
}
}