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