Skip to content

Commit 124f98e

Browse files
committed
fix sigv4 tests
1 parent 3827f98 commit 124f98e

File tree

7 files changed

+24
-34
lines changed

7 files changed

+24
-34
lines changed

aws-distro-opentelemetry-node-autoinstrumentation/src/aws-opentelemetry-configurator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ export class AwsSpanProcessorProvider {
608608
case 'http/protobuf':
609609
if (otlpExporterTracesEndpoint && isAwsOtlpEndpoint(otlpExporterTracesEndpoint, 'xray')) {
610610
diag.debug('Detected XRay OTLP Traces endpoint. Switching exporter to OtlpAwsSpanExporter');
611-
return new OTLPAwsSpanExporter(otlpExporterTracesEndpoint);
611+
return new OTLPAwsSpanExporter(otlpExporterTracesEndpoint, { compression: CompressionAlgorithm.GZIP });
612612
}
613613
return new OTLPProtoTraceExporter();
614614
case 'udp':
@@ -618,7 +618,7 @@ export class AwsSpanProcessorProvider {
618618
diag.warn(`Unsupported OTLP traces protocol: ${protocol}. Using http/protobuf.`);
619619
if (otlpExporterTracesEndpoint && isAwsOtlpEndpoint(otlpExporterTracesEndpoint, 'xray')) {
620620
diag.debug('Detected XRay OTLP Traces endpoint. Switching exporter to OtlpAwsSpanExporter');
621-
return new OTLPAwsSpanExporter(otlpExporterTracesEndpoint);
621+
return new OTLPAwsSpanExporter(otlpExporterTracesEndpoint, { compression: CompressionAlgorithm.GZIP });
622622
}
623623
return new OTLPProtoTraceExporter();
624624
}

aws-distro-opentelemetry-node-autoinstrumentation/src/exporter/otlp/aws/common/aws-authenticator.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,30 @@ if (getNodeVersion() >= 16) {
2626
const SIG_V4_HEADERS = ['x-amz-date', 'authorization', 'x-amz-content-sha256', 'x-amz-security-token'];
2727

2828
export class AwsAuthenticator {
29+
private endpoint: URL;
2930
private region: string;
3031
private service: string;
3132

32-
constructor(region: string, service: string) {
33-
this.region = region;
33+
constructor(endpoint: string, service: string) {
34+
this.endpoint = new URL(endpoint);
35+
this.region = endpoint.split('.')[1];
3436
this.service = service;
3537
}
3638

37-
public async authenticate(endpoint: string, headers: Record<string, string>, serializedData: Uint8Array | undefined) {
39+
public async authenticate(headers: Record<string, string>, serializedData: Uint8Array | undefined) {
3840
// Only do SigV4 Signing if the required dependencies are installed.
39-
if (dependenciesLoaded) {
40-
const url = new URL(endpoint);
41-
42-
if (serializedData === undefined) {
43-
diag.error('Given serialized data is undefined. Not authenticating.');
44-
return headers;
45-
}
46-
41+
if (dependenciesLoaded && serializedData) {
4742
const cleanedHeaders = this.removeSigV4Headers(headers);
4843

4944
const request = new HttpRequest({
5045
method: 'POST',
5146
protocol: 'https',
52-
hostname: url.hostname,
53-
path: url.pathname,
47+
hostname: this.endpoint.hostname,
48+
path: this.endpoint.pathname,
5449
body: serializedData,
5550
headers: {
5651
...cleanedHeaders,
57-
host: url.hostname,
52+
host: this.endpoint.hostname,
5853
},
5954
});
6055

@@ -74,6 +69,7 @@ export class AwsAuthenticator {
7469
}
7570
}
7671

72+
diag.debug('Given serialized data is undefined. Not authenticating.');
7773
return headers;
7874
}
7975

aws-distro-opentelemetry-node-autoinstrumentation/src/exporter/otlp/aws/logs/otlp-aws-log-exporter.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { diag } from '@opentelemetry/api';
2121
export class OTLPAwsLogExporter extends OTLPProtoLogExporter {
2222
private compression: CompressionAlgorithm | undefined;
2323
private endpoint: string;
24-
private region: string;
2524
private serializer: PassthroughSerializer<IExportLogsServiceResponse>;
2625
private authenticator: AwsAuthenticator;
2726

@@ -34,9 +33,8 @@ export class OTLPAwsLogExporter extends OTLPProtoLogExporter {
3433

3534
super(modifiedConfig);
3635
this.compression = config?.compression;
37-
this.region = endpoint.split('.')[1];
3836
this.endpoint = endpoint;
39-
this.authenticator = new AwsAuthenticator(this.region, 'logs');
37+
this.authenticator = new AwsAuthenticator(this.endpoint, 'logs');
4038

4139
// This is used in order to prevent serializing and compressing the data twice. Once for signing Sigv4 and
4240
// once when we pass the data to super.export() which will serialize and compress the data again.
@@ -84,7 +82,7 @@ export class OTLPAwsLogExporter extends OTLPProtoLogExporter {
8482
delete headers['Content-Encoding'];
8583
}
8684

87-
const signedRequestHeaders = await this.authenticator.authenticate(this.endpoint, headers, serializedLogs);
85+
const signedRequestHeaders = await this.authenticator.authenticate(headers, serializedLogs);
8886

8987
if ('authorization' in signedRequestHeaders) {
9088
this['_delegate']._transport._transport._parameters.headers = () => signedRequestHeaders;

aws-distro-opentelemetry-node-autoinstrumentation/src/exporter/otlp/aws/traces/otlp-aws-span-exporter.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { diag } from '@opentelemetry/api';
2121
export class OTLPAwsSpanExporter extends OTLPProtoTraceExporter {
2222
private compression: CompressionAlgorithm | undefined;
2323
private endpoint: string;
24-
private region: string;
2524
private serializer: PassthroughSerializer<IExportTraceServiceResponse>;
2625
private authenticator: AwsAuthenticator;
2726

@@ -33,9 +32,8 @@ export class OTLPAwsSpanExporter extends OTLPProtoTraceExporter {
3332
};
3433

3534
super(modifiedConfig);
36-
this.region = endpoint.split('.')[1];
3735
this.endpoint = endpoint;
38-
this.authenticator = new AwsAuthenticator(this.region, 'xray');
36+
this.authenticator = new AwsAuthenticator(this.endpoint, 'xray');
3937

4038
// This is used in order to prevent serializing and compressing the data twice. Once for signing Sigv4 and
4139
// once when we pass the data to super.export() which will serialize and compress the data again.
@@ -81,7 +79,7 @@ export class OTLPAwsSpanExporter extends OTLPProtoTraceExporter {
8179
delete headers['Content-Encoding'];
8280
}
8381

84-
const signedRequestHeaders = await this.authenticator.authenticate(this.endpoint, headers, serializedSpans);
82+
const signedRequestHeaders = await this.authenticator.authenticate(headers, serializedSpans);
8583

8684
if ('authorization' in signedRequestHeaders) {
8785
this['_delegate']._transport._transport._parameters.headers = () => signedRequestHeaders;

aws-distro-opentelemetry-node-autoinstrumentation/test/exporter/otlp/aws/common/aws-authenticator.test.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ describe('AwsAuthenticator', () => {
5454

5555
const { AwsAuthenticator: MockThrowableModuleAuthenticator } = require(AWS_AUTH_PATH);
5656

57-
const result = await new MockThrowableModuleAuthenticator('us-east-1', 'xray').authenticate(
58-
AWS_OTLP_TRACES_ENDPOINT,
57+
const result = await new MockThrowableModuleAuthenticator(AWS_OTLP_TRACES_ENDPOINT, 'xray').authenticate(
5958
{},
6059
new Uint8Array()
6160
);
@@ -68,8 +67,8 @@ describe('AwsAuthenticator', () => {
6867
});
6968

7069
it('should not inject SigV4 Headers if serialized data is undefined', async () => {
71-
const authenticator = new AwsAuthenticator('us-east-1', 'xray');
72-
const result = await authenticator.authenticate(AWS_OTLP_TRACES_ENDPOINT, {}, undefined);
70+
const authenticator = new AwsAuthenticator(AWS_OTLP_TRACES_ENDPOINT, 'xray');
71+
const result = await authenticator.authenticate({}, undefined);
7372

7473
expect(result).not.toHaveProperty(AUTHORIZATION_HEADER);
7574
expect(result).not.toHaveProperty(X_AMZ_DATE_HEADER);
@@ -99,8 +98,7 @@ describe('AwsAuthenticator', () => {
9998
},
10099
}).AwsAuthenticator;
101100

102-
const result = await new AwsAuthenticatorWithMock('us-east-1', 'xray').authenticate(
103-
AWS_OTLP_TRACES_ENDPOINT,
101+
const result = await new AwsAuthenticatorWithMock(AWS_OTLP_TRACES_ENDPOINT, 'xray').authenticate(
104102
{ test: 'test' },
105103
new Uint8Array()
106104
);
@@ -149,8 +147,7 @@ describe('AwsAuthenticator', () => {
149147
},
150148
}).AwsAuthenticator;
151149

152-
const result = await new AwsAuthenticatorWithMock('us-east-1', 'xray').authenticate(
153-
AWS_OTLP_TRACES_ENDPOINT,
150+
const result = await new AwsAuthenticatorWithMock(AWS_OTLP_TRACES_ENDPOINT, 'xray').authenticate(
154151
notExpected,
155152
new Uint8Array()
156153
);

aws-distro-opentelemetry-node-autoinstrumentation/test/exporter/otlp/aws/logs/otlp-aws-log-exporter.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('OTLPAwsLogExporter', () => {
3636
'../common/aws-authenticator': {
3737
AwsAuthenticator: class MockAwsAuthenticator {
3838
constructor() {}
39-
async authenticate(endpoint: string, headers: Record<string, string>) {
39+
async authenticate(headers: Record<string, string>, serializedData: Uint8Array | undefined) {
4040
return {
4141
...headers,
4242
[AUTHORIZATION_HEADER]: EXPECTED_AUTH_HEADER,
@@ -61,6 +61,7 @@ describe('OTLPAwsLogExporter', () => {
6161
.then(() => {
6262
scope.on('request', (req, interceptor, body) => {
6363
const headers = req.headers;
64+
console.log('SDJASDJAJSDAJSDJJDAJSDJAS' + JSON.stringify(req.headers));
6465
expect(headers).toHaveProperty(AUTHORIZATION_HEADER.toLowerCase());
6566
expect(headers).toHaveProperty(X_AMZ_SECURITY_TOKEN_HEADER.toLowerCase());
6667
expect(headers).toHaveProperty(X_AMZ_DATE_HEADER.toLowerCase());

aws-distro-opentelemetry-node-autoinstrumentation/test/exporter/otlp/aws/traces/otlp-aws-span-exporter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('OTLPAwsSpanExporter', () => {
3535
'../common/aws-authenticator': {
3636
AwsAuthenticator: class MockAwsAuthenticator {
3737
constructor() {}
38-
async authenticate(endpoint: string, headers: Record<string, string>) {
38+
async authenticate(headers: Record<string, string>, serializedData: Uint8Array | undefined) {
3939
return {
4040
...headers,
4141
[AUTHORIZATION_HEADER]: EXPECTED_AUTH_HEADER,

0 commit comments

Comments
 (0)