Skip to content

Commit f63305b

Browse files
authored
fix(m365): Container security fixes (#12)
* fix(m365): set user in docker container to ContainerUser * fix(m365): manually install opa exe for better control of version * fix: always run image scan so we can compare PRs
1 parent 1e263d0 commit f63305b

3 files changed

Lines changed: 21 additions & 9 deletions

File tree

.github/workflows/m365_image_build.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ jobs:
128128
exit 0
129129
outputs:
130130
image: ${{ steps.build-and-push.outputs.image }}
131-
m365-scan:
131+
scan:
132132
name: Scan
133-
if: github.ref == 'refs/heads/main'
133+
# This condition prevents duplicate runs.
134+
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
134135
permissions:
135136
security-events: write
136137
needs: build

m365/image/Dockerfile

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
FROM mcr.microsoft.com/windows/servercore:ltsc2022
2+
SHELL ["powershell"]
3+
24

35
ARG SCUBAGEAR_VERSION=1.5.0
6+
ARG OPA_VERSION=1.3.0
47
# Get static URL for current version: curl -s -D- https://aka.ms/downloadazcopy-v10-windows | grep ^Location
58
# https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-v10?tabs=dnf#obtain-a-static-download-link
69
ARG AZCOPY_URL=https://azcopyvnext-awgzd8g7aagqhzhe.b02.azurefd.net/releases/release-10.29.0-20250428/azcopy_windows_amd64_10.29.0.zip
@@ -10,15 +13,22 @@ LABEL scubagear_version=${SCUBAGEAR_VERSION}
1013
WORKDIR /app
1114

1215
# download azcopy exe to workdir
13-
RUN powershell Invoke-WebRequest -Uri %AZCOPY_URL% -OutFile AzCopy.zip -UseBasicParsing
14-
RUN powershell Expand-Archive .\AzCopy.zip ./AzCopy -Force
15-
RUN powershell $item = Get-ChildItem .\AzCopy\*\azcopy.exe; Move-Item -Path $item -Destination .
16-
RUN powershell Remove-Item AzCopy.zip; Remove-Item -r .\AzCopy
16+
RUN $ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest -Uri $Env:AZCOPY_URL -OutFile AzCopy.zip -UseBasicParsing
17+
RUN Expand-Archive .\AzCopy.zip ./AzCopy -Force
18+
RUN $item = Get-ChildItem .\AzCopy\*\azcopy.exe; Move-Item -Path $item -Destination .
19+
RUN Remove-Item AzCopy.zip; Remove-Item -r .\AzCopy
1720

1821
# Needed for setup module installs
19-
RUN powershell Install-PackageProvider -Name NuGet -Force
20-
RUN powershell Install-Module -Name ScubaGear -RequiredVersion %SCUBAGEAR_VERSION% -Force
21-
RUN powershell Initialize-SCuBA
22+
RUN Install-PackageProvider -Name NuGet -Force
23+
RUN Install-Module -Name ScubaGear -RequiredVersion $Env:SCUBAGEAR_VERSION -Force
24+
RUN Initialize-SCuBA -Scope AllUsers -NoOPA
2225
COPY run_container.ps1 .
2326

27+
# manually install OPA, grant ContainerUser execute permissions, then switch to user
28+
ENV OPA_NAME="opa_windows_amd64.exe"
29+
RUN $ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest -Uri https://openpolicyagent.org/downloads/v$($Env:OPA_VERSION)/opa_windows_amd64.exe -OutFile $Env:OPA_NAME -UseBasicParsing
30+
RUN if ((Get-FileHash $Env:OPA_NAME -Algorithm SHA256).Hash -ne ([System.Text.Encoding]::ASCII.GetString((Invoke-WebRequest -Uri https://openpolicyagent.org/downloads/v$($Env:OPA_VERSION)/opa_windows_amd64.exe.sha256 -UseBasicParsing).Content) -split ' ')[0]) { exit 1 }
31+
RUN icacls.exe $env:OPA_NAME /grant 'User Manager\ContainerUser:RX'
32+
USER ContainerUser
33+
2434
CMD [ "powershell", ".\\run_container.ps1" ]

m365/image/run_container.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Foreach ($tenantConfig in $(Get-ChildItem 'input\')) {
4040
AppID = $Env:APP_ID; # App ID; Needed for Service Principal Auth
4141
Organization = $org; # primary domain of the tenantConfig needed for Service Principal Auth
4242
OutPath = ".\reports\$($org)"; # The folder path where the output will be stored
43+
OPAPath = "."
4344
ConfigFilePath = $tenantConfig.FullName
4445
Quiet = $true;
4546
}

0 commit comments

Comments
 (0)