Skip to content

Commit

Permalink
Tags via custom HTTP header with Unit Test - provide some authenticat…
Browse files Browse the repository at this point in the history
…ion (OpenTSDB#1003)

* Allow tags to be added via custom HTTP header

* Add unit test for custom http header

* Correction of an error caused by a bad file name in makefile

* Suppression of modifications for test travis compilation

Signed-off-by: Chris Larsen <[email protected]>
  • Loading branch information
bhourlier authored and manolama committed May 22, 2018
1 parent 26355ac commit e1937d3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/core/IncomingDataPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ public final String getTSUID() {
return tsuid;
}

/** @param moretags the hashmap of kv pair to add */
public final void addTags(HashMap<String, String> moretags) {
this.tags.putAll(moretags);
}

/** @param metric the metric to set */
public final void setMetric(String metric) {
this.metric = metric;
Expand Down
10 changes: 10 additions & 0 deletions src/tsd/AbstractHttpQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ public Map<String, String> getHeaders() {
return headers;
}

/**
* Return the value of the given HTTP Header
* first match wins
* @return Header value as string
*/
public String getHeaderValue(final String headerName) {
if (headerName == null) { return null; }
return request.headers().get(headerName);
}

/** @param stats The stats object to mark after writing is complete */
public void setStats(final QueryStats stats) {
this.stats = stats;
Expand Down
20 changes: 19 additions & 1 deletion src/tsd/PutDataPointRpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ public <T extends IncomingDataPoint> void processDataPoint(final TSDB tsdb,
throw new BadRequestException("No datapoints found in content");
}

final HashMap<String, String> query_tags = new HashMap<String, String>();
final boolean show_details = query.hasQueryStringParam("details");
final boolean show_summary = query.hasQueryStringParam("summary");
final boolean synchronous = query.hasQueryStringParam("sync");
Expand All @@ -326,7 +327,18 @@ public <T extends IncomingDataPoint> void processDataPoint(final TSDB tsdb,
int queued = 0;
final List<Deferred<Boolean>> deferreds = synchronous ?
new ArrayList<Deferred<Boolean>>(dps.size()) : null;


if (tsdb.getConfig().enable_header_tag()) {
LOG.debug("Looking for tag header " + tsdb.getConfig().get_name_header_tag());
final String header_tag_value = query.getHeaderValue(tsdb.getConfig().get_name_header_tag()) ;
if (header_tag_value != null) {
LOG.debug(" header found with value:" + header_tag_value);
Tags.parse(query_tags, header_tag_value);
} else {
LOG.debug(" no such header in request");
}
}

for (final IncomingDataPoint dp : dps) {
final DataPointType type;
if (dp instanceof RollUpDataPoint) {
Expand Down Expand Up @@ -387,10 +399,16 @@ public Boolean call(final Object obj) {
}

try {
/** Add additionnal tags from HTTP header */
if ( (query_tags != null) && (query_tags.size() > 0) ) {
dp.addTags(query_tags);
}

if (!dp.validate(details)) {
illegal_arguments.incrementAndGet();
continue;
}

// TODO - refactor the add calls someday or move some of this into the
// actual data point class.
final Deferred<Boolean> deferred;
Expand Down
17 changes: 17 additions & 0 deletions src/utils/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public class Config {
/** tsd.storage.fix_duplicates */
private boolean fix_duplicates = false;

/** tsd.http.header_tag */
private String http_header_tag = null;

/** tsd.http.request.max_chunk */
private int max_chunked_requests = 4096;

Expand Down Expand Up @@ -253,6 +256,16 @@ public int scanner_maxNumRows() {
return scanner_max_num_rows;
}

/** @return whether or not additional http header tag is allowed */
public boolean enable_header_tag() {
return http_header_tag != null ;
}

/** @return the lookup value for additional http header tag */
public String get_name_header_tag() {
return http_header_tag ;
}

/** @return whether or not chunked requests are supported */
public boolean enable_chunked_requests() {
return enable_chunked_requests;
Expand Down Expand Up @@ -597,6 +610,7 @@ protected void setDefaults() {
default_map.put("tsd.core.stats_with_port", "false");
default_map.put("tsd.http.show_stack_trace", "true");
default_map.put("tsd.http.query.allow_delete", "false");
default_map.put("tsd.http.header_tag", "");
default_map.put("tsd.http.request.enable_chunked", "false");
default_map.put("tsd.http.request.max_chunk", "4096");
default_map.put("tsd.http.request.cors_domains", "");
Expand Down Expand Up @@ -717,6 +731,9 @@ public void loadStaticVariables() {
if (this.hasProperty("tsd.http.request.max_chunk")) {
max_chunked_requests = this.getInt("tsd.http.request.max_chunk");
}
if (this.hasProperty("tsd.http.header_tag")) {
http_header_tag = this.getString("tsd.http.header_tag");
}
enable_tree_processing = this.getBoolean("tsd.core.tree.enable_processing");
fix_duplicates = this.getBoolean("tsd.storage.fix_duplicates");
scanner_max_num_rows = this.getInt("tsd.storage.hbase.scanner.maxNumRows");
Expand Down

0 comments on commit e1937d3

Please sign in to comment.