http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/30711973/webapp/src/main/java/org/apache/hadoop/metadata/web/resources/TypesResource.java ---------------------------------------------------------------------- diff --git a/webapp/src/main/java/org/apache/hadoop/metadata/web/resources/TypesResource.java b/webapp/src/main/java/org/apache/hadoop/metadata/web/resources/TypesResource.java deleted file mode 100755 index 541c2f4..0000000 --- a/webapp/src/main/java/org/apache/hadoop/metadata/web/resources/TypesResource.java +++ /dev/null @@ -1,185 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.hadoop.metadata.web.resources; - -import com.sun.jersey.api.client.ClientResponse; -import org.apache.hadoop.metadata.MetadataException; -import org.apache.hadoop.metadata.MetadataServiceClient; -import org.apache.hadoop.metadata.services.MetadataService; -import org.apache.hadoop.metadata.typesystem.types.DataTypes; -import org.apache.hadoop.metadata.web.util.Servlets; -import org.codehaus.jettison.json.JSONArray; -import org.codehaus.jettison.json.JSONException; -import org.codehaus.jettison.json.JSONObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.inject.Inject; -import javax.inject.Singleton; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.Consumes; -import javax.ws.rs.DefaultValue; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.Response; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * This class provides RESTful API for Types. - * - * A type is the description of any representable item; - * e.g. a Hive table - * - * You could represent any meta model representing any domain using these types. - */ -@Path("types") -@Singleton -public class TypesResource { - - private static final Logger LOG = LoggerFactory.getLogger(EntityResource.class); - - private final MetadataService metadataService; - - static final String TYPE_ALL = "all"; - - @Inject - public TypesResource(MetadataService metadataService) { - this.metadataService = metadataService; - } - - /** - * Submits a type definition corresponding to a given type representing a meta model of a - * domain. Could represent things like Hive Database, Hive Table, etc. - */ - @POST - @Consumes(Servlets.JSON_MEDIA_TYPE) - @Produces(Servlets.JSON_MEDIA_TYPE) - public Response submit(@Context HttpServletRequest request) { - try { - final String typeDefinition = Servlets.getRequestPayload(request); - LOG.debug("Creating type with definition {} ", typeDefinition); - - JSONObject typesJson = metadataService.createType(typeDefinition); - final JSONArray typesJsonArray = typesJson.getJSONArray(MetadataServiceClient.TYPES); - - JSONArray typesResponse = new JSONArray(); - for (int i = 0; i < typesJsonArray.length(); i++) { - final String name = typesJsonArray.getString(i); - typesResponse.put(new JSONObject() {{ - put(MetadataServiceClient.NAME, name); - }}); - } - - JSONObject response = new JSONObject(); - response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId()); - response.put(MetadataServiceClient.TYPES, typesResponse); - return Response.status(ClientResponse.Status.CREATED).entity(response).build(); - } catch (MetadataException | IllegalArgumentException e) { - LOG.error("Unable to persist types", e); - throw new WebApplicationException( - Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST)); - } catch (Throwable e) { - LOG.error("Unable to persist types", e); - throw new WebApplicationException( - Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR)); - } - } - - /** - * Fetch the complete definition of a given type name which is unique. - * - * @param typeName name of a type which is unique. - */ - @GET - @Path("{typeName}") - @Produces(Servlets.JSON_MEDIA_TYPE) - public Response getDefinition(@Context HttpServletRequest request, - @PathParam("typeName") String typeName) { - try { - final String typeDefinition = metadataService.getTypeDefinition(typeName); - - JSONObject response = new JSONObject(); - response.put(MetadataServiceClient.TYPENAME, typeName); - response.put(MetadataServiceClient.DEFINITION, typeDefinition); - response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId()); - - return Response.ok(response).build(); - } catch (MetadataException e) { - LOG.error("Unable to get type definition for type {}", typeName, e); - throw new WebApplicationException( - Servlets.getErrorResponse(e, Response.Status.NOT_FOUND)); - } catch (JSONException | IllegalArgumentException e) { - LOG.error("Unable to get type definition for type {}", typeName, e); - throw new WebApplicationException( - Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST)); - } catch (Throwable e) { - LOG.error("Unable to get type definition for type {}", typeName, e); - throw new WebApplicationException( - Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR)); - } - } - - /** - * Gets the list of trait type names registered in the type system. - * - * @param type type should be the name of enum - * org.apache.hadoop.metadata.typesystem.types.DataTypes.TypeCategory - * Typically, would be one of all, TRAIT, CLASS, ENUM, STRUCT - * @return entity names response payload as json - */ - @GET - @Produces(Servlets.JSON_MEDIA_TYPE) - public Response getTypesByFilter(@Context HttpServletRequest request, - @DefaultValue(TYPE_ALL) @QueryParam("type") String type) { - try { - List<String> result; - if (TYPE_ALL.equals(type)) { - result = metadataService.getTypeNamesList(); - } else { - DataTypes.TypeCategory typeCategory = DataTypes.TypeCategory.valueOf(type); - result = metadataService.getTypeNamesByCategory(typeCategory); - } - - JSONObject response = new JSONObject(); - response.put(MetadataServiceClient.RESULTS, new JSONArray(result)); - response.put(MetadataServiceClient.COUNT, result.size()); - response.put(MetadataServiceClient.REQUEST_ID, Servlets.getRequestId()); - - return Response.ok(response).build(); - } catch (IllegalArgumentException | MetadataException ie) { - LOG.error("Unsupported typeName while retrieving type list {}", type); - throw new WebApplicationException( - Servlets.getErrorResponse("Unsupported type " + type, Response.Status.BAD_REQUEST)); - } catch (Throwable e) { - LOG.error("Unable to get types list", e); - throw new WebApplicationException( - Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR)); - } - } -}
http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/30711973/webapp/src/main/java/org/apache/hadoop/metadata/web/service/EmbeddedServer.java ---------------------------------------------------------------------- diff --git a/webapp/src/main/java/org/apache/hadoop/metadata/web/service/EmbeddedServer.java b/webapp/src/main/java/org/apache/hadoop/metadata/web/service/EmbeddedServer.java deleted file mode 100755 index 8c3b8ab..0000000 --- a/webapp/src/main/java/org/apache/hadoop/metadata/web/service/EmbeddedServer.java +++ /dev/null @@ -1,87 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.hadoop.metadata.web.service; - -import org.apache.commons.configuration.ConfigurationException; -import org.apache.commons.configuration.PropertiesConfiguration; -import org.mortbay.jetty.Connector; -import org.mortbay.jetty.Server; -import org.mortbay.jetty.bio.SocketConnector; -import org.mortbay.jetty.webapp.WebAppContext; - -import java.io.IOException; - -/** - * This class embeds a Jetty server and a connector. - */ -public class EmbeddedServer { - private static final int DEFAULT_BUFFER_SIZE = 16192; - - protected final Server server = new Server(); - - public EmbeddedServer(int port, String path) throws IOException { - Connector connector = getConnector(port); - server.addConnector(connector); - - WebAppContext application = new WebAppContext(path, "/"); - server.setHandler(application); - } - - public static EmbeddedServer newServer(int port, String path, boolean secure) throws IOException { - if (secure) { - return new SecureEmbeddedServer(port, path); - } else { - return new EmbeddedServer(port, path); - } - } - - protected Connector getConnector(int port) throws IOException { - Connector connector = new SocketConnector(); - connector.setPort(port); - connector.setHost("0.0.0.0"); - - // this is to enable large header sizes when Kerberos is enabled with AD - final Integer bufferSize = getBufferSize(); - connector.setHeaderBufferSize(bufferSize); - connector.setRequestBufferSize(bufferSize); - - return connector; - } - - private Integer getBufferSize() { - try { - PropertiesConfiguration configuration = new PropertiesConfiguration( - "application.properties"); - return configuration.getInt("metadata.jetty.request.buffer.size", DEFAULT_BUFFER_SIZE); - } catch (ConfigurationException e) { - // do nothing - } - - return DEFAULT_BUFFER_SIZE; - } - - public void start() throws Exception { - server.start(); - server.join(); - } - - public void stop() throws Exception { - server.stop(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/30711973/webapp/src/main/java/org/apache/hadoop/metadata/web/service/SecureEmbeddedServer.java ---------------------------------------------------------------------- diff --git a/webapp/src/main/java/org/apache/hadoop/metadata/web/service/SecureEmbeddedServer.java b/webapp/src/main/java/org/apache/hadoop/metadata/web/service/SecureEmbeddedServer.java deleted file mode 100755 index 6e71366..0000000 --- a/webapp/src/main/java/org/apache/hadoop/metadata/web/service/SecureEmbeddedServer.java +++ /dev/null @@ -1,109 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.hadoop.metadata.web.service; - -import org.apache.commons.configuration.ConfigurationException; -import org.apache.commons.configuration.PropertiesConfiguration; -import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.security.alias.CredentialProvider; -import org.apache.hadoop.security.alias.CredentialProviderFactory; -import org.mortbay.jetty.Connector; -import org.mortbay.jetty.security.SslSocketConnector; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -import java.io.IOException; - -import static org.apache.hadoop.metadata.security.SecurityProperties.*; - -/** - * This is a jetty server which requires client auth via certificates. - */ -public class SecureEmbeddedServer extends EmbeddedServer { - - private static final Logger LOG = LoggerFactory.getLogger(SecureEmbeddedServer.class); - - public SecureEmbeddedServer(int port, String path) throws IOException { - super(port, path); - } - - protected Connector getConnector(int port) throws IOException { - PropertiesConfiguration config = getConfiguration(); - - SslSocketConnector connector = new SslSocketConnector(); - connector.setPort(port); - connector.setHost("0.0.0.0"); - connector.setKeystore(config.getString(KEYSTORE_FILE_KEY, - System.getProperty(KEYSTORE_FILE_KEY, DEFAULT_KEYSTORE_FILE_LOCATION))); - connector.setKeyPassword(getPassword(config, KEYSTORE_PASSWORD_KEY)); - connector.setTruststore(config.getString(TRUSTSTORE_FILE_KEY, - System.getProperty(TRUSTSTORE_FILE_KEY, DEFATULT_TRUSTORE_FILE_LOCATION))); - connector.setTrustPassword(getPassword(config, TRUSTSTORE_PASSWORD_KEY)); - connector.setPassword(getPassword(config, SERVER_CERT_PASSWORD_KEY)); - connector.setWantClientAuth(config.getBoolean(CLIENT_AUTH_KEY, Boolean.getBoolean(CLIENT_AUTH_KEY))); - return connector; - } - - /** - * Retrieves a password from a configured credential provider or prompts for the password and stores it in the - * configured credential provider. - * @param config application configuration - * @param key the key/alias for the password. - * @return the password. - * @throws IOException - */ - private String getPassword(PropertiesConfiguration config, String key) throws IOException { - - String password = null; - - String provider = config.getString(CERT_STORES_CREDENTIAL_PROVIDER_PATH); - if (provider != null) { - LOG.info("Attempting to retrieve password from configured credential provider path"); - Configuration c = new Configuration(); - c.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, provider); - CredentialProvider credentialProvider = - CredentialProviderFactory.getProviders(c).get(0); - CredentialProvider.CredentialEntry entry = credentialProvider.getCredentialEntry(key); - if (entry == null) { - throw new IOException(String.format("No credential entry found for %s. " + - "Please create an entry in the configured credential provider", key)); - } else { - password = String.valueOf(entry.getCredential()); - } - - } else { - throw new IOException("No credential provider path configured for storage of certificate store passwords"); - } - - return password; - } - - /** - * Returns the application configuration. - * @return - */ - protected PropertiesConfiguration getConfiguration() { - try { - return new PropertiesConfiguration("application.properties"); - } catch (ConfigurationException e) { - throw new RuntimeException("Unable to load configuration: application.properties"); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/30711973/webapp/src/main/java/org/apache/hadoop/metadata/web/util/DateTimeHelper.java ---------------------------------------------------------------------- diff --git a/webapp/src/main/java/org/apache/hadoop/metadata/web/util/DateTimeHelper.java b/webapp/src/main/java/org/apache/hadoop/metadata/web/util/DateTimeHelper.java deleted file mode 100755 index 6ad2f24..0000000 --- a/webapp/src/main/java/org/apache/hadoop/metadata/web/util/DateTimeHelper.java +++ /dev/null @@ -1,123 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.hadoop.metadata.web.util; - -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.TimeZone; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Support function to parse and format date. - */ -public final class DateTimeHelper { - - public static final String ISO8601_FORMAT = "yyyy-MM-dd'T'HH:mm'Z'"; - private static final String DATE_PATTERN = - "(2\\d\\d\\d|19\\d\\d)-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])T" + - "([0-1][0-9]|2[0-3]):([0-5][0-9])Z"; - private static final Pattern PATTERN = Pattern.compile(DATE_PATTERN); - - private DateTimeHelper() { - } - - public static String getTimeZoneId(TimeZone tz) { - return tz.getID(); - } - - public static DateFormat getDateFormat() { - DateFormat dateFormat = new SimpleDateFormat(ISO8601_FORMAT); - dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - return dateFormat; - } - - public static String formatDateUTC(Date date) { - return (date != null) ? getDateFormat().format(date) : null; - } - - public static Date parseDateUTC(String dateStr) { - if (!validate(dateStr)) { - throw new IllegalArgumentException(dateStr + " is not a valid UTC string"); - } - try { - return getDateFormat().parse(dateStr); - } catch (ParseException e) { - throw new RuntimeException(e); - } - } - - public static String formatDateUTCToISO8601(final String dateString, - final String dateStringFormat) { - - try { - DateFormat dateFormat = new SimpleDateFormat( - dateStringFormat.substring(0, dateString.length())); - dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - return DateTimeHelper.formatDateUTC(dateFormat.parse(dateString)); - } catch (ParseException e) { - throw new RuntimeException(e); - } - } - - /** - * Validate date format with regular expression. - * - * @param date date address for validation - * @return true valid date fromat, false invalid date format - */ - public static boolean validate(final String date) { - - Matcher matcher = PATTERN.matcher(date); - - if (matcher.matches()) { - - matcher.reset(); - - if (matcher.find()) { - - int year = Integer.parseInt(matcher.group(1)); - String month = matcher.group(2); - String day = matcher.group(3); - - if (day.equals("31") - && (month.equals("4") || month.equals("6") - || month.equals("9") || month.equals("11") - || month.equals("04") || month.equals("06") || month.equals("09"))) { - return false; // only 1,3,5,7,8,10,12 has 31 days - } else if (month.equals("2") || month.equals("02")) { - // leap year - if (year % 4 == 0) { - return !(day.equals("30") || day.equals("31")); - } else { - return !(day.equals("29") || day.equals("30") || day.equals("31")); - } - } else { - return true; - } - } else { - return false; - } - } else { - return false; - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/30711973/webapp/src/main/java/org/apache/hadoop/metadata/web/util/Servlets.java ---------------------------------------------------------------------- diff --git a/webapp/src/main/java/org/apache/hadoop/metadata/web/util/Servlets.java b/webapp/src/main/java/org/apache/hadoop/metadata/web/util/Servlets.java deleted file mode 100755 index 47435a4..0000000 --- a/webapp/src/main/java/org/apache/hadoop/metadata/web/util/Servlets.java +++ /dev/null @@ -1,152 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.hadoop.metadata.web.util; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.StringEscapeUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.hadoop.metadata.MetadataServiceClient; -import org.apache.hadoop.metadata.ParamChecker; -import org.codehaus.jettison.json.JSONException; -import org.codehaus.jettison.json.JSONObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.io.IOException; -import java.io.PrintWriter; -import java.io.StringWriter; - -/** - * Utility functions for dealing with servlets. - */ -public final class Servlets { - - private static final Logger LOG = LoggerFactory.getLogger(Servlets.class); - private Servlets() { - /* singleton */ - } - - public static final String JSON_MEDIA_TYPE = MediaType.APPLICATION_JSON + "; charset=UTF-8"; - - /** - * Returns the user of the given request. - * - * @param httpRequest an HTTP servlet request - * @return the user - */ - public static String getUserFromRequest(HttpServletRequest httpRequest) { - String user = httpRequest.getRemoteUser(); - if (!StringUtils.isEmpty(user)) { - return user; - } - - user = httpRequest.getParameter("user.name"); // available in query-param - if (!StringUtils.isEmpty(user)) { - return user; - } - - user = httpRequest.getHeader("Remote-User"); // backwards-compatibility - if (!StringUtils.isEmpty(user)) { - return user; - } - - return null; - } - - /** - * Returns the URI of the given request. - * - * @param httpRequest an HTTP servlet request - * @return the URI, including the query string - */ - public static String getRequestURI(HttpServletRequest httpRequest) { - final StringBuilder url = new StringBuilder(100).append(httpRequest.getRequestURI()); - if (httpRequest.getQueryString() != null) { - url.append('?').append(httpRequest.getQueryString()); - } - - return url.toString(); - } - - /** - * Returns the full URL of the given request. - * - * @param httpRequest an HTTP servlet request - * @return the full URL, including the query string - */ - public static String getRequestURL(HttpServletRequest httpRequest) { - final StringBuilder url = new StringBuilder(100).append(httpRequest.getRequestURL()); - if (httpRequest.getQueryString() != null) { - url.append('?').append(httpRequest.getQueryString()); - } - - return url.toString(); - } - - public static Response getErrorResponse(Throwable e, Response.Status status) { - Response response = getErrorResponse(e.getMessage(), status); - JSONObject responseJson = (JSONObject) response.getEntity(); - try { - responseJson.put(MetadataServiceClient.STACKTRACE, printStackTrace(e)); - } catch (JSONException e1) { - LOG.warn("Could not construct error Json rensponse", e1); - } - return response; - } - - private static String printStackTrace(Throwable t) { - StringWriter sw = new StringWriter(); - t.printStackTrace(new PrintWriter(sw)); - return sw.toString(); - } - - public static Response getErrorResponse(String message, Response.Status status) { - JSONObject errorJson = new JSONObject(); - Object errorEntity = Servlets.escapeJsonString(message); - try { - errorJson.put(MetadataServiceClient.ERROR, errorEntity); - errorEntity = errorJson; - } catch (JSONException jsonE) { - LOG.warn("Could not construct error Json rensponse", jsonE); - } - return Response - .status(status) - .entity(errorEntity) - .type(JSON_MEDIA_TYPE) - .build(); - } - - public static String getRequestPayload(HttpServletRequest request) throws IOException { - StringWriter writer = new StringWriter(); - IOUtils.copy(request.getInputStream(), writer); - return writer.toString(); - } - - public static String getRequestId() { - return Thread.currentThread().getName(); - } - - public static String escapeJsonString(String inputStr) { - ParamChecker.notNull(inputStr, "Input String cannot be null"); - return StringEscapeUtils.escapeJson(inputStr); - } -} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/30711973/webapp/src/test/java/org/apache/atlas/CredentialProviderUtilityIT.java ---------------------------------------------------------------------- diff --git a/webapp/src/test/java/org/apache/atlas/CredentialProviderUtilityIT.java b/webapp/src/test/java/org/apache/atlas/CredentialProviderUtilityIT.java new file mode 100755 index 0000000..2bb70d0 --- /dev/null +++ b/webapp/src/test/java/org/apache/atlas/CredentialProviderUtilityIT.java @@ -0,0 +1,272 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.atlas; + +import org.apache.atlas.security.SecurityProperties; +import org.apache.atlas.util.CredentialProviderUtility; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.security.alias.CredentialProvider; +import org.apache.hadoop.security.alias.CredentialProviderFactory; +import org.apache.hadoop.security.alias.JavaKeyStoreProvider; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +/** + * + */ +public class CredentialProviderUtilityIT { + + private char[] defaultPass = new char[]{'k', 'e', 'y', 'p', 'a', 's', 's'}; + + @Test + public void testEnterValidValues() throws Exception { + Path testPath = null; + try { + testPath = new Path(Files.createTempDirectory("tempproviders").toString(), "test.jks"); + } catch (IOException e) { + e.printStackTrace(); + } + new File(testPath.toUri().getPath()).delete(); + final Path finalTestPath = testPath; + CredentialProviderUtility.textDevice = new CredentialProviderUtility.TextDevice() { + @Override + public void printf(String fmt, Object... params) { + System.out.print(String.format(fmt, params)); + } + + public String readLine(String fmt, Object ... args) { + return finalTestPath.toString(); + } + + @Override + public char[] readPassword(String fmt, Object ... args) { + return defaultPass; + } + }; + + CredentialProviderUtility.main(new String[] {}); + + String providerUrl = JavaKeyStoreProvider.SCHEME_NAME + "://file" + testPath.toUri(); + Configuration conf = new Configuration(false); + + conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, providerUrl); + + CredentialProvider provider = + CredentialProviderFactory.getProviders(conf).get(0); + + CredentialProvider.CredentialEntry entry = + provider.getCredentialEntry(SecurityProperties.KEYSTORE_PASSWORD_KEY); + assertCredentialEntryCorrect(entry); + entry = provider.getCredentialEntry(SecurityProperties.TRUSTSTORE_PASSWORD_KEY); + assertCredentialEntryCorrect(entry); + entry = provider.getCredentialEntry(SecurityProperties.SERVER_CERT_PASSWORD_KEY); + assertCredentialEntryCorrect(entry); + } + + protected void assertCredentialEntryCorrect(CredentialProvider.CredentialEntry entry) { + assertCredentialEntryCorrect(entry, defaultPass); + } + + protected void assertCredentialEntryCorrect(CredentialProvider.CredentialEntry entry, char[] password) { + Assert.assertNotNull(entry); + Assert.assertEquals(entry.getCredential(), password); + } + + @Test + public void testEnterEmptyValues() throws Exception { + Path testPath = null; + try { + testPath = new Path(Files.createTempDirectory("tempproviders").toString(), "test.jks"); + } catch (IOException e) { + e.printStackTrace(); + } + new File(testPath.toUri().getPath()).delete(); + final Path finalTestPath = testPath; + CredentialProviderUtility.textDevice = new CredentialProviderUtility.TextDevice() { + + private Random random = new Random(); + + @Override + public void printf(String fmt, Object... params) { + System.out.print(String.format(fmt, params)); + } + + public String readLine(String fmt, Object ... args) { + return finalTestPath.toString(); + } + + @Override + public char[] readPassword(String fmt, Object ... args) { + List<char[]> responses = new ArrayList<>(); + responses.add(new char[0]); + responses.add(defaultPass); + + int size = responses.size(); + int item = random.nextInt(size); + return responses.get(item); + } + }; + + CredentialProviderUtility.main(new String[] {}); + + String providerUrl = JavaKeyStoreProvider.SCHEME_NAME + "://file" + testPath.toUri(); + Configuration conf = new Configuration(false); + + conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, providerUrl); + + CredentialProvider provider = + CredentialProviderFactory.getProviders(conf).get(0); + + CredentialProvider.CredentialEntry entry = + provider.getCredentialEntry(SecurityProperties.KEYSTORE_PASSWORD_KEY); + assertCredentialEntryCorrect(entry); + entry = provider.getCredentialEntry(SecurityProperties.TRUSTSTORE_PASSWORD_KEY); + assertCredentialEntryCorrect(entry); + entry = provider.getCredentialEntry(SecurityProperties.SERVER_CERT_PASSWORD_KEY); + assertCredentialEntryCorrect(entry); + } + + @Test + public void testEnterMismatchedValues() throws Exception { + Path testPath = null; + try { + testPath = new Path(Files.createTempDirectory("tempproviders").toString(), "test.jks"); + } catch (IOException e) { + e.printStackTrace(); + } + new File(testPath.toUri().getPath()).delete(); + final Path finalTestPath = testPath; + CredentialProviderUtility.textDevice = new CredentialProviderUtility.TextDevice() { + + int i = 0; + @Override + public void printf(String fmt, Object... params) { + System.out.print(String.format(fmt, params)); + } + + public String readLine(String fmt, Object ... args) { + return finalTestPath.toString(); + } + + @Override + public char[] readPassword(String fmt, Object ... args) { + List<char[]> responses = new ArrayList<>(); + responses.add(defaultPass); + responses.add(new char[] {'b', 'a', 'd', 'p', 'a', 's', 's'}); + responses.add(defaultPass); + + int item = i % 3; + i++; + return responses.get(item); + } + }; + + CredentialProviderUtility.main(new String[] {}); + + String providerUrl = JavaKeyStoreProvider.SCHEME_NAME + "://file" + testPath.toUri(); + Configuration conf = new Configuration(false); + + conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, providerUrl); + + CredentialProvider provider = + CredentialProviderFactory.getProviders(conf).get(0); + + CredentialProvider.CredentialEntry entry = + provider.getCredentialEntry(SecurityProperties.KEYSTORE_PASSWORD_KEY); + assertCredentialEntryCorrect(entry); + entry = provider.getCredentialEntry(SecurityProperties.TRUSTSTORE_PASSWORD_KEY); + assertCredentialEntryCorrect(entry); + entry = provider.getCredentialEntry(SecurityProperties.SERVER_CERT_PASSWORD_KEY); + assertCredentialEntryCorrect(entry); + } + + @Test + public void testOverwriteValues() throws Exception { + Path testPath = null; + try { + testPath = new Path(Files.createTempDirectory("tempproviders").toString(), "test.jks"); + } catch (IOException e) { + e.printStackTrace(); + } + new File(testPath.toUri().getPath()).delete(); + final Path finalTestPath = testPath; + CredentialProviderUtility.textDevice = new CredentialProviderUtility.TextDevice() { + @Override + public void printf(String fmt, Object... params) { + System.out.print(String.format(fmt, params)); + } + + public String readLine(String fmt, Object ... args) { + return finalTestPath.toString(); + } + + @Override + public char[] readPassword(String fmt, Object ... args) { + return defaultPass; + } + }; + + CredentialProviderUtility.main(new String[] {}); + + // now attempt to overwrite values + CredentialProviderUtility.textDevice = new CredentialProviderUtility.TextDevice() { + + int i = 0; + + @Override + public void printf(String fmt, Object... params) { + System.out.print(String.format(fmt, params)); + } + + public String readLine(String fmt, Object ... args) { + return i++ == 0 ? finalTestPath.toString() : "y"; + } + + @Override + public char[] readPassword(String fmt, Object ... args) { + return new char[] {'n', 'e', 'w', 'p', 'a', 's', 's'}; + } + }; + + CredentialProviderUtility.main(new String[] {}); + + String providerUrl = JavaKeyStoreProvider.SCHEME_NAME + "://file" + testPath.toUri(); + Configuration conf = new Configuration(false); + + conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, providerUrl); + + CredentialProvider provider = + CredentialProviderFactory.getProviders(conf).get(0); + + char[] newpass = "newpass".toCharArray(); + CredentialProvider.CredentialEntry entry = + provider.getCredentialEntry(SecurityProperties.KEYSTORE_PASSWORD_KEY); + assertCredentialEntryCorrect(entry, newpass); + entry = provider.getCredentialEntry(SecurityProperties.TRUSTSTORE_PASSWORD_KEY); + assertCredentialEntryCorrect(entry, newpass); + entry = provider.getCredentialEntry(SecurityProperties.SERVER_CERT_PASSWORD_KEY); + assertCredentialEntryCorrect(entry, newpass); + } +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/30711973/webapp/src/test/java/org/apache/atlas/web/filters/MetadataAuthenticationKerberosFilterIT.java ---------------------------------------------------------------------- diff --git a/webapp/src/test/java/org/apache/atlas/web/filters/MetadataAuthenticationKerberosFilterIT.java b/webapp/src/test/java/org/apache/atlas/web/filters/MetadataAuthenticationKerberosFilterIT.java new file mode 100644 index 0000000..5782af7 --- /dev/null +++ b/webapp/src/test/java/org/apache/atlas/web/filters/MetadataAuthenticationKerberosFilterIT.java @@ -0,0 +1,179 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.atlas.web.filters; + +import org.apache.atlas.security.BaseSecurityTest; +import org.apache.atlas.web.service.EmbeddedServer; +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.io.FileUtils; +import org.apache.hadoop.hdfs.web.URLConnectionFactory; +import org.mortbay.jetty.Server; +import org.testng.Assert; +import org.testng.annotations.Test; + +import javax.security.auth.Subject; +import javax.security.auth.callback.Callback; +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.callback.NameCallback; +import javax.security.auth.callback.PasswordCallback; +import javax.security.auth.callback.UnsupportedCallbackException; +import javax.security.auth.login.LoginContext; +import javax.security.auth.login.LoginException; +import java.io.File; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.security.PrivilegedExceptionAction; +import java.util.Properties; + +/** + * + */ +public class MetadataAuthenticationKerberosFilterIT extends BaseSecurityTest { + public static final String TEST_USER_JAAS_SECTION = "TestUser"; + public static final String TESTUSER = "testuser"; + public static final String TESTPASS = "testpass"; + + private File userKeytabFile; + private File httpKeytabFile; + + class TestEmbeddedServer extends EmbeddedServer { + public TestEmbeddedServer(int port, String path) throws IOException { + super(port, path); + } + + Server getServer() { + return server; + } + } + + @Test (enabled = false) + public void testKerberosBasedLogin() throws Exception { + String originalConf = System.getProperty("metadata.conf"); + System.setProperty("metadata.conf", System.getProperty("user.dir")); + + setupKDCAndPrincipals(); + TestEmbeddedServer server = null; + + try { + // setup the application.properties file + generateKerberosTestProperties(); + + // need to create the web application programmatically in order to control the injection of the test + // application properties + server = new TestEmbeddedServer(23000, "webapp/target/apache-atlas"); + + startEmbeddedServer(server.getServer()); + + final URLConnectionFactory connectionFactory = URLConnectionFactory.DEFAULT_SYSTEM_CONNECTION_FACTORY; + // attempt to hit server and get rejected + URL url = new URL("http://localhost:23000/"); + HttpURLConnection connection = (HttpURLConnection) connectionFactory.openConnection(url, false); + connection.setRequestMethod("GET"); + connection.connect(); + + Assert.assertEquals(connection.getResponseCode(), 401); + + // need to populate the ticket cache with a local user, so logging in... + Subject subject = loginTestUser(); + + Subject.doAs(subject, new PrivilegedExceptionAction<Object>() { + @Override + public Object run() throws Exception { + // attempt to hit server and get rejected + URL url = new URL("http://localhost:23000/"); + HttpURLConnection connection = (HttpURLConnection) connectionFactory.openConnection(url, true); + connection.setRequestMethod("GET"); + connection.connect(); + + Assert.assertEquals(connection.getResponseCode(), 200); + + return null; + } + }); + } finally { + server.getServer().stop(); + kdc.stop(); + + if (originalConf != null) { + System.setProperty("metadata.conf", originalConf); + } else { + System.clearProperty("metadata.conf"); + } + + } + + + } + + protected Subject loginTestUser() throws LoginException, IOException { + LoginContext lc = new LoginContext(TEST_USER_JAAS_SECTION, new CallbackHandler() { + + @Override + public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { + for (int i = 0; i < callbacks.length; i++) { + if (callbacks[i] instanceof PasswordCallback) { + PasswordCallback passwordCallback = (PasswordCallback) callbacks[i]; + passwordCallback.setPassword(TESTPASS.toCharArray()); + } + if (callbacks[i] instanceof NameCallback) { + NameCallback nameCallback = (NameCallback) callbacks[i]; + nameCallback.setName(TESTUSER); + } + } + } + }); + // attempt authentication + lc.login(); + return lc.getSubject(); + } + + protected void generateKerberosTestProperties() throws IOException, ConfigurationException { + Properties props = new Properties(); + props.setProperty("atlas.http.authentication.enabled", "true"); + props.setProperty("atlas.http.authentication.type", "kerberos"); + props.setProperty("atlas.http.authentication.kerberos.principal", "HTTP/localhost@" + kdc.getRealm()); + props.setProperty("atlas.http.authentication.kerberos.keytab", httpKeytabFile.getAbsolutePath()); + props.setProperty("atlas.http.authentication.kerberos.name.rules", + "RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*//\nDEFAULT"); + + generateTestProperties(props); + } + + public void setupKDCAndPrincipals() throws Exception { + // set up the KDC + File kdcWorkDir = startKDC(); + + userKeytabFile = createKeytab(kdc, kdcWorkDir, "dgi", "dgi.keytab"); + httpKeytabFile = createKeytab(kdc, kdcWorkDir, "HTTP", "spnego.service.keytab"); + + // create a test user principal + kdc.createPrincipal(TESTUSER, TESTPASS); + + StringBuilder jaas = new StringBuilder(1024); + jaas.append("TestUser {\n" + + " com.sun.security.auth.module.Krb5LoginModule required\nuseTicketCache=true;\n" + + "};\n"); + jaas.append(createJAASEntry("Client", "dgi", userKeytabFile)); + jaas.append(createJAASEntry("Server", "HTTP", httpKeytabFile)); + + File jaasFile = new File(kdcWorkDir, "jaas.txt"); + FileUtils.write(jaasFile, jaas.toString()); + bindJVMtoJAASFile(jaasFile); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/30711973/webapp/src/test/java/org/apache/atlas/web/filters/MetadataAuthenticationSimpleFilterIT.java ---------------------------------------------------------------------- diff --git a/webapp/src/test/java/org/apache/atlas/web/filters/MetadataAuthenticationSimpleFilterIT.java b/webapp/src/test/java/org/apache/atlas/web/filters/MetadataAuthenticationSimpleFilterIT.java new file mode 100644 index 0000000..aa6f0f9 --- /dev/null +++ b/webapp/src/test/java/org/apache/atlas/web/filters/MetadataAuthenticationSimpleFilterIT.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.atlas.web.filters; + +import org.apache.atlas.security.BaseSecurityTest; +import org.apache.atlas.web.service.EmbeddedServer; +import org.apache.commons.configuration.ConfigurationException; +import org.mortbay.jetty.Server; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Properties; + +/** + * + */ +public class MetadataAuthenticationSimpleFilterIT extends BaseSecurityTest { + + class TestEmbeddedServer extends EmbeddedServer { + public TestEmbeddedServer(int port, String path) throws IOException { + super(port, path); + } + + Server getServer() { + return server; + } + } + + @Test (enabled = false) + public void testSimpleLogin() throws Exception { + String originalConf = System.getProperty("metadata.conf"); + System.setProperty("metadata.conf", System.getProperty("user.dir")); + generateSimpleLoginConfiguration(); + + TestEmbeddedServer server = new TestEmbeddedServer(23001, "webapp/target/apache-atlas"); + + try { + startEmbeddedServer(server.getServer()); + + URL url = new URL("http://localhost:23001"); + HttpURLConnection connection = (HttpURLConnection)url.openConnection(); + connection.setRequestMethod("GET"); + connection.connect(); + + try { + Assert.assertEquals(connection.getResponseCode(), 403); + } catch (Exception e) { + e.printStackTrace(); + } + + url = new URL("http://localhost:23001/?user.name=testuser"); + connection = (HttpURLConnection)url.openConnection(); + connection.setRequestMethod("GET"); + connection.connect(); + + Assert.assertEquals(connection.getResponseCode(), 200); + } finally { + server.getServer().stop(); + if (originalConf != null) { + System.setProperty("metadata.conf", originalConf); + } else { + System.clearProperty("metadata.conf"); + } + } + + + } + + protected void generateSimpleLoginConfiguration() throws IOException, ConfigurationException { + Properties config = new Properties(); + config.setProperty("atlas.http.authentication.enabled", "true"); + config.setProperty("atlas.http.authentication.type", "simple"); + + generateTestProperties(config); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/30711973/webapp/src/test/java/org/apache/atlas/web/listeners/LoginProcessorIT.java ---------------------------------------------------------------------- diff --git a/webapp/src/test/java/org/apache/atlas/web/listeners/LoginProcessorIT.java b/webapp/src/test/java/org/apache/atlas/web/listeners/LoginProcessorIT.java new file mode 100644 index 0000000..a250bca --- /dev/null +++ b/webapp/src/test/java/org/apache/atlas/web/listeners/LoginProcessorIT.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.atlas.web.listeners; + +import org.apache.atlas.security.BaseSecurityTest; +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.CommonConfigurationKeysPublic; +import org.apache.hadoop.security.UserGroupInformation; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.io.File; + +/** + * + */ +public class LoginProcessorIT extends BaseSecurityTest { + + protected static final String kerberosRule = + "RULE:[1:$1@$0](.*@EXAMPLE.COM)s/@.*//\nDEFAULT"; + + @Test + public void testDefaultSimpleLogin() throws Exception { + LoginProcessor processor = new LoginProcessor() { + @Override + protected PropertiesConfiguration getPropertiesConfiguration() throws ConfigurationException { + return new PropertiesConfiguration(); + } + }; + processor.login(); + + Assert.assertNotNull(UserGroupInformation.getCurrentUser()); + Assert.assertFalse(UserGroupInformation.isLoginKeytabBased()); + Assert.assertFalse(UserGroupInformation.isSecurityEnabled()); + } + + @Test + public void testKerberosLogin() throws Exception { + final File keytab = setupKDCAndPrincipals(); + + LoginProcessor processor = new LoginProcessor() { + @Override + protected PropertiesConfiguration getPropertiesConfiguration() throws ConfigurationException { + PropertiesConfiguration config = new PropertiesConfiguration(); + config.setProperty("atlas.authentication.method", "kerberos"); + config.setProperty("atlas.authentication.principal", "[email protected]"); + config.setProperty("atlas.authentication.keytab", keytab.getAbsolutePath()); + return config; + } + + @Override + protected Configuration getHadoopConfiguration() { + Configuration config = new Configuration(false); + config.set(CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHENTICATION, "kerberos"); + config.setBoolean(CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHORIZATION, true); + config.set(CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTH_TO_LOCAL, kerberosRule); + + return config; + } + + @Override + protected boolean isHadoopCluster() { + return true; + } + }; + processor.login(); + + Assert.assertTrue(UserGroupInformation.getLoginUser().getShortUserName().endsWith("dgi")); + Assert.assertNotNull(UserGroupInformation.getCurrentUser()); + Assert.assertTrue(UserGroupInformation.isLoginKeytabBased()); + Assert.assertTrue(UserGroupInformation.isSecurityEnabled()); + + kdc.stop(); + + } + + private File setupKDCAndPrincipals() throws Exception { + // set up the KDC + File kdcWorkDir = startKDC(); + + Assert.assertNotNull(kdc.getRealm()); + + File keytabFile = createKeytab(kdc, kdcWorkDir, "dgi", "dgi.keytab"); + + return keytabFile; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/30711973/webapp/src/test/java/org/apache/atlas/web/resources/AdminJerseyResourceIT.java ---------------------------------------------------------------------- diff --git a/webapp/src/test/java/org/apache/atlas/web/resources/AdminJerseyResourceIT.java b/webapp/src/test/java/org/apache/atlas/web/resources/AdminJerseyResourceIT.java new file mode 100755 index 0000000..a0410cb --- /dev/null +++ b/webapp/src/test/java/org/apache/atlas/web/resources/AdminJerseyResourceIT.java @@ -0,0 +1,69 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.atlas.web.resources; + +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import org.apache.atlas.web.util.Servlets; +import org.apache.commons.configuration.PropertiesConfiguration; +import org.codehaus.jettison.json.JSONObject; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import javax.ws.rs.HttpMethod; +import javax.ws.rs.core.Response; + +/** + * Integration test for Admin jersey resource. + */ +public class AdminJerseyResourceIT extends BaseResourceIT { + + @BeforeClass + public void setUp() throws Exception { + super.setUp(); + } + + @Test + public void testGetVersion() throws Exception { + WebResource resource = service + .path("api/atlas/admin/version"); + + ClientResponse clientResponse = resource + .accept(Servlets.JSON_MEDIA_TYPE) + .type(Servlets.JSON_MEDIA_TYPE) + .method(HttpMethod.GET, ClientResponse.class); + Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode()); + + String responseAsString = clientResponse.getEntity(String.class); + Assert.assertNotNull(responseAsString); + + PropertiesConfiguration buildConfiguration = + new PropertiesConfiguration("atlas-buildinfo.properties"); + + + JSONObject response = new JSONObject(responseAsString); + Assert.assertEquals(response.get("Version"), + buildConfiguration.getString("build.version")); + Assert.assertEquals(response.get("Name"), + buildConfiguration.getString("project.name")); + Assert.assertEquals(response.get("Description"), + buildConfiguration.getString("project.description")); + } +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/30711973/webapp/src/test/java/org/apache/atlas/web/resources/BaseResourceIT.java ---------------------------------------------------------------------- diff --git a/webapp/src/test/java/org/apache/atlas/web/resources/BaseResourceIT.java b/webapp/src/test/java/org/apache/atlas/web/resources/BaseResourceIT.java new file mode 100755 index 0000000..7c723e6 --- /dev/null +++ b/webapp/src/test/java/org/apache/atlas/web/resources/BaseResourceIT.java @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.atlas.web.resources; + +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.config.DefaultClientConfig; +import org.apache.atlas.MetadataServiceClient; +import org.apache.atlas.typesystem.Referenceable; +import org.apache.atlas.typesystem.TypesDef; +import org.apache.atlas.typesystem.json.InstanceSerialization; +import org.apache.atlas.typesystem.json.TypesSerialization; +import org.apache.atlas.typesystem.persistence.Id; +import org.apache.atlas.typesystem.types.ClassType; +import org.apache.atlas.typesystem.types.HierarchicalTypeDefinition; +import org.apache.atlas.web.util.Servlets; +import org.codehaus.jettison.json.JSONObject; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; + +import javax.ws.rs.HttpMethod; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; + +/** + * Base class for integration tests. + * Sets up the web resource and has helper methods to create type and entity. + */ +public abstract class BaseResourceIT { + + protected WebResource service; + protected MetadataServiceClient serviceClient; + public static String baseUrl = "http://localhost:21000/"; + + @BeforeClass + public void setUp() throws Exception { + + DefaultClientConfig config = new DefaultClientConfig(); + Client client = Client.create(config); + client.resource(UriBuilder.fromUri(baseUrl).build()); + + service = client.resource(UriBuilder.fromUri(baseUrl).build()); + serviceClient = new MetadataServiceClient(baseUrl); + } + + protected void createType(TypesDef typesDef) throws Exception { + HierarchicalTypeDefinition<ClassType> sampleType = typesDef.classTypesAsJavaList().get(0); + if (serviceClient.getType(sampleType.typeName) == null ) { + String typesAsJSON = TypesSerialization.toJson(typesDef); + createType(typesAsJSON); + } + } + + protected void createType(String typesAsJSON) throws Exception { + WebResource resource = service + .path("api/atlas/types"); + + ClientResponse clientResponse = resource + .accept(Servlets.JSON_MEDIA_TYPE) + .type(Servlets.JSON_MEDIA_TYPE) + .method(HttpMethod.POST, ClientResponse.class, typesAsJSON); + Assert.assertEquals(clientResponse.getStatus(), Response.Status.CREATED.getStatusCode()); + + String responseAsString = clientResponse.getEntity(String.class); + Assert.assertNotNull(responseAsString); + + JSONObject response = new JSONObject(responseAsString); + Assert.assertNotNull(response.get("types")); + Assert.assertNotNull(response.get(MetadataServiceClient.REQUEST_ID)); + } + + protected Id createInstance(Referenceable referenceable) throws Exception { + String typeName = referenceable.getTypeName(); + System.out.println("creating instance of type " + typeName); + + String entityJSON = InstanceSerialization.toJson(referenceable, true); + System.out.println("Submitting new entity= " + entityJSON); + JSONObject jsonObject = serviceClient.createEntity(entityJSON); + String guid = jsonObject.getString(MetadataServiceClient.GUID); + System.out.println("created instance for type " + typeName + ", guid: " + guid); + + // return the reference to created instance with guid + return new Id(guid, 0, referenceable.getTypeName()); + } +}
