Skip to content

Commit 701f380

Browse files
authored
Remove signatures in-place from AppInsights agent jar before adding it to worker package (#810)
* remove signature files in place * clean up script
1 parent 9121453 commit 701f380

File tree

1 file changed

+131
-60
lines changed

1 file changed

+131
-60
lines changed

package-pipeline.ps1

+131-60
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,159 @@
11
param (
2-
[string]$buildNumber
2+
[string]$buildNumber
33
)
44

5-
# A function that checks exit codes and fails script if an error is found
5+
# A helper function that stops the entire script if the last command failed.
66
function StopOnFailedExecution {
7-
if ($LastExitCode)
8-
{
9-
exit $LastExitCode
10-
}
7+
if ($LastExitCode) {
8+
exit $LastExitCode
9+
}
1110
}
1211

13-
Write-Host "Building azure-functions-java-worker with appinsights profile"
12+
# --------------------------------------------------------------------
13+
# Build the azure-functions-java-worker (using the "appinsights" profile)
14+
# --------------------------------------------------------------------
15+
Write-Host "=== Building azure-functions-java-worker with 'appinsights' profile ==="
1416
mvn clean package --no-transfer-progress -B -P appinsights
1517
StopOnFailedExecution
1618

17-
Write-Host "Creating nuget package Microsoft.Azure.Functions.JavaWorker"
18-
Write-Host "buildNumber: " $buildNumber
19-
Get-Command nuget
19+
# --------------------------------------------------------------------
20+
# Prepare the final "pkg" folder and copy core worker artifacts
21+
# --------------------------------------------------------------------
22+
Write-Host "`n=== Creating NuGet package: Microsoft.Azure.Functions.JavaWorker ==="
23+
Write-Host "Using buildNumber: $buildNumber"
24+
25+
# Ensure 'nuget' command is available
26+
Get-Command nuget | Out-Null
2027
StopOnFailedExecution
21-
remove-item pkg -Recurse -ErrorAction Ignore
22-
mkdir pkg
23-
Get-ChildItem -Path .\target\* -Include 'azure*' -Exclude '*shaded.jar','*tests.jar' | %{ Copy-Item $_.FullName .\pkg\azure-functions-java-worker.jar }
28+
29+
Write-Host "Removing old 'pkg' folder (if present)..."
30+
Remove-Item -Recurse -Force -ErrorAction Ignore .\pkg
31+
32+
Write-Host "Creating new 'pkg' folder..."
33+
New-Item -ItemType Directory -Path .\pkg | Out-Null
34+
35+
Write-Host "Copying azure-functions-java-worker.jar to 'pkg'..."
36+
Get-ChildItem -Path .\target\* -Include 'azure*' -Exclude '*shaded.jar','*tests.jar' |
37+
ForEach-Object { Copy-Item $_.FullName .\pkg\azure-functions-java-worker.jar }
2438
StopOnFailedExecution
25-
copy-item ./worker.config.json pkg
26-
copy-item ./tools/AzureFunctionsJavaWorker.nuspec pkg/
27-
copy-item ./annotationLib pkg/annotationLib -Recurse
2839

29-
# locate the agent jar produced by the `appinsights` Maven profile
30-
$ApplicationInsightsAgentFile = [System.IO.Path]::Combine($PSScriptRoot, 'target', 'agent', 'applicationinsights-agent.jar')
40+
Write-Host "Copying supporting files into 'pkg' folder..."
41+
Copy-Item .\worker.config.json .\pkg\
42+
Copy-Item .\tools\AzureFunctionsJavaWorker.nuspec .\pkg\
43+
Copy-Item .\annotationLib .\pkg\annotationLib -Recurse
3144

32-
if (!(Test-Path -Path $ApplicationInsightsAgentFile)) {
33-
Write-Host "Error: $ApplicationInsightsAgentFile not found."
34-
Write-Host "Make sure you enabled the 'appinsights' Maven profile that copies the AI agent to target/agent/."
45+
# --------------------------------------------------------------------
46+
# Locate the Application Insights agent built by the Maven profile
47+
# --------------------------------------------------------------------
48+
$AgentSourcePath = Join-Path $PSScriptRoot 'target\agent\applicationinsights-agent.jar'
49+
if (!(Test-Path -Path $AgentSourcePath)) {
50+
Write-Host "`nERROR: Application Insights agent not found at '$AgentSourcePath'."
51+
Write-Host "Make sure you enabled the 'appinsights' Maven profile."
3552
exit 1
3653
}
3754

38-
# local testing cleanup
39-
$oldOutput = [System.IO.Path]::Combine($PSScriptRoot, "agent")
40-
if (Test-Path -Path $oldOutput) {
41-
Remove-Item -Path $oldOutput -Recurse
42-
}
55+
# --------------------------------------------------------------------
56+
# Create a local 'agent' folder and copy the agent jar there
57+
# --------------------------------------------------------------------
58+
Write-Host "`n=== Setting up the agent folder ==="
4359

44-
# local testing cleanup
45-
$oldExtract = [System.IO.Path]::Combine($PSScriptRoot, "extract")
46-
if (Test-Path -Path $oldExtract) {
47-
Remove-Item -Path $oldExtract -Recurse
48-
}
60+
$AgentFolder = Join-Path $PSScriptRoot 'agent'
61+
$AgentFilename = 'applicationinsights-agent.jar'
62+
$PackagedAgentFile = Join-Path $AgentFolder $AgentFilename
4963

50-
$extract = new-item -type directory -force $PSScriptRoot\extract
51-
if (-not(Test-Path -Path $extract)) {
52-
echo "Fail to create a new directory $extract"
53-
exit 1
64+
Write-Host "Removing old 'agent' folder (if present)..."
65+
if (Test-Path -Path $AgentFolder) {
66+
Remove-Item -Recurse -Force $AgentFolder
5467
}
5568

56-
echo "Start extracting content from $ApplicationInsightsAgentFilename to extract folder"
57-
cd -Path $extract -PassThru
58-
jar xf $ApplicationInsightsAgentFile
59-
cd $PSScriptRoot
60-
echo "Done extracting"
69+
Write-Host "Creating a new 'agent' folder..."
70+
New-Item -ItemType Directory -Path $AgentFolder | Out-Null
6171

62-
echo "Unsign $ApplicationInsightsAgentFilename"
63-
Remove-Item $extract\META-INF\MSFTSIG.*
64-
$manifest = "$extract\META-INF\MANIFEST.MF"
65-
$newContent = (Get-Content -Raw $manifest | Select-String -Pattern '(?sm)^(.*?\r?\n)\r?\n').Matches[0].Groups[1].Value
66-
Set-Content -Path $manifest $newContent
72+
Write-Host "Copying agent from '$AgentSourcePath' to '$PackagedAgentFile'..."
73+
Copy-Item $AgentSourcePath -Destination $PackagedAgentFile
74+
StopOnFailedExecution
6775

68-
$agent = new-item -type directory -force $PSScriptRoot\agent
69-
$filename = "applicationinsights-agent.jar"
70-
$result = [System.IO.Path]::Combine($agent, $filename)
71-
echo "re-jar $filename"
76+
# --------------------------------------------------------------------
77+
# Remove signature files and adjust MANIFEST.MF in-place (no full extraction)
78+
# --------------------------------------------------------------------
79+
Write-Host "`n=== Removing signature files from '$PackagedAgentFile' ==="
7280

73-
cd -Path $extract -PassThru
74-
jar cfm $result META-INF/MANIFEST.MF .
81+
# Load .NET assemblies for ZipArchive on Windows
82+
Add-Type -AssemblyName System.IO.Compression, System.IO.Compression.FileSystem
7583

76-
if (-not(Test-Path -Path $result)) {
77-
echo "Fail to re-archive $filename"
78-
exit 1
84+
$fileStream = [System.IO.File]::Open($PackagedAgentFile, [System.IO.FileMode]::Open)
85+
$zipArchive = New-Object System.IO.Compression.ZipArchive($fileStream, [System.IO.Compression.ZipArchiveMode]::Update)
86+
87+
try {
88+
Write-Host "Deleting signature files from META-INF..."
89+
$entriesToRemove = $zipArchive.Entries | Where-Object {
90+
$_.FullName -like "META-INF/MSFTSIG.*" `
91+
-or $_.FullName -like "META-INF/*.SF" `
92+
-or $_.FullName -like "META-INF/*.RSA" `
93+
-or $_.FullName -like "META-INF/*.DSA"
94+
}
95+
96+
foreach ($entry in $entriesToRemove) {
97+
Write-Host " Removing: $($entry.FullName)"
98+
$entry.Delete()
99+
}
100+
101+
Write-Host "Checking MANIFEST.MF for extra signature references..."
102+
$manifestEntry = $zipArchive.Entries | Where-Object { $_.FullName -eq "META-INF/MANIFEST.MF" }
103+
if ($manifestEntry) {
104+
$reader = New-Object System.IO.StreamReader($manifestEntry.Open())
105+
$manifestContent = $reader.ReadToEnd()
106+
$reader.Close()
107+
108+
# Regex to remove blank line(s) after the main attributes
109+
$pattern = '(?sm)^(.*?\r?\n)\r?\n'
110+
$matches = [regex]::Matches($manifestContent, $pattern)
111+
112+
if ($matches.Count -gt 0) {
113+
Write-Host " Removing signature-related lines after main attributes."
114+
$cleanedManifest = $matches[0].Groups[1].Value
115+
116+
$manifestEntry.Delete()
117+
118+
$newManifestEntry = $zipArchive.CreateEntry("META-INF/MANIFEST.MF")
119+
$writer = New-Object System.IO.StreamWriter($newManifestEntry.Open())
120+
$writer.Write($cleanedManifest)
121+
$writer.Flush()
122+
$writer.Close()
123+
124+
Write-Host " MANIFEST.MF updated successfully."
125+
}
126+
else {
127+
Write-Host " No extra blank lines found in MANIFEST.MF."
128+
}
129+
}
130+
else {
131+
Write-Host "No MANIFEST.MF found in the JAR (unexpected?)."
132+
}
133+
}
134+
finally {
135+
# Always dispose archive and file streams
136+
$zipArchive.Dispose()
137+
$fileStream.Dispose()
79138
}
80-
Write-Host "Creating the functions.codeless file"
81-
New-Item -path $PSScriptRoot\agent -type file -name "functions.codeless"
82139

83-
cd $PSScriptRoot
84-
Copy-Item $PSScriptRoot/agent $PSScriptRoot/pkg/agent -Recurse -Verbose
140+
Write-Host "Done removing signature files from '$PackagedAgentFile'."
141+
142+
# --------------------------------------------------------------------
143+
# Add 'functions.codeless' marker and copy agent folder to 'pkg'
144+
# --------------------------------------------------------------------
145+
Write-Host "`n=== Creating 'functions.codeless' marker file ==="
146+
New-Item -Path $AgentFolder -Name "functions.codeless" -ItemType File | Out-Null
85147

86-
set-location pkg
148+
Write-Host "Copying 'agent' folder into the 'pkg' folder..."
149+
Copy-Item $AgentFolder (Join-Path $PSScriptRoot 'pkg\agent') -Recurse -Force -Verbose
150+
151+
# --------------------------------------------------------------------
152+
# Package everything into the final NuGet package
153+
# --------------------------------------------------------------------
154+
Write-Host "`n=== Creating the NuGet package ==="
155+
Push-Location pkg
87156
nuget pack -Properties version=$buildNumber
88-
set-location ..
157+
Pop-Location
158+
159+
Write-Host "`n=== Script completed successfully. NuGet package created. ==="

0 commit comments

Comments
 (0)