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 */
018package org.apache.hadoop.hbase.security.token;
019
020import org.apache.hadoop.conf.Configuration;
021import org.apache.hadoop.hbase.Abortable;
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.HBaseConfiguration;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.testclassification.SecurityTests;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
028import org.apache.hadoop.hbase.util.Writables;
029import org.apache.hadoop.hbase.zookeeper.ZKUtil;
030import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
031import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
032import org.junit.AfterClass;
033import org.junit.Assert;
034import org.junit.BeforeClass;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041/**
042 * Test the refreshKeys in ZKSecretWatcher
043 */
044@Category({ SecurityTests.class, SmallTests.class })
045public class TestZKSecretWatcherRefreshKeys {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049      HBaseClassTestRule.forClass(TestZKSecretWatcherRefreshKeys.class);
050
051  private static final Logger LOG = LoggerFactory.getLogger(TestZKSecretWatcherRefreshKeys.class);
052  private static HBaseTestingUtil TEST_UTIL;
053
054  private static class MockAbortable implements Abortable {
055    private boolean abort;
056    @Override
057    public void abort(String reason, Throwable e) {
058      LOG.info("Aborting: "+reason, e);
059      abort = true;
060    }
061
062    @Override
063    public boolean isAborted() {
064      return abort;
065    }
066  }
067
068  @BeforeClass
069  public static void setupBeforeClass() throws Exception {
070    TEST_UTIL = new HBaseTestingUtil();
071    TEST_UTIL.startMiniZKCluster();
072  }
073
074  @AfterClass
075  public static void tearDownAfterClass() throws Exception {
076    TEST_UTIL.shutdownMiniZKCluster();
077  }
078
079  private static ZKWatcher newZK(Configuration conf, String name,
080                                 Abortable abort) throws Exception {
081    Configuration copy = HBaseConfiguration.create(conf);
082    ZKWatcher zk = new ZKWatcher(copy, name, abort);
083    return zk;
084  }
085
086  @Test
087  public void testRefreshKeys() throws Exception {
088    Configuration conf = TEST_UTIL.getConfiguration();
089    ZKWatcher zk = newZK(conf, "127.0.0.1", new MockAbortable());
090    AuthenticationTokenSecretManager keyManager =
091        new AuthenticationTokenSecretManager(conf, zk, "127.0.0.1",
092            60 * 60 * 1000, 60 * 1000);
093    ZKSecretWatcher watcher = new ZKSecretWatcher(conf, zk, keyManager);
094    ZKUtil.deleteChildrenRecursively(zk, watcher.getKeysParentZNode());
095    Integer[] keys = { 1, 2, 3, 4, 5, 6 };
096    for (Integer key : keys) {
097      AuthenticationKey ak = new AuthenticationKey(key,
098        EnvironmentEdgeManager.currentTime() + 600 * 1000, null);
099      ZKUtil.createWithParents(zk,
100        ZNodePaths.joinZNode(watcher.getKeysParentZNode(), key.toString()),
101        Writables.getBytes(ak));
102    }
103    Assert.assertNull(keyManager.getCurrentKey());
104    watcher.refreshKeys();
105    for (Integer key : keys) {
106      Assert.assertNotNull(keyManager.getKey(key.intValue()));
107    }
108  }
109}