-
-
Notifications
You must be signed in to change notification settings - Fork 426
/
Dsn.java
313 lines (275 loc) · 8.43 KB
/
Dsn.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package io.sentry.dsn;
import io.sentry.config.Lookup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.util.*;
/**
* Data Source name allowing a direct connection to a Sentry server.
*/
public class Dsn {
/**
* Default DSN to use when auto detection fails.
*/
public static final String DEFAULT_DSN = "noop://user:password@localhost:0/0";
private static final Logger logger = LoggerFactory.getLogger(Dsn.class);
private String secretKey;
private String publicKey;
private String projectId;
private String protocol;
private String host;
private int port;
private String path;
private Set<String> protocolSettings;
private Map<String, String> options;
private URI uri;
/**
* Creates a DSN based on the {@link #dsnLookup()} result.
*/
public Dsn() {
this(dsnLookup());
}
/**
* Creates a DSN based on a String.
*
* @param dsn DSN in a string form.
* @throws InvalidDsnException the given DSN is not valid.
*/
public Dsn(String dsn) throws InvalidDsnException {
this(URI.create(dsn));
}
/**
* Creates a DSN based on a URI.
*
* @param dsn DSN in URI form.
* @throws InvalidDsnException the given DSN is not valid.
*/
public Dsn(URI dsn) throws InvalidDsnException {
if (dsn == null) {
throw new InvalidDsnException("The sentry DSN must be provided and not be null");
}
options = new HashMap<>();
protocolSettings = new HashSet<>();
extractProtocolInfo(dsn);
extractUserKeys(dsn);
extractHostInfo(dsn);
extractPathInfo(dsn);
extractOptions(dsn);
makeOptionsImmutable();
validate();
try {
uri = new URI(protocol, null, host, port, path, null, null);
} catch (URISyntaxException e) {
throw new InvalidDsnException("Impossible to determine Sentry's URI from the DSN '" + dsn + "'", e);
}
}
/**
* Looks for a DSN configuration within JNDI, the System environment or Java properties.
*
* @return a DSN configuration or null if nothing could be found.
*/
public static String dsnLookup() {
String dsn = Lookup.lookup("dsn");
if (dsn == null) {
logger.warn("Couldn't find a suitable DSN, defaulting to a Noop one.");
dsn = DEFAULT_DSN;
}
return dsn;
}
/**
* Extracts the path and the project ID from the DSN provided as an {@code URI}.
*
* @param dsnUri DSN as an URI.
*/
private void extractPathInfo(URI dsnUri) {
String uriPath = dsnUri.getPath();
if (uriPath == null) {
return;
}
int projectIdStart = uriPath.lastIndexOf("/") + 1;
path = uriPath.substring(0, projectIdStart);
projectId = uriPath.substring(projectIdStart);
}
/**
* Extracts the hostname and port of the Sentry server from the DSN provided as an {@code URI}.
*
* @param dsnUri DSN as an URI.
*/
private void extractHostInfo(URI dsnUri) {
host = dsnUri.getHost();
port = dsnUri.getPort();
}
/**
* Extracts the scheme and additional protocol options from the DSN provided as an {@code URI}.
*
* @param dsnUri DSN as an URI.
*/
private void extractProtocolInfo(URI dsnUri) {
String scheme = dsnUri.getScheme();
if (scheme == null) {
return;
}
String[] schemeDetails = scheme.split("\\+");
protocolSettings.addAll(Arrays.asList(schemeDetails).subList(0, schemeDetails.length - 1));
protocol = schemeDetails[schemeDetails.length - 1];
}
/**
* Extracts the public and secret keys from the DSN provided as an {@code URI}.
*
* @param dsnUri DSN as an URI.
*/
private void extractUserKeys(URI dsnUri) {
String userInfo = dsnUri.getUserInfo();
if (userInfo == null) {
return;
}
String[] userDetails = userInfo.split(":");
publicKey = userDetails[0];
if (userDetails.length > 1) {
secretKey = userDetails[1];
}
}
/**
* Extracts the DSN options from the DSN provided as an {@code URI}.
*
* @param dsnUri DSN as an URI.
*/
private void extractOptions(URI dsnUri) {
String query = dsnUri.getQuery();
if (query == null || query.isEmpty()) {
return;
}
for (String optionPair : query.split("&")) {
try {
String[] pairDetails = optionPair.split("=");
String key = URLDecoder.decode(pairDetails[0], "UTF-8");
String value = pairDetails.length > 1 ? URLDecoder.decode(pairDetails[1], "UTF-8") : null;
options.put(key, value);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("Impossible to decode the query parameter '" + optionPair + "'", e);
}
}
}
/**
* Makes protocol and dsn options immutable to allow external usage.
*/
private void makeOptionsImmutable() {
// Make the options immutable
options = Collections.unmodifiableMap(options);
protocolSettings = Collections.unmodifiableSet(protocolSettings);
}
/**
* Validates internally the DSN, and check for mandatory elements.
* <p>
* Mandatory elements are the {@link #host}, {@link #publicKey}, {@link #secretKey} and {@link #projectId}.
*/
private void validate() {
List<String> missingElements = new LinkedList<>();
if (host == null) {
missingElements.add("host");
}
if (publicKey == null) {
missingElements.add("public key");
}
if (secretKey == null) {
missingElements.add("secret key");
}
if (projectId == null || projectId.isEmpty()) {
missingElements.add("project ID");
}
if (!missingElements.isEmpty()) {
throw new InvalidDsnException("Invalid DSN, the following properties aren't set '" + missingElements + "'");
}
}
public String getSecretKey() {
return secretKey;
}
public String getPublicKey() {
return publicKey;
}
public String getProjectId() {
return projectId;
}
public String getProtocol() {
return protocol;
}
public String getHost() {
return host;
}
public int getPort() {
return port;
}
public String getPath() {
return path;
}
public Set<String> getProtocolSettings() {
return protocolSettings;
}
public Map<String, String> getOptions() {
return options;
}
/**
* Creates the URI of the Sentry server.
*
* @return the URI of the Sentry server.
*/
public URI getUri() {
return uri;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Dsn dsn = (Dsn) o;
if (port != dsn.port) {
return false;
}
if (!host.equals(dsn.host)) {
return false;
}
if (!options.equals(dsn.options)) {
return false;
}
if (!path.equals(dsn.path)) {
return false;
}
if (!projectId.equals(dsn.projectId)) {
return false;
}
if (protocol != null ? !protocol.equals(dsn.protocol) : dsn.protocol != null) {
return false;
}
if (!protocolSettings.equals(dsn.protocolSettings)) {
return false;
}
if (!publicKey.equals(dsn.publicKey)) {
return false;
}
if (!secretKey.equals(dsn.secretKey)) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = publicKey.hashCode();
result = 31 * result + projectId.hashCode();
result = 31 * result + host.hashCode();
result = 31 * result + port;
result = 31 * result + path.hashCode();
return result;
}
@Override
public String toString() {
return "Dsn{"
+ "uri=" + uri
+ '}';
}
}