Skip to content

Commit

Permalink
Getting there...
Browse files Browse the repository at this point in the history
  • Loading branch information
cgivre committed May 11, 2022
1 parent 87ccc20 commit baccc47
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
Expand Up @@ -73,7 +73,6 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


public class TestUserTranslationInHttpPlugin extends ClusterTest {

private static final int MOCK_SERVER_PORT = 47775;
Expand Down Expand Up @@ -122,14 +121,12 @@ public static void setup() throws Exception {
Map<String, HttpApiConfig> configs = new HashMap<>();
configs.put("sharedEndpoint", testEndpoint);


Map<String, String> credentials = new HashMap<>();
credentials.put("username", "user2user");
credentials.put("password", "user2pass");

PlainCredentialsProvider credentialsProvider = new PlainCredentialsProvider(TEST_USER_2, credentials);


HttpStoragePluginConfig mockStorageConfigWithWorkspace = new HttpStoragePluginConfig(false, configs, 2, null, null, "", 80, "", "", "", null, credentialsProvider, AuthMode.USER_TRANSLATION.name());
mockStorageConfigWithWorkspace.setEnabled(true);
cluster.defineStoragePlugin("local", mockStorageConfigWithWorkspace);
Expand All @@ -156,7 +153,11 @@ public void testEmptyUserCredentials() throws Exception {
@Test
public void testQueryWithValidCredentials() throws Exception {
// This test validates that the correct credentials are sent down to the HTTP API.
ClientFixture client = cluster.clientBuilder().property(DrillProperties.USER, TEST_USER_2).property(DrillProperties.PASSWORD, TEST_USER_2_PASSWORD).build();
ClientFixture client = cluster
.clientBuilder()
.property(DrillProperties.USER, TEST_USER_2)
.property(DrillProperties.PASSWORD, TEST_USER_2_PASSWORD)
.build();

try (MockWebServer server = startServer()) {
server.enqueue(new MockResponse().setResponseCode(200).setBody(TEST_JSON_RESPONSE_WITH_DATATYPES));
Expand All @@ -176,7 +177,11 @@ public void testQueryWithValidCredentials() throws Exception {
@Test
public void testQueryWithMissingCredentials() throws Exception {
// This test validates that the correct credentials are sent down to the HTTP API.
ClientFixture client = cluster.clientBuilder().property(DrillProperties.USER, TEST_USER_1).property(DrillProperties.PASSWORD, TEST_USER_1_PASSWORD).build();
ClientFixture client = cluster
.clientBuilder()
.property(DrillProperties.USER, TEST_USER_1)
.property(DrillProperties.PASSWORD, TEST_USER_1_PASSWORD)
.build();

try (MockWebServer server = startServer()) {
server.enqueue(new MockResponse().setResponseCode(200).setBody(TEST_JSON_RESPONSE_WITH_DATATYPES));
Expand Down Expand Up @@ -206,7 +211,11 @@ private boolean makeLoginRequest(String username, String password) throws IOExce
public void testUnrelatedQueryWithUser() throws Exception {
// This test verifies that a query with a user that does NOT have credentials
// for a plugin using user translation will still execute.
ClientFixture client = cluster.clientBuilder().property(DrillProperties.USER, TEST_USER_1).property(DrillProperties.PASSWORD, TEST_USER_1_PASSWORD).build();
ClientFixture client = cluster
.clientBuilder()
.property(DrillProperties.USER, TEST_USER_1)
.property(DrillProperties.PASSWORD, TEST_USER_1_PASSWORD)
.build();

String sql = "SHOW FILES IN dfs";
QuerySummary result = client.queryBuilder().sql(sql).run();
Expand Down
Expand Up @@ -307,6 +307,7 @@ public Response updateOAuthTokens(@PathParam("name") String name,
@GET
@Path("/storage/{name}/update_oauth2_authtoken")
@Produces(MediaType.TEXT_HTML)
@Operation(externalDocs = @ExternalDocumentation(description = "Apache Drill REST API documentation:", url = "https://drill.apache.org/docs/rest-api-introduction/"))
public Response updateAuthToken(@PathParam("name") String name, @QueryParam("code") String code) {
try {
if (storage.getPlugin(name).getConfig() instanceof CredentialedStoragePluginConfig) {
Expand All @@ -317,10 +318,12 @@ public Response updateAuthToken(@PathParam("name") String name, @QueryParam("cod
// Now exchange the authorization token for an access token
Builder builder = new OkHttpClient.Builder();
OkHttpClient client = builder.build();

Request accessTokenRequest = OAuthUtils.getAccessTokenRequest(credentialsProvider, code, callbackURL);
Map<String, String> updatedTokens = OAuthUtils.getOAuthTokens(client, accessTokenRequest);

// Add to token registry
// If USER_TRANSLATION is enabled, Drill will create a token table for each user.
TokenRegistry tokenRegistry = ((AbstractStoragePlugin) storage.getPlugin(name))
.getContext()
.getoAuthTokenProvider()
Expand Down Expand Up @@ -541,6 +544,12 @@ public Response deletePluginViaGet(@PathParam("name") String name) {
return deletePlugin(name);
}

/**
* This function checks to see if a given storage plugin is using USER_TRANSLATION mode.
* If so, it will return the active user name. If not it will return null.
* @param config {@link StoragePluginConfig} The current plugin configuration
* @return If USER_TRANSLATION is enabled, returns the active user. If not, returns null.
*/
private String getActiveUser(StoragePluginConfig config) {
if (config.getAuthMode() == AuthMode.USER_TRANSLATION) {
return sc.getUserPrincipal().getName();
Expand Down
Expand Up @@ -112,7 +112,6 @@
function authorize(finalURL) {
console.log(finalURL);
var tokenGetterWindow = window.open(finalURL, 'Authorize Drill', "toolbar=no,menubar=no,scrollbars=yes,resizable=yes,top=500,left=500,width=450,height=600");
var timer = setInterval(function () {
if (tokenGetterWindow.closed) {
clearInterval(timer);
Expand Down

0 comments on commit baccc47

Please sign in to comment.