Skip to content

Commit

Permalink
YARN-11225. Fix CheckStyle.
Browse files Browse the repository at this point in the history
  • Loading branch information
slfan1989 committed Dec 15, 2022
1 parent 38dca6b commit d03e5ae
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import org.apache.hadoop.classification.InterfaceAudience.Public;
import org.apache.hadoop.classification.InterfaceStability.Unstable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.security.token.Token;
import org.apache.hadoop.util.ReflectionUtils;
import org.apache.hadoop.util.StringUtils;
import org.apache.hadoop.yarn.exceptions.YarnException;
import org.apache.hadoop.yarn.exceptions.YarnRuntimeException;
import org.apache.hadoop.yarn.security.client.RMDelegationTokenIdentifier;
import org.apache.hadoop.yarn.api.protocolrecords.GetDelegationTokenResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public long getRenewDate(RMDelegationTokenIdentifier ident)
}
return info.getRenewDate();
}

@Override
protected synchronized int incrementDelegationTokenSeqNum() {
return federationFacade.incrementDelegationTokenSeqNum();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1632,25 +1632,55 @@ private Response createDelegationToken(DelegationToken dtoken, UserGroupInformat
return this.getRouterClientRMService().getDelegationToken(createReq);
});

org.apache.hadoop.yarn.api.records.Token token = resp.getRMDelegationToken();
byte[] identifier = token.getIdentifier().array();
byte[] password = token.getPassword().array();
Text kind = new Text(token.getKind());
Text service = new Text(token.getService());
Token<RMDelegationTokenIdentifier> tk = new Token<>(identifier, password, kind, service);
DelegationToken respToken = getDelegationToken(renewer, resp);
return Response.status(Status.OK).entity(respToken).build();
}

/**
* Get DelegationToken.
*
* @param renewer renewer.
* @param resp GetDelegationTokenResponse.
* @return DelegationToken.
* @throws IOException if there are I/O errors.
*/
private DelegationToken getDelegationToken(String renewer, GetDelegationTokenResponse resp)
throws IOException {
// Step1. Parse token from GetDelegationTokenResponse.
Token<RMDelegationTokenIdentifier> tk = getToken(resp);
String tokenKind = tk.getKind().toString();
RMDelegationTokenIdentifier tokenIdentifier = tk.decodeIdentifier();
String owner = tokenIdentifier.getOwner().toString();
long maxDate = tokenIdentifier.getMaxDate();

// Step2. Call the interface to get the expiration time of Token.
RouterClientRMService clientRMService = this.getRouterClientRMService();
RouterDelegationTokenSecretManager tokenSecretManager =
clientRMService.getRouterDTSecretManager();
long currentExpiration = tokenSecretManager.getRenewDate(tokenIdentifier);

String owner = tokenIdentifier.getOwner().toString();
String tokenKind = tk.getKind().toString();
long maxDate = tokenIdentifier.getMaxDate();
DelegationToken respToken = new DelegationToken(tk.encodeToUrlString(),
// Step3. Generate Delegation token.
DelegationToken delegationToken = new DelegationToken(tk.encodeToUrlString(),
renewer, owner, tokenKind, currentExpiration, maxDate);
return Response.status(Status.OK).entity(respToken).build();

return delegationToken;
}

/**
* GetToken.
* We convert RMDelegationToken in GetDelegationTokenResponse to Token.
*
* @param resp GetDelegationTokenResponse.
* @return Token.
*/
private static Token<RMDelegationTokenIdentifier> getToken(GetDelegationTokenResponse resp) {
org.apache.hadoop.yarn.api.records.Token token = resp.getRMDelegationToken();
byte[] identifier = token.getIdentifier().array();
byte[] password = token.getPassword().array();
Text kind = new Text(token.getKind());
Text service = new Text(token.getService());
Token<RMDelegationTokenIdentifier> tk = new Token<>(identifier, password, kind, service);
return tk;
}

/**
Expand All @@ -1675,12 +1705,7 @@ public Response postDelegationTokenExpiration(HttpServletRequest hsr)
// get Caller UserGroupInformation
Configuration conf = federationFacade.getConf();
UserGroupInformation callerUGI = getKerberosUserGroupInformation(conf, hsr);

// renew Delegation Token
DelegationToken requestToken = new DelegationToken();
String token = extractToken(hsr).encodeToUrlString();
requestToken.setToken(token);
return renewDelegationToken(requestToken, callerUGI);
return renewDelegationToken(hsr, callerUGI);
} catch (YarnException e) {
LOG.error("Renew delegation token request failed.", e);
return Response.status(Status.FORBIDDEN).entity(e.getMessage()).build();
Expand All @@ -1690,14 +1715,20 @@ public Response postDelegationTokenExpiration(HttpServletRequest hsr)
/**
* Renew DelegationToken.
*
* @param tokenData DelegationToken.
* @param hsr HttpServletRequest.
* @param callerUGI UserGroupInformation.
* @return Response
* @throws IOException if there are I/O errors.
* @throws InterruptedException if any thread has interrupted.
*/
private Response renewDelegationToken(DelegationToken tokenData, UserGroupInformation callerUGI)
private Response renewDelegationToken(HttpServletRequest hsr, UserGroupInformation callerUGI)
throws IOException, InterruptedException {

// renew Delegation Token
DelegationToken tokenData = new DelegationToken();
String encodeToken = extractToken(hsr).encodeToUrlString();
tokenData.setToken(encodeToken);

// Parse token data
Token<RMDelegationTokenIdentifier> token = extractToken(tokenData.getToken());
org.apache.hadoop.yarn.api.records.Token dToken =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public abstract class BaseRouterWebServicesTest {
private RouterWebServices routerWebService;

@Before
public void setUp() throws IOException {
public void setUp() throws IOException, YarnException {
this.conf = createConfiguration();

router = spy(new Router());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public class TestFederationInterceptorREST extends BaseRouterWebServicesTest {
private static final String TEST_RENEWER = "test-renewer";

@Override
public void setUp() throws IOException {
public void setUp() throws IOException, YarnException {
super.setUpConfig();
interceptor = new TestableFederationInterceptorREST();

Expand All @@ -161,15 +161,10 @@ public void setUp() throws IOException {

subClusters = new ArrayList<>();

try {
for (int i = 0; i < NUM_SUBCLUSTER; i++) {
SubClusterId sc = SubClusterId.newInstance(Integer.toString(i));
stateStoreUtil.registerSubCluster(sc);
subClusters.add(sc);
}
} catch (YarnException e) {
LOG.error(e.getMessage());
Assert.fail();
for (int i = 0; i < NUM_SUBCLUSTER; i++) {
SubClusterId sc = SubClusterId.newInstance(Integer.toString(i));
stateStoreUtil.registerSubCluster(sc);
subClusters.add(sc);
}

RouterClientRMService routerClientRMService = new RouterClientRMService();
Expand Down

0 comments on commit d03e5ae

Please sign in to comment.