Search in sources :

Example 1 with SecureAdmin

use of com.sun.enterprise.config.serverbeans.SecureAdmin in project Payara by payara.

the class ProxyImpl method proxyRequest.

@Override
public Properties proxyRequest(UriInfo sourceUriInfo, Client client, ServiceLocator habitat) {
    Properties proxiedResponse = new Properties();
    try {
        Domain domain = habitat.getService(Domain.class);
        String forwardInstanceName = extractTargetInstanceName(sourceUriInfo);
        Server forwardInstance = domain.getServerNamed(forwardInstanceName);
        if (forwardInstance != null) {
            UriBuilder forwardUriBuilder = constructForwardURLPath(sourceUriInfo);
            // Host and Port are replaced to that of forwardInstanceName
            URI forwardURI = forwardUriBuilder.scheme("https").host(forwardInstance.getAdminHost()).port(forwardInstance.getAdminPort()).build();
            client = addAuthenticationInfo(client, forwardInstance, habitat);
            WebTarget resourceBuilder = client.target(forwardURI);
            SecureAdmin secureAdmin = habitat.getService(SecureAdmin.class);
            Builder builder = resourceBuilder.request(MediaType.APPLICATION_JSON).header(SecureAdmin.Util.ADMIN_INDICATOR_HEADER_NAME, secureAdmin.getSpecialAdminIndicator());
            // TODO if the target server is down, we get ClientResponseException. Need to handle it
            Response response = builder.get(Response.class);
            Response.Status status = Response.Status.fromStatusCode(response.getStatus());
            if (status.getFamily() == javax.ws.rs.core.Response.Status.Family.SUCCESSFUL) {
                String jsonDoc = response.readEntity(String.class);
                Map responseMap = MarshallingUtils.buildMapFromDocument(jsonDoc);
                Map resultExtraProperties = (Map) responseMap.get("extraProperties");
                if (resultExtraProperties != null) {
                    Object entity = resultExtraProperties.get("entity");
                    if (entity != null) {
                        proxiedResponse.put("entity", entity);
                    }
                    @SuppressWarnings({ "unchecked" }) Map<String, String> childResources = (Map<String, String>) resultExtraProperties.get("childResources");
                    for (Map.Entry<String, String> entry : childResources.entrySet()) {
                        String targetURL = null;
                        try {
                            URL originalURL = new URL(entry.getValue());
                            // Construct targetURL which has host+port of DAS and path from originalURL
                            targetURL = constructTargetURLPath(sourceUriInfo, originalURL).build().toASCIIString();
                        } catch (MalformedURLException e) {
                        // TODO There was an exception while parsing URL. Need to decide what to do. For now ignore the child entry
                        }
                        entry.setValue(targetURL);
                    }
                    proxiedResponse.put("childResources", childResources);
                }
                Object message = responseMap.get("message");
                if (message != null) {
                    proxiedResponse.put("message", message);
                }
                Object properties = responseMap.get("properties");
                if (properties != null) {
                    proxiedResponse.put("properties", properties);
                }
            } else {
                throw new WebApplicationException(response.readEntity(String.class), status);
            }
        } else {
        // server == null
        // TODO error to user. Can not locate server for whom data is being looked for
        }
    } catch (Exception ex) {
        throw new WebApplicationException(ex, Response.Status.INTERNAL_SERVER_ERROR);
    }
    return proxiedResponse;
}
Also used : MalformedURLException(java.net.MalformedURLException) Server(com.sun.enterprise.config.serverbeans.Server) WebApplicationException(javax.ws.rs.WebApplicationException) ClientBuilder(javax.ws.rs.client.ClientBuilder) Builder(javax.ws.rs.client.Invocation.Builder) UriBuilder(javax.ws.rs.core.UriBuilder) Properties(java.util.Properties) URI(java.net.URI) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) WebApplicationException(javax.ws.rs.WebApplicationException) Response(javax.ws.rs.core.Response) SecureAdmin(com.sun.enterprise.config.serverbeans.SecureAdmin) WebTarget(javax.ws.rs.client.WebTarget) Domain(com.sun.enterprise.config.serverbeans.Domain) UriBuilder(javax.ws.rs.core.UriBuilder) Map(java.util.Map)

Example 2 with SecureAdmin

use of com.sun.enterprise.config.serverbeans.SecureAdmin in project Payara by payara.

the class AdminConsoleAuthModule method getAuthenticationURL.

/**
 * Compute the rest URL needed to authenticate a user
 * @return
 */
private String getAuthenticationURL() {
    ServiceLocator habitat = SecurityServicesUtil.getInstance().getHabitat();
    Domain domain = habitat.getService(Domain.class);
    SecureAdmin secureAdmin = habitat.getService(SecureAdmin.class);
    NetworkListener adminListener = domain.getServerNamed("server").getConfig().getNetworkConfig().getNetworkListener("admin-listener");
    String host = adminListener.getAddress();
    String port = adminListener.getPort();
    return (SecureAdmin.Util.isEnabled(secureAdmin) ? "https://" : "http://") + (host.equals("0.0.0.0") ? "localhost" : host) + ":" + port + "/management/sessions";
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) SecureAdmin(com.sun.enterprise.config.serverbeans.SecureAdmin) Domain(com.sun.enterprise.config.serverbeans.Domain) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener)

Example 3 with SecureAdmin

use of com.sun.enterprise.config.serverbeans.SecureAdmin in project Payara by payara.

the class RestUtil method initialize.

// ******************************************************************************************************************
// Jersey client methods
// ******************************************************************************************************************
public static ClientBuilder initialize(ClientBuilder clientBuilder) {
    try {
        ServiceLocator habitat = SecurityServicesUtil.getInstance().getHabitat();
        SecureAdmin secureAdmin = habitat.getService(SecureAdmin.class);
        final SSLContext sslContext = habitat.<SSLUtils>getService(SSLUtils.class).getAdminSSLContext(SecureAdmin.Util.DASAlias(secureAdmin), null);
        // Instruct Jersey to use HostNameVerifier and SSLContext provided by us.
        clientBuilder.hostnameVerifier(new BasicHostnameVerifier()).sslContext(sslContext).register(CsrfProtectionFilter.class);
    } catch (Exception ex) {
        GuiUtil.getLogger().warning("RestUtil.initialize() failed");
        if (GuiUtil.getLogger().isLoggable(Level.FINE)) {
            ex.printStackTrace();
        }
    }
    return clientBuilder;
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) SecureAdmin(com.sun.enterprise.config.serverbeans.SecureAdmin) SSLContext(javax.net.ssl.SSLContext) SSLUtils(com.sun.enterprise.security.ssl.SSLUtils) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 4 with SecureAdmin

use of com.sun.enterprise.config.serverbeans.SecureAdmin in project Payara by payara.

the class ProxyImpl method addAuthenticationInfo.

/**
 * Use SSL to authenticate
 */
private Client addAuthenticationInfo(Client client, Server server, ServiceLocator habitat) {
    SecureAdmin secureAdmin = habitat.getService(SecureAdmin.class);
    // TODO need to get hardcoded "TLS" from corresponding ServerRemoteAdminCommand constant);
    final SSLContext sslContext = habitat.<SSLUtils>getService(SSLUtils.class).getAdminSSLContext(SecureAdmin.Util.DASAlias(secureAdmin), "TLS");
    // Instruct Jersey to use HostNameVerifier and SSLContext provided by us.
    final ClientBuilder clientBuilder = ClientBuilder.newBuilder().withConfig(client.getConfiguration()).hostnameVerifier(new BasicHostnameVerifier(server.getAdminHost())).sslContext(sslContext);
    return clientBuilder.build();
}
Also used : SecureAdmin(com.sun.enterprise.config.serverbeans.SecureAdmin) SSLContext(javax.net.ssl.SSLContext) SSLUtils(com.sun.enterprise.security.ssl.SSLUtils) ClientBuilder(javax.ws.rs.client.ClientBuilder)

Aggregations

SecureAdmin (com.sun.enterprise.config.serverbeans.SecureAdmin)4 Domain (com.sun.enterprise.config.serverbeans.Domain)2 SSLUtils (com.sun.enterprise.security.ssl.SSLUtils)2 SSLContext (javax.net.ssl.SSLContext)2 ClientBuilder (javax.ws.rs.client.ClientBuilder)2 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)2 Server (com.sun.enterprise.config.serverbeans.Server)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URL (java.net.URL)1 Map (java.util.Map)1 Properties (java.util.Properties)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Builder (javax.ws.rs.client.Invocation.Builder)1 WebTarget (javax.ws.rs.client.WebTarget)1 Response (javax.ws.rs.core.Response)1 UriBuilder (javax.ws.rs.core.UriBuilder)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)1