Skip to content

Commit 7baa497

Browse files
azure-sdkscbedd
andauthored
Sync eng/common directory with azure-sdk-tools for PR 1909 (Azure#20298)
* add test-proxy invocations to eng/common folder Co-authored-by: scbedd <[email protected]>
1 parent 7ba9f04 commit 7baa497

File tree

9 files changed

+226
-0
lines changed

9 files changed

+226
-0
lines changed

eng/common/scripts/common.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ $GetDocsMsMetadataForPackageFn = "Get-${Language}-DocsMsMetadataForPackage"
4444
$GetDocsMsDevLanguageSpecificPackageInfoFn = "Get-${Language}-DocsMsDevLanguageSpecificPackageInfo"
4545
$GetGithubIoDocIndexFn = "Get-${Language}-GithubIoDocIndex"
4646
$FindArtifactForApiReviewFn = "Find-${Language}-Artifacts-For-Apireview"
47+
$TestProxyTrustCertFn = "Import-Dev-Cert-${Language}"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
. $PSScriptRoot/common.ps1
2+
3+
if ($TestProxyTrustCertFn -and (Test-Path "Function:$TestProxyTrustCertFn"))
4+
{
5+
&$TestProxyTrustCertFn
6+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
TMP_PATH=$CERT_FOLDER
3+
PFXFILE=$CERT_FOLDER/dotnet-devcert.pfx
4+
CRTFILE=$CERT_FOLDER/dotnet-devcert.crt
5+
6+
NSSDB_PATHS=(
7+
"$HOME/.pki/nssdb"
8+
"$HOME/snap/chromium/current/.pki/nssdb"
9+
"$HOME/snap/postman/current/.pki/nssdb"
10+
)
11+
12+
function configure_nssdb() {
13+
echo "Configuring nssdb for $1"
14+
certutil -d sql:$1 -D -n dotnet-devcert
15+
certutil -d sql:$1 -A -t "CP,," -n dotnet-devcert -i $CRTFILE
16+
}
17+
18+
for NSSDB in ${NSSDB_PATHS[@]}; do
19+
if [ -d "$NSSDB" ]; then
20+
configure_nssdb $NSSDB
21+
fi
22+
done
23+
24+
if [ $(id -u) -ne 0 ]; then
25+
SUDO='sudo'
26+
fi
27+
28+
$SUDO cp $CRTFILE "/usr/local/share/ca-certificates"
29+
$SUDO update-ca-certificates
30+
31+
dotnet dev-certs https --clean --import $PFXFILE -p "password"
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env pwsh -c
2+
3+
<#
4+
.DESCRIPTION
5+
Start the docker proxy container. If it is already running, quietly exit. Any other error should fail.
6+
.PARAMETER Mode
7+
"start" or "stop" to start up or stop the test-proxy instance.
8+
.PARAMETER TargetFolder
9+
The folder in which context the test proxy will be started. Defaults to current working directory.
10+
#>
11+
[CmdletBinding(SupportsShouldProcess = $true)]
12+
param(
13+
[ValidateSet("start", "stop")]
14+
[String]
15+
$Mode,
16+
[String]
17+
$TargetFolder = "."
18+
)
19+
20+
try {
21+
docker --version | Out-Null
22+
}
23+
catch {
24+
Write-Error "A invocation of docker --version failed. This indicates that docker is not properly installed or running."
25+
Write-Error "Please check your docker invocation and try running the script again."
26+
}
27+
28+
$SELECTED_IMAGE_TAG = "1037115"
29+
$CONTAINER_NAME = "ambitious_azsdk_test_proxy"
30+
$LINUX_IMAGE_SOURCE = "azsdkengsys.azurecr.io/engsys/testproxy-lin:${SELECTED_IMAGE_TAG}"
31+
$WINDOWS_IMAGE_SOURCE = "azsdkengsys.azurecr.io/engsys/testproxy-win:${SELECTED_IMAGE_TAG}"
32+
$root = (Resolve-Path $TargetFolder).Path.Replace("`\", "/")
33+
34+
function Get-Proxy-Container(){
35+
return (docker container ls -a --format "{{ json . }}" --filter "name=$CONTAINER_NAME" `
36+
| ConvertFrom-Json `
37+
| Select-Object -First 1)
38+
}
39+
40+
41+
$SelectedImage = $LINUX_IMAGE_SOURCE
42+
$Initial = ""
43+
44+
# most of the time, running this script on a windows machine will work just fine, as docker defaults to linux containers
45+
# however, in CI, windows images default to _windows_ containers. We cannot swap them. We can tell if we're in a CI build by
46+
# checking for the environment variable TF_BUILD.
47+
if ($IsWindows -and $env:TF_BUILD){
48+
$SelectedImage = $WINDOWS_IMAGE_SOURCE
49+
$Initial = "C:"
50+
}
51+
52+
if ($Mode -eq "start"){
53+
$proxyContainer = Get-Proxy-Container
54+
55+
# if we already have one, we just need to check the state
56+
if($proxyContainer){
57+
if ($proxyContainer.State -eq "running")
58+
{
59+
Write-Host "Discovered an already running instance of the test-proxy!. Exiting"
60+
exit(0)
61+
}
62+
}
63+
# else we need to create it
64+
else {
65+
Write-Host "Attempting creation of Docker host $CONTAINER_NAME"
66+
Write-Host "docker container create -v `"${root}:${Initial}/etc/testproxy`" -p 5001:5001 -p 5000:5000 --name $CONTAINER_NAME $SelectedImage"
67+
docker container create -v "${root}:${Initial}/etc/testproxy" -p 5001:5001 -p 5000:5000 --name $CONTAINER_NAME $SelectedImage
68+
}
69+
70+
Write-Host "Attempting start of Docker host $CONTAINER_NAME"
71+
docker container start $CONTAINER_NAME
72+
}
73+
74+
if ($Mode -eq "stop"){
75+
$proxyContainer = Get-Proxy-Container
76+
77+
if($proxyContainer){
78+
if($proxyContainer.State -eq "running"){
79+
Write-Host "Found a running instance of $CONTAINER_NAME, shutting it down."
80+
docker container stop $CONTAINER_NAME
81+
}
82+
}
83+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDSDCCAjCgAwIBAgIUPMKpJ/j10eQrcQBNnkImIaOYHakwDQYJKoZIhvcNAQEL
3+
BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTIxMDgwNTAwMzU1NloXDTIyMDgw
4+
NTAwMzU1NlowFDESMBAGA1UEAwwJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEF
5+
AAOCAQ8AMIIBCgKCAQEAxe/ZseXgOTVoF7uTjX5Leknk95jIoyGc+VlxA8BhzGOr
6+
r4u6VNQZRCMq+svHY36tW4+u/xHNe2kvbwy2mnS8cFFLfst+94qBZVJDBxSGZ9I/
7+
wekErNsjFsik4UrMvcC+ZlGPh7hb3f7tSx29tn1DIkAUXVnbZ6TT5s+mYRQpZ6fW
8+
6kR3RNfc0A1IUM7Zs9yfNEr0O2H41P2HcLKoOPtvd7GvTQm9Ofh3srKvII+sZn/J
9+
WH7r76oRQMX904mOMdryQwZLObsqX4dXIEbafKVSecB3PBVIhv8gVtJhcZbQP1pI
10+
mMiWd6PHv46ZhGf7+cKnYUSa8Ia2t/wetK1wd00dFwIDAQABo4GRMIGOMA8GA1Ud
11+
EwEB/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgGmMBYGA1UdJQEB/wQMMAoGCCsGAQUF
12+
BwMBMBcGA1UdEQEB/wQNMAuCCWxvY2FsaG9zdDA6BgorBgEEAYI3VAEBBCwMKkFT
13+
UC5ORVQgQ29yZSBIVFRQUyBkZXZlbG9wbWVudCBjZXJ0aWZpY2F0ZTANBgkqhkiG
14+
9w0BAQsFAAOCAQEAIj2VlBVcXGSly6KCBg6lgwFi+henWfSox77iuGAaAxDjN3jd
15+
9lZahW4MPNLHKSrPRb4YNSLZ2jh7zdcttQrqd4qH65o1q56q5JrCmli99iIzY9Y8
16+
RdYyxK4Zzr31wjpsyFiWQfqJTuSFUUg9uDDj0negwEZLIGlt7nr12wflt2+QOJtD
17+
byMeSZLbB5dPzn341DK0qfJEJMMgL0XsPEVZ3TQ6Alc9zq5wI608C/mXnz3xJE05
18+
UTYD8pRJJ/DyG0empvOVE8Sg93msHPquAbgqO9aqCpykgg/a8CFvI4wRdfvGEFlv
19+
8XJKL8Y/PFsmFeO3axq3zUYKFVdc9Un4dFIaag==
20+
-----END CERTIFICATE-----
2.39 KB
Binary file not shown.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[req]
2+
prompt = no
3+
default_bits = 2048
4+
distinguished_name = subject
5+
req_extensions = req_ext
6+
x509_extensions = x509_ext
7+
8+
[ subject ]
9+
commonName = localhost
10+
11+
[req_ext]
12+
basicConstraints = critical, CA:true
13+
subjectAltName = @alt_names
14+
15+
[x509_ext]
16+
basicConstraints = critical, CA:true
17+
keyUsage = critical, keyCertSign, cRLSign, digitalSignature,keyEncipherment
18+
extendedKeyUsage = critical, serverAuth
19+
subjectAltName = critical, @alt_names
20+
1.3.6.1.4.1.311.84.1.1 = ASN1:UTF8String:ASP.NET Core HTTPS development certificate # Needed to get it imported by dotnet dev-certs
21+
22+
[alt_names]
23+
DNS.1 = localhost
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
parameters:
2+
rootFolder: '$(Build.SourcesDirectory)'
3+
4+
steps:
5+
- pwsh: |
6+
$(Build.SourcesDirectory)/eng/common/scripts/trust-proxy-certificate.ps1
7+
displayName: 'Language Specific Certificate Trust'
8+
9+
- pwsh: |
10+
$(Build.SourcesDirectory)/eng/common/testproxy/docker-start-proxy.ps1 -Mode start -TargetFolder "${{ parameters.rootFolder }}"
11+
displayName: 'Run the docker container'
12+
13+
- pwsh: |
14+
docker container ls -a
15+
displayName: Check running container
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
parameters:
2+
rootFolder: '$(Build.SourcesDirectory)'
3+
4+
steps:
5+
- pwsh: |
6+
$(Build.SourcesDirectory)/eng/common/scripts/trust-proxy-certificate.ps1
7+
displayName: 'Language Specific Certificate Trust'
8+
9+
- pwsh: |
10+
Write-Host "##vso[task.setvariable variable=OriginalPath]$env:PATH"
11+
displayName: 'Store Path Value'
12+
13+
- pwsh: |
14+
Write-Host "##vso[task.setvariable variable=ASPNETCORE_Kestrel__Certificates__Default__Path]$(Build.SourcesDirectory)/eng/common/testproxy/dotnet-devcert.pfx"
15+
Write-Host "##vso[task.setvariable variable=ASPNETCORE_Kestrel__Certificates__Default__Password]password"
16+
displayName: 'Configure Kestrel Environment Variables'
17+
18+
- task: UseDotNet@2
19+
displayName: "Use .NET Core SDK"
20+
inputs:
21+
packageType: sdk
22+
version: 5.0.205
23+
24+
- pwsh: |
25+
dotnet tool install azure.sdk.tools.testproxy `
26+
--tool-path $(Build.BinariesDirectory)/test-proxy `
27+
--add-source https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json `
28+
--version 1.0.0-dev.20210811.2
29+
displayName: "Install test-proxy"
30+
31+
- pwsh: |
32+
Start-Process $(Build.BinariesDirectory)/test-proxy/test-proxy.exe `
33+
-ArgumentList "--storage-location '${{ parameters.rootFolder }}'" `
34+
-NoNewWindow -PassThru
35+
displayName: 'Run the testproxy - windows'
36+
condition: and(succeeded(), eq(variables['Agent.OS'],'Windows_NT'))
37+
38+
# nohup does NOT continue beyond the current session if you use it within powershell
39+
- bash: |
40+
sudo nohup $(Build.BinariesDirectory)/test-proxy/test-proxy &
41+
displayName: "Run the testproxy - linux/mac"
42+
condition: and(succeeded(), ne(variables['Agent.OS'],'Windows_NT'))
43+
workingDirectory: "${{ parameters.rootFolder }}"
44+
45+
- pwsh: |
46+
Write-Host "##vso[task.setvariable variable=PATH]$(OriginalPath)"
47+
displayName: 'Restore .NET version by resetting path'

0 commit comments

Comments
 (0)