|
1 | 1 | package com.datadog.aiguard; |
2 | 2 |
|
| 3 | +import static datadog.trace.util.Strings.isBlank; |
3 | 4 | import static java.util.Collections.singletonMap; |
4 | 5 |
|
5 | 6 | import com.squareup.moshi.JsonAdapter; |
@@ -70,12 +71,12 @@ public static void install() { |
70 | 71 | final Config config = Config.get(); |
71 | 72 | final String apiKey = config.getApiKey(); |
72 | 73 | final String appKey = config.getApplicationKey(); |
73 | | - if (isEmpty(apiKey) || isEmpty(appKey)) { |
| 74 | + if (isBlank(apiKey) || isBlank(appKey)) { |
74 | 75 | throw new BadConfigurationException( |
75 | 76 | "AI Guard: Missing api and/or application key, use DD_API_KEY and DD_APP_KEY"); |
76 | 77 | } |
77 | 78 | String endpoint = config.getAiGuardEndpoint(); |
78 | | - if (isEmpty(endpoint)) { |
| 79 | + if (isBlank(endpoint)) { |
79 | 80 | endpoint = String.format("https://app.%s/api/v2/ai-guard", config.getSite()); |
80 | 81 | } |
81 | 82 | final Map<String, String> headers = mapOf("DD-API-KEY", apiKey, "DD-APPLICATION-KEY", appKey); |
@@ -157,8 +158,8 @@ private static String getToolName(final Message current, final List<Message> mes |
157 | 158 | return null; |
158 | 159 | } |
159 | 160 |
|
160 | | - private boolean isBlockingEnabled(final Object isBlockingEnabled) { |
161 | | - return isBlockingEnabled != null && isBlockingEnabled.toString().equalsIgnoreCase("true"); |
| 161 | + private boolean isBlockingEnabled(final Options options, final Object isBlockingEnabled) { |
| 162 | + return options.block() && "true".equalsIgnoreCase(isBlockingEnabled.toString()); |
162 | 163 | } |
163 | 164 |
|
164 | 165 | @Override |
@@ -202,8 +203,9 @@ public Evaluation evaluate(final List<Message> messages, final Options options) |
202 | 203 | final String reason = (String) result.get("reason"); |
203 | 204 | span.setTag(ACTION_TAG, action); |
204 | 205 | span.setTag(REASON_TAG, reason); |
205 | | - final boolean blockingEnabled = isBlockingEnabled(result.get("is_blocking_enabled")); |
206 | | - if (blockingEnabled && options.block() && action != Action.ALLOW) { |
| 206 | + final boolean blockingEnabled = |
| 207 | + isBlockingEnabled(options, result.get("is_blocking_enabled")); |
| 208 | + if (blockingEnabled && action != Action.ALLOW) { |
207 | 209 | span.setTag(BLOCKED_TAG, true); |
208 | 210 | throw new AIGuardAbortError(action, reason); |
209 | 211 | } |
@@ -246,10 +248,6 @@ private static OkHttpClient buildClient(final HttpUrl url, final long timeout) { |
246 | 248 | return OkHttpUtils.buildHttpClient(url, timeout).newBuilder().build(); |
247 | 249 | } |
248 | 250 |
|
249 | | - private static boolean isEmpty(final String value) { |
250 | | - return value == null || value.isEmpty(); |
251 | | - } |
252 | | - |
253 | 251 | private static Map<String, String> mapOf( |
254 | 252 | final String key1, final String prop1, final String key2, final String prop2) { |
255 | 253 | final Map<String, String> map = new HashMap<>(2); |
|
0 commit comments