Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Autodiscover default credentials when nothing is explicitly specified.
Browse files Browse the repository at this point in the history
This change reversed one made in PR #228 that broke automatic credential
discovery and instead replaced it with a "No Credentials" implementation.

Autodiscovery is a better default, and matches the behaviour documented
by the properties `gcs.credentials.path` and `gcs.credentials.json`
which state

> If not provided, the connector will try to detect the credentials
> automatically

Closes #291
  • Loading branch information
markallanson committed Aug 2, 2023
1 parent 699969d commit b9908cb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
5 changes: 0 additions & 5 deletions src/main/java/io/aiven/kafka/connect/gcs/GcsSinkConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import io.aiven.kafka.connect.common.templating.Template;

import com.google.auth.oauth2.OAuth2Credentials;
import com.google.cloud.NoCredentials;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.threeten.bp.Duration;
Expand Down Expand Up @@ -329,10 +328,6 @@ private void validate() {
public OAuth2Credentials getCredentials() {
final String credentialsPath = getString(GCS_CREDENTIALS_PATH_CONFIG);
final Password credentialsJsonPwd = getPassword(GCS_CREDENTIALS_JSON_CONFIG);
if (credentialsPath == null && credentialsJsonPwd == null) {
LOG.warn("No GCS credentials provided, trying to connect without credentials.");
return NoCredentials.getInstance();
}
try {
String credentialsJson = null;
if (credentialsJsonPwd != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;

import java.io.IOException;
import java.net.URL;
Expand Down Expand Up @@ -50,13 +52,17 @@
import io.aiven.kafka.connect.common.templating.Template;
import io.aiven.kafka.connect.common.templating.VariableTemplatePart;
import io.aiven.kafka.connect.gcs.GcsSinkConfig;
import io.aiven.kafka.connect.gcs.GoogleCredentialsBuilder;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.OAuth2Credentials;
import com.google.auth.oauth2.UserCredentials;
import com.google.common.io.Resources;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.MockedStatic;
import org.threeten.bp.Duration;

/**
Expand Down Expand Up @@ -377,6 +383,23 @@ void gcsCredentialsJson() throws IOException {
assertEquals("test-client-secret", credentials.getClientSecret());
}

@Test
void gcsCredentialsDefault() {
final Map<String, String> properties = Map.of("gcs.bucket.name", "test-bucket");

assertConfigDefValidationPasses(properties);

try (MockedStatic<GoogleCredentialsBuilder> mocked = mockStatic(GoogleCredentialsBuilder.class)) {
final GoogleCredentials googleCredentials = mock(GoogleCredentials.class);
mocked.when(() -> GoogleCredentialsBuilder.build(null, null)).thenReturn(googleCredentials);

final GcsSinkConfig config = new GcsSinkConfig(properties);

final OAuth2Credentials credentials = config.getCredentials();
assertEquals(googleCredentials, credentials);
}
}

@Test
void gcsCredentialsExclusivity() throws IOException {
final Map<String, String> properties = new HashMap<>();
Expand Down

0 comments on commit b9908cb

Please sign in to comment.