|
| 1 | +/* |
| 2 | + * Copyright The Hypertrace Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.hypertrace.agent.filter.opa.custom; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.databind.JsonNode; |
| 20 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | +import java.io.IOException; |
| 22 | +import java.security.KeyManagementException; |
| 23 | +import java.security.NoSuchAlgorithmException; |
| 24 | +import java.security.cert.CertificateException; |
| 25 | +import javax.net.ssl.*; |
| 26 | +import okhttp3.Interceptor; |
| 27 | +import okhttp3.OkHttpClient; |
| 28 | +import okhttp3.Request; |
| 29 | +import okhttp3.Response; |
| 30 | +import org.hypertrace.agent.filter.opa.custom.data.BlockingData; |
| 31 | +import org.slf4j.Logger; |
| 32 | +import org.slf4j.LoggerFactory; |
| 33 | + |
| 34 | +public class OpaCommunicator { |
| 35 | + private static final Logger log = LoggerFactory.getLogger(OpaCommunicator.class); |
| 36 | + |
| 37 | + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
| 38 | + private static final String PATH = "/v1/data"; |
| 39 | + |
| 40 | + private OkHttpClient httpClient; |
| 41 | + private Request request; |
| 42 | + |
| 43 | + private BlockingData blockingData; |
| 44 | + |
| 45 | + protected OpaCommunicator() {} |
| 46 | + |
| 47 | + public void init(String endpoint, String authToken, boolean skipVerify) { |
| 48 | + OkHttpClient.Builder builder = new OkHttpClient.Builder(); |
| 49 | + if (authToken != null && !authToken.isEmpty()) { |
| 50 | + log.info("Adding authentication key"); |
| 51 | + builder = withAuth(builder, authToken); |
| 52 | + } |
| 53 | + if (skipVerify) { |
| 54 | + builder = withSkipVerify(builder); |
| 55 | + } |
| 56 | + this.httpClient = builder.build(); |
| 57 | + if (endpoint.endsWith("/")) { |
| 58 | + endpoint = endpoint.substring(0, endpoint.length() - 1); |
| 59 | + } |
| 60 | + this.request = new Request.Builder().url(endpoint + PATH).get().build(); |
| 61 | + } |
| 62 | + |
| 63 | + public void pollBlockingData() { |
| 64 | + if (httpClient == null) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + Response response; |
| 69 | + try { |
| 70 | + response = httpClient.newCall(request).execute(); |
| 71 | + } catch (IOException e) { |
| 72 | + log.warn("Unable to make a successful get call to the OPA service.", e); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + log.trace("Received response from OPA service: {}", response); |
| 77 | + if (response.isSuccessful()) { |
| 78 | + try { |
| 79 | + JsonNode jsonNode = OBJECT_MAPPER.readTree(response.body().byteStream()); |
| 80 | + if (log.isDebugEnabled()) { |
| 81 | + log.debug("Received blocking data from OPA service: {}", jsonNode); |
| 82 | + } |
| 83 | + blockingData = |
| 84 | + OBJECT_MAPPER.treeToValue( |
| 85 | + jsonNode.at("/result/traceable/http/request"), BlockingData.class); |
| 86 | + } catch (IOException e) { |
| 87 | + log.warn("Unable to retrieve blocking data from the OPA service.", e); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public BlockingData getBlockingData() { |
| 93 | + return blockingData; |
| 94 | + } |
| 95 | + |
| 96 | + public void clear() { |
| 97 | + httpClient = null; |
| 98 | + request = null; |
| 99 | + blockingData = null; |
| 100 | + } |
| 101 | + |
| 102 | + private static OkHttpClient.Builder withAuth(OkHttpClient.Builder builder, String authToken) { |
| 103 | + builder.addInterceptor(getAuthInterceptor("Bearer " + authToken)); |
| 104 | + return builder; |
| 105 | + } |
| 106 | + |
| 107 | + private static OkHttpClient.Builder withSkipVerify(OkHttpClient.Builder builder) { |
| 108 | + // Install the all-trusting trust manager |
| 109 | + try { |
| 110 | + TrustManager[] trustAllCertsManagers = getTrustAllCertsManagers(); |
| 111 | + final SSLContext sslContext = SSLContext.getInstance("SSL"); |
| 112 | + sslContext.init(null, trustAllCertsManagers, new java.security.SecureRandom()); |
| 113 | + // Create an ssl socket factory with our all-trusting manager |
| 114 | + final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); |
| 115 | + builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCertsManagers[0]); |
| 116 | + builder.hostnameVerifier(getSkipVerifyHostnameVerifier()); |
| 117 | + } catch (NoSuchAlgorithmException e) { |
| 118 | + log.warn("Error in getting SSL context. SkipVerify could not be set to true.", e); |
| 119 | + } catch (KeyManagementException e) { |
| 120 | + log.warn("Error in initializing SSL context. SkipVerify could not be set to true.", e); |
| 121 | + } |
| 122 | + return builder; |
| 123 | + } |
| 124 | + |
| 125 | + private static Interceptor getAuthInterceptor(final String headerValue) { |
| 126 | + return new Interceptor() { |
| 127 | + @Override |
| 128 | + public Response intercept(Chain chain) throws IOException { |
| 129 | + return chain.proceed( |
| 130 | + chain.request().newBuilder().addHeader("Authorization", headerValue).build()); |
| 131 | + } |
| 132 | + }; |
| 133 | + } |
| 134 | + |
| 135 | + private static TrustManager[] getTrustAllCertsManagers() { |
| 136 | + return new TrustManager[] { |
| 137 | + new X509TrustManager() { |
| 138 | + @Override |
| 139 | + public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) |
| 140 | + throws CertificateException {} |
| 141 | + |
| 142 | + @Override |
| 143 | + public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) |
| 144 | + throws CertificateException {} |
| 145 | + |
| 146 | + @Override |
| 147 | + public java.security.cert.X509Certificate[] getAcceptedIssuers() { |
| 148 | + return new java.security.cert.X509Certificate[] {}; |
| 149 | + } |
| 150 | + } |
| 151 | + }; |
| 152 | + } |
| 153 | + |
| 154 | + private static HostnameVerifier getSkipVerifyHostnameVerifier() { |
| 155 | + return new HostnameVerifier() { |
| 156 | + @Override |
| 157 | + public boolean verify(String hostname, SSLSession session) { |
| 158 | + return true; |
| 159 | + } |
| 160 | + }; |
| 161 | + } |
| 162 | +} |
0 commit comments