/** * Creates secure {@link HBaseMetadataService} which delegates to {@link Admin} instantiated with default * {@link HBaseConfiguration} and {@code hbase-site.xml} properties overridden with the config * for the cluster imported in the service pool (either manually or using Ambari) */ public static HBaseMetadataService newInstance(EnvironmentService environmentService, Long clusterId, SecurityContext securityContext, Subject subject) throws IOException, EntityNotFoundException { return newInstance( overrideConfig(HBaseConfiguration.create(), environmentService, clusterId), securityContext, subject, getHbaseMasterComponent(environmentService, clusterId), getHbaseMasterComponentProcesses(environmentService, clusterId)); }
private <T, E extends Exception> T executeSecure(SupplierException<T, E> action) throws PrivilegedActionException, E, IOException, InterruptedException { return execute(action, securityContext, user); }
/** * @return All namespaces */ public HBaseNamespaces getHBaseNamespaces() throws IOException, PrivilegedActionException, InterruptedException { final HBaseNamespaces namespaces = HBaseNamespaces.newInstance( executeSecure(() -> hBaseAdmin.listNamespaceDescriptors()), securityContext, true, getPrincipals(), getKeytabs()); LOG.debug("HBase namespaces {}", namespaces); return namespaces; }
@Override public Map<String, Object> getHintsOnCluster(Cluster cluster, SecurityContext securityContext, Subject subject) { Map<String, Object> hintMap = new HashMap<>(); try (HBaseMetadataService hBaseMetadataService = HBaseMetadataService.newInstance(environmentService, cluster.getId(), securityContext, subject)) { hintMap.put(FIELD_NAME_TABLE, hBaseMetadataService.getHBaseTables().getTables()); } catch (ServiceNotFoundException e) { // we access it from mapping information so shouldn't be here throw new IllegalStateException("Service " + Constants.HBase.SERVICE_NAME + " in cluster " + cluster.getName() + " not found but mapping information exists."); } catch (ServiceConfigurationNotFoundException e) { // there's HBASE service but not enough configuration info. } catch (Exception e) { throw new RuntimeException(e); } return hintMap; }
@GET @Path("/clusters/{clusterId}/services/hbase/namespaces") @Timed public Response getNamespacesByClusterId(@PathParam("clusterId") Long clusterId, @Context SecurityContext securityContext) throws Exception { SecurityUtil.checkPermissions(authorizer, securityContext, Cluster.NAMESPACE, clusterId, READ); try (HBaseMetadataService hbaseMetadataService = HBaseMetadataService .newInstance(environmentService, clusterId, securityContext, subject)) { return WSUtils.respondEntity(hbaseMetadataService.getHBaseNamespaces(), OK); } catch (EntityNotFoundException ex) { throw com.hortonworks.streamline.common.exception.service.exception.request.EntityNotFoundException.byId(ex.getMessage()); } }
@GET @Path("/clusters/{clusterId}/services/hbase/tables") @Timed public Response getTablesByClusterId(@PathParam("clusterId") Long clusterId, @Context SecurityContext securityContext) throws Exception { SecurityUtil.checkPermissions(authorizer, securityContext, Cluster.NAMESPACE, clusterId, READ); try (HBaseMetadataService hbaseMetadataService = HBaseMetadataService .newInstance(environmentService, clusterId, securityContext, subject)) { return WSUtils.respondEntity(Subject.doAs(subject, (PrivilegedExceptionAction<Tables>) hbaseMetadataService::getHBaseTables), OK); } catch (EntityNotFoundException ex) { throw com.hortonworks.streamline.common.exception.service.exception.request.EntityNotFoundException.byId(ex.getMessage()); } }
@Override public void close() throws Exception { executeSecure(() -> { final Connection connection = hBaseAdmin.getConnection(); hBaseAdmin.close(); connection.close(); return null; }); }
private static Configuration overrideConfig(Configuration hbaseConfig, EnvironmentService environmentService, Long clusterId) throws IOException, ServiceConfigurationNotFoundException, ServiceNotFoundException { return OverrideHadoopConfiguration.override(environmentService, clusterId, ServiceConfigurations.HBASE, AMBARI_JSON_CONFIG_HBASE_SITE, hbaseConfig, getMaxHBaseClientRetries()); }
/** * Creates secure {@link HBaseMetadataService} which delegates to {@link Admin} * instantiated with with the {@link Configuration} provided using the first parameter */ public static HBaseMetadataService newInstance(Configuration hbaseConfig, SecurityContext securityContext, Subject subject, Component hbaseMaster, Collection<ComponentProcess> hbaseMasterProcesses) throws IOException, EntityNotFoundException { if (SecurityUtil.isKerberosAuthenticated(securityContext)) { UserGroupInformation.setConfiguration(hbaseConfig); // Sets Kerberos rules final UserGroupInformation ugiFromSubject = UserGroupInformation.getUGIFromSubject(subject); // Adds User principal to the subject final UserGroupInformation proxyUserForImpersonation = UserGroupInformation .createProxyUser(securityContext.getUserPrincipal().getName(), ugiFromSubject); final User user = User.create(proxyUserForImpersonation); return new HBaseMetadataService(ConnectionFactory.createConnection(hbaseConfig, user) .getAdmin(), securityContext, subject, user, hbaseMaster, hbaseMasterProcesses); } else { return new HBaseMetadataService(ConnectionFactory.createConnection(hbaseConfig).getAdmin(), securityContext, subject, null, hbaseMaster, hbaseMasterProcesses); } }
@GET @Path("/clusters/{clusterId}/services/hbase/namespaces/{namespace}/tables") @Timed public Response getNamespaceTablesByClusterId(@PathParam("clusterId") Long clusterId, @PathParam("namespace") String namespace, @Context SecurityContext securityContext) throws Exception { SecurityUtil.checkPermissions(authorizer, securityContext, Cluster.NAMESPACE, clusterId, READ); try (HBaseMetadataService hbaseMetadataService = HBaseMetadataService .newInstance(environmentService, clusterId, securityContext, subject)) { return WSUtils.respondEntity(Subject.doAs(subject, (PrivilegedExceptionAction<Tables>)() -> hbaseMetadataService.getHBaseTables(namespace)), OK); } catch (EntityNotFoundException ex) { throw com.hortonworks.streamline.common.exception.service.exception.request.EntityNotFoundException.byId(ex.getMessage()); } } }
public Keytabs getKeytabs() throws InterruptedException, IOException, PrivilegedActionException { return executeSecure(() -> Keytabs.fromServiceProperties(hBaseAdmin.getConfiguration() .getValByRegex(PROP_HBASE_MASTER_KEYTAB_FILE))); }
/** * @return All tables for all namespaces */ public Tables getHBaseTables() throws Exception { final TableName[] tableNames = executeSecure(() -> hBaseAdmin.listTableNames()); LOG.debug("HBase tables {}", Arrays.toString(tableNames)); return Tables.newInstance(tableNames, securityContext, true, getPrincipals(), getKeytabs()); }
public Principals getPrincipals() throws InterruptedException, IOException, PrivilegedActionException { return executeSecure(() -> Principals.fromServiceProperties(hBaseAdmin.getConfiguration() .getValByRegex(PROP_HBASE_MASTER_KERBEROS_PRINCIPAL), new Pair<>(hbaseMaster, hbaseMasterProcesses))); }
/** * @param namespace Namespace for which to get table names * @return All tables for the namespace given as parameter */ public Tables getHBaseTables(final String namespace) throws IOException, PrivilegedActionException, InterruptedException { final TableName[] tableNames = executeSecure(() -> hBaseAdmin.listTableNamesByNamespace(namespace)); LOG.debug("HBase namespace [{}] has tables {}", namespace, Arrays.toString(tableNames)); return Tables.newInstance(tableNames, securityContext, true, getPrincipals(), getKeytabs()); }