Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable/Disable analytics based on user consent #3467

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
18 changes: 15 additions & 3 deletions Backend.Tests/Otel/OtelKernelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ namespace Backend.Tests.Otel
{
public class OtelKernelTests : IDisposable
{

private const string FrontendConsentKey = "otelConsent";
private const string FrontendSessionIdKey = "sessionId";
private const string OtelConsentKey = "otelConsent";
private const string OtelSessionIdKey = "sessionId";
private const string OtelConsentBaggageKey = "otelConsentBaggage";
private const string OtelSessionBaggageKey = "sessionBaggage";

private LocationEnricher _locationEnricher = null!;

public void Dispose()
Expand All @@ -32,41 +37,46 @@ protected virtual void Dispose(bool disposing)
}

[Test]
public void BuildersSetSessionBaggageFromHeader()
public void BuildersSetConsentAndSessionBaggageFromHeader()
{
// Arrange
var httpContext = new DefaultHttpContext();
httpContext.Request.Headers[FrontendConsentKey] = "true";
httpContext.Request.Headers[FrontendSessionIdKey] = "123";
var activity = new Activity("testActivity").Start();

// Act
TrackConsent(activity, httpContext.Request);
TrackSession(activity, httpContext.Request);

// Assert
Assert.That(activity.Baggage.Any(_ => _.Key == OtelConsentBaggageKey));
Assert.That(activity.Baggage.Any(_ => _.Key == OtelSessionBaggageKey));
}

[Test]
public void OnEndSetsSessionTagFromBaggage()
public void OnEndSetsConsentAndSessionTagFromBaggage()
{
// Arrange
var activity = new Activity("testActivity").Start();
activity.SetBaggage(OtelConsentBaggageKey, "true");
activity.SetBaggage(OtelSessionBaggageKey, "test session id");

// Act
_locationEnricher.OnEnd(activity);

// Assert
Assert.That(activity.Tags.Any(_ => _.Key == OtelConsentKey));
Assert.That(activity.Tags.Any(_ => _.Key == OtelSessionIdKey));
}


[Test]
public void OnEndSetsLocationTags()
{
// Arrange
_locationEnricher = new LocationEnricher(new LocationProviderMock());
var activity = new Activity("testActivity").Start();
activity.SetBaggage(OtelConsentBaggageKey, "true");

// Act
_locationEnricher.OnEnd(activity);
Expand All @@ -81,11 +91,13 @@ public void OnEndSetsLocationTags()
Assert.That(activity.Tags, Is.SupersetOf(testLocation));
}

[Test]
public void OnEndRedactsIp()
{
// Arrange
_locationEnricher = new LocationEnricher(new LocationProviderMock());
var activity = new Activity("testActivity").Start();
activity.SetBaggage(OtelConsentBaggageKey, "true");
activity.SetTag("url.full", $"{LocationProvider.locationGetterUri}100.0.0.0");

// Act
Expand Down
14 changes: 14 additions & 0 deletions Backend/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public class User
[BsonElement("username")]
public string Username { get; set; }

[BsonElement("otelConsent")]
public bool OtelConsent { get; set; }

[BsonElement("answeredConsent")]
public bool AnsweredConsent { get; set; }

[BsonElement("uiLang")]
public string UILang { get; set; }

Expand Down Expand Up @@ -97,6 +103,8 @@ public User()
Agreement = false;
Password = "";
Username = "";
OtelConsent = true;
AnsweredConsent = false;
UILang = "";
GlossSuggestion = AutocompleteSetting.On;
Token = "";
Expand All @@ -119,6 +127,8 @@ public User Clone()
Agreement = Agreement,
Password = Password,
Username = Username,
OtelConsent = OtelConsent,
AnsweredConsent = AnsweredConsent,
UILang = UILang,
GlossSuggestion = GlossSuggestion,
Token = Token,
Expand All @@ -141,6 +151,8 @@ public bool ContentEquals(User other)
other.Agreement == Agreement &&
other.Password.Equals(Password, StringComparison.Ordinal) &&
other.Username.Equals(Username, StringComparison.Ordinal) &&
other.OtelConsent == OtelConsent &&
other.AnsweredConsent == AnsweredConsent &&
other.UILang.Equals(UILang, StringComparison.Ordinal) &&
other.GlossSuggestion.Equals(GlossSuggestion) &&
other.Token.Equals(Token, StringComparison.Ordinal) &&
Expand Down Expand Up @@ -178,6 +190,8 @@ public override int GetHashCode()
hash.Add(Agreement);
hash.Add(Password);
hash.Add(Username);
hash.Add(OtelConsent);
hash.Add(AnsweredConsent);
hash.Add(UILang);
hash.Add(GlossSuggestion);
hash.Add(Token);
Expand Down
43 changes: 28 additions & 15 deletions Backend/Otel/OtelKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public static void AddOpenTelemetryInstrumentation(this IServiceCollection servi
);
}

internal static void TrackConsent(Activity activity, HttpRequest request)
{
var consent = request.Headers.TryGetValue("otelConsent", out var values) ? bool.TryParse(values.FirstOrDefault(), out bool _) : false;
activity.SetBaggage("otelConsentBaggage", consent.ToString());
}

internal static void TrackSession(Activity activity, HttpRequest request)
{
var sessionId = request.Headers.TryGetValue("sessionId", out var values) ? values.FirstOrDefault() : null;
Expand Down Expand Up @@ -67,6 +73,7 @@ private static void AspNetCoreBuilder(AspNetCoreTraceInstrumentationOptions opti
options.EnrichWithHttpRequest = (activity, request) =>
{
GetContentLengthAspNet(activity, request.Headers, "inbound.http.request.body.size");
TrackConsent(activity, request);
TrackSession(activity, request);
};
options.EnrichWithHttpResponse = (activity, response) =>
Expand Down Expand Up @@ -98,22 +105,28 @@ internal class LocationEnricher(ILocationProvider locationProvider) : BaseProces
{
public override async void OnEnd(Activity data)
{
var uriPath = (string?)data.GetTagItem("url.full");
var locationUri = LocationProvider.locationGetterUri;
if (uriPath is null || !uriPath.Contains(locationUri))
{
var location = await locationProvider.GetLocation();
data?.AddTag("country", location?.Country);
data?.AddTag("regionName", location?.RegionName);
data?.AddTag("city", location?.City);
}
data?.SetTag("sessionId", data?.GetBaggageItem("sessionBaggage"));
if (uriPath is not null && uriPath.Contains(locationUri))
var consentString = data?.GetBaggageItem("otelConsentBaggage");
data?.AddTag("otelConsent", consentString);
var consent = bool.TryParse(consentString, out bool value) ? value : false;
if (consent)
{
// When getting location externally, url.full includes site URI and user IP.
// In such cases, only add url without IP information to traces.
data?.SetTag("url.full", "");
data?.SetTag("url.redacted.ip", LocationProvider.locationGetterUri);
var uriPath = (string?)data?.GetTagItem("url.full");
var locationUri = LocationProvider.locationGetterUri;
if (uriPath is null || !uriPath.Contains(locationUri))
{
var location = await locationProvider.GetLocation();
data?.AddTag("country", location?.Country);
data?.AddTag("regionName", location?.RegionName);
data?.AddTag("city", location?.City);
}
data?.SetTag("sessionId", data?.GetBaggageItem("sessionBaggage"));
if (uriPath is not null && uriPath.Contains(locationUri))
{
// When getting location externally, url.full includes site URI and user IP.
// In such cases, only add url without IP information to traces.
data?.SetTag("url.full", "");
data?.SetTag("url.redacted.ip", LocationProvider.locationGetterUri);
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions Backend/Repositories/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ public async Task<ResultOfUpdate> Update(string userId, User user, bool updateIs
.Set(x => x.ProjectRoles, user.ProjectRoles)
.Set(x => x.Agreement, user.Agreement)
.Set(x => x.Username, user.Username)
.Set(x => x.OtelConsent, user.OtelConsent)
.Set(x => x.AnsweredConsent, user.AnsweredConsent)
.Set(x => x.UILang, user.UILang)
.Set(x => x.GlossSuggestion, user.GlossSuggestion);

Expand Down
14 changes: 8 additions & 6 deletions public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
"pressEnter": "Press Enter to save word",
"vernacular": "Vernacular"
},
"analyticsConsent": {
"consentModal": {
"acceptAllBtn": "Yes, allow analytics cookies",
"acceptNecessaryBtn": "No, reject analytics cookies",
"description": "The Combine stores basic info about your current session on your device. This info is necessary and isn't shared with anybody. The Combine also uses analytics cookies, which are only for us to fix bugs and compile anonymized statistics. Do you consent to our usage of analytics cookies?",
"title": "Cookies on The Combine"
}
},
"appBar": {
"dataEntry": "Data Entry",
"dataCleanup": "Data Cleanup",
Expand Down Expand Up @@ -129,12 +137,6 @@
"userSettings": {
"analyticsConsent": {
"button": "Change consent",
"consentModal": {
"acceptAllBtn": "Yes, allow analytics cookies",
"acceptNecessaryBtn": "No, reject analytics cookies",
"description": "The Combine stores basic info about your current session on your device. This info is necessary and isn't shared with anybody. The Combine also uses analytics cookies, which are only for us to fix bugs and compile anonymized statistics. Do you consent to our usage of analytics cookies?",
"title": "Cookies on The Combine"
},
"consentNo": "You have not consented to our use of analytics cookies.",
"consentYes": "You have consented to our use of analytics cookies.",
"title": "Analytics cookies"
Expand Down
24 changes: 18 additions & 6 deletions src/api/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,38 @@ export interface User {
username: string;
/**
*
* @type {string}
* @type {boolean}
* @memberof User
*/
uiLang?: string | null;
otelConsent?: boolean;
/**
*
* @type {string}
* @type {boolean}
* @memberof User
*/
token: string;
answeredConsent?: boolean;
/**
*
* @type {boolean}
* @type {string}
* @memberof User
*/
isAdmin: boolean;
uiLang?: string | null;
/**
*
* @type {AutocompleteSetting}
* @memberof User
*/
glossSuggestion: AutocompleteSetting;
/**
*
* @type {string}
* @memberof User
*/
token: string;
/**
*
* @type {boolean}
* @memberof User
*/
isAdmin: boolean;
}
5 changes: 4 additions & 1 deletion src/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ const whiteListedErrorUrls = [
// Create an axios instance to allow for attaching interceptors to it.
const axiosInstance = axios.create({ baseURL: apiBaseURL });
axiosInstance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
config.headers.sessionId = getSessionId();
LocalStorage.getCurrentUser()?.otelConsent
? ((config.headers.otelConsent = true),
(config.headers.sessionId = getSessionId()))
: (config.headers.otelConsent = false);
return config;
});
axiosInstance.interceptors.response.use(undefined, (err: AxiosError) => {
Expand Down
53 changes: 53 additions & 0 deletions src/components/AnalyticsConsent/AnalyticsConsent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { List, ListItemButton, Typography } from "@mui/material";
import Drawer from "@mui/material/Drawer";
import { ReactElement } from "react";
import { useTranslation } from "react-i18next";

interface ConsentProps {
onChangeConsent: (consentVal: boolean | undefined) => void;
required: boolean;
}

export function AnalyticsConsent(props: ConsentProps): ReactElement {
const { t } = useTranslation();

const acceptAnalytics = (): void => {
props.onChangeConsent(true);
};
const rejectAnalytics = (): void => {
props.onChangeConsent(false);
};

const clickedAway = (): void => {
props.onChangeConsent(undefined);
};

return (
<div>
<Drawer
anchor={"bottom"}
open
onClose={!props.required ? clickedAway : undefined}
>
<List>
<ListItemButton
onClick={acceptAnalytics}
style={{ justifyContent: "center" }}
>
<Typography>
{t("analyticsConsent.consentModal.acceptAllBtn")}
</Typography>
</ListItemButton>
<ListItemButton
onClick={rejectAnalytics}
style={{ justifyContent: "center" }}
>
<Typography>
{t("analyticsConsent.consentModal.acceptNecessaryBtn")}
</Typography>
</ListItemButton>
</List>
</Drawer>
</div>
);
}
23 changes: 23 additions & 0 deletions src/components/App/AppLoggedIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { Theme, ThemeProvider, createTheme } from "@mui/material/styles";
import { ReactElement, useEffect, useMemo, useState } from "react";
import { Route, Routes } from "react-router-dom";

import { updateUser } from "backend";
import { getCurrentUser } from "backend/localStorage";
import { AnalyticsConsent } from "components/AnalyticsConsent/AnalyticsConsent";
import DatePickersLocalizationProvider from "components/App/DatePickersLocalizationProvider";
import SignalRHub from "components/App/SignalRHub";
import AppBar from "components/AppBar/AppBarComponent";
Expand Down Expand Up @@ -47,6 +50,20 @@ export default function AppWithBar(): ReactElement {
const projFonts = useMemo(() => new ProjectFonts(proj), [proj]);

const [styleOverrides, setStyleOverrides] = useState<string>();
const [answeredConsent, setAnsweredConsent] = useState(
getCurrentUser()?.answeredConsent
);

async function handleConsentChange(
otelConsent: boolean | undefined
): Promise<void> {
await updateUser({
...getCurrentUser()!,
otelConsent,
answeredConsent: true,
});
setAnsweredConsent(true);
}

useEffect(() => {
updateLangFromUser();
Expand Down Expand Up @@ -83,6 +100,12 @@ export default function AppWithBar(): ReactElement {
<FontContext.Provider value={projFonts}>
<ThemeProvider theme={overrideThemeFont}>
<CssBaseline />
{answeredConsent ? null : (
<AnalyticsConsent
onChangeConsent={handleConsentChange}
required
></AnalyticsConsent>
)}
<Routes>
<Route path={routerPath(Path.DataEntry)} element={<DataEntry />} />
<Route path={routerPath(Path.Goals)} element={<GoalTimeline />} />
Expand Down
Loading
Loading