/** * Update the server address if the address corresponding to the host * name has changed. * * @return true if an addr change was detected. * @throws IOException when the hostname cannot be resolved. */ private synchronized boolean updateAddress() throws IOException { // Do a fresh lookup with the old host name. InetSocketAddress currentAddr = NetUtils.createSocketAddrForHost( server.getHostName(), server.getPort()); if (!server.equals(currentAddr)) { LOG.warn("Address change detected. Old: " + server.toString() + " New: " + currentAddr.toString()); server = currentAddr; return true; } return false; }
/** * Returns an InetSocketAddress that a client can use to connect to the * given listening address. * * @param addr of a listener * @return socket address that a client can use to connect to the server. */ public static InetSocketAddress getConnectAddress(InetSocketAddress addr) { if (!addr.isUnresolved() && addr.getAddress().isAnyLocalAddress()) { try { addr = new InetSocketAddress(InetAddress.getLocalHost(), addr.getPort()); } catch (UnknownHostException uhe) { // shouldn't get here unless the host doesn't have a loopback iface addr = createSocketAddrForHost("127.0.0.1", addr.getPort()); } } return addr; }
/** * Set the socket address a client can use to connect for the * <code>name</code> property as a <code>host:port</code>. The wildcard * address is replaced with the local host's address. If the host and address * properties are configured the host component of the address will be combined * with the port component of the addr to generate the address. This is to allow * optional control over which host name is used in multi-home bind-host * cases where a host can have multiple names * @param hostProperty the bind-host configuration name * @param addressProperty the service address configuration name * @param defaultAddressValue the service default address configuration value * @param addr InetSocketAddress of the service listener * @return InetSocketAddress for clients to connect */ public InetSocketAddress updateConnectAddr( String hostProperty, String addressProperty, String defaultAddressValue, InetSocketAddress addr) { final String host = get(hostProperty); final String connectHostPort = getTrimmed(addressProperty, defaultAddressValue); if (host == null || host.isEmpty() || connectHostPort == null || connectHostPort.isEmpty()) { //not our case, fall back to original logic return updateConnectAddr(addressProperty, addr); } final String connectHost = connectHostPort.split(":")[0]; // Create connect address using client address hostname and server port. return updateConnectAddr(addressProperty, NetUtils.createSocketAddrForHost( connectHost, addr.getPort())); }
/** * Set the socket address a client can use to connect for the * <code>name</code> property as a <code>host:port</code>. The wildcard * address is replaced with the local host's address. If the host and address * properties are configured the host component of the address will be combined * with the port component of the addr to generate the address. This is to allow * optional control over which host name is used in multi-home bind-host * cases where a host can have multiple names * @param hostProperty the bind-host configuration name * @param addressProperty the service address configuration name * @param defaultAddressValue the service default address configuration value * @param addr InetSocketAddress of the service listener * @return InetSocketAddress for clients to connect */ public InetSocketAddress updateConnectAddr( String hostProperty, String addressProperty, String defaultAddressValue, InetSocketAddress addr) { final String host = get(hostProperty); final String connectHostPort = getTrimmed(addressProperty, defaultAddressValue); if (host == null || host.isEmpty() || connectHostPort == null || connectHostPort.isEmpty()) { //not our case, fall back to original logic return updateConnectAddr(addressProperty, addr); } final String connectHost = connectHostPort.split(":")[0]; // Create connect address using client address hostname and server port. return updateConnectAddr(addressProperty, NetUtils.createSocketAddrForHost( connectHost, addr.getPort())); }
public static RPC.Server startProtocolServer(int srvPort, int numHandlers, AtomicReference<InetSocketAddress> bindAddress, Configuration conf, BlockingService impl, Class<?> protocolClass, SecretManager<?> secretManager, PolicyProvider provider, ConfVars... aclVars) { InetSocketAddress addr = new InetSocketAddress(srvPort); RPC.Server server; try { server = createRpcServer(protocolClass, addr, conf, numHandlers, impl, secretManager, provider, aclVars); server.start(); } catch (IOException e) { LOG.error("Failed to run RPC Server on port: " + srvPort, e); throw new RuntimeException(e); } InetSocketAddress serverBindAddress = NetUtils.getConnectAddress(server); InetSocketAddress bindAddressVal = NetUtils.createSocketAddrForHost( serverBindAddress.getAddress().getCanonicalHostName(), serverBindAddress.getPort()); if (bindAddress != null) { bindAddress.set(bindAddressVal); } LOG.info("Instantiated " + protocolClass.getSimpleName() + " at " + bindAddressVal); return server; }
); return createSocketAddrForHost(host, port);
return updateConnectAddr(addressProperty, NetUtils.createSocketAddrForHost( connectHost, addr.getPort()));
protected LlapTaskUmbilicalProtocol createUmbilical(final AMNodeInfo amNodeInfo) throws IOException, InterruptedException { final InetSocketAddress address = NetUtils.createSocketAddrForHost( amNodeInfo.amNodeId.getHostname(), amNodeInfo.amNodeId.getPort()); SecurityUtil.setTokenService(amNodeInfo.jobToken, address); UserGroupInformation ugi = UserGroupInformation.createRemoteUser(amNodeInfo.umbilicalUser); ugi.addToken(amNodeInfo.jobToken); return ugi.doAs(new PrivilegedExceptionAction<LlapTaskUmbilicalProtocol>() { @Override public LlapTaskUmbilicalProtocol run() throws Exception { return RPC .getProxy(LlapTaskUmbilicalProtocol.class, LlapTaskUmbilicalProtocol.versionID, address, UserGroupInformation.getCurrentUser(), amNodeInfo.conf, amNodeInfo.socketFactory, (int) (amNodeInfo.timeout)); } }); }
nodeToken = new Token<TokenType>(token); SecurityUtil.setTokenService(nodeToken, NetUtils.createSocketAddrForHost( nodeId.getHostname(), nodeId.getPort())); ugi.addToken(nodeToken);
this.isExternalQuery = isExternalQuery; final InetSocketAddress address = NetUtils.createSocketAddrForHost(amNodeId.getHostname(), amNodeId.getPort()); SecurityUtil.setTokenService(appToken, address);
taskOwner.addToken(jobToken); final InetSocketAddress address = NetUtils.createSocketAddrForHost(request.getAmHost(), request.getAmPort()); SecurityUtil.setTokenService(jobToken, address); NetUtils.createSocketAddrForHost(request.getAmHost(), request.getAmPort()); umbilical = taskOwner.doAs(new PrivilegedExceptionAction<LlapTaskUmbilicalProtocol>() { @Override
private void addClientToAMTokenToUGI(ApplicationReport report) throws IOException { InetSocketAddress serviceAddr = NetUtils.createSocketAddrForHost(report.getHost(), report.getRpcPort()); if (UserGroupInformation.isSecurityEnabled()) { org.apache.hadoop.yarn.api.records.Token clientToAMToken = report.getClientToAMToken(); Token<ClientToAMTokenIdentifier> token = ConverterUtils.convertFromYarn(clientToAMToken, serviceAddr); UserGroupInformation.getCurrentUser().addToken(token); } }
public void testUpdateSocketAddress() throws IOException { InetSocketAddress addr = NetUtils.createSocketAddrForHost("host", 1); InetSocketAddress connectAddr = conf.updateConnectAddr("myAddress", addr); assertEquals(connectAddr.getHostName(), addr.getHostName()); addr = new InetSocketAddress(1); connectAddr = conf.updateConnectAddr("myAddress", addr); assertEquals(connectAddr.getHostName(), InetAddress.getLocalHost().getHostName()); }
public void testUpdateSocketAddress() throws IOException { InetSocketAddress addr = NetUtils.createSocketAddrForHost("host", 1); InetSocketAddress connectAddr = conf.updateConnectAddr("myAddress", addr); assertEquals(connectAddr.getHostName(), addr.getHostName()); addr = new InetSocketAddress(1); connectAddr = conf.updateConnectAddr("myAddress", addr); assertEquals(connectAddr.getHostName(), InetAddress.getLocalHost().getHostName()); }
@Test public void testGetConnectAddress() throws IOException { NetUtils.addStaticResolution("host", "127.0.0.1"); InetSocketAddress addr = NetUtils.createSocketAddrForHost("host", 1); InetSocketAddress connectAddr = NetUtils.getConnectAddress(addr); assertEquals(addr.getHostName(), connectAddr.getHostName()); addr = new InetSocketAddress(1); connectAddr = NetUtils.getConnectAddress(addr); assertEquals(InetAddress.getLocalHost().getHostName(), connectAddr.getHostName()); }
public static Token newContainerToken(NodeId nodeId, byte[] password, ContainerTokenIdentifier tokenIdentifier) { // RPC layer client expects ip:port as service for tokens InetSocketAddress addr = NetUtils.createSocketAddrForHost(nodeId.getHost(), nodeId.getPort()); // NOTE: use SecurityUtil.setTokenService if this becomes a "real" token Token containerToken = Token.newInstance(tokenIdentifier.getBytes(), ContainerTokenIdentifier.KIND.toString(), password, SecurityUtil .buildTokenService(addr).toString()); return containerToken; }
public static Token newContainerToken(NodeId nodeId, byte[] password, ContainerTokenIdentifier tokenIdentifier) { // RPC layer client expects ip:port as service for tokens InetSocketAddress addr = NetUtils.createSocketAddrForHost(nodeId.getHost(), nodeId.getPort()); // NOTE: use SecurityUtil.setTokenService if this becomes a "real" token Token containerToken = Token.newInstance(tokenIdentifier.getBytes(), ContainerTokenIdentifier.KIND.toString(), password, SecurityUtil .buildTokenService(addr).toString()); return containerToken; }
public static Token newContainerToken(NodeId nodeId, byte[] password, ContainerTokenIdentifier tokenIdentifier) { // RPC layer client expects ip:port as service for tokens InetSocketAddress addr = NetUtils.createSocketAddrForHost(nodeId.getHost(), nodeId.getPort()); // NOTE: use SecurityUtil.setTokenService if this becomes a "real" token Token containerToken = Token.newInstance(tokenIdentifier.getBytes(), ContainerTokenIdentifier.KIND.toString(), password, SecurityUtil .buildTokenService(addr).toString()); return containerToken; }
@Test public void testGoodHostsAndPorts() { InetSocketAddress compare = NetUtils.createSocketAddrForHost("localhost", 123); runGoodCases(compare, "localhost", 123); runGoodCases(compare, "localhost:", 123); runGoodCases(compare, "localhost:123", 456); }
@Test public void testGoodHostsAndPorts() { InetSocketAddress compare = NetUtils.createSocketAddrForHost("localhost", 123); runGoodCases(compare, "localhost", 123); runGoodCases(compare, "localhost:", 123); runGoodCases(compare, "localhost:123", 456); }