Skip to content

Commit 0343715

Browse files
sandy2008Kielekalanwest
authored
Improve Integration Test by Generating TLS/mTLS Certificates via MSBuild (open-telemetry#5818)
Co-authored-by: Piotr Kiełkowicz <[email protected]> Co-authored-by: Alan West <[email protected]>
1 parent cddc091 commit 0343715

File tree

9 files changed

+238
-39
lines changed

9 files changed

+238
-39
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
# Self-signed cert generated by integration test
22
otel-collector.crt
33
otel-collector.key
4+
otel-client.crt
5+
otel-client.key
6+
otel-untrusted-collector.crt
7+
otel-untrusted-collector.key
8+
certs/*
9+
certs

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
# This should be run from the root of the repo:
33
# docker build --file test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/Dockerfile
44

5-
ARG BUILD_SDK_VERSION=8.0
6-
ARG TEST_SDK_VERSION=8.0
5+
ARG BUILD_SDK_VERSION=9.0
6+
ARG TEST_SDK_VERSION=9.0
77

88
FROM mcr.microsoft.com/dotnet/sdk:${BUILD_SDK_VERSION} AS build
99
ARG PUBLISH_CONFIGURATION=Release
10-
ARG PUBLISH_FRAMEWORK=net8.0
10+
ARG PUBLISH_FRAMEWORK=net9.0
1111
WORKDIR /repo
1212
COPY . ./
1313
WORKDIR "/repo/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests"

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/create-cert.sh

Lines changed: 0 additions & 21 deletions
This file was deleted.

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/docker-compose.yml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,39 @@
55
version: '3.7'
66

77
services:
8-
create-cert:
9-
image: mcr.microsoft.com/dotnet/sdk:7.0
8+
init-service:
9+
image: otel-test-image
10+
build:
11+
context: .
12+
dockerfile: ./test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/Dockerfile
1013
volumes:
1114
- ./test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest:/cfg
12-
command: /cfg/create-cert.sh
15+
command: >
16+
sh -c "
17+
mkdir -p /cfg/certs;
18+
cp /test/*.pem /cfg/certs/;
19+
chmod 644 /cfg/certs/*;
20+
sleep 1000;
21+
"
1322
1423
otel-collector:
1524
image: otel/opentelemetry-collector
1625
volumes:
1726
- ./test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest:/cfg
1827
command: --config=/cfg/otel-collector-config.yaml
1928
depends_on:
20-
- create-cert
29+
- init-service
2130

2231
tests:
32+
image: otel-test-image
2333
build:
2434
context: .
2535
dockerfile: ./test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/Dockerfile
2636
volumes:
2737
- ./test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest:/cfg
2838
command: /cfg/run-test.sh
2939
environment:
30-
- OTEL_COLLECTOR_HOSTNAME=otel-collector
31-
- OTEL_MOCK_COLLECTOR_HOSTNAME=mock-otel-collector
40+
OTEL_COLLECTOR_HOSTNAME: otel-collector
41+
OTEL_MOCK_COLLECTOR_HOSTNAME: mock-otel-collector
3242
depends_on:
3343
- otel-collector

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/otel-collector-config.yaml

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,39 @@ receivers:
1616
grpc:
1717
endpoint: 0.0.0.0:5317
1818
tls:
19-
cert_file: /cfg/otel-collector.crt
20-
key_file: /cfg/otel-collector.key
19+
cert_file: /cfg/certs/otel-test-server-cert.pem
20+
key_file: /cfg/certs/otel-test-server-key.pem
2121
http:
2222
endpoint: 0.0.0.0:5318
2323
tls:
24-
cert_file: /cfg/otel-collector.crt
25-
key_file: /cfg/otel-collector.key
24+
cert_file: /cfg/certs/otel-test-server-cert.pem
25+
key_file: /cfg/certs/otel-test-server-key.pem
26+
otlp/untrustedtls:
27+
protocols:
28+
grpc:
29+
endpoint: 0.0.0.0:6317
30+
tls:
31+
cert_file: /cfg/certs/otel-untrusted-collector-cert.pem
32+
key_file: /cfg/certs/otel-untrusted-collector-key.pem
33+
http:
34+
endpoint: 0.0.0.0:6318
35+
tls:
36+
cert_file: /cfg/certs/otel-untrusted-collector-cert.pem
37+
key_file: /cfg/certs/otel-untrusted-collector-key.pem
38+
otlp/mtls:
39+
protocols:
40+
grpc:
41+
endpoint: 0.0.0.0:7317
42+
tls:
43+
cert_file: /cfg/certs/otel-test-server-cert.pem
44+
key_file: /cfg/certs/otel-test-server-key.pem
45+
client_ca_file: /cfg/certs/otel-test-ca-cert.pem
46+
http:
47+
endpoint: 0.0.0.0:7318
48+
tls:
49+
cert_file: /cfg/certs/otel-test-server-cert.pem
50+
key_file: /cfg/certs/otel-test-server-key.pem
51+
client_ca_file: /cfg/certs/otel-test-ca-cert.pem
2652

2753
exporters:
2854
debug:
@@ -31,11 +57,11 @@ exporters:
3157
service:
3258
pipelines:
3359
traces:
34-
receivers: [otlp, otlp/tls]
60+
receivers: [otlp, otlp/tls, otlp/untrustedtls, otlp/mtls]
3561
exporters: [debug]
3662
metrics:
37-
receivers: [otlp, otlp/tls]
63+
receivers: [otlp, otlp/tls, otlp/untrustedtls, otlp/mtls]
3864
exporters: [debug]
3965
logs:
40-
receivers: [otlp, otlp/tls]
66+
receivers: [otlp, otlp/tls, otlp/untrustedtls, otlp/mtls]
4167
exporters: [debug]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/bin/bash
22
set -e
33

4-
# Trust the self-signed certificated used by the collector
5-
cp /cfg/otel-collector.crt /usr/local/share/ca-certificates/
4+
# Trust the self-signed certificate used by the collector
5+
cp /cfg/certs/otel-test-ca-cert.pem /usr/local/share/ca-certificates/otel-test-ca-cert.crt
66
update-ca-certificates --verbose
77

88
dotnet test OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests.dll --TestCaseFilter:CategoryName=CollectorIntegrationTests --logger "console;verbosity=detailed"

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests.csproj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44
<TargetFrameworks>$(TargetFrameworksForTests)</TargetFrameworks>
55
</PropertyGroup>
66

7+
<!-- Add MSBuild Task to Generate Certificates -->
8+
<Target Name="GenerateTestCertificates" BeforeTargets="Publish">
9+
<Exec Condition="$(OS) == 'Unix'"
10+
Command="/bin/bash gen_test_cert.sh $(IntermediateOutputPath)"
11+
ConsoleToMsBuild="true"
12+
WorkingDirectory="$(ProjectDir)" />
13+
14+
<Exec Condition="$(OS) == 'Windows_NT'"
15+
Command="pwsh -NonInteractive -executionpolicy Unrestricted -command &quot;&amp; { ./gen_test_cert.ps1 -OutDir $(IntermediateOutputPath) } &quot;"
16+
ConsoleToMsBuild="true"
17+
WorkingDirectory="$(ProjectDir)" />
18+
19+
<ItemGroup>
20+
<TestCertificates Include="$(IntermediateOutputPath)*.pem"/>
21+
</ItemGroup>
22+
23+
<Copy SourceFiles="@(TestCertificates)" DestinationFolder="$(PublishDir)/%(RecursiveDir)"/>
24+
</Target>
25+
726
<ItemGroup>
827
<PackageReference Include="Grpc.AspNetCore.Server" Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'" />
928
<PackageReference Include="Microsoft.AspNetCore.TestHost" Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'" />
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using namespace System.Security.Cryptography;
2+
using namespace System.Security.Cryptography.X509Certificates;
3+
4+
param (
5+
[string] $OutDir
6+
)
7+
8+
function Write-Certificate {
9+
param (
10+
[X509Certificate2] $Cert,
11+
[string] $Name,
12+
[string] $Dir
13+
)
14+
15+
# write cert content
16+
$certPem = $Cert.ExportCertificatePem();
17+
$certPemPath = Join-Path $Dir -ChildPath "$Name-cert.pem";
18+
[System.IO.File]::WriteAllText($certPemPath, $certPem);
19+
20+
# write pkey
21+
[AsymmetricAlgorithm] $pkey = [RSACertificateExtensions]::GetRSAPrivateKey($Cert);
22+
[string] $pkeyPem = $null;
23+
24+
if ($null -ne $pkey) {
25+
$pkeyPem = $pkey.ExportRSAPrivateKeyPem();
26+
}
27+
28+
if ($null -eq $pkey) {
29+
$pkey = [ECDsaCertificateExtensions]::GetECDsaPrivateKey($Cert);
30+
$pkeyPem = $pkey.ExportECPrivateKeyPem();
31+
}
32+
33+
if ($null -eq $pkeyPem) {
34+
return;
35+
}
36+
37+
38+
$pKeyPath = Join-Path $Dir -ChildPath "$Name-key.pem";
39+
[System.IO.File]::WriteAllText($pKeyPath, $pkeyPem);
40+
}
41+
42+
$ca = New-SelfSignedCertificate -CertStoreLocation 'Cert:\CurrentUser\My' `
43+
-DnsName "otel-test-ca" `
44+
-NotAfter (Get-Date).AddYears(20) `
45+
-FriendlyName "otel-test-ca" `
46+
-KeyAlgorithm ECDSA_nistP256 `
47+
-KeyExportPolicy Exportable `
48+
-KeyUsageProperty All -KeyUsage CertSign, CRLSign, DigitalSignature;
49+
50+
51+
try {
52+
Write-Certificate -Cert $ca -Name "otel-test-ca" -Dir $OutDir;
53+
$serverCert = New-SelfSignedCertificate -CertStoreLocation 'Cert:\CurrentUser\My' `
54+
-DnsName "otel-collector" `
55+
-Signer $ca `
56+
-NotAfter (Get-Date).AddYears(20) `
57+
-FriendlyName "otel-test-server" `
58+
-KeyAlgorithm ECDSA_nistP256 `
59+
-KeyUsageProperty All `
60+
-KeyExportPolicy Exportable `
61+
-KeyUsage CertSign, CRLSign, DigitalSignature `
62+
-TextExtension @("2.5.29.19={text}CA=1&pathlength=1", "2.5.29.37={text}1.3.6.1.5.5.7.3.1");
63+
64+
try {
65+
Write-Certificate -Cert $serverCert -Name "otel-test-server" -Dir $OutDir;
66+
67+
$clientCert = New-SelfSignedCertificate -CertStoreLocation 'Cert:\CurrentUser\My' `
68+
-DnsName "otel-test-client" `
69+
-Signer $ca `
70+
-NotAfter (Get-Date).AddYears(20) `
71+
-FriendlyName "otel-test-client" `
72+
-KeyAlgorithm ECDSA_nistP256 `
73+
-KeyUsageProperty All `
74+
-KeyExportPolicy Exportable `
75+
-KeyUsage CertSign, CRLSign, DigitalSignature `
76+
-TextExtension @("2.5.29.19={text}CA=1&pathlength=1", "2.5.29.37={text}1.3.6.1.5.5.7.3.2");
77+
try {
78+
Write-Certificate -Cert $clientCert -Name "otel-test-client" -Dir $OutDir;
79+
}
80+
finally {
81+
Get-Item -Path "Cert:\CurrentUser\My\$($clientCert.Thumbprint)" | Remove-Item;
82+
}
83+
}
84+
finally {
85+
Get-Item -Path "Cert:\CurrentUser\My\$($serverCert.Thumbprint)" | Remove-Item;
86+
}
87+
}
88+
finally {
89+
Get-Item -Path "Cert:\CurrentUser\My\$($ca.Thumbprint)" | Remove-Item;
90+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/bash
2+
3+
# Set output directory, default is the current directory
4+
OUT_DIR=${1:-"."}
5+
6+
# Create output directory if it doesn't exist
7+
mkdir -p "$OUT_DIR"
8+
9+
# Generate CA certificate (Certificate Authority)
10+
openssl req -new -newkey rsa:2048 -days 3650 -nodes -x509 \
11+
-subj "/CN=otel-test-ca" \
12+
-keyout "$OUT_DIR/otel-test-ca-key.pem" -out "$OUT_DIR/otel-test-ca-cert.pem"
13+
14+
# Create the extension configuration file for the server certificate
15+
cat > "$OUT_DIR/server_cert_ext.cnf" <<EOF
16+
[ v3_req ]
17+
basicConstraints = CA:FALSE
18+
nsCertType = server
19+
nsComment = "OpenSSL Generated Server Certificate"
20+
subjectKeyIdentifier = hash
21+
authorityKeyIdentifier = keyid,issuer:always
22+
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
23+
extendedKeyUsage = serverAuth
24+
subjectAltName = @alt_names
25+
26+
[ alt_names ]
27+
DNS.1 = otel-collector
28+
EOF
29+
30+
# Generate server certificate private key and CSR (Certificate Signing Request)
31+
openssl req -new -newkey rsa:2048 -sha256 -nodes \
32+
-keyout "$OUT_DIR/otel-test-server-key.pem" -out "$OUT_DIR/otel-test-server-csr.pem" \
33+
-subj "/CN=otel-collector"
34+
35+
# Sign the server certificate using the CA certificate
36+
openssl x509 -req -in "$OUT_DIR/otel-test-server-csr.pem" \
37+
-extfile "$OUT_DIR/server_cert_ext.cnf" -extensions v3_req \
38+
-CA "$OUT_DIR/otel-test-ca-cert.pem" -CAkey "$OUT_DIR/otel-test-ca-key.pem" -CAcreateserial \
39+
-out "$OUT_DIR/otel-test-server-cert.pem" \
40+
-days 3650 -sha256
41+
42+
# Create the extension configuration file for the client certificate
43+
cat > "$OUT_DIR/client_cert_ext.cnf" <<EOF
44+
[ v3_client ]
45+
basicConstraints = CA:FALSE
46+
nsCertType = client, email
47+
nsComment = "OpenSSL Generated Client Certificate"
48+
subjectKeyIdentifier = hash
49+
authorityKeyIdentifier = keyid,issuer
50+
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
51+
extendedKeyUsage = clientAuth, emailProtection
52+
EOF
53+
54+
# Generate client certificate private key and CSR
55+
openssl req -new -newkey rsa:2048 -sha256 -nodes \
56+
-keyout "$OUT_DIR/otel-test-client-key.pem" -out "$OUT_DIR/otel-test-client-csr.pem" \
57+
-subj "/CN=otel-test-client"
58+
59+
# Sign the client certificate using the CA certificate
60+
openssl x509 -req -in "$OUT_DIR/otel-test-client-csr.pem" \
61+
-extfile "$OUT_DIR/client_cert_ext.cnf" -extensions v3_client \
62+
-CA "$OUT_DIR/otel-test-ca-cert.pem" -CAkey "$OUT_DIR/otel-test-ca-key.pem" -CAcreateserial \
63+
-out "$OUT_DIR/otel-test-client-cert.pem" \
64+
-days 3650 -sha256
65+
66+
# Generate an untrusted self-signed certificate (not signed by the CA)
67+
openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 \
68+
-subj "/CN=otel-untrusted-collector" \
69+
-keyout "$OUT_DIR/otel-untrusted-collector-key.pem" -out "$OUT_DIR/otel-untrusted-collector-cert.pem"

0 commit comments

Comments
 (0)