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