|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * <p> |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * <p> |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.ozone.om.request.security; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | + |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +import com.google.common.base.Optional; |
| 27 | +import org.apache.hadoop.ozone.om.OMMetadataManager; |
| 28 | +import org.apache.hadoop.ozone.om.OzoneManager; |
| 29 | +import org.apache.hadoop.ozone.om.ratis.utils.OzoneManagerDoubleBufferHelper; |
| 30 | +import org.apache.hadoop.ozone.om.request.OMClientRequest; |
| 31 | +import org.apache.hadoop.ozone.om.response.OMClientResponse; |
| 32 | +import org.apache.hadoop.ozone.om.response.security.OMRenewDelegationTokenResponse; |
| 33 | +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos; |
| 34 | +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest; |
| 35 | +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMResponse; |
| 36 | +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RenewDelegationTokenResponseProto; |
| 37 | +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.UpdateRenewDelegationTokenRequest; |
| 38 | +import org.apache.hadoop.ozone.protocolPB.OMPBHelper; |
| 39 | +import org.apache.hadoop.ozone.security.OzoneTokenIdentifier; |
| 40 | +import org.apache.hadoop.security.proto.SecurityProtos.RenewDelegationTokenRequestProto; |
| 41 | +import org.apache.hadoop.security.token.Token; |
| 42 | +import org.apache.hadoop.utils.db.cache.CacheKey; |
| 43 | +import org.apache.hadoop.utils.db.cache.CacheValue; |
| 44 | + |
| 45 | +/** |
| 46 | + * Handle RenewDelegationToken Request. |
| 47 | + */ |
| 48 | +public class OMRenewDelegationTokenRequest extends OMClientRequest { |
| 49 | + |
| 50 | + private static final Logger LOG = |
| 51 | + LoggerFactory.getLogger(OMRenewDelegationTokenRequest.class); |
| 52 | + |
| 53 | + public OMRenewDelegationTokenRequest(OMRequest omRequest) { |
| 54 | + super(omRequest); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public OMRequest preExecute(OzoneManager ozoneManager) throws IOException { |
| 59 | + RenewDelegationTokenRequestProto renewDelegationTokenRequest = |
| 60 | + getOmRequest().getRenewDelegationTokenRequest(); |
| 61 | + |
| 62 | + // Call OM to renew token |
| 63 | + long renewTime = ozoneManager.renewDelegationToken( |
| 64 | + OMPBHelper.convertToDelegationToken( |
| 65 | + renewDelegationTokenRequest.getToken())); |
| 66 | + |
| 67 | + RenewDelegationTokenResponseProto.Builder renewResponse = |
| 68 | + RenewDelegationTokenResponseProto.newBuilder(); |
| 69 | + |
| 70 | + renewResponse.setResponse(org.apache.hadoop.security.proto.SecurityProtos |
| 71 | + .RenewDelegationTokenResponseProto.newBuilder() |
| 72 | + .setNewExpiryTime(renewTime)); |
| 73 | + |
| 74 | + |
| 75 | + // Client issues RenewDelegationToken request, when received by OM leader |
| 76 | + // it will renew the token. Original RenewDelegationToken request is |
| 77 | + // converted to UpdateRenewDelegationToken request with the token and renew |
| 78 | + // information. This updated request will be submitted to Ratis. In this |
| 79 | + // way delegation token renewd by leader, will be replicated across all |
| 80 | + // OMs. With this approach, original RenewDelegationToken request from |
| 81 | + // client does not need any proto changes. |
| 82 | + |
| 83 | + // Create UpdateRenewDelegationTokenRequest with original request and |
| 84 | + // expiry time. |
| 85 | + OMRequest.Builder omRequest = OMRequest.newBuilder() |
| 86 | + .setUserInfo(getUserInfo()) |
| 87 | + .setUpdatedRenewDelegationTokenRequest( |
| 88 | + UpdateRenewDelegationTokenRequest.newBuilder() |
| 89 | + .setRenewDelegationTokenRequest(renewDelegationTokenRequest) |
| 90 | + .setRenewDelegationTokenResponse(renewResponse)) |
| 91 | + .setCmdType(getOmRequest().getCmdType()) |
| 92 | + .setClientId(getOmRequest().getClientId()); |
| 93 | + |
| 94 | + if (getOmRequest().hasTraceID()) { |
| 95 | + omRequest.setTraceID(getOmRequest().getTraceID()); |
| 96 | + } |
| 97 | + |
| 98 | + return omRequest.build(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public OMClientResponse validateAndUpdateCache(OzoneManager ozoneManager, |
| 103 | + long transactionLogIndex, |
| 104 | + OzoneManagerDoubleBufferHelper ozoneManagerDoubleBufferHelper) { |
| 105 | + |
| 106 | + UpdateRenewDelegationTokenRequest updateRenewDelegationTokenRequest = |
| 107 | + getOmRequest().getUpdatedRenewDelegationTokenRequest(); |
| 108 | + |
| 109 | + Token<OzoneTokenIdentifier> ozoneTokenIdentifierToken = |
| 110 | + OMPBHelper.convertToDelegationToken(updateRenewDelegationTokenRequest |
| 111 | + .getRenewDelegationTokenRequest().getToken()); |
| 112 | + |
| 113 | + long renewTime = updateRenewDelegationTokenRequest |
| 114 | + .getRenewDelegationTokenResponse().getResponse().getNewExpiryTime(); |
| 115 | + |
| 116 | + OMMetadataManager omMetadataManager = ozoneManager.getMetadataManager(); |
| 117 | + |
| 118 | + OMClientResponse omClientResponse = null; |
| 119 | + OMResponse.Builder omResponse = |
| 120 | + OMResponse.newBuilder() |
| 121 | + .setCmdType(OzoneManagerProtocolProtos.Type.RenewDelegationToken) |
| 122 | + .setStatus(OzoneManagerProtocolProtos.Status.OK) |
| 123 | + .setSuccess(true); |
| 124 | + try { |
| 125 | + |
| 126 | + OzoneTokenIdentifier ozoneTokenIdentifier = |
| 127 | + ozoneTokenIdentifierToken.decodeIdentifier(); |
| 128 | + |
| 129 | + // Update in memory map of token. |
| 130 | + ozoneManager.getDelegationTokenMgr() |
| 131 | + .updateRenewToken(ozoneTokenIdentifierToken, ozoneTokenIdentifier, |
| 132 | + renewTime); |
| 133 | + |
| 134 | + // Update Cache. |
| 135 | + omMetadataManager.getDelegationTokenTable().addCacheEntry( |
| 136 | + new CacheKey<>(ozoneTokenIdentifier), |
| 137 | + new CacheValue<>(Optional.of(renewTime), transactionLogIndex)); |
| 138 | + |
| 139 | + omClientResponse = |
| 140 | + new OMRenewDelegationTokenResponse(ozoneTokenIdentifier, renewTime, |
| 141 | + omResponse.setRenewDelegationTokenResponse( |
| 142 | + updateRenewDelegationTokenRequest |
| 143 | + .getRenewDelegationTokenResponse()).build()); |
| 144 | + } catch (IOException ex) { |
| 145 | + LOG.error("Error in Updating Renew DelegationToken {}", |
| 146 | + ozoneTokenIdentifierToken, ex); |
| 147 | + omClientResponse = new OMRenewDelegationTokenResponse(null, -1L, |
| 148 | + createErrorOMResponse(omResponse, ex)); |
| 149 | + } finally { |
| 150 | + if (omClientResponse != null) { |
| 151 | + omClientResponse.setFlushFuture( |
| 152 | + ozoneManagerDoubleBufferHelper.add(omClientResponse, |
| 153 | + transactionLogIndex)); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + if (LOG.isDebugEnabled()) { |
| 158 | + LOG.debug("Updated renew delegation token in-memory map: {} with expiry" + |
| 159 | + " time {}", ozoneTokenIdentifierToken, renewTime); |
| 160 | + } |
| 161 | + |
| 162 | + return omClientResponse; |
| 163 | + } |
| 164 | +} |
0 commit comments