001package org.hl7.fhir.utilities.http; 002 003/* 004 Copyright (c) 2011+, HL7, Inc. 005 All rights reserved. 006 007 Redistribution and use in source and binary forms, with or without modification, 008 are permitted provided that the following conditions are met: 009 010 * Redistributions of source code must retain the above copyright notice, this 011 list of conditions and the following disclaimer. 012 * Redistributions in binary form must reproduce the above copyright notice, 013 this list of conditions and the following disclaimer in the documentation 014 and/or other materials provided with the distribution. 015 * Neither the name of HL7 nor the names of its contributors may be used to 016 endorse or promote products derived from this software without specific 017 prior written permission. 018 019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 022 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 024 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 025 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 028 POSSIBILITY OF SUCH DAMAGE. 029 030 */ 031 032 033 034import java.io.IOException; 035import java.util.*; 036 037import lombok.Getter; 038import org.hl7.fhir.utilities.settings.FhirSettings; 039import org.hl7.fhir.utilities.settings.ServerDetailsPOJO; 040 041/** 042 * see security.md - manages web access by the FHIR HAPI Core library 043 * <p/> 044 * By using accessPolicy, allowedDomains and accessor, a host java application can control 045 * whether this library has direct access to the web (and which domains it is allowed to access), 046 * or whether the host application provides controlled access, or whether no access is allowed at all 047 * (in which case other information providers need to be provided) 048 * 049 * @author Grahame 050 * 051 */ 052public class ManagedWebAccess { 053 054 public interface IWebAccessor { 055 HTTPResult get(Iterable<String> serverTypes, String url, String accept, Map<String, String> headers) throws IOException; 056 HTTPResult post(Iterable<String> serverTypes, String url, byte[] bytes, String contentType, String accept, Map<String, String> headers) throws IOException; 057 HTTPResult put(Iterable<String> serverTypes, String url, byte[] bytes, String contentType, String accept, Map<String, String> headers) throws IOException; 058 } 059 060 public interface IFhirWebAccessor { 061 HTTPResult httpCall(HTTPRequest httpRequest); 062 } 063 064 public enum WebAccessPolicy { 065 DIRECT, // open access to the web, though access can be restricted only to domains in AllowedDomains 066 MANAGED, // no access except by the IWebAccessor 067 PROHIBITED, // no access at all to the web 068 } 069 070 private static WebAccessPolicy accessPolicy = WebAccessPolicy.DIRECT; // for legacy reasons 071 //TODO get this from fhir settings 072 private static List<String> allowedDomains = new ArrayList<>(); 073 @Getter 074 private static IWebAccessor accessor; 075 076 @Getter 077 private static IFhirWebAccessor fhirWebAccessor; 078 079 @Getter 080 private static String userAgent; 081 private static List<ServerDetailsPOJO> serverAuthDetails; 082 083 public static WebAccessPolicy getAccessPolicy() { 084 return accessPolicy; 085 } 086 087 public static void setAccessPolicy(WebAccessPolicy accessPolicy) { 088 ManagedWebAccess.accessPolicy = accessPolicy; 089 } 090 091 static boolean inAllowedPaths(String pathname) { 092 if (allowedDomains.isEmpty()) { 093 return true; 094 } 095 for (String s : allowedDomains) { 096 if (pathname.startsWith(s)) { 097 return true; 098 } 099 } 100 return false; 101 } 102 103 public static void setUserAgent(String userAgent) { 104 ManagedWebAccess.userAgent = userAgent; 105 } 106 107 public static ManagedWebAccessor accessor(Iterable<String> serverTypes) { 108 return new ManagedWebAccessor(serverTypes, userAgent, serverAuthDetails); 109 } 110 111 public static ManagedFhirWebAccessor fhirAccessor() { 112 return new ManagedFhirWebAccessor(userAgent, serverAuthDetails); 113 } 114 115 public static HTTPResult get(Iterable<String> serverTypes, String url) throws IOException { 116 return accessor(serverTypes).get(url); 117 } 118 119 public static HTTPResult get(Iterable<String> serverTypes, String url, String accept) throws IOException { 120 return accessor(serverTypes).get(url, accept); 121 } 122 123 public static HTTPResult post(Iterable<String> serverTypes, String url, byte[] content, String contentType, String accept) throws IOException { 124 return accessor(serverTypes).post(url, content, contentType, accept); 125 } 126 127 public static HTTPResult put(Iterable<String> serverTypes, String url, byte[] content, String contentType, String accept) throws IOException { 128 return accessor(serverTypes).put(url, content, contentType, accept); 129 } 130 131 public static HTTPResult httpCall(HTTPRequest httpRequest) throws IOException { 132 return fhirAccessor().httpCall(httpRequest); 133 } 134 135 public static void loadFromFHIRSettings() { 136 setAccessPolicy(FhirSettings.isProhibitNetworkAccess() ? WebAccessPolicy.PROHIBITED : WebAccessPolicy.DIRECT); 137 setUserAgent("hapi-fhir-tooling-client"); 138 serverAuthDetails = new ArrayList<>(); 139 serverAuthDetails.addAll(FhirSettings.getServers()); 140 } 141 142 public static void loadFromFHIRSettings(FhirSettings settings) { 143 setAccessPolicy(settings.isProhibitNetworkAccess() ? WebAccessPolicy.PROHIBITED : WebAccessPolicy.DIRECT); 144 setUserAgent("hapi-fhir-tooling-client"); 145 serverAuthDetails = new ArrayList<>(); 146 serverAuthDetails.addAll(settings.getServers()); 147 } 148}