001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.hadoop.hdfs.security.token.block;
019
020 import java.io.IOException;
021 import java.util.EnumSet;
022 import java.util.HashMap;
023 import java.util.Map;
024
025 import org.apache.hadoop.hdfs.protocol.ExtendedBlock;
026 import org.apache.hadoop.hdfs.security.token.block.BlockTokenSecretManager.AccessMode;
027 import org.apache.hadoop.security.token.SecretManager;
028 import org.apache.hadoop.security.token.Token;
029
030 /**
031 * Manages a {@link BlockTokenSecretManager} per block pool. Routes the requests
032 * given a block pool Id to corresponding {@link BlockTokenSecretManager}
033 */
034 public class BlockPoolTokenSecretManager extends
035 SecretManager<BlockTokenIdentifier> {
036
037 private final Map<String, BlockTokenSecretManager> map =
038 new HashMap<String, BlockTokenSecretManager>();
039
040 /**
041 * Add a block pool Id and corresponding {@link BlockTokenSecretManager} to map
042 * @param bpid block pool Id
043 * @param secretMgr {@link BlockTokenSecretManager}
044 */
045 public synchronized void addBlockPool(String bpid,
046 BlockTokenSecretManager secretMgr) {
047 map.put(bpid, secretMgr);
048 }
049
050 synchronized BlockTokenSecretManager get(String bpid) {
051 BlockTokenSecretManager secretMgr = map.get(bpid);
052 if (secretMgr == null) {
053 throw new IllegalArgumentException("Block pool " + bpid
054 + " is not found");
055 }
056 return secretMgr;
057 }
058
059 public synchronized boolean isBlockPoolRegistered(String bpid) {
060 return map.containsKey(bpid);
061 }
062
063 /** Return an empty BlockTokenIdentifer */
064 @Override
065 public BlockTokenIdentifier createIdentifier() {
066 return new BlockTokenIdentifier();
067 }
068
069 @Override
070 public byte[] createPassword(BlockTokenIdentifier identifier) {
071 return get(identifier.getBlockPoolId()).createPassword(identifier);
072 }
073
074 @Override
075 public byte[] retrievePassword(BlockTokenIdentifier identifier)
076 throws InvalidToken {
077 return get(identifier.getBlockPoolId()).retrievePassword(identifier);
078 }
079
080 /**
081 * See {@link BlockTokenSecretManager#checkAccess(BlockTokenIdentifier,
082 * String, ExtendedBlock, AccessMode)}
083 */
084 public void checkAccess(BlockTokenIdentifier id, String userId,
085 ExtendedBlock block, AccessMode mode) throws InvalidToken {
086 get(block.getBlockPoolId()).checkAccess(id, userId, block, mode);
087 }
088
089 /**
090 * See {@link BlockTokenSecretManager#checkAccess(Token, String,
091 * ExtendedBlock, AccessMode)}
092 */
093 public void checkAccess(Token<BlockTokenIdentifier> token,
094 String userId, ExtendedBlock block, AccessMode mode) throws InvalidToken {
095 get(block.getBlockPoolId()).checkAccess(token, userId, block, mode);
096 }
097
098 /**
099 * See {@link BlockTokenSecretManager#setKeys(ExportedBlockKeys)}
100 */
101 public void setKeys(String bpid, ExportedBlockKeys exportedKeys)
102 throws IOException {
103 get(bpid).setKeys(exportedKeys);
104 }
105
106 /**
107 * See {@link BlockTokenSecretManager#generateToken(ExtendedBlock, EnumSet)}
108 */
109 public Token<BlockTokenIdentifier> generateToken(ExtendedBlock b,
110 EnumSet<AccessMode> of) throws IOException {
111 return get(b.getBlockPoolId()).generateToken(b, of);
112 }
113 }