/*
 * 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.facebook.presto.tests.querystats;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Exposed;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.PrivateModule;
import com.google.inject.Provides;
import com.teradata.tempto.configuration.Configuration;
import com.teradata.tempto.initialization.AutoModuleProvider;
import com.teradata.tempto.initialization.SuiteModuleProvider;
import io.airlift.http.client.BasicAuthRequestFilter;
import io.airlift.http.client.HttpClientConfig;
import io.airlift.http.client.HttpRequestFilter;
import io.airlift.http.client.jetty.JettyHttpClient;
import io.airlift.json.ObjectMapperProvider;

import javax.inject.Named;

import java.net.URI;
import java.util.Set;

@AutoModuleProvider
public class QueryStatsClientModuleProvider
        implements SuiteModuleProvider
{
    private static HttpQueryStatsClient httpQueryStatsClient;

    @Override
    public Module getModule(Configuration configuration)
    {
        return new PrivateModule()
        {
            @Override
            protected void configure()
            {
                bind(ObjectMapper.class).toProvider(ObjectMapperProvider.class);
            }

            @Inject
            @Provides
            @Exposed
            QueryStatsClient getQueryStatsClient(
                    ObjectMapper objectMapper,
                    @Named("databases.presto.server_address") String serverAddress,
                    @Named("databases.presto.jdbc_user") String user,
                    @Named("databases.presto.jdbc_password") String password)
            {
                // @Singleton does not work due: https://github.com/prestodb/tempto/issues/94
                if (httpQueryStatsClient == null) {
                    HttpClientConfig httpClientConfig = new HttpClientConfig();
                    httpClientConfig.setKeyStorePath(configuration.getString("databases.presto.https_keystore_path").orElse(null));
                    httpClientConfig.setKeyStorePassword(configuration.getString("databases.presto.https_keystore_password").orElse(null));
                    Set<HttpRequestFilter> filters = ImmutableSet.of(new BasicAuthRequestFilter(user, password));
                    httpQueryStatsClient = new HttpQueryStatsClient(new JettyHttpClient(httpClientConfig, filters), objectMapper, URI.create(serverAddress));
                }
                return httpQueryStatsClient;
            }
        };
    }
}

@@ -1261,16 +1261,69 @@
 Client;%0A
+import io.airlift.http.client.spnego.KerberosConfig;%0A
 import i
@@ -1404,15 +1404,63 @@
 ava.
-net.URI
+io.File;%0Aimport java.net.URI;%0Aimport java.util.Optional
 ;%0Aim
@@ -2837,32 +2837,974 @@
 .orElse(null));%0A
+%0A                    httpClientConfig.setAuthenticationEnabled(configuration.getBoolean(%22databases.presto.cli_kerberos_authentication%22).orElse(false));%0A                    httpClientConfig.setKerberosPrincipal(configuration.getString(%22databases.presto.cli_kerberos_principal%22).orElse(null));%0A                    httpClientConfig.setKerberosRemoteServiceName(configuration.getString(%22databases.presto.cli_kerberos_service_name%22).orElse(null));%0A                    KerberosConfig kerberosConfig = new KerberosConfig();%0A                    kerberosConfig.setConfig(new File(configuration.getString(%22databases.presto.cli_kerberos_config_path%22).orElse(%22%22)));%0A                    kerberosConfig.setKeytab(new File(configuration.getString(%22databases.presto.cli_kerberos_keytab%22).orElse(%22%22)));%0A                    kerberosConfig.setUseCanonicalHostname(configuration.getBoolean(%22databases.presto.cli_kerberos_use_canonical_hostname%22).orElse(true));%0A%0A
                 
@@ -4006,16 +4006,50 @@
 tConfig,
+ kerberosConfig, Optional.empty(),
  filters

Fix the http client used in tests to use kerberos credentials