add authentication

This commit is contained in:
Simon Larsen
2023-11-12 15:12:49 +00:00
parent d8d5556f1a
commit 502f298a3a
11 changed files with 86 additions and 65 deletions

View File

@@ -9,6 +9,8 @@ var builder = WebApplication.CreateBuilder(args);
const string endpoint = "http://localhost:4317";
Console.WriteLine($"Env var: {Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS")?.ToString()}");
// Logging.
builder.Logging.ClearProviders();
@@ -34,7 +36,7 @@ builder.Logging.AddOpenTelemetry(logging =>
opt.Endpoint = new Uri(endpoint);
// Set headers in OTLP exporter
opt.Headers = "oneuptime-service-token=0a00ebc0-7f39-11ee-ac8c-3fb43926b224";
// opt.Headers = "oneuptime-service-token=0a00ebc0-7f39-11ee-ac8c-3fb43926b224";
}
System.Console.WriteLine($"OTLP Exporter is using {opt.Protocol} protocol and endpoint {opt.Endpoint}");
@@ -55,7 +57,7 @@ builder.Services.AddOpenTelemetry()
{
opt.Endpoint = new Uri(endpoint);
// Set headers in OTLP exporter
opt.Headers = "oneuptime-service-token=0a00ebc0-7f39-11ee-ac8c-3fb43926b224";
// opt.Headers = "oneuptime-service-token=0a00ebc0-7f39-11ee-ac8c-3fb43926b224";
}
@@ -85,7 +87,7 @@ builder.Services.AddOpenTelemetry()
{
opt.Endpoint = new Uri(endpoint);
// Set headers in OTLP exporter
opt.Headers = "oneuptime-service-token=0a00ebc0-7f39-11ee-ac8c-3fb43926b224";
// opt.Headers = "oneuptime-service-token=0a00ebc0-7f39-11ee-ac8c-3fb43926b224";
}
System.Console.WriteLine($"OTLP Exporter is using {opt.Protocol} protocol and endpoint {opt.Endpoint}");
@@ -123,6 +125,8 @@ async Task<String> SendGreeting(ILogger<Program> logger)
// Add a tag to the Activity
activity?.SetTag("greeting", "Hello World!");
//log out env var
histogram.Record("Hello World!".Length);
return $"Hello World! OpenTelemetry Trace: {Activity.Current?.Id}";

View File

@@ -5,5 +5,5 @@
Please use
```bash
dotnet run --urls=http://localhost:7856/
export OTEL_EXPORTER_OTLP_HEADERS=x-oneuptime-service-token=0a00ebc0-7f39-11ee-ac8c-3fb43926b224 && dotnet run --urls=http://localhost:7856/
```

View File

@@ -20,3 +20,12 @@
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0

View File

@@ -22,9 +22,9 @@ import LogService from 'CommonServer/Services/LogService';
import ObjectID from 'Common/Types/ObjectID';
import { JSONArray, JSONObject } from 'Common/Types/JSON';
import OTelIngestService from '../Service/OTelIngest';
// import GlobalCache from 'CommonServer/Infrastructure/GlobalCache';
// import ServiceService from 'CommonServer/Services/ServiceService';
// import Service from 'Model/Models/Service';
import GlobalCache from 'CommonServer/Infrastructure/GlobalCache';
import ServiceService from 'CommonServer/Services/ServiceService';
import Service from 'Model/Models/Service';
// Load proto file for OTel
@@ -72,63 +72,66 @@ router.use(
// check header.
// if (!req.headers['oneuptime-service-token']) {
// throw new BadRequestException(
// 'Missing header: oneuptime-service-token'
// );
// }
const serviceTokenInHeader: string | undefined = req.headers['x-oneuptime-service-token'] as string | undefined;
// const cachedServiceId: string | null = await GlobalCache.getString(
// 'service-token',
// req.headers['oneuptime-service-token'] as string
// );
// const serviceProjectId: string | null = await GlobalCache.getString(
// 'service-project-id',
// req.headers['oneuptime-service-token'] as string
// );
// if (!cachedServiceId || !serviceProjectId) {
// // load from the database and set the cache.
// const service: Service | null = await ServiceService.findOneBy({
// query: {
// serviceToken: new ObjectID(
// req.headers['oneuptime-service-token'] as string
// ),
// },
// select: {
// _id: true,
// projectId: true,
// },
// props: {
// isRoot: true,
// },
// });
if (!serviceTokenInHeader) {
throw new BadRequestException(
'Missing header: oneuptime-service-token'
);
}
// if (!service) {
// throw new BadRequestException('Invalid service token');
// }
const cachedServiceId: string | null = await GlobalCache.getString(
'service-token',
serviceTokenInHeader as string
);
const serviceProjectId: string | null = await GlobalCache.getString(
'service-project-id',
serviceTokenInHeader as string
);
// await GlobalCache.setString(
// 'service-token',
// req.headers['oneuptime-service-token'] as string,
// service._id?.toString() as string
// );
// await GlobalCache.setString(
// 'service-project-id',
// req.headers['oneuptime-service-token'] as string,
// service.projectId?.toString() as string
// );
if (!cachedServiceId || !serviceProjectId) {
// load from the database and set the cache.
const service: Service | null = await ServiceService.findOneBy({
query: {
serviceToken: new ObjectID(
serviceTokenInHeader as string
),
},
select: {
_id: true,
projectId: true,
},
props: {
isRoot: true,
},
});
// (req as OtelRequest).serviceId = service.id as ObjectID;
// (req as OtelRequest).projectId = service.projectId as ObjectID;
// }
if (!service) {
throw new BadRequestException('Invalid service token');
}
// (req as OtelRequest).serviceId = ObjectID.fromString(
// cachedServiceId as string
// );
// (req as OtelRequest).projectId = ObjectID.fromString(
// serviceProjectId as string
// );
await GlobalCache.setString(
'service-token',
serviceTokenInHeader as string,
service._id?.toString() as string
);
await GlobalCache.setString(
'service-project-id',
serviceTokenInHeader as string,
service.projectId?.toString() as string
);
(req as OtelRequest).serviceId = service.id as ObjectID;
(req as OtelRequest).projectId = service.projectId as ObjectID;
}
(req as OtelRequest).serviceId = ObjectID.fromString(
cachedServiceId as string
);
(req as OtelRequest).projectId = ObjectID.fromString(
serviceProjectId as string
);
next();
} catch (err) {
@@ -456,8 +459,6 @@ router.post(
): Promise<void> => {
try {
debugger;
logger.info('OTel Ingestor API called');
req.body = req.body.toJSON();

View File

@@ -1,9 +1,9 @@
extensions:
headers_setter:
headers:
- action: insert
key: oneuptime-service-token
from_context: oneuptime-service-token
- action: upsert
key: x-oneuptime-service-token
from_context: x-oneuptime-service-token
processors:
@@ -11,6 +11,7 @@ receivers:
otlp:
protocols:
grpc:
include_metadata: true
http:
include_metadata: true
@@ -20,8 +21,13 @@ exporters:
otlphttp:
endpoint: "http://ingestor:3400/otel"
headers: {"Content-Type": "application/json"}
auth:
authenticator: headers_setter
service:
telemetry:
logs:
level: "debug"
extensions: [ headers_setter ]
pipelines:
traces:

View File

@@ -72,7 +72,7 @@ import './Jobs/UserOnCallLog/TimeoutStuckExecutions';
import './Jobs/IncomingRequestMonitor/CheckHeartbeat';
import { ClickhouseAppInstance } from 'CommonServer/Infrastructure/ClickhouseDatabase';
// import AnalyticsTableManagement from './Utils/AnalyticsDatabase/TableManegement';
import AnalyticsTableManagement from './Utils/AnalyticsDatabase/TableManegement';
import './Jobs/Workflow/TimeoutJobs';
@@ -102,7 +102,7 @@ const init: () => Promise<void> = async (): Promise<void> => {
await RunDatabaseMigrations();
// create tables in analytics database
// await AnalyticsTableManagement.createTables();
await AnalyticsTableManagement.createTables();
// Job process.
QueueWorker.getWorker(

View File

@@ -365,6 +365,7 @@ services:
restart: always
ports:
- '4317:4317'
- '4318:4318'
ingestor: