ServiceUtil { public boolean isValidateHttpsAuthentication( String serviceName, HttpServletRequest request) { boolean isValidAuthentication=false; boolean httpEnabled = PropertiesUtil.getBooleanProperty("ranger.service.http.enabled",true); X509Certificate[] certchain = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate"); String ipAddress = request.getHeader("X-FORWARDED-FOR"); if (ipAddress == null) { ipAddress = request.getRemoteAddr(); } boolean isSecure = request.isSecure(); if (serviceName == null || serviceName.isEmpty()) { LOG.error("ServiceName not provided"); throw restErrorUtil.createRESTException("Unauthorized access.", MessageEnums.OPER_NOT_ALLOWED_FOR_ENTITY); } RangerService service = null; try { service = svcStore.getServiceByName(serviceName); } catch (Exception e) { LOG.error("Requested Service not found. serviceName=" + serviceName); throw restErrorUtil.createRESTException("Service:" + serviceName + " not found", MessageEnums.DATA_NOT_FOUND); } if(service==null){ LOG.error("Requested Service not found. serviceName=" + serviceName); throw restErrorUtil.createRESTException(HttpServletResponse.SC_NOT_FOUND, RangerServiceNotFoundException.buildExceptionMsg(serviceName), false); } if(!service.getIsEnabled()){ LOG.error("Requested Service is disabled. serviceName=" + serviceName); throw restErrorUtil.createRESTException("Unauthorized access.", MessageEnums.OPER_NOT_ALLOWED_FOR_STATE); } if (!httpEnabled) { if (!isSecure) { LOG.error("Unauthorized access. Only https is allowed. serviceName=" + serviceName); throw restErrorUtil.createRESTException("Unauthorized access -" + " only https allowed", MessageEnums.OPER_NOT_ALLOWED_FOR_ENTITY); } if (certchain == null || certchain.length == 0) { LOG.error("Unauthorized access. Unable to get client certificate. serviceName=" + serviceName); throw restErrorUtil.createRESTException("Unauthorized access -" + " unable to get client certificate", MessageEnums.OPER_NOT_ALLOWED_FOR_ENTITY); } Map<String, String> configMap = service.getConfigs(); String cnFromConfig = configMap.get("commonNameForCertificate"); if (cnFromConfig == null || "".equals(cnFromConfig.trim())) { LOG.error("Unauthorized access. No common name for certificate set. Please check your service config"); throw restErrorUtil.createRESTException("Unauthorized access. No common name for certificate set. Please check your service config", MessageEnums.OPER_NOT_ALLOWED_FOR_ENTITY); } String cnFromConfigForTest = cnFromConfig; boolean isRegEx = cnFromConfig.toLowerCase().startsWith(REGEX_PREFIX_STR); if (isRegEx) { cnFromConfigForTest = cnFromConfig.substring(REGEX_PREFIX_STR_LENGTH); } try { Collection<List<?>> subjectAltNames = certchain[0].getSubjectAlternativeNames(); if (subjectAltNames != null) { for (List<?> sanItem : subjectAltNames) { if (sanItem.size() == 2) { Integer sanType = (Integer) sanItem.get(0); String sanValue = (String) sanItem.get(1); if ( (sanType == 2 || sanType == 7) && (matchNames(sanValue, cnFromConfigForTest,isRegEx)) ) { if (LOG.isDebugEnabled()) LOG.debug("Client Cert verification successful, matched SAN:" + sanValue); isValidAuthentication=true; break; } } } } } catch (Throwable e) { LOG.error("Unauthorized access. Error getting SAN from certificate", e); throw restErrorUtil.createRESTException("Unauthorized access - Error getting SAN from client certificate", MessageEnums.OPER_NOT_ALLOWED_FOR_ENTITY); } if (!isValidAuthentication) { String commonName = null; if (certchain != null) { X509Certificate clientCert = certchain[0]; String dn = clientCert.getSubjectX500Principal().getName(); try { LdapName ln = new LdapName(dn); for (Rdn rdn : ln.getRdns()) { if ("CN".equalsIgnoreCase(rdn.getType())) { commonName = rdn.getValue() + ""; break; } } if (commonName == null) { LOG.error("Unauthorized access. CName is null. serviceName=" + serviceName); throw restErrorUtil.createRESTException( "Unauthorized access - Unable to find Common Name from [" + dn + "]", MessageEnums.OPER_NOT_ALLOWED_FOR_ENTITY); } } catch (InvalidNameException e) { LOG.error("Invalid Common Name. CName=" + commonName + ", serviceName=" + serviceName, e); throw restErrorUtil.createRESTException( "Unauthorized access - Invalid Common Name", MessageEnums.OPER_NOT_ALLOWED_FOR_ENTITY); } } if (commonName != null) { if (matchNames(commonName, cnFromConfigForTest,isRegEx)) { if (LOG.isDebugEnabled()) LOG.debug("Client Cert verification successful, matched CN " + commonName + " with " + cnFromConfigForTest + ", wildcard match = " + isRegEx); isValidAuthentication=true; } if (!isValidAuthentication) { LOG.error("Unauthorized access. expected [" + cnFromConfigForTest + "], found [" + commonName + "], serviceName=" + serviceName); throw restErrorUtil.createRESTException( "Unauthorized access. expected [" + cnFromConfigForTest + "], found [" + commonName + "]", MessageEnums.OPER_NOT_ALLOWED_FOR_ENTITY); } } } } else { isValidAuthentication = true; } return isValidAuthentication; }  RangerService getServiceByName(@PathParam("name") String name); RangerService toRangerService(VXAsset asset); VXAsset toVXAsset(RangerService service); VXRepository toVXRepository(RangerService service); RangerPolicy toRangerPolicy(VXResource resource, RangerService service); VXResource toVXResource(RangerPolicy policy, RangerService service); VXAsset publicObjecttoVXAsset(VXRepository vXRepository); VXRepository vXAssetToPublicObject(VXAsset asset); static String toAccessType(int permType); SearchCriteria getMappedSearchParams(HttpServletRequest request,
			SearchCriteria searchCriteria); VXRepositoryList rangerServiceListToPublicObjectList(List<RangerService> serviceList); VXPolicy toVXPolicy(RangerPolicy policy, RangerService service); List<VXPermMap> getVXPermMapList(RangerPolicy policy); List<VXPermObj> mapPermMapToPermObj(List<VXPermMap> permMapList); RangerPolicy toRangerPolicy(VXPolicy vXPolicy, RangerService service ); VXPolicyList rangerPolicyListToPublic(List<RangerPolicy> rangerPolicyList,SearchFilter filter); GrantRevokeRequest toGrantRevokeRequest(VXPolicy vXPolicy); boolean isValidateHttpsAuthentication( String serviceName, HttpServletRequest request); boolean isValidService(String serviceName, HttpServletRequest request); List<RangerPolicy> getMatchingPoliciesForResource(HttpServletRequest request,
			List<RangerPolicy> policyLists);  }
@Test public void testIsValidateHttpsAuthentication() throws Exception{ RangerService rangerService = new RangerService(); rangerService.setId(1L); rangerService.setName("hiveService"); rangerService.setIsEnabled(true); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); String serviceName = "hiveService"; Mockito.when(svcStore.getServiceByName(serviceName)).thenReturn(rangerService); boolean isValidAuthentication = serviceUtil.isValidateHttpsAuthentication(serviceName, request); Assert.assertTrue(isValidAuthentication); }