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.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 HBaseTestingUtility TEST_UTIL;
053
054  private static class MockAbortable implements Abortable {
055    private boolean abort;
056
057    @Override
058    public void abort(String reason, Throwable e) {
059      LOG.info("Aborting: " + reason, e);
060      abort = true;
061    }
062
063    @Override
064    public boolean isAborted() {
065      return abort;
066    }
067  }
068
069  @BeforeClass
070  public static void setupBeforeClass() throws Exception {
071    TEST_UTIL = new HBaseTestingUtility();
072    TEST_UTIL.startMiniZKCluster();
073  }
074
075  @AfterClass
076  public static void tearDownAfterClass() throws Exception {
077    TEST_UTIL.shutdownMiniZKCluster();
078  }
079
080  private static ZKWatcher newZK(Configuration conf, String name, Abortable abort)
081    throws Exception {
082    Configuration copy = HBaseConfiguration.create(conf);
083    ZKWatcher zk = new ZKWatcher(copy, name, abort);
084    return zk;
085  }
086
087  @Test
088  public void testRefreshKeys() throws Exception {
089    Configuration conf = TEST_UTIL.getConfiguration();
090    ZKWatcher zk = newZK(conf, "127.0.0.1", new MockAbortable());
091    AuthenticationTokenSecretManager keyManager =
092      new AuthenticationTokenSecretManager(conf, zk, "127.0.0.1", 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 =
098        new AuthenticationKey(key, EnvironmentEdgeManager.currentTime() + 600 * 1000, null);
099      ZKUtil.createWithParents(zk,
100        ZNodePaths.joinZNode(watcher.getKeysParentZNode(), key.toString()), 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}