github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/clients/hadoopfs/src/main/java/io/lakefs/LakeFSClient.java (about)

     1  package io.lakefs;
     2  
     3  import io.lakefs.auth.LakeFSTokenProvider;
     4  import io.lakefs.auth.LakeFSTokenProviderFactory;
     5  import io.lakefs.clients.sdk.*;
     6  import io.lakefs.clients.sdk.auth.HttpBasicAuth;
     7  import io.lakefs.clients.sdk.auth.HttpBearerAuth;
     8  import org.apache.hadoop.conf.Configuration;
     9  import org.slf4j.Logger;
    10  import org.slf4j.LoggerFactory;
    11  
    12  import java.io.IOException;
    13  
    14  /**
    15   * Provides access to lakeFS API using client library.
    16   * This class uses the configuration to initialize API client and instance per API interface we expose.
    17   */
    18  public class LakeFSClient {
    19      private static final Logger LOG = LoggerFactory.getLogger(LakeFSClient.class);
    20      private static final String BASIC_AUTH = "basic_auth";
    21      private static final String JWT_TOKEN_AUTH = "jwt_token";
    22      LakeFSTokenProvider provider;
    23      private final ObjectsApi objectsApi;
    24      private final StagingApi stagingApi;
    25      private final RepositoriesApi repositoriesApi;
    26      private final BranchesApi branchesApi;
    27      private final ConfigApi configApi;
    28      private final InternalApi internalApi;
    29  
    30      public LakeFSClient(String scheme, Configuration conf) throws IOException {
    31          String authProvider = FSConfiguration.get(conf, scheme, Constants.LAKEFS_AUTH_PROVIDER_KEY_SUFFIX, LakeFSClient.BASIC_AUTH);
    32          ApiClient apiClient;
    33          LOG.info("Initiating lakeFS auth provider: {}", authProvider);
    34          if (authProvider == BASIC_AUTH) {
    35              String accessKey = FSConfiguration.get(conf, scheme, Constants.ACCESS_KEY_KEY_SUFFIX);
    36              if (accessKey == null) {
    37                  throw new IOException("Missing lakeFS access key");
    38              }
    39  
    40              String secretKey = FSConfiguration.get(conf, scheme, Constants.SECRET_KEY_KEY_SUFFIX);
    41              if (secretKey == null) {
    42                  throw new IOException("Missing lakeFS secret key");
    43              }
    44              apiClient = newApiClientNoAuth(scheme, conf);
    45              HttpBasicAuth basicAuth = (HttpBasicAuth) apiClient.getAuthentication(BASIC_AUTH);
    46              basicAuth.setUsername(accessKey);
    47              basicAuth.setPassword(secretKey);
    48          } else {
    49              this.provider = LakeFSTokenProviderFactory.newLakeFSTokenProvider(Constants.DEFAULT_SCHEME, conf);
    50              String lakeFSToken = provider.getToken();
    51              apiClient = newApiClientNoAuth(scheme, conf);
    52              HttpBearerAuth tokenAuth = (HttpBearerAuth) apiClient.getAuthentication(JWT_TOKEN_AUTH);
    53              tokenAuth.setBearerToken(lakeFSToken);
    54          }
    55  
    56          this.objectsApi = new ObjectsApi(apiClient);
    57          this.stagingApi = new StagingApi(apiClient);
    58          this.repositoriesApi = new RepositoriesApi(apiClient);
    59          this.branchesApi = new BranchesApi(apiClient);
    60          this.configApi = new ConfigApi(apiClient);
    61          this.internalApi = new InternalApi(apiClient);
    62      }
    63  
    64      ApiClient newApiClientNoAuth(String scheme, Configuration conf) {
    65  
    66          ApiClient apiClient = io.lakefs.clients.sdk.Configuration.getDefaultApiClient();
    67          String endpoint = FSConfiguration.get(conf, scheme, Constants.ENDPOINT_KEY_SUFFIX, Constants.DEFAULT_CLIENT_ENDPOINT);
    68          if (endpoint.endsWith(Constants.SEPARATOR)) {
    69              endpoint = endpoint.substring(0, endpoint.length() - 1);
    70          }
    71          apiClient.setBasePath(endpoint);
    72          apiClient.addDefaultHeader("X-Lakefs-Client", "lakefs-hadoopfs/" + getClass().getPackage().getImplementationVersion());
    73  
    74          String sessionId = FSConfiguration.get(conf, scheme, Constants.SESSION_ID);
    75          if (sessionId != null) {
    76              apiClient.addDefaultCookie("sessionId", sessionId);
    77          }
    78          return apiClient;
    79      }
    80  
    81      public ObjectsApi getObjectsApi() {
    82          return objectsApi;
    83      }
    84  
    85      public StagingApi getStagingApi() {
    86          return stagingApi;
    87      }
    88  
    89      public RepositoriesApi getRepositoriesApi() {
    90          return repositoriesApi;
    91      }
    92  
    93      public BranchesApi getBranchesApi() {
    94          return branchesApi;
    95      }
    96  
    97      public ConfigApi getConfigApi() {
    98          return configApi;
    99      }
   100  
   101      public InternalApi getInternalApi() {
   102          return internalApi;
   103      }
   104  }