summaryrefslogtreecommitdiff
path: root/test/integration/targets/win_package/library/win_make_appx.ps1
blob: eaf6e8e1ee5269d8435bb7b9a00798621fb9bfcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!powershell

# Copyright: (c) 2020, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

#AnsibleRequires -CSharpUtil Ansible.Basic
#Requires -Module Ansible.ModuleUtils.ArgvParser
#Requires -Module Ansible.ModuleUtils.CommandUtil

$spec = @{
    options = @{
        packages = @{
            type = "list"
            elements = "dict"
            options = @{
                identity = @{ type = "str"; required = $true }
                version = @{ type = "str"; required = $true }
                architecture = @{ type = "str" }
                resource_id = @{ type = "str" }
                min_version = @{ type = "str"; default = "10.0.17763.0" }
                max_version = @{ type = "str"; default = "10.0.18362.0" }
                filename = @{ type = "str"; required = $true }
            }
        }
        bundles = @{
            type = "list"
            elements = "dict"
            options = @{
                files = @{ type = "list"; elements = "str"; required = $true }
                filename = @{ type = "str"; required = $true }
            }
        }
        publisher = @{ type = "str"; required = $true }
        path = @{ type = "str"; required = $true }
        makeappx_path = @{ type = "str"; required = $true }
        signtool_path = @{ type = "str"; required = $true }
    }
}

$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)

$packages = $module.Params.packages
$bundles = $module.Params.bundles
$publisher = $module.Params.publisher
$path = $module.Params.path
$makeappxPath = $module.Params.makeappx_path
$signtoolPath = $module.Params.signtool_path

if (-not (Test-Path -LiteralPath $path)) {
    $module.FailJson("The path at '$path' does not exist")
}

$manifest = @'
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities">
    <Identity Name="{0}" Version="{1}" Publisher="{2}"{3}{4} />
    <Properties>
        <DisplayName>{0}DisplayName</DisplayName>
        <PublisherDisplayName>PublisherDisplayName</PublisherDisplayName>
        <Description>Test MSIX Package for win_package</Description>
        <Logo>icon.png</Logo>
    </Properties>
    <Resources>
        <Resource Language="en-us" />
    </Resources>
    <Dependencies>
        <TargetDeviceFamily Name="Windows.Desktop" MinVersion="{5}" MaxVersionTested="{6}" />
    </Dependencies>
    <Capabilities>
        <rescap:Capability Name="runFullTrust"/>
    </Capabilities>
    <Applications>
        <Application Id="MsixPackage" Executable="test.exe" EntryPoint="Windows.FullTrustApplication">
            <uap:VisualElements DisplayName="{0}AppDisplayName" Description="Description" Square150x150Logo="icon.png" Square44x44Logo="icon.png" BackgroundColor="#464646"/>
        </Application>
    </Applications>
</Package>
'@

# bytes of http://1x1px.me/000000-0.png
$iconBytes = [System.Convert]::FromBase64String('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR4nGNiYAAAAAkAAxkR2eQAAAAASUVORK5CYII=')

$certParams = @{
    # Can only create in the My store, so store it there temporarily.
    CertStoreLocation = 'Cert:\CurrentUser\My'
    FriendlyName = 'win_package test'
    KeyUsage = 'DigitalSignature'
    Subject = $publisher
    TextExtension = @("2.5.29.37={text}1.3.6.1.5.5.7.3.3", "2.5.29.19={text}")
    Type = 'Custom'
}
$cert = New-SelfSignedCertificate @certParams

try {
    # Need to create a temporary pfx for signtool.exe and we need to import the cert to the Trusted Root store.
    $module.Result.thumbprint = $cert.Thumbprint
    $certPath = Join-Path -Path $module.Tmpdir -ChildPath 'cert.pfx'
    $certPassword = ([char[]]([char]33..[char]126) | Sort-Object {Get-Random})[0..16] -join ''
    $certPasswordSS = ConvertTo-SecureString -String $certPassword -AsPlainText -Force
    $null = $cert |  Export-PfxCertificate -FilePath $certPath -Password $certPasswordSS

    $importParams = @{
        FilePath = $certPath
        CertStoreLocation = 'Cert:\LocalMachine\Root'
        Password = $certPasswordSS
    }
    $null = Import-PfxCertificate @importParams
} finally {
    $cert | Remove-Item -Force
}

$module.Result.changed = $true

foreach ($info in $packages) {
    $architectureAttribute = ""
    if ($info.architecture) {
        $architectureAttribute = " ProcessorArchitecture=`"$($info.architecture)`""
    }

    $resourceIdAttribute = ""
    if ($info.resource_id) {
        $resourceIdAttribute = " ResourceId=`"$($info.resource_id)`""
    }

    $xml = $manifest -f @(
        $info.identity, $info.version, $publisher, $architectureAttribute, $resourceIdAttribute, $info.min_version,
        $info.max_version
    )

    $tempDir = Join-Path -Path $module.Tmpdir -ChildPath ([System.IO.Path]::GetRandomFileName())
    New-Item -Path $tempDir -ItemType Directory > $null
    Set-Content -LiteralPath (Join-Path -Path $tempDir -ChildPath 'AppxManifest.xml') -Value $xml
    Set-Content -LiteralPath (Join-Path -Path $tempDir -ChildPath 'icon.png') -Value $iconBytes
    Set-Content -LiteralPath (Join-Path -Path $tempDir -ChildPath 'test.exe') -Value ''

    $outPath = Join-Path -Path $path -ChildPath $info.filename
    $makeArguments = @($makeappxPath, 'pack', '/d', $tempDir, '/p', $outPath, '/o')
    $res = Run-Command -command (Argv-ToString -arguments $makeArguments)

    if ($res.rc -ne 0) {
        $module.Result.rc = $res.rc
        $module.Result.stdout = $res.stdout
        $module.Result.stderr = $res.stderr
        $module.FailJson("Failed to make package for $($info.filename): see stdout and stderr for more info")
    }

    Remove-Item -Literalpath $tempDir -Force -Recurse

    $signArguments = @($signtoolPath, 'sign', '/a', '/v', '/fd', 'SHA256', '/f', $certPath, '/p', $certPassword,
        $outPath)
    $res = Run-Command -command (Argv-ToString -arguments $signArguments)

    if ($res.rc -ne 0) {
        $module.Result.rc = $res.rc
        $module.Result.stdout = $res.stdout
        $module.Result.stderr = $res.stderr
        $module.FailJson("Failed to sign package for $($info.filename): see stdout and stderr for more info")
    }
}

foreach ($info in $bundles) {
    $tempDir = Join-Path -Path $module.Tmpdir -ChildPath ([System.IO.Path]::GetRandomFileName())
    New-Item -Path $tempDir -ItemType Directory > $null
    foreach ($name in $info.files) {
        $sourcePath = Join-Path -Path $path -ChildPath $name
        $targetPath = Join-Path -Path $tempDir -ChildPath $name
        Move-Item -LiteralPath $sourcePath -Destination $targetPath
    }
    $outPath = Join-Path -Path $path -ChildPath $info.filename
    $makeArguments = @($makeappxPath, 'bundle', '/d', $tempDir, '/p', $outPath, '/o')
    $res = Run-Command -command (Argv-ToString -arguments $makeArguments)

    if ($res.rc -ne 0) {
        $module.Result.rc = $res.rc
        $module.Result.stdout = $res.stdout
        $module.Result.stderr = $res.stderr
        $module.FailJson("Failed to make bundle for $($info.filename): see stdout and stderr for more info")
    }

    Remove-Item -LiteralPath $tempDir -Force -Recurse

    $signArguments = @($signtoolPath, 'sign', '/a', '/v', '/fd', 'SHA256', '/f', $certPath, '/p', $certPassword,
        $outPath)
    $res = Run-Command -command (Argv-ToString -arguments $signArguments)

    if ($res.rc -ne 0) {
        $module.Result.rc = $res.rc
        $module.Result.stdout = $res.stdout
        $module.Result.stderr = $res.stderr
        $module.FailJson("Failed to sign bundle for $($info.filename): see stdout and stderr for more info")
    }
}

$module.ExitJson()