Skip to content
This repository has been archived by the owner on Mar 27, 2021. It is now read-only.

Enforce Mandatory Client ID (Github Issue #704) #727

Merged
merged 36 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
fe4b2c2
implemented configurable Bigtable write batch size.
sming Oct 19, 2020
a18faa1
added extra tests for config parsing
sming Oct 21, 2020
a4fc63b
Merge branch 'master' into feature/add-configurable-bt-batch-size
sming Oct 21, 2020
2719c42
implemented PR review comments
sming Oct 21, 2020
46bfffa
Merge branch 'master' into feature/add-configurable-bt-batch-size
sming Oct 23, 2020
8154645
fixed checkstyle whinging
sming Oct 25, 2020
81f85a6
added comprehensive Bigtable config logging
sming Oct 27, 2020
dd0c125
Merge branch 'feature/add-configurable-bt-batch-size' into feature/ma…
sming Oct 31, 2020
cab64cf
WIP Mandatory Client ID implementation
sming Nov 3, 2020
a73f54a
Merge remote-tracking branch 'origin/master' into feature/mandatory-c…
sming Nov 20, 2020
e58f93d
WIP impl mostly in place
sming Nov 23, 2020
e841202
merged in master
sming Jan 13, 2021
fca7748
tidied up remainder of master merge-in
sming Jan 13, 2021
7f0011e
got it working. ripped out unneccesary config code
sming Jan 15, 2021
b7543d2
fixed parameter name warning
sming Jan 15, 2021
81fae0a
removed old documentation, fixed compilation warning
sming Jan 17, 2021
c1ac514
added decent unit x-client-id tests
sming Jan 17, 2021
e7f3bcb
undid accidental test modification
sming Jan 18, 2021
1108330
Merge branch 'master' into feature/mandatory-client-id
sming Jan 18, 2021
ef36ff3
added x-client-id header to system test request
sming Jan 18, 2021
4e7478c
Merge remote-tracking branch 'origin/feature/mandatory-client-id' int…
sming Jan 18, 2021
5956468
removed extraneous additional test clause
sming Jan 19, 2021
b815591
add negative test for x-client-id enforcement
sming Jan 19, 2021
21eecec
correct negative test for x-client-id enforcement
sming Jan 19, 2021
f22dd70
corrected merge error, formatting
sming Jan 19, 2021
6691105
merged in master
sming Jan 24, 2021
2af4f8f
merged in master
sming Jan 24, 2021
51a9da8
merged in master
sming Jan 24, 2021
38e3d72
Restore Module.java
sming Jan 24, 2021
55b7b1c
Restore FilterAggregationTest.java
sming Jan 24, 2021
1b2160d
Restore FilterKAreaAggregationTest.java
sming Jan 24, 2021
281c79b
Restore AggregationCombiner.java
sming Jan 24, 2021
8f572b5
Restore AnyBucket.java
sming Jan 24, 2021
b2d6061
Revert "Restore Module.java"
sming Jan 24, 2021
d1b14a0
Delete WIP_inject_attempt_peter.patch
sming Jan 25, 2021
e333355
Merge branch 'master' into feature/mandatory-client-id
sming Jan 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@
import com.spotify.heroic.grammar.IntegerExpression;
import dagger.Component;
import eu.toolchain.serializer.SerializerFramework;

import java.util.Optional;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.Optional;

public class Module implements HeroicModule {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.spotify.heroic.aggregation.simple;

import static org.junit.Assert.assertEquals;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.spotify.heroic.aggregation.AggregationInstance;
import com.spotify.heroic.test.FakeModuleLoader;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class FilterAggregationTest {
private ObjectMapper mapper;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.spotify.heroic.aggregation.simple;

import static org.junit.Assert.assertEquals;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.spotify.heroic.aggregation.AggregationInstance;
Expand All @@ -12,17 +14,14 @@
import com.spotify.heroic.common.DateRange;
import com.spotify.heroic.common.Series;
import com.spotify.heroic.metric.Point;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class FilterKAreaAggregationTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.spotify.heroic.aggregation.simple;


import static org.junit.Assert.assertEquals;


import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.ByteString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
import com.spotify.heroic.aggregation.DoubleBucket;
import com.spotify.heroic.aggregation.TDigestBucket;
import com.spotify.heroic.metric.DistributionPoint;
import com.spotify.heroic.metric.HeroicDistribution;
import com.spotify.heroic.metric.Point;
import com.tdunning.math.stats.MergingDigest;
import com.tdunning.math.stats.TDigest;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -25,7 +22,6 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.google.protobuf.ByteString;

public abstract class ValueBucketIntegrationTest {
private static final int NCPU = Runtime.getRuntime().availableProcessors();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

package com.spotify.heroic.consumer.pubsub;

import com.google.common.collect.ImmutableMap;
import com.spotify.heroic.common.Statistics;
import com.spotify.heroic.consumer.Consumer;
import com.spotify.heroic.lifecycle.LifeCycleRegistry;
import com.spotify.heroic.lifecycle.LifeCycles;
import com.google.common.collect.ImmutableMap;
import eu.toolchain.async.AsyncFramework;
import eu.toolchain.async.AsyncFuture;
import eu.toolchain.async.Managed;
Expand Down
4 changes: 3 additions & 1 deletion docs/_layouts/api-endpoint.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ <h4>Example Request</h4>
<h4>Example Curl</h4>
<pre><code class="language-bash">
{%- capture headers %}
{%- unless page.empty %}-H "Content-Type: application/json" {% endunless %}
{%- unless page.empty %}-H "X-Client-Id: my_app_name" -H "Content-Type: application/json"
{%
endunless %}
{%- endcapture %}
$ curl -X{{ page.method }} {{ headers -}}
http://localhost:8080{{ page.endpoint }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
package com.spotify.heroic.aggregation;

import com.spotify.heroic.metric.DistributionPoint;
import com.spotify.heroic.metric.Payload;
import com.spotify.heroic.metric.MetricGroup;
import com.spotify.heroic.metric.Payload;
import com.spotify.heroic.metric.Point;
import com.spotify.heroic.metric.Spread;

import com.spotify.heroic.metric.TdigestPoint;
import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.spotify.heroic.metric.Payload;
import com.spotify.heroic.metric.Point;
import com.spotify.heroic.metric.Spread;

import com.spotify.heroic.metric.TdigestPoint;
import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;

import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import com.google.protobuf.ByteString;
import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

package com.spotify.heroic.metric;

import com.google.auto.value.AutoValue;
import com.google.protobuf.ByteString;
import java.nio.ByteBuffer;
import com.google.auto.value.AutoValue;
import com.google.protobuf.ByteString;
import java.nio.ByteBuffer;

@AutoValue
public abstract class HeroicDistribution implements Distribution {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
package com.spotify.heroic.metric;

import com.google.common.collect.ImmutableMap;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static io.opencensus.trace.AttributeValue.longAttributeValue;
import static io.opencensus.trace.AttributeValue.stringAttributeValue;


import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableSortedSet;
import com.spotify.heroic.aggregation.Aggregation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.spotify.heroic.jetty.JettyServerConnector;
import com.spotify.heroic.lifecycle.LifeCycleRegistry;
import com.spotify.heroic.lifecycle.LifeCycles;
import com.spotify.heroic.servlet.MandatoryClientIdFilter;
import com.spotify.heroic.servlet.ShutdownFilter;
import com.spotify.heroic.tracing.TracingConfig;
import eu.toolchain.async.AsyncFramework;
Expand Down Expand Up @@ -212,6 +213,7 @@ private HandlerCollection setupHandler() throws Exception {

context.addServlet(jerseyServlet, "/*");
context.addFilter(new FilterHolder(new ShutdownFilter(stopping, mapper)), "/*", null);
context.addFilter(new FilterHolder(new MandatoryClientIdFilter()), "/*", null);
context.setErrorHandler(new JettyJSONErrorHandler(mapper));

final RequestLogHandler requestLogHandler = new RequestLogHandler();
Expand All @@ -221,6 +223,7 @@ private HandlerCollection setupHandler() throws Exception {
final RewriteHandler rewrite = new RewriteHandler();
makeRewriteRules(rewrite);


final HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[]{rewrite, context, requestLogHandler});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2015 Spotify AB.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package com.spotify.heroic.servlet;

import com.google.common.base.Strings;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response.Status;

/**
* Rejects anonymous requests. That is, requests to any API endpoint that are
* missing a non-null X-Client-Id HTTP header.
*/
// Note that this @Suppress has to go here even though it's just for the doFilter
// method, else the JavaDoc for it doesn't render. Weird.
@SuppressWarnings("checkstyle:LineLength")
public class MandatoryClientIdFilter implements Filter {

public static final String X_CLIENT_ID_HEADER_NAME = "X-Client-Id";

@Override
public void init(FilterConfig filterConfig) throws ServletException { /* intentionally empty
*/ }

/**
* Reject (with a 400) the request, if the X-Client-Id HTTP header is not present
* or is non-null/empty.<p>
* Calling {@link javax.servlet.FilterChain#doFilter}
* effectively "passes" this filter and the next
* filter gets a stab at it. <p>
* Conversely, not calling doFilter halts "happy path" processing altogether
* and that's the mechanism with which we stop anonymous requests.<p>
* Finally, using
* {@link javax.servlet.http.HttpServletResponse#sendError(int, java.lang.String)} to
* return a status message didn't work and instead sent text of "internal error" back
* to the client.
*/
@Override
public void doFilter(
ServletRequest request, ServletResponse response, FilterChain chain
) throws IOException, ServletException {
if (passesFilter(request)) {
chain.doFilter(request, response);
} else {
var httpResponse = HttpServletResponse.class.cast(response);
httpResponse.setStatus(Status.BAD_REQUEST.getStatusCode());
}
}

/**
* Returns true if the HTTP header X-Client-Id is present and non-null and not empty.
* @param request request to pluck X-Client-Id's value from
* @return see above
*/
public static boolean passesFilter(ServletRequest request) {
var req = HttpServletRequest.class.cast(request);
return !Strings.isNullOrEmpty(req.getHeader(X_CLIENT_ID_HEADER_NAME));
}

@Override
public void destroy() { /* intentionally empty */ }
};
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,19 @@ public void setup() {
final QueryLoggerFactory queryLoggerFactory = mock(QueryLoggerFactory.class);
when(queryLoggerFactory.create(any())).thenReturn(queryLogger);

manager = new LocalMetricManager(groupLimit, seriesLimit, aggregationLimit, dataLimit,
concurrentQueriesBackoff, fetchParallelism, failOnLimits, async, groupSet, metadata,
reporter, queryLoggerFactory);
manager = new LocalMetricManager(
groupLimit,
seriesLimit,
aggregationLimit,
dataLimit,
concurrentQueriesBackoff,
fetchParallelism,
failOnLimits,
async,
groupSet,
metadata,
reporter,
queryLoggerFactory);
}

@Test
Expand Down
Loading