|
| 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 | +package org.apache.hadoop.hbase.security.token; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertArrayEquals; |
| 21 | +import com.nimbusds.jose.JOSEException; |
| 22 | +import com.nimbusds.jose.JWSAlgorithm; |
| 23 | +import com.nimbusds.jose.JWSHeader; |
| 24 | +import com.nimbusds.jose.JWSSigner; |
| 25 | +import com.nimbusds.jose.crypto.RSASSASigner; |
| 26 | +import com.nimbusds.jose.jwk.JWKSet; |
| 27 | +import com.nimbusds.jose.jwk.KeyUse; |
| 28 | +import com.nimbusds.jose.jwk.RSAKey; |
| 29 | +import com.nimbusds.jose.jwk.gen.RSAKeyGenerator; |
| 30 | +import com.nimbusds.jwt.JWTClaimsSet; |
| 31 | +import com.nimbusds.jwt.SignedJWT; |
| 32 | +import java.io.File; |
| 33 | +import java.io.FileOutputStream; |
| 34 | +import java.io.IOException; |
| 35 | +import java.io.OutputStream; |
| 36 | +import java.io.OutputStreamWriter; |
| 37 | +import java.time.LocalDate; |
| 38 | +import java.util.UUID; |
| 39 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 40 | +import org.apache.hadoop.hbase.TableName; |
| 41 | +import org.apache.hadoop.hbase.client.Admin; |
| 42 | +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; |
| 43 | +import org.apache.hadoop.hbase.client.Connection; |
| 44 | +import org.apache.hadoop.hbase.client.ConnectionFactory; |
| 45 | +import org.apache.hadoop.hbase.client.Get; |
| 46 | +import org.apache.hadoop.hbase.client.Put; |
| 47 | +import org.apache.hadoop.hbase.client.Result; |
| 48 | +import org.apache.hadoop.hbase.client.Table; |
| 49 | +import org.apache.hadoop.hbase.client.TableDescriptor; |
| 50 | +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; |
| 51 | +import org.apache.hadoop.hbase.security.User; |
| 52 | +import org.apache.hadoop.hbase.testclassification.MediumTests; |
| 53 | +import org.apache.hadoop.hbase.testclassification.SecurityTests; |
| 54 | +import org.apache.hadoop.hbase.util.Bytes; |
| 55 | +import org.junit.BeforeClass; |
| 56 | +import org.junit.ClassRule; |
| 57 | +import org.junit.Rule; |
| 58 | +import org.junit.Test; |
| 59 | +import org.junit.experimental.categories.Category; |
| 60 | +import org.junit.rules.TestName; |
| 61 | + |
| 62 | +@Category({ SecurityTests.class, MediumTests.class }) |
| 63 | +public class TestOAuthBearerAuthentication extends SecureTestCluster { |
| 64 | + |
| 65 | + @ClassRule |
| 66 | + public static final HBaseClassTestRule CLASS_RULE = |
| 67 | + HBaseClassTestRule.forClass(TestOAuthBearerAuthentication.class); |
| 68 | + |
| 69 | + private static final String AUDIENCE = "valid-hbase-instance"; |
| 70 | + private static final String ISSUER = "authorized-issuer"; |
| 71 | + |
| 72 | + private static RSAKey RSA; |
| 73 | + private static File JWKS_FILE; |
| 74 | + |
| 75 | + @BeforeClass |
| 76 | + public static void setUp() throws Exception { |
| 77 | + initRSA(); |
| 78 | + |
| 79 | + TEST_UTIL.getConfiguration().set("hbase.client.sasl.provider.extras", |
| 80 | + "org.apache.hadoop.hbase.security.provider.OAuthBearerSaslClientAuthenticationProvider"); |
| 81 | + TEST_UTIL.getConfiguration().set("hbase.server.sasl.provider.extras", |
| 82 | + "org.apache.hadoop.hbase.security.provider.OAuthBearerSaslServerAuthenticationProvider"); |
| 83 | + TEST_UTIL.getConfiguration().set("hbase.client.sasl.provider.class", |
| 84 | + "org.apache.hadoop.hbase.security.provider.OAuthBearerSaslProviderSelector"); |
| 85 | + TEST_UTIL.getConfiguration().set("hbase.security.oauth.jwt.jwks.file", |
| 86 | + JWKS_FILE.getAbsolutePath()); |
| 87 | + TEST_UTIL.getConfiguration().set("hbase.security.oauth.jwt.audience", AUDIENCE); |
| 88 | + TEST_UTIL.getConfiguration().set("hbase.security.oauth.jwt.issuer", ISSUER); |
| 89 | + |
| 90 | + SecureTestCluster.setUp(); |
| 91 | + } |
| 92 | + |
| 93 | + @Rule |
| 94 | + public TestName testName = new TestName(); |
| 95 | + |
| 96 | + private static void initRSA() throws JOSEException, IOException { |
| 97 | + RSA = new RSAKeyGenerator(2048) |
| 98 | + .keyUse(KeyUse.SIGNATURE) // indicate the intended use of the key |
| 99 | + .keyID(UUID.randomUUID().toString()) // give the key a unique ID |
| 100 | + .generate(); |
| 101 | + JWKSet jwkSet = new JWKSet(RSA.toPublicJWK()); |
| 102 | + JWKS_FILE = File.createTempFile("oauth_", ".jwks"); |
| 103 | + JWKS_FILE.deleteOnExit(); |
| 104 | + |
| 105 | + try (OutputStream os = new FileOutputStream(JWKS_FILE); |
| 106 | + OutputStreamWriter osw = new OutputStreamWriter(os)) { |
| 107 | + osw.write(jwkSet.toString(true)); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private String generateBase64EncodedToken(String principal) throws JOSEException { |
| 112 | + JWSSigner signer = new RSASSASigner(RSA); |
| 113 | + LocalDate now = LocalDate.now(); |
| 114 | + |
| 115 | + JWTClaimsSet claimsSet = new JWTClaimsSet.Builder() |
| 116 | + .subject(principal) |
| 117 | + .issuer(ISSUER) |
| 118 | + .audience(AUDIENCE) |
| 119 | + .expirationTime(java.sql.Date.valueOf(now.plusDays(1))) |
| 120 | + .build(); |
| 121 | + |
| 122 | + SignedJWT signedJWT = new SignedJWT( |
| 123 | + new JWSHeader.Builder(JWSAlgorithm.RS256).keyID(RSA.getKeyID()).build(), claimsSet); |
| 124 | + |
| 125 | + signedJWT.sign(signer); |
| 126 | + |
| 127 | + return signedJWT.serialize(); |
| 128 | + } |
| 129 | + |
| 130 | + private TableName getTestTableName() { |
| 131 | + return TableName.valueOf(testName.getMethodName().replaceAll("[^0-9A-Za-z]", "_")); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void testOAuthBearerLogin() throws IOException, JOSEException { |
| 136 | + TableName tableName = getTestTableName(); |
| 137 | + byte[] family = Bytes.toBytes("f"); |
| 138 | + byte[] qualifier = Bytes.toBytes("q"); |
| 139 | + byte[] row = Bytes.toBytes("row"); |
| 140 | + byte[] value = Bytes.toBytes("data"); |
| 141 | + |
| 142 | + User user = User.createUserForTesting(TEST_UTIL.getConfiguration(), "testuser_jwt", |
| 143 | + new String[] {}); |
| 144 | + OAuthBearerTokenUtil.addTokenForUser(user, generateBase64EncodedToken(user.getName()), 0); |
| 145 | + |
| 146 | + try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration(), user)) { |
| 147 | + Admin admin = conn.getAdmin(); |
| 148 | + TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName) |
| 149 | + .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).build(); |
| 150 | + admin.createTable(tableDescriptor); |
| 151 | + try (Table table = conn.getTable(tableName)) { |
| 152 | + table.put(new Put(row).addColumn(family, qualifier, value)); |
| 153 | + Result result = table.get(new Get(row)); |
| 154 | + assertArrayEquals(value, result.getValue(family, qualifier)); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments