mirror of
https://github.com/google/nomulus.git
synced 2025-05-13 07:57:13 +02:00
A test has been added to RequestHandlerTest, making sure that, while we merely log errors for the time being, the correct dummy AuthResult is being created. Most actions use the default settings, which have been changed to INTERNAL / APP / IGNORED. Actions with non-default settings are: INTERNAL/NONE/PUBLIC (non-auth public endpoints) CheckApiAction WhoisHttpServer Rdap*Action INTERNAL,API/APP/ADMIN (things currently protected by web.xml) EppTlsAction EppToolAction CreateGroupsAction CreatePremiumListAction DeleteEntityAction List*sAction UpdatePremiumListAction VerifyOteAction WhoisServer INTERNAL,API,LEGACY/USER/PUBLIC (registrar console) RegistrarPaymentAction RegistrarPaymentSetupAction RegistrarSettingsAction EppConsoleAction INTERNAL,API,LEGACY/NONE/PUBLIC (registrar console main page) ConsoleUiAction ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=149761652
113 lines
4.5 KiB
Java
113 lines
4.5 KiB
Java
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
|
//
|
|
// Licensed 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 google.registry.whois;
|
|
|
|
import static google.registry.request.Action.Method.POST;
|
|
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
|
|
import static javax.servlet.http.HttpServletResponse.SC_OK;
|
|
|
|
import com.google.common.net.MediaType;
|
|
import google.registry.config.RegistryConfig.Config;
|
|
import google.registry.request.Action;
|
|
import google.registry.request.Response;
|
|
import google.registry.request.auth.Auth;
|
|
import google.registry.request.auth.AuthLevel;
|
|
import google.registry.util.Clock;
|
|
import google.registry.util.FormattingLogger;
|
|
import google.registry.whois.WhoisMetrics.WhoisMetric;
|
|
import google.registry.whois.WhoisResponse.WhoisResponseResults;
|
|
import java.io.Reader;
|
|
import javax.inject.Inject;
|
|
import org.joda.time.DateTime;
|
|
|
|
/**
|
|
* HTTP request handler for WHOIS protocol requests sent to us by a proxy.
|
|
*
|
|
* <p>All commands and responses conform to the WHOIS spec as defined in RFC 3912. Commands must be
|
|
* sent via an HTTP POST in the request body.
|
|
*
|
|
* <p>This servlet is meant to serve as a low level interface for the proxy app which forwards us
|
|
* requests received on port 43. However this interface is technically higher level because it sends
|
|
* back proper HTTP error codes such as 200, 400, 500, etc. These are discarded by the proxy because
|
|
* WHOIS specifies no manner for differentiating successful and erroneous requests.
|
|
*
|
|
* @see WhoisHttpServer
|
|
* @see <a href="http://www.ietf.org/rfc/rfc3912.txt">RFC 3912: WHOIS Protocol Specification</a>
|
|
*/
|
|
@Action(
|
|
path = "/_dr/whois",
|
|
method = POST,
|
|
auth =
|
|
@Auth(
|
|
methods = {Auth.AuthMethod.INTERNAL, Auth.AuthMethod.API},
|
|
minimumLevel = AuthLevel.APP,
|
|
userPolicy = Auth.UserPolicy.ADMIN
|
|
)
|
|
)
|
|
public class WhoisServer implements Runnable {
|
|
|
|
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
|
|
|
|
/** WHOIS doesn't define an encoding, nor any way to specify an encoding in the protocol. */
|
|
static final MediaType CONTENT_TYPE = MediaType.PLAIN_TEXT_UTF_8;
|
|
|
|
/**
|
|
* As stated above, this is the low level interface intended for port 43, and as such, it
|
|
* always prefers ASCII.
|
|
*/
|
|
static final boolean PREFER_UNICODE = false;
|
|
|
|
@Inject Clock clock;
|
|
@Inject Reader input;
|
|
@Inject Response response;
|
|
@Inject WhoisReader whoisReader;
|
|
@Inject @Config("whoisDisclaimer") String disclaimer;
|
|
@Inject WhoisMetric.Builder metricBuilder;
|
|
@Inject WhoisMetrics whoisMetrics;
|
|
@Inject WhoisServer() {}
|
|
|
|
@Override
|
|
public void run() {
|
|
String responseText;
|
|
DateTime now = clock.nowUtc();
|
|
try {
|
|
WhoisCommand command = whoisReader.readCommand(input, now);
|
|
metricBuilder.setCommand(command);
|
|
WhoisResponseResults results =
|
|
command.executeQuery(now).getResponse(PREFER_UNICODE, disclaimer);
|
|
responseText = results.plainTextOutput();
|
|
metricBuilder.setNumResults(results.numResults());
|
|
metricBuilder.setStatus(SC_OK);
|
|
} catch (WhoisException e) {
|
|
WhoisResponseResults results = e.getResponse(PREFER_UNICODE, disclaimer);
|
|
responseText = results.plainTextOutput();
|
|
metricBuilder.setNumResults(0);
|
|
metricBuilder.setStatus(e.getStatus());
|
|
} catch (Throwable t) {
|
|
logger.severe(t, "WHOIS request crashed");
|
|
responseText = "Internal Server Error";
|
|
metricBuilder.setNumResults(0);
|
|
metricBuilder.setStatus(SC_INTERNAL_SERVER_ERROR);
|
|
}
|
|
// Note that we always return 200 (OK) even if an error was hit. This is because returning an
|
|
// non-OK HTTP status code will cause the proxy server to silently close the connection. Since
|
|
// WHOIS has no way to return errors, it's better to convert any such errors into strings and
|
|
// return them directly.
|
|
response.setStatus(SC_OK);
|
|
response.setContentType(CONTENT_TYPE);
|
|
response.setPayload(responseText);
|
|
whoisMetrics.recordWhoisMetric(metricBuilder.build());
|
|
}
|
|
}
|