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.rest;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertNotNull;
023
024import java.io.ByteArrayInputStream;
025import java.io.IOException;
026import java.io.StringWriter;
027import javax.xml.bind.JAXBContext;
028import javax.xml.bind.JAXBException;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.hbase.HBaseTestingUtil;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.Admin;
033import org.apache.hadoop.hbase.rest.client.Client;
034import org.apache.hadoop.hbase.rest.client.Cluster;
035import org.apache.hadoop.hbase.rest.client.Response;
036import org.apache.hadoop.hbase.rest.model.ColumnSchemaModel;
037import org.apache.hadoop.hbase.rest.model.TableSchemaModel;
038import org.apache.hadoop.hbase.rest.model.TestTableSchemaModel;
039import org.apache.hadoop.hbase.testclassification.MediumTests;
040import org.apache.hadoop.hbase.testclassification.RestTests;
041import org.apache.hadoop.hbase.util.Bytes;
042import org.apache.http.Header;
043import org.apache.http.message.BasicHeader;
044import org.junit.jupiter.api.AfterAll;
045import org.junit.jupiter.api.AfterEach;
046import org.junit.jupiter.api.Tag;
047import org.junit.jupiter.api.Test;
048
049@Tag(RestTests.TAG)
050@Tag(MediumTests.TAG)
051public class SchemaResourceTestBase {
052
053  private static String TABLE1 = "TestSchemaResource1";
054  private static String TABLE2 = "TestSchemaResource2";
055
056  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
057  private static final HBaseRESTTestingUtility REST_TEST_UTIL = new HBaseRESTTestingUtility();
058  private static Client client;
059  private static JAXBContext context;
060  private static Configuration conf;
061  private static TestTableSchemaModel testTableSchemaModel;
062  private static Header extraHdr = null;
063
064  protected static boolean csrfEnabled = true;
065
066  protected static void initialize() throws Exception {
067    conf = TEST_UTIL.getConfiguration();
068    conf.setBoolean(RESTServer.REST_CSRF_ENABLED_KEY, csrfEnabled);
069    if (csrfEnabled) {
070      conf.set(RESTServer.REST_CSRF_BROWSER_USERAGENTS_REGEX_KEY, ".*");
071    }
072    extraHdr = new BasicHeader(RESTServer.REST_CSRF_CUSTOM_HEADER_DEFAULT, "");
073    TEST_UTIL.startMiniCluster();
074    REST_TEST_UTIL.startServletContainer(conf);
075    client = new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort()));
076    testTableSchemaModel = new TestTableSchemaModel();
077    context = JAXBContext.newInstance(ColumnSchemaModel.class, TableSchemaModel.class);
078  }
079
080  @AfterAll
081  public static void tearDownAfterClass() throws Exception {
082    REST_TEST_UTIL.shutdownServletContainer();
083    TEST_UTIL.shutdownMiniCluster();
084  }
085
086  @AfterEach
087  public void tearDown() throws Exception {
088    Admin admin = TEST_UTIL.getAdmin();
089
090    for (String table : new String[] { TABLE1, TABLE2 }) {
091      TableName t = TableName.valueOf(table);
092      if (admin.tableExists(t)) {
093        admin.disableTable(t);
094        admin.deleteTable(t);
095      }
096    }
097
098    conf.set("hbase.rest.readonly", "false");
099  }
100
101  private static byte[] toXML(TableSchemaModel model) throws JAXBException {
102    StringWriter writer = new StringWriter();
103    context.createMarshaller().marshal(model, writer);
104    return Bytes.toBytes(writer.toString());
105  }
106
107  private static TableSchemaModel fromXML(byte[] content) throws JAXBException {
108    return (TableSchemaModel) context.createUnmarshaller()
109      .unmarshal(new ByteArrayInputStream(content));
110  }
111
112  @Test
113  public void testTableCreateAndDeleteXML() throws IOException, JAXBException {
114    String schemaPath = "/" + TABLE1 + "/schema";
115    TableSchemaModel model;
116    Response response;
117
118    Admin admin = TEST_UTIL.getAdmin();
119    assertFalse(admin.tableExists(TableName.valueOf(TABLE1)),
120      "Table " + TABLE1 + " should not exist");
121
122    // create the table
123    model = testTableSchemaModel.buildTestModel(TABLE1);
124    testTableSchemaModel.checkModel(model, TABLE1);
125    if (csrfEnabled) {
126      // test put operation is forbidden without custom header
127      response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model));
128      assertEquals(400, response.getCode());
129    }
130
131    response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model), extraHdr);
132    assertEquals(201, response.getCode(),
133      "put failed with csrf " + (csrfEnabled ? "enabled" : "disabled"));
134
135    // recall the same put operation but in read-only mode
136    conf.set("hbase.rest.readonly", "true");
137    response = client.put(schemaPath, Constants.MIMETYPE_XML, toXML(model), extraHdr);
138    assertEquals(403, response.getCode());
139
140    // retrieve the schema and validate it
141    response = client.get(schemaPath, Constants.MIMETYPE_XML);
142    assertEquals(200, response.getCode());
143    assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
144    model = fromXML(response.getBody());
145    testTableSchemaModel.checkModel(model, TABLE1);
146
147    // with json retrieve the schema and validate it
148    response = client.get(schemaPath, Constants.MIMETYPE_JSON);
149    assertEquals(200, response.getCode());
150    assertEquals(Constants.MIMETYPE_JSON, response.getHeader("content-type"));
151    model = testTableSchemaModel.fromJSON(Bytes.toString(response.getBody()));
152    testTableSchemaModel.checkModel(model, TABLE1);
153
154    if (csrfEnabled) {
155      // test delete schema operation is forbidden without custom header
156      response = client.delete(schemaPath);
157      assertEquals(400, response.getCode());
158    }
159
160    // test delete schema operation is forbidden in read-only mode
161    response = client.delete(schemaPath, extraHdr);
162    assertEquals(403, response.getCode());
163
164    // return read-only setting back to default
165    conf.set("hbase.rest.readonly", "false");
166
167    // delete the table and make sure HBase concurs
168    response = client.delete(schemaPath, extraHdr);
169    assertEquals(200, response.getCode());
170    assertFalse(admin.tableExists(TableName.valueOf(TABLE1)));
171  }
172
173  @Test
174  public void testTableCreateAndDeletePB() throws IOException {
175    String schemaPath = "/" + TABLE2 + "/schema";
176    TableSchemaModel model;
177    Response response;
178
179    Admin admin = TEST_UTIL.getAdmin();
180    assertFalse(admin.tableExists(TableName.valueOf(TABLE2)));
181
182    // create the table
183    model = testTableSchemaModel.buildTestModel(TABLE2);
184    testTableSchemaModel.checkModel(model, TABLE2);
185
186    if (csrfEnabled) {
187      // test put operation is forbidden without custom header
188      response = client.put(schemaPath, Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput());
189      assertEquals(400, response.getCode());
190    }
191    response =
192      client.put(schemaPath, Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput(), extraHdr);
193    assertEquals(201, response.getCode(),
194      "put failed with csrf " + (csrfEnabled ? "enabled" : "disabled"));
195
196    // recall the same put operation but in read-only mode
197    conf.set("hbase.rest.readonly", "true");
198    response =
199      client.put(schemaPath, Constants.MIMETYPE_PROTOBUF, model.createProtobufOutput(), extraHdr);
200    assertNotNull(extraHdr);
201    assertEquals(403, response.getCode());
202
203    // retrieve the schema and validate it
204    response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF);
205    assertEquals(200, response.getCode());
206    assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
207    model = new TableSchemaModel();
208    model.getObjectFromMessage(response.getBody());
209    testTableSchemaModel.checkModel(model, TABLE2);
210
211    // retrieve the schema and validate it with alternate pbuf type
212    response = client.get(schemaPath, Constants.MIMETYPE_PROTOBUF_IETF);
213    assertEquals(200, response.getCode());
214    assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
215    model = new TableSchemaModel();
216    model.getObjectFromMessage(response.getBody());
217    testTableSchemaModel.checkModel(model, TABLE2);
218
219    if (csrfEnabled) {
220      // test delete schema operation is forbidden without custom header
221      response = client.delete(schemaPath);
222      assertEquals(400, response.getCode());
223    }
224
225    // test delete schema operation is forbidden in read-only mode
226    response = client.delete(schemaPath, extraHdr);
227    assertEquals(403, response.getCode());
228
229    // return read-only setting back to default
230    conf.set("hbase.rest.readonly", "false");
231
232    // delete the table and make sure HBase concurs
233    response = client.delete(schemaPath, extraHdr);
234    assertEquals(200, response.getCode());
235    assertFalse(admin.tableExists(TableName.valueOf(TABLE2)));
236  }
237}