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

Do not URL-decode refresh header #332

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import com.fasterxml.jackson.annotation.JsonTypeName;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.SecurityContext;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import org.apache.commons.codec.binary.Base64;
import org.apache.hadoop.hdfs.web.oauth2.OAuth2Constants;
import org.apache.iceberg.rest.auth.OAuth2Properties;
Expand Down Expand Up @@ -80,8 +78,8 @@ public Response getToken(
}
LOGGER.debug("Found credentials in auth header - treating as client_credentials");
String[] parts = credentials.split(":", 2);
clientId = URLDecoder.decode(parts[0], Charset.defaultCharset());
clientSecret = URLDecoder.decode(parts[1], Charset.defaultCharset());
clientId = parts[0];
clientSecret = parts[1];
}
TokenResponse tokenResponse =
switch (subjectTokenType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import static org.apache.polaris.service.throttling.RequestThrottlingErrorResponse.RequestThrottlingErrorType.REQUEST_TOO_LARGE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import io.dropwizard.testing.ConfigOverride;
import io.dropwizard.testing.ResourceHelpers;
Expand Down Expand Up @@ -57,8 +59,13 @@
import org.apache.iceberg.exceptions.RESTException;
import org.apache.iceberg.hadoop.HadoopFileIO;
import org.apache.iceberg.io.ResolvingFileIO;
import org.apache.iceberg.rest.HTTPClient;
import org.apache.iceberg.rest.RESTClient;
import org.apache.iceberg.rest.RESTSessionCatalog;
import org.apache.iceberg.rest.auth.AuthConfig;
import org.apache.iceberg.rest.auth.ImmutableAuthConfig;
import org.apache.iceberg.rest.auth.OAuth2Properties;
import org.apache.iceberg.rest.auth.OAuth2Util;
import org.apache.iceberg.types.Types;
import org.apache.iceberg.util.EnvironmentUtil;
import org.apache.polaris.core.admin.model.AwsStorageConfigInfo;
Expand Down Expand Up @@ -88,6 +95,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.shaded.com.google.common.collect.ImmutableMap;

@ExtendWith({
DropwizardExtensionsSupport.class,
Expand Down Expand Up @@ -699,4 +707,37 @@ public void testRequestBodyTooLarge() {
.equals(REQUEST_TOO_LARGE));
}
}

@Test
public void testRefreshToken() throws IOException {
String path =
String.format("http://localhost:%d/api/catalog/v1/oauth/tokens", EXT.getLocalPort());
try (RESTClient client =
HTTPClient.builder(ImmutableMap.of())
.withHeader(REALM_PROPERTY_KEY, realm)
.uri(path)
.build()) {
String credentialString =
snowmanCredentials.clientId() + ":" + snowmanCredentials.clientSecret();
var authConfig =
AuthConfig.builder().credential(credentialString).scope("PRINCIPAL_ROLE:ALL").build();
ImmutableAuthConfig configSpy = spy(authConfig);
when(configSpy.expiresAtMillis()).thenReturn(0L);
assertThat(configSpy.expiresAtMillis()).isEqualTo(0L);
when(configSpy.oauth2ServerUri()).thenReturn(path);

var parentSession = new OAuth2Util.AuthSession(Map.of(), configSpy);
var session =
OAuth2Util.AuthSession.fromAccessToken(client, null, userToken, 0L, parentSession);

OAuth2Util.AuthSession sessionSpy = spy(session);
when(sessionSpy.expiresAtMillis()).thenReturn(0L);
assertThat(sessionSpy.expiresAtMillis()).isEqualTo(0L);
assertThat(sessionSpy.token()).isEqualTo(userToken);

sessionSpy.refresh(client);
assertThat(sessionSpy.credential()).isNotNull();
assertThat(sessionSpy.credential()).isNotEqualTo(userToken);
}
}
}