View Javadoc

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    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.hbase.security.token;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.Abortable;
25  import org.apache.hadoop.hbase.HBaseConfiguration;
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.apache.hadoop.hbase.util.Writables;
29  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
30  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
31  import org.junit.AfterClass;
32  import org.junit.Assert;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  /**
38   * Test the refreshKeys in ZKSecretWatcher
39   */
40  @Category({ SmallTests.class })
41  public class TestZKSecretWatcherRefreshKeys {
42    private static final Log LOG = LogFactory.getLog(TestZKSecretWatcherRefreshKeys.class);
43    private static HBaseTestingUtility TEST_UTIL;
44  
45    private static class MockAbortable implements Abortable {
46      private boolean abort;
47      public void abort(String reason, Throwable e) {
48        LOG.info("Aborting: "+reason, e);
49        abort = true;
50      }
51  
52      public boolean isAborted() {
53        return abort;
54      }
55    }
56    
57    @BeforeClass
58    public static void setupBeforeClass() throws Exception {
59      TEST_UTIL = new HBaseTestingUtility();
60      TEST_UTIL.startMiniZKCluster();
61    }
62  
63    @AfterClass
64    public static void tearDownAfterClass() throws Exception {
65      TEST_UTIL.shutdownMiniZKCluster();
66    }
67  
68    private static ZooKeeperWatcher newZK(Configuration conf, String name,
69        Abortable abort) throws Exception {
70      Configuration copy = HBaseConfiguration.create(conf);
71      ZooKeeperWatcher zk = new ZooKeeperWatcher(copy, name, abort);
72      return zk;
73    }
74  
75    @Test
76    public void testRefreshKeys() throws Exception {
77      Configuration conf = TEST_UTIL.getConfiguration();
78      ZooKeeperWatcher zk = newZK(conf, "127.0.0.1", new MockAbortable());
79      AuthenticationTokenSecretManager keyManager = 
80          new AuthenticationTokenSecretManager(conf, zk, "127.0.0.1", 
81              60 * 60 * 1000, 60 * 1000);
82      ZKSecretWatcher watcher = new ZKSecretWatcher(conf, zk, keyManager);
83      ZKUtil.deleteChildrenRecursively(zk, watcher.getKeysParentZNode());
84      Integer[] keys = { 1, 2, 3, 4, 5, 6 };
85      for (Integer key : keys) {
86        AuthenticationKey ak = new AuthenticationKey(key,
87            System.currentTimeMillis() + 600 * 1000, null);
88        ZKUtil.createWithParents(zk,
89            ZKUtil.joinZNode(watcher.getKeysParentZNode(), key.toString()),
90            Writables.getBytes(ak));
91      }
92      Assert.assertNull(keyManager.getCurrentKey());
93      watcher.refreshKeys();
94      for (Integer key : keys) {
95        Assert.assertNotNull(keyManager.getKey(key.intValue()));
96      }
97    }
98  }