native-packages 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CONTRIBUTING.md +1 -1
- data/README.md +5 -2
- data/docs/_guides/building-packages.md +5 -0
- data/docs/_guides/distribution-recipes.md +3 -1
- data/docs/_guides/getting-started.md +2 -2
- data/docs/_guides/github-actions.md +6 -4
- data/docs/_guides/hosted-repositories.md +170 -0
- data/docs/_guides/multi-platform.md +1 -1
- data/docs/_guides/native-recipes.md +6 -2
- data/docs/_guides/omarchy.md +131 -0
- data/docs/_guides/prereleases.md +1 -1
- data/docs/_guides/publishing.md +19 -0
- data/docs/_guides/release-signing.md +4 -4
- data/docs/_guides/windows-signing.md +259 -0
- data/docs/_reference/commands.md +32 -3
- data/docs/_reference/configuration.md +33 -3
- data/docs/_reference/platforms.md +126 -8
- data/docs/_reference/troubleshooting.md +22 -2
- data/docs/index.md +1 -1
- data/docs/releasing.md +2 -2
- data/examples/native-packages-all-formats.yaml +1 -1
- data/examples/native-packages-linux-distribution.yaml +76 -0
- data/examples/native-packages.yaml +1 -1
- data/lib/native_packages/appimage.rb +4 -66
- data/lib/native_packages/apt_repository.rb +190 -0
- data/lib/native_packages/build.rb +59 -6
- data/lib/native_packages/cli.rb +19 -2
- data/lib/native_packages/configuration.rb +29 -2
- data/lib/native_packages/dnf_repository.rb +131 -0
- data/lib/native_packages/flatpak.rb +171 -0
- data/lib/native_packages/flatpak_repository.rb +63 -0
- data/lib/native_packages/hosted_repository.rb +73 -0
- data/lib/native_packages/inspection.rb +3 -2
- data/lib/native_packages/macos_signing.rb +20 -2
- data/lib/native_packages/omarchy.rb +140 -0
- data/lib/native_packages/openpgp.rb +114 -0
- data/lib/native_packages/snap.rb +166 -0
- data/lib/native_packages/staging.rb +74 -0
- data/lib/native_packages/support.rb +1 -1
- data/lib/native_packages/windows_signing.rb +284 -0
- metadata +15 -1
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module NativePackages
|
|
4
|
+
# OpenPGP signatures for hosted package repositories, made with GnuPG.
|
|
5
|
+
#
|
|
6
|
+
# The key is an armored secret key in NATIVE_PACKAGES_GPG_KEY, imported
|
|
7
|
+
# into a temporary keyring that is deleted afterwards, or else a key the
|
|
8
|
+
# user's own GnuPG (and its agent) already holds. Either way it must be the
|
|
9
|
+
# configured fingerprint, and every signature is checked against that
|
|
10
|
+
# key's public half alone before it is published.
|
|
11
|
+
class OpenPGP
|
|
12
|
+
include Support
|
|
13
|
+
KEY ="NATIVE_PACKAGES_GPG_KEY"
|
|
14
|
+
PASSPHRASE = "NATIVE_PACKAGES_GPG_PASSPHRASE"
|
|
15
|
+
FINGERPRINT = /\A(?:\h{40}|\h{64})\z/
|
|
16
|
+
|
|
17
|
+
# The signing key and passphrase from the environment, removed from it
|
|
18
|
+
# so that no later subprocess inherits them.
|
|
19
|
+
def self.environment = { secret: ENV.delete(KEY), passphrase: ENV.delete(PASSPHRASE) }
|
|
20
|
+
|
|
21
|
+
def self.fingerprint(value, label)
|
|
22
|
+
fingerprint = value.is_a?(String) ? value.delete(" ").upcase : ""
|
|
23
|
+
raise Error, "#{label}: expected the signing key's full fingerprint (40 or 64 hexadecimal digits)" unless FINGERPRINT.match?(fingerprint)
|
|
24
|
+
fingerprint
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
attr_reader :fingerprint, :home
|
|
28
|
+
|
|
29
|
+
def initialize(fingerprint, secret: nil, passphrase: nil)
|
|
30
|
+
@fingerprint = fingerprint
|
|
31
|
+
@secret = secret
|
|
32
|
+
@passphrase = passphrase
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Yields self, ready to sign, then deletes any temporary keyring.
|
|
36
|
+
def session
|
|
37
|
+
raise Error, "install gpg (GnuPG) to sign repositories" unless available?("gpg")
|
|
38
|
+
Dir.mktmpdir("native-packages-gpg-") do |directory|
|
|
39
|
+
directory = Pathname.new(directory)
|
|
40
|
+
homes = []
|
|
41
|
+
begin
|
|
42
|
+
if @secret && !@secret.empty?
|
|
43
|
+
@home = keyring(directory / "signing", homes)
|
|
44
|
+
gpg("--import", input: @secret)
|
|
45
|
+
end
|
|
46
|
+
# gpg fails outright when it has no such key.
|
|
47
|
+
listed = begin
|
|
48
|
+
gpg("--list-secret-keys", "--with-colons", fingerprint).lines.grep(/\Afpr:/).map { |line| line.split(":")[9] }
|
|
49
|
+
rescue Error
|
|
50
|
+
[]
|
|
51
|
+
end
|
|
52
|
+
unless listed.include?(fingerprint)
|
|
53
|
+
raise Error, "GnuPG has no secret key #{fingerprint}; set #{KEY} to its armored secret key, or import it"
|
|
54
|
+
end
|
|
55
|
+
@public = gpg("--export", fingerprint)
|
|
56
|
+
@verifier = keyring(directory / "verifying", homes)
|
|
57
|
+
gpg("--import", input: @public, home: @verifier)
|
|
58
|
+
yield self
|
|
59
|
+
ensure
|
|
60
|
+
# Stop the agents the temporary keyrings started.
|
|
61
|
+
homes.each { |home| Open3.capture3("gpgconf", "--homedir", home.to_s, "--kill", "all") } if available?("gpgconf")
|
|
62
|
+
@home = nil
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The public key, binary or ASCII-armored.
|
|
68
|
+
def public_key(armor: false) = armor ? gpg("--armor", "--export", fingerprint, home: @verifier) : @public
|
|
69
|
+
|
|
70
|
+
# Options that make flatpak sign with this key.
|
|
71
|
+
def flatpak_options = ["--gpg-sign=#{fingerprint}", *(home ? ["--gpg-homedir=#{home}"] : [])]
|
|
72
|
+
|
|
73
|
+
def clearsign(input, output)
|
|
74
|
+
sign("--clearsign", input, output)
|
|
75
|
+
verify(output)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def detach_sign(input, output)
|
|
79
|
+
sign("--detach-sign", input, output, "--armor")
|
|
80
|
+
verify(output, input)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Fails unless `signature` (over `data`, when detached) was made by the
|
|
84
|
+
# configured key.
|
|
85
|
+
def verify(signature, data = nil)
|
|
86
|
+
status = gpg("--status-fd", "1", "--verify", signature.to_s, *(data ? [data.to_s] : []), home: @verifier)
|
|
87
|
+
signers = status.lines.grep(/\A\[GNUPG:\] VALIDSIG /).map { |line| line.split.last }
|
|
88
|
+
raise Error, "#{Pathname.new(signature).basename} is not signed by #{fingerprint}" unless signers == [fingerprint]
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
private
|
|
92
|
+
|
|
93
|
+
def keyring(path, homes)
|
|
94
|
+
path.mkpath
|
|
95
|
+
path.chmod(0o700)
|
|
96
|
+
homes << path
|
|
97
|
+
path
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def sign(mode, input, output, *options)
|
|
101
|
+
unlock = @passphrase ? ["--pinentry-mode", "loopback", "--passphrase-fd", "0"] : []
|
|
102
|
+
gpg(*unlock, "--local-user", fingerprint, "--digest-algo", "SHA512", *options, mode, "--output", output.to_s, input.to_s,
|
|
103
|
+
input: @passphrase && "#{@passphrase}\n")
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Runs gpg and returns its output bytes, unchanged.
|
|
107
|
+
def gpg(*arguments, input: nil, home: @home)
|
|
108
|
+
command = ["gpg", "--batch", "--yes", "--no-tty", "--quiet", *(home ? ["--homedir", home.to_s] : []), *arguments]
|
|
109
|
+
output, error, status = Open3.capture3(*command, stdin_data: input.to_s, binmode: true)
|
|
110
|
+
raise Error, "gpg failed: #{error.strip}" unless status.success?
|
|
111
|
+
output
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "staging"
|
|
4
|
+
|
|
5
|
+
module NativePackages
|
|
6
|
+
# Builds a .snap from the same contents nFPM packages: the files become the
|
|
7
|
+
# snap's root, beside a generated meta/snap.yaml, packed with mksquashfs
|
|
8
|
+
# as `snap pack` packs a prime directory. No snapcraft, LXD or network is
|
|
9
|
+
# involved, and no extension (such as gnome) is applied, so the snap uses
|
|
10
|
+
# its base snap's libraries and whatever the package installs.
|
|
11
|
+
class Snap
|
|
12
|
+
include Support
|
|
13
|
+
include Staging
|
|
14
|
+
# nFPM architecture => Snap architecture.
|
|
15
|
+
ARCHES = { "amd64" => "amd64", "arm64" => "arm64", "arm7" => "armhf", "ppc64le" => "ppc64el", "s390x" => "s390x",
|
|
16
|
+
"riscv64" => "riscv64" }.freeze
|
|
17
|
+
KEYS = %w[name summary description title license base confinement grade icon apps plugs slots layout environment store].freeze
|
|
18
|
+
APP_KEYS = %w[command desktop plugs slots environment daemon command-chain common-id restart-condition stop-command
|
|
19
|
+
install-mode autostart].freeze
|
|
20
|
+
NAME = /\A(?=.{2,40}\z)(?=[a-z0-9-]*[a-z])[a-z0-9]+(?:-[a-z0-9]+)*\z/
|
|
21
|
+
APP = /\A[a-zA-Z0-9](?:-?[a-zA-Z0-9])*\z/
|
|
22
|
+
CHANNEL = %r{\A(?:[a-z0-9][a-z0-9._-]*/)?(?:stable|candidate|beta|edge)(?:/[a-z0-9][a-z0-9._-]*)?\z}
|
|
23
|
+
CREDENTIALS = "SNAPCRAFT_STORE_CREDENTIALS"
|
|
24
|
+
attr_reader :root, :settings, :name
|
|
25
|
+
|
|
26
|
+
def initialize(configuration)
|
|
27
|
+
@root = configuration.root
|
|
28
|
+
@settings = configuration.data.fetch("snap", {})
|
|
29
|
+
@name = settings.fetch("name", configuration.name)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.architecture(target)
|
|
33
|
+
ARCHES.fetch(target.fetch("arch")) { raise Error, "snap does not support #{target.fetch('arch')}; use #{ARCHES.keys.join(', ')}" }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.validate(data)
|
|
37
|
+
building = data.fetch("targets").values.any? { |target| target.fetch("formats", []).include?("snap") }
|
|
38
|
+
settings = data["snap"]
|
|
39
|
+
raise Error, "snap targets need a top-level snap section" if building && settings.nil?
|
|
40
|
+
return if settings.nil?
|
|
41
|
+
raise Error, "snap: expected a mapping" unless settings.is_a?(Hash)
|
|
42
|
+
raise Error, "snap: no target builds the snap format" unless building
|
|
43
|
+
unknown = settings.keys - KEYS
|
|
44
|
+
raise Error, "snap: unknown fields #{unknown.join(', ')}" unless unknown.empty?
|
|
45
|
+
name = settings.fetch("name", data.fetch("nfpm").fetch("name"))
|
|
46
|
+
raise Error, "snap.name: use 2 to 40 lowercase letters, digits and single hyphens, as #{name.inspect} is not" unless name.is_a?(String) && NAME.match?(name)
|
|
47
|
+
raise Error, "snap.base: expected a base such as core24" unless settings["base"].is_a?(String) && /\A(?:core\d{2}|bare)\z/.match?(settings["base"])
|
|
48
|
+
raise Error, "snap.confinement: use strict, classic or devmode" unless %w[strict classic devmode].include?(settings.fetch("confinement", "strict"))
|
|
49
|
+
raise Error, "snap.grade: use stable or devel" unless %w[stable devel].include?(settings.fetch("grade", "stable"))
|
|
50
|
+
summary = settings.fetch("summary", data.fetch("nfpm")["description"].to_s.lines.first.to_s.strip)
|
|
51
|
+
raise Error, "snap.summary: write a summary of at most 78 characters" unless summary.is_a?(String) && !summary.empty? && summary.length <= 78
|
|
52
|
+
%w[description title license].each do |key|
|
|
53
|
+
raise Error, "snap.#{key}: expected a string" if settings.key?(key) && !settings[key].is_a?(String)
|
|
54
|
+
end
|
|
55
|
+
%w[plugs slots layout environment].each do |key|
|
|
56
|
+
raise Error, "snap.#{key}: expected a mapping" if settings.key?(key) && !settings[key].is_a?(Hash)
|
|
57
|
+
end
|
|
58
|
+
if settings.key?("icon") && !(settings["icon"].is_a?(String) && settings["icon"].match?(/\.(?:png|svg)\z/))
|
|
59
|
+
raise Error, "snap.icon: name a .png or .svg file the package installs, such as /usr/share/icons/hicolor/scalable/apps/app.svg"
|
|
60
|
+
end
|
|
61
|
+
apps = settings["apps"]
|
|
62
|
+
raise Error, "snap.apps: declare at least one app with a command" unless apps.is_a?(Hash) && !apps.empty?
|
|
63
|
+
apps.each do |app, definition|
|
|
64
|
+
raise Error, "snap.apps: invalid app name #{app.inspect}" unless app.is_a?(String) && APP.match?(app)
|
|
65
|
+
raise Error, "snap.apps.#{app}: expected a mapping" unless definition.is_a?(Hash)
|
|
66
|
+
unknown = definition.keys - APP_KEYS
|
|
67
|
+
raise Error, "snap.apps.#{app}: unknown fields #{unknown.join(', ')}" unless unknown.empty?
|
|
68
|
+
unless definition["command"].is_a?(String) && !definition["command"].strip.empty?
|
|
69
|
+
raise Error, "snap.apps.#{app}.command: name a program the package installs, such as usr/bin/#{name}"
|
|
70
|
+
end
|
|
71
|
+
%w[plugs slots].each do |key|
|
|
72
|
+
next unless definition.key?(key)
|
|
73
|
+
raise Error, "snap.apps.#{app}.#{key}: expected a list of names" unless definition[key].is_a?(Array) && definition[key].all?(String)
|
|
74
|
+
end
|
|
75
|
+
raise Error, "snap.apps.#{app}.desktop: name a .desktop file the package installs" if definition.key?("desktop") && !definition["desktop"].to_s.end_with?(".desktop")
|
|
76
|
+
end
|
|
77
|
+
store = settings["store"]
|
|
78
|
+
return if store.nil?
|
|
79
|
+
raise Error, "snap.store: expected a mapping with channels" unless store.is_a?(Hash) && store.keys == ["channels"]
|
|
80
|
+
channels = store["channels"]
|
|
81
|
+
unless channels.is_a?(Array) && !channels.empty? && channels.all? { |channel| channel.is_a?(String) && CHANNEL.match?(channel) }
|
|
82
|
+
raise Error, "snap.store.channels: list channels such as stable or latest/edge"
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def filename(version, target) = "#{name}_#{version}_#{self.class.architecture(target)}.snap"
|
|
87
|
+
|
|
88
|
+
def doctor(target)
|
|
89
|
+
self.class.architecture(target)
|
|
90
|
+
raise Error, "install mksquashfs (squashfs-tools) to build snaps" unless available?("mksquashfs")
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Returns the snap's path and what went into it.
|
|
94
|
+
def build(package, target, destination, version:, epoch:)
|
|
95
|
+
doctor(target)
|
|
96
|
+
raise Error, "snap versions are at most 32 characters" if version.length > 32
|
|
97
|
+
output = destination / filename(version, target)
|
|
98
|
+
Dir.mktmpdir("native-packages-snap-") do |directory|
|
|
99
|
+
prime = Pathname.new(directory) / "prime"
|
|
100
|
+
prime.mkpath
|
|
101
|
+
stage(package, prime, "snap")
|
|
102
|
+
raise Error, "snap: the package must not install /meta or /snap, which the snap's own metadata uses" if (prime / "meta").exist? || (prime / "snap").exist?
|
|
103
|
+
apps = settings.fetch("apps").to_h { |app, definition| [app, integrate(prime, app, definition)] }
|
|
104
|
+
if settings["icon"]
|
|
105
|
+
icon = inside(prime, settings["icon"], "snap")
|
|
106
|
+
raise Error, "snap.icon: the package installs no #{settings['icon']}" unless icon.file?
|
|
107
|
+
write(prime / "meta/gui/icon#{icon.extname}", icon.binread)
|
|
108
|
+
apps.each_key do |app|
|
|
109
|
+
desktop = prime / "meta/gui/#{app}.desktop"
|
|
110
|
+
write(desktop, desktop.read.gsub(/^Icon=.*$/, "Icon=${SNAP}/meta/gui/icon#{icon.extname}")) if desktop.file?
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
write(prime / "meta/snap.yaml", YAML.dump(metadata(package, version, target, apps)).delete_prefix("---\n"))
|
|
114
|
+
normalize(prime, epoch)
|
|
115
|
+
run "mksquashfs", prime, output, "-noappend", "-comp", "xz", "-no-fragments", "-no-progress", "-quiet", "-all-root", "-no-xattrs",
|
|
116
|
+
env: { "SOURCE_DATE_EPOCH" => epoch.to_s }
|
|
117
|
+
check(output)
|
|
118
|
+
[output, { "name" => name, "base" => settings.fetch("base"), "confinement" => settings.fetch("confinement", "strict"),
|
|
119
|
+
"apps" => apps.keys, "libraries" => "base" }]
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# meta/snap.yaml. The summary, description and license default to the
|
|
124
|
+
# package's.
|
|
125
|
+
def metadata(package, version, target, apps)
|
|
126
|
+
result = { "name" => name, "version" => version, "summary" => settings.fetch("summary", package.fetch("description").lines.first.strip),
|
|
127
|
+
"description" => settings.fetch("description", package.fetch("description")) }
|
|
128
|
+
result["title"] = settings["title"] if settings["title"]
|
|
129
|
+
license = settings.fetch("license", package["license"])
|
|
130
|
+
result["license"] = license if license
|
|
131
|
+
result.merge!("base" => settings.fetch("base"), "grade" => settings.fetch("grade", "stable"),
|
|
132
|
+
"confinement" => settings.fetch("confinement", "strict"), "architectures" => [self.class.architecture(target)], "apps" => apps)
|
|
133
|
+
%w[plugs slots layout environment].each { |key| result[key] = settings[key] if settings[key] }
|
|
134
|
+
result
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Checks the app's command and puts its desktop entry in meta/gui, where
|
|
138
|
+
# snapd looks for it, launching the snap's app.
|
|
139
|
+
def integrate(prime, app, definition)
|
|
140
|
+
command = Shellwords.split(definition.fetch("command")).first.delete_prefix("$SNAP/")
|
|
141
|
+
executable = prime / relative_path(command)
|
|
142
|
+
raise Error, "snap.apps.#{app}.command: the package installs no executable #{command}" unless executable.file? && executable.executable?
|
|
143
|
+
if (desktop = definition["desktop"])
|
|
144
|
+
source = inside(prime, desktop.start_with?("/") ? desktop : "/#{desktop}", "snap")
|
|
145
|
+
raise Error, "snap.apps.#{app}.desktop: the package installs no #{desktop}" unless source.file?
|
|
146
|
+
launcher = app == name ? name : "#{name}.#{app}"
|
|
147
|
+
entry = source.read.gsub(/^Exec=(\S+)/) { "Exec=#{launcher}" }
|
|
148
|
+
write(prime / "meta/gui/#{app}.desktop", entry)
|
|
149
|
+
end
|
|
150
|
+
definition.except("desktop")
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Fails unless `path` is a squashfs image, the format of every snap.
|
|
154
|
+
def check(path)
|
|
155
|
+
raise Error, "not a snap (squashfs) file: #{path}" unless path.binread(4) == "hsqs".b
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def publish(paths)
|
|
159
|
+
channels = settings.dig("store", "channels") or raise Error, "configure snap.store.channels to publish --to snapcraft"
|
|
160
|
+
raise Error, "set #{CREDENTIALS} to a Snap Store login from snapcraft export-login" if ENV.fetch(CREDENTIALS, "").empty?
|
|
161
|
+
raise Error, "install snapcraft to publish to the Snap Store" unless available?("snapcraft")
|
|
162
|
+
raise Error, "the build contains no snaps" if paths.empty?
|
|
163
|
+
paths.each { |path| run "snapcraft", "upload", "--release=#{channels.join(',')}", path }
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module NativePackages
|
|
4
|
+
# Lays out a package's nFPM contents as a directory tree, for the formats
|
|
5
|
+
# nFPM does not write (AppImage, Flatpak and Snap), and fixes its modes and
|
|
6
|
+
# times so the same inputs give the same bytes.
|
|
7
|
+
module Staging
|
|
8
|
+
include Support
|
|
9
|
+
|
|
10
|
+
# nFPM overrides replace the contents list when one is supplied.
|
|
11
|
+
def contents(package, packager)
|
|
12
|
+
package.fetch("overrides", {}).fetch(packager, {}).fetch("contents", package.fetch("contents"))
|
|
13
|
+
.reject { |item| item["packager"] && item["packager"] != packager }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def inside(tree, destination, packager)
|
|
17
|
+
raise Error, "#{packager}: content destinations must be absolute: #{destination}" unless destination.to_s.start_with?("/")
|
|
18
|
+
tree / relative_path(destination.to_s.delete_prefix("/"))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def stage(package, tree, packager)
|
|
22
|
+
contents(package, packager).each do |item|
|
|
23
|
+
type = item.fetch("type", "file")
|
|
24
|
+
next if type == "ghost"
|
|
25
|
+
target = inside(tree, item.fetch("dst"), packager)
|
|
26
|
+
mode = item.dig("file_info", "mode")
|
|
27
|
+
case type
|
|
28
|
+
when "dir"
|
|
29
|
+
target.mkpath
|
|
30
|
+
when "symlink"
|
|
31
|
+
target.dirname.mkpath
|
|
32
|
+
File.symlink(item.fetch("src"), target)
|
|
33
|
+
when "file", "tree", "config", "config|noreplace", "config|missingok"
|
|
34
|
+
source = File.expand_path(item.fetch("src"), root)
|
|
35
|
+
paths = package["disable_globbing"] ? [source].select { |path| File.exist?(path) } : Dir.glob(source).sort
|
|
36
|
+
raise Error, "missing package content: #{source}" if paths.empty?
|
|
37
|
+
# As nFPM does: a glob source, or a destination ending in a slash,
|
|
38
|
+
# makes the destination a directory that takes each match's name.
|
|
39
|
+
pattern = !package["disable_globbing"] && item.fetch("src").match?(/[*?\[{]/)
|
|
40
|
+
single = paths.length == 1 && File.file?(paths.first) && type != "tree" && !pattern && !item.fetch("dst").end_with?("/")
|
|
41
|
+
paths.each do |path|
|
|
42
|
+
if File.directory?(path)
|
|
43
|
+
target.mkpath
|
|
44
|
+
FileUtils.cp_r(Dir.children(path).map { |child| File.join(path, child) }, target)
|
|
45
|
+
else
|
|
46
|
+
file = single ? target : target / File.basename(path)
|
|
47
|
+
file.dirname.mkpath
|
|
48
|
+
FileUtils.cp(path, file)
|
|
49
|
+
file.chmod(mode) if mode
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
else
|
|
53
|
+
raise Error, "#{packager}: unsupported content type #{type}"
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# The same bytes for the same inputs: fixed modes and times.
|
|
59
|
+
def normalize(tree, epoch)
|
|
60
|
+
time = Time.at(epoch)
|
|
61
|
+
Pathname.glob(tree / "**/*", File::FNM_DOTMATCH).each do |path|
|
|
62
|
+
next if path.basename.to_s == "."
|
|
63
|
+
if path.symlink?
|
|
64
|
+
File.lutime(time, time, path)
|
|
65
|
+
else
|
|
66
|
+
path.chmod(path.directory? || path.executable? ? 0o755 : 0o644)
|
|
67
|
+
File.utime(time, time, path)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
tree.chmod(0o755)
|
|
71
|
+
File.utime(time, time, tree)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module NativePackages
|
|
4
|
+
# Authenticode signing through Azure Artifact Signing (formerly Trusted
|
|
5
|
+
# Signing). The configuration opts in; the account coordinates come from the
|
|
6
|
+
# environment, and Azure authentication from an existing Azure CLI sign-in
|
|
7
|
+
# (`az login`, or azure/login with OIDC in CI). No secret is read or stored.
|
|
8
|
+
class WindowsSigning
|
|
9
|
+
include Support
|
|
10
|
+
PROVIDER = "azure-artifact-signing"
|
|
11
|
+
KEYS = %w[provider description description_url publisher].freeze
|
|
12
|
+
VARIABLES = %w[AZURE_SIGNING_ENDPOINT AZURE_SIGNING_ACCOUNT AZURE_SIGNING_PROFILE].freeze
|
|
13
|
+
TIMESTAMP = "http://timestamp.acs.microsoft.com"
|
|
14
|
+
# Every DefaultAzureCredential source except the Azure CLI's, so a stray
|
|
15
|
+
# client secret or cached account can never sign in its place.
|
|
16
|
+
EXCLUDED_CREDENTIALS = %w[EnvironmentCredential WorkloadIdentityCredential ManagedIdentityCredential
|
|
17
|
+
SharedTokenCacheCredential VisualStudioCredential VisualStudioCodeCredential AzurePowerShellCredential
|
|
18
|
+
AzureDeveloperCliCredential InteractiveBrowserCredential].freeze
|
|
19
|
+
# The versions azure/artifact-signing-action 2.0.0 installs, pinned by hash.
|
|
20
|
+
TOOLS = {
|
|
21
|
+
"signtool" => { "package" => "microsoft.windows.sdk.buildtools", "version" => "10.0.26100.4188",
|
|
22
|
+
"sha256" => "180deb372659029864c10a0c04787833234d64aacd1d2c0661d2c00295d8e022",
|
|
23
|
+
"directory" => "bin/10.0.26100.0/x64", "file" => "signtool.exe", "override" => "NATIVE_PACKAGES_SIGNTOOL" },
|
|
24
|
+
"dlib" => { "package" => "microsoft.artifactsigning.client", "version" => "1.0.128",
|
|
25
|
+
"sha256" => "74bd7d27e6ce1051409c38d9b46bc8df0400ecd643d51ffbf2ac00869061e40b",
|
|
26
|
+
"directory" => "bin/x64", "file" => "Azure.CodeSigning.Dlib.dll", "override" => "NATIVE_PACKAGES_ARTIFACT_SIGNING_DLIB" }
|
|
27
|
+
}.freeze
|
|
28
|
+
# Command lines on Windows hold 32,767 characters; leave room for options.
|
|
29
|
+
BATCH_CHARACTERS = 16_000
|
|
30
|
+
SIGNATURES = <<~'POWERSHELL'
|
|
31
|
+
$ErrorActionPreference = 'Stop'
|
|
32
|
+
$simple = [System.Security.Cryptography.X509Certificates.X509NameType]::SimpleName
|
|
33
|
+
$paths = @(Get-Content -LiteralPath $env:NATIVE_PACKAGES_SIGNATURE_LIST -Encoding UTF8)
|
|
34
|
+
$result = @(foreach ($line in $paths) {
|
|
35
|
+
# Plain strings: lines from Get-Content carry note properties that
|
|
36
|
+
# ConvertTo-Json would otherwise write out as an object.
|
|
37
|
+
$path = [string]$line
|
|
38
|
+
$signature = Get-AuthenticodeSignature -LiteralPath $path
|
|
39
|
+
$certificate = $signature.SignerCertificate
|
|
40
|
+
$publisher = $null
|
|
41
|
+
$subject = $null
|
|
42
|
+
if ($certificate) {
|
|
43
|
+
$publisher = $certificate.GetNameInfo($simple, $false)
|
|
44
|
+
$subject = $certificate.Subject
|
|
45
|
+
}
|
|
46
|
+
[pscustomobject]@{ path = $path; status = [string]$signature.Status; message = [string]$signature.StatusMessage;
|
|
47
|
+
publisher = $publisher; subject = $subject; timestamped = [bool]$signature.TimeStamperCertificate }
|
|
48
|
+
})
|
|
49
|
+
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -InputObject $result -Compress -Depth 3)))
|
|
50
|
+
POWERSHELL
|
|
51
|
+
|
|
52
|
+
# Only the tool's own directory leaves the NuGet package (a ZIP file).
|
|
53
|
+
EXTRACT = <<~'POWERSHELL'
|
|
54
|
+
$ErrorActionPreference = 'Stop'
|
|
55
|
+
Add-Type -AssemblyName System.IO.Compression.FileSystem
|
|
56
|
+
$prefix = $env:NATIVE_PACKAGES_PREFIX + '/'
|
|
57
|
+
$zip = [System.IO.Compression.ZipFile]::OpenRead($env:NATIVE_PACKAGES_ARCHIVE)
|
|
58
|
+
try {
|
|
59
|
+
foreach ($entry in $zip.Entries) {
|
|
60
|
+
if (-not $entry.FullName.StartsWith($prefix) -or -not $entry.Name -or $entry.FullName.Contains('..')) { continue }
|
|
61
|
+
$target = Join-Path $env:NATIVE_PACKAGES_DESTINATION $entry.FullName
|
|
62
|
+
New-Item -ItemType Directory -Force -Path (Split-Path -Parent $target) | Out-Null
|
|
63
|
+
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $target, $true)
|
|
64
|
+
}
|
|
65
|
+
} finally {
|
|
66
|
+
$zip.Dispose()
|
|
67
|
+
}
|
|
68
|
+
POWERSHELL
|
|
69
|
+
|
|
70
|
+
attr_reader :root, :settings
|
|
71
|
+
|
|
72
|
+
def self.validate(data)
|
|
73
|
+
windows = data.fetch("windows", {})
|
|
74
|
+
raise Error, "windows: expected a mapping" unless windows.is_a?(Hash)
|
|
75
|
+
unknown = windows.keys - %w[signing]
|
|
76
|
+
raise Error, "windows: unknown fields #{unknown.join(', ')}" unless unknown.empty?
|
|
77
|
+
return unless windows.key?("signing")
|
|
78
|
+
signing = windows["signing"]
|
|
79
|
+
raise Error, "windows.signing: expected a mapping" unless signing.is_a?(Hash)
|
|
80
|
+
unknown = signing.keys - KEYS
|
|
81
|
+
raise Error, "windows.signing: unknown fields #{unknown.join(', ')}" unless unknown.empty?
|
|
82
|
+
raise Error, "windows.signing.provider: use #{PROVIDER}" unless signing["provider"] == PROVIDER
|
|
83
|
+
%w[description publisher].each do |key|
|
|
84
|
+
next unless signing.key?(key)
|
|
85
|
+
raise Error, "windows.signing.#{key}: expected a nonempty string" unless signing[key].is_a?(String) && !signing[key].strip.empty?
|
|
86
|
+
end
|
|
87
|
+
if signing.key?("description_url") && !(signing["description_url"].is_a?(String) && %r{\Ahttps://\S+\z}.match?(signing["description_url"]))
|
|
88
|
+
raise Error, "windows.signing.description_url: expected an https:// URL"
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def initialize(configuration, environment = ENV)
|
|
93
|
+
@root = configuration.root
|
|
94
|
+
self.class.validate(configuration.data)
|
|
95
|
+
@settings = configuration.data.fetch("windows", {}).fetch("signing", nil)
|
|
96
|
+
@environment = environment
|
|
97
|
+
@values = VARIABLES.to_h { |name| [name, environment[name].to_s.strip] }
|
|
98
|
+
@description = settings && settings.fetch("description", configuration.name)
|
|
99
|
+
@description_url = settings && (settings["description_url"] || configuration.data.fetch("nfpm")["homepage"])
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def configured? = !settings.nil?
|
|
103
|
+
def enabled? = configured? && @values.values.any? { |value| !value.empty? }
|
|
104
|
+
def state = !configured? ? "off" : enabled? ? "enabled" : "off: #{VARIABLES.join(', ')} not set"
|
|
105
|
+
|
|
106
|
+
def doctor
|
|
107
|
+
return unless enabled?
|
|
108
|
+
missing = @values.select { |_, value| value.empty? }.keys
|
|
109
|
+
raise Error, "incomplete Azure Artifact Signing settings: set #{missing.join(', ')}" unless missing.empty?
|
|
110
|
+
unless %r{\Ahttps://[a-z0-9-]+\.codesigning\.azure\.net/?\z}.match?(@values.fetch("AZURE_SIGNING_ENDPOINT"))
|
|
111
|
+
raise Error, "AZURE_SIGNING_ENDPOINT must be an Artifact Signing endpoint such as https://weu.codesigning.azure.net/"
|
|
112
|
+
end
|
|
113
|
+
%w[AZURE_SIGNING_ACCOUNT AZURE_SIGNING_PROFILE].each do |name|
|
|
114
|
+
raise Error, "#{name} must be a name, not a path or URL" unless /\A[A-Za-z0-9][A-Za-z0-9-]*\z/.match?(@values.fetch(name))
|
|
115
|
+
end
|
|
116
|
+
raise Error, "Windows signing must run on Windows" unless NativeRecipe.host == "windows"
|
|
117
|
+
tools = %w[powershell az]
|
|
118
|
+
tools << "curl" if TOOLS.values.any? { |tool| @environment[tool.fetch("override")].to_s.empty? }
|
|
119
|
+
missing_tools = tools.reject { |tool| available?(tool) }
|
|
120
|
+
raise Error, "install required Windows signing tools: #{missing_tools.join(', ')}" unless missing_tools.empty?
|
|
121
|
+
TOOLS.each_value do |tool|
|
|
122
|
+
path = @environment[tool.fetch("override")].to_s
|
|
123
|
+
raise Error, "#{tool.fetch('override')} does not name a file: #{path}" unless path.empty? || File.file?(path)
|
|
124
|
+
end
|
|
125
|
+
begin
|
|
126
|
+
execute "az", "account", "show", "--output", "none"
|
|
127
|
+
rescue Error
|
|
128
|
+
raise Error, "sign in to Azure before signing: az login, or azure/login with OIDC in GitHub Actions"
|
|
129
|
+
end
|
|
130
|
+
nil
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def with_identity
|
|
134
|
+
doctor
|
|
135
|
+
return yield(nil) unless enabled?
|
|
136
|
+
Dir.mktmpdir("native-packages-windows-signing-") do |directory|
|
|
137
|
+
directory = Pathname.new(directory)
|
|
138
|
+
@signtool = tool("signtool", directory)
|
|
139
|
+
@dlib = tool("dlib", directory)
|
|
140
|
+
@metadata = directory / "metadata.json"
|
|
141
|
+
write(@metadata, JSON.pretty_generate("Endpoint" => @values.fetch("AZURE_SIGNING_ENDPOINT"),
|
|
142
|
+
"CodeSigningAccountName" => @values.fetch("AZURE_SIGNING_ACCOUNT"),
|
|
143
|
+
"CertificateProfileName" => @values.fetch("AZURE_SIGNING_PROFILE"),
|
|
144
|
+
"ExcludeCredentials" => EXCLUDED_CREDENTIALS) + "\n")
|
|
145
|
+
yield self
|
|
146
|
+
ensure
|
|
147
|
+
@signtool = @dlib = @metadata = nil
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# A package recipe gets a signed copy of its input. Code that already
|
|
152
|
+
# carries a valid signature, ours from sign-windows or a vendor's, stays.
|
|
153
|
+
def sign_payload(payload)
|
|
154
|
+
binaries = NativeRecipe.new(root).tree(payload).filter_map do |relative, kind, *|
|
|
155
|
+
path = payload / relative
|
|
156
|
+
path if kind == "file" && portable_executable?(path)
|
|
157
|
+
end
|
|
158
|
+
unsigned = signatures(binaries).reject { |record| record.fetch("status") == "Valid" }.map { |record| Pathname.new(record.fetch("path")) }
|
|
159
|
+
sign(unsigned) unless unsigned.empty?
|
|
160
|
+
puts "Signed #{unsigned.length} of #{binaries.length} Windows binaries in the package input."
|
|
161
|
+
unsigned.length
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def sign_package(package)
|
|
165
|
+
record = sign([package]).first
|
|
166
|
+
{ "signature" => "authenticode", "provider" => PROVIDER, "publisher" => record.fetch("publisher"),
|
|
167
|
+
"timestamped" => true }
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Signs files in place, in as few SignTool calls as fit a command line,
|
|
171
|
+
# then checks every signature.
|
|
172
|
+
def sign(paths)
|
|
173
|
+
paths = paths.map { |path| Pathname.new(path).expand_path(root) }
|
|
174
|
+
paths.each { |path| raise Error, "not a regular file: #{path}" unless path.file? && !path.symlink? }
|
|
175
|
+
batches = paths.each_with_object([[]]) do |path, groups|
|
|
176
|
+
length = groups.last.sum { |item| item.to_s.length + 3 } + path.to_s.length + 3
|
|
177
|
+
groups << [] if !groups.last.empty? && length > BATCH_CHARACTERS
|
|
178
|
+
groups.last << path
|
|
179
|
+
end
|
|
180
|
+
batches.each do |batch|
|
|
181
|
+
arguments = [@signtool, "sign", "/v", "/fd", "SHA256", "/tr", TIMESTAMP, "/td", "SHA256",
|
|
182
|
+
"/dlib", @dlib, "/dmdf", @metadata, "/d", @description]
|
|
183
|
+
arguments += ["/du", @description_url] if @description_url
|
|
184
|
+
output = signtool(*arguments, *batch)
|
|
185
|
+
puts output unless output.empty?
|
|
186
|
+
end
|
|
187
|
+
verify(paths)
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def verify(paths)
|
|
191
|
+
signatures(paths).map do |record|
|
|
192
|
+
path = record.fetch("path")
|
|
193
|
+
unless record.fetch("status") == "Valid" && record.fetch("timestamped")
|
|
194
|
+
raise Error, "#{path} is not validly signed and timestamped: #{record.fetch('status')} #{record.fetch('message')}".strip
|
|
195
|
+
end
|
|
196
|
+
if settings["publisher"] && record.fetch("publisher") != settings["publisher"]
|
|
197
|
+
raise Error, "#{path} is signed by #{record.fetch('publisher').inspect}, not windows.signing.publisher #{settings['publisher'].inspect}"
|
|
198
|
+
end
|
|
199
|
+
puts "#{File.basename(path)}: signed by #{record.fetch('subject')}, timestamped"
|
|
200
|
+
record
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def signatures(paths)
|
|
205
|
+
return [] if paths.empty?
|
|
206
|
+
records = Dir.mktmpdir("native-packages-signatures-") do |directory|
|
|
207
|
+
list = Pathname.new(directory) / "files.txt"
|
|
208
|
+
write(list, paths.map { |path| Pathname.new(path).expand_path(root).to_s }.join("\n") + "\n")
|
|
209
|
+
JSON.parse(powershell(SIGNATURES, "NATIVE_PACKAGES_SIGNATURE_LIST" => list.to_s).strip.unpack1("m0").force_encoding(Encoding::UTF_8))
|
|
210
|
+
end
|
|
211
|
+
raise Error, "unexpected signature report" unless records.is_a?(Array) && records.length == paths.length
|
|
212
|
+
records
|
|
213
|
+
rescue ArgumentError, JSON::ParserError
|
|
214
|
+
raise Error, "could not read Authenticode signatures"
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# `native-packages sign-windows FILE...`: explicit, so it never skips.
|
|
218
|
+
def sign_files(paths)
|
|
219
|
+
raise Error, "add windows.signing to the configuration to sign Windows files" unless configured?
|
|
220
|
+
raise Error, "set #{VARIABLES.join(', ')} to sign Windows files" unless enabled?
|
|
221
|
+
raise Error, "sign-windows needs at least one file" if paths.empty?
|
|
222
|
+
with_identity { |signing| signing.sign(paths) }
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def portable_executable?(path)
|
|
226
|
+
File.open(path, "rb") do |file|
|
|
227
|
+
next false unless file.size >= 64 && file.read(2) == "MZ"
|
|
228
|
+
file.seek(0x3c)
|
|
229
|
+
offset = file.read(4).unpack1("L<")
|
|
230
|
+
next false unless offset >= 64 && offset + 4 <= file.size
|
|
231
|
+
file.seek(offset)
|
|
232
|
+
file.read(4) == "PE\0\0"
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# SignTool and the Artifact Signing client from nuget.org, hash-checked on
|
|
237
|
+
# every use, or local copies named by the override variables.
|
|
238
|
+
def tool(name, directory)
|
|
239
|
+
tool = TOOLS.fetch(name)
|
|
240
|
+
override = @environment[tool.fetch("override")].to_s
|
|
241
|
+
return Pathname.new(override).expand_path unless override.empty?
|
|
242
|
+
package, version = tool.values_at("package", "version")
|
|
243
|
+
archive = cache_directory / "#{package}.#{version}.nupkg"
|
|
244
|
+
download("https://api.nuget.org/v3-flatcontainer/#{package}/#{version}/#{package}.#{version}.nupkg", archive)
|
|
245
|
+
unless sha256(archive) == tool.fetch("sha256")
|
|
246
|
+
archive.delete
|
|
247
|
+
raise Error, "#{package} #{version} from nuget.org does not match its pinned SHA-256"
|
|
248
|
+
end
|
|
249
|
+
destination = directory / name
|
|
250
|
+
destination.mkpath
|
|
251
|
+
powershell(EXTRACT, "NATIVE_PACKAGES_ARCHIVE" => archive.to_s, "NATIVE_PACKAGES_PREFIX" => tool.fetch("directory"),
|
|
252
|
+
"NATIVE_PACKAGES_DESTINATION" => destination.to_s)
|
|
253
|
+
path = destination / tool.fetch("directory") / tool.fetch("file")
|
|
254
|
+
raise Error, "#{package} #{version} has no #{tool.fetch('file')}" unless path.file?
|
|
255
|
+
path
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def cache_directory
|
|
259
|
+
base = @environment["LOCALAPPDATA"].to_s.empty? ? File.join(Dir.home, ".cache") : @environment["LOCALAPPDATA"]
|
|
260
|
+
Pathname.new(base) / "native-packages/windows-signing"
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
# SignTool and the Artifact Signing client report failures on standard
|
|
264
|
+
# output, so a failure shows both streams.
|
|
265
|
+
def signtool(*arguments)
|
|
266
|
+
output, status = Open3.capture2e(*arguments.map(&:to_s), chdir: root.to_s, binmode: true)
|
|
267
|
+
raise Error, "SignTool failed:\n#{output.strip}" unless status.success?
|
|
268
|
+
output.strip
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
# Windows PowerShell started from PowerShell 7 inherits a module path
|
|
272
|
+
# where its own Microsoft.PowerShell.Security cannot load; without one, it
|
|
273
|
+
# uses its default.
|
|
274
|
+
def powershell(script, env)
|
|
275
|
+
encoded = [script.encode(Encoding::UTF_16LE)].pack("m0")
|
|
276
|
+
execute("powershell", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-EncodedCommand", encoded,
|
|
277
|
+
env: env.merge("PSModulePath" => nil))
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
def execute(*arguments, env: {})
|
|
281
|
+
capture(*arguments, env: env)
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
end
|