|
| 1 | +package buildpkg |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/bep/macosnotarylib" |
| 10 | + "github.com/golang-jwt/jwt/v4" |
| 11 | +) |
| 12 | + |
| 13 | +// New creates a new Builder. |
| 14 | +func New(opts Options) (*Builder, error) { |
| 15 | + if err := opts.init(); err != nil { |
| 16 | + return nil, err |
| 17 | + } |
| 18 | + return &Builder{Options: opts}, nil |
| 19 | +} |
| 20 | + |
| 21 | +type Builder struct { |
| 22 | + Options |
| 23 | +} |
| 24 | + |
| 25 | +// Build signs the binary, builds the package and signs and notarizes and staples it. |
| 26 | +// It' currently limited to 1 file only. |
| 27 | +// The notarization part requires the following environment variables to be set: |
| 28 | +// - MACOSNOTARYLIB_ISSUER_ID |
| 29 | +// - MACOSNOTARYLIB_KID |
| 30 | +// - MACOSNOTARYLIB_PRIVATE_KEY (in base64 format). |
| 31 | +func (b *Builder) Build() error { |
| 32 | + files, err := os.ReadDir(b.StagingDirectory) |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + if len(files) != 1 || files[0].IsDir() { |
| 37 | + return fmt.Errorf("opts: StagingDirectory must contain exactly one file") |
| 38 | + } |
| 39 | + |
| 40 | + if !b.SkipCodeSigning { |
| 41 | + for _, fi := range files { |
| 42 | + if err := b.runCommand("codesign", "-s", b.SigningIdentity, "--options=runtime", filepath.Join(b.StagingDirectory, fi.Name())); err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + tempPackageOutputPath := b.PackageOutputPath + ".tmp" |
| 49 | + |
| 50 | + args := []string{ |
| 51 | + "--root", b.StagingDirectory, |
| 52 | + "--identifier", b.Identifier, |
| 53 | + "--version", b.Version, |
| 54 | + "--install-location", b.InstallLocation, |
| 55 | + } |
| 56 | + |
| 57 | + if b.ScriptsDirectory != "" { |
| 58 | + args = append(args, "--scripts", b.ScriptsDirectory) |
| 59 | + } |
| 60 | + |
| 61 | + if b.SkipInstallerSigning { |
| 62 | + tempPackageOutputPath = b.PackageOutputPath |
| 63 | + } |
| 64 | + |
| 65 | + args = append(args, tempPackageOutputPath) |
| 66 | + |
| 67 | + if err := b.runCommand("pkgbuild", args...); err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + if !b.SkipInstallerSigning { |
| 72 | + // Sign the package |
| 73 | + if err := b.runCommand("productsign", "--sign", b.SigningIdentity, tempPackageOutputPath, b.PackageOutputPath); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + if err := os.Remove(filepath.Join(b.Dir, tempPackageOutputPath)); err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + // Check the package signature. |
| 82 | + if err := b.runCommand("pkgutil", "--check-signature", b.PackageOutputPath); err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if !b.SkipNotarization { |
| 88 | + // Notarize the package. |
| 89 | + if err := b.notarizePackage(filepath.Join("testdata", b.PackageOutputPath)); err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + // Staple the package. |
| 94 | + if err := b.runCommand("stapler", "staple", b.PackageOutputPath); err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +func (b *Builder) notarizePackage(filename string) error { |
| 103 | + issuerID := os.Getenv("MACOSNOTARYLIB_ISSUER_ID") |
| 104 | + |
| 105 | + kid := os.Getenv("MACOSNOTARYLIB_KID") |
| 106 | + if kid == "" || issuerID == "" { |
| 107 | + return fmt.Errorf("env: MACOSNOTARYLIB_ISSUER_ID and MACOSNOTARYLIB_KID must be set") |
| 108 | + } |
| 109 | + |
| 110 | + // This test also depends on the private key from env MACOSNOTARYLIB_PRIVATE_KEY in base64 format. See below. |
| 111 | + |
| 112 | + n, err := macosnotarylib.New( |
| 113 | + macosnotarylib.Options{ |
| 114 | + InfoLoggerf: b.Infof, |
| 115 | + IssuerID: issuerID, |
| 116 | + Kid: kid, |
| 117 | + SignFunc: func(token *jwt.Token) (string, error) { |
| 118 | + key, err := macosnotarylib.LoadPrivateKeyFromEnvBase64("MACOSNOTARYLIB_PRIVATE_KEY") |
| 119 | + if err != nil { |
| 120 | + return "", err |
| 121 | + } |
| 122 | + return token.SignedString(key) |
| 123 | + }, |
| 124 | + }, |
| 125 | + ) |
| 126 | + |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + return n.Submit(filename) |
| 132 | + |
| 133 | +} |
| 134 | + |
| 135 | +func (b *Builder) runCommand(name string, args ...string) error { |
| 136 | + fmt.Println(name, args) |
| 137 | + cmd := exec.Command(name, args...) |
| 138 | + cmd.Dir = b.Dir |
| 139 | + cmd.Stdout = os.Stdout |
| 140 | + cmd.Stderr = os.Stderr |
| 141 | + return cmd.Run() |
| 142 | +} |
| 143 | + |
| 144 | +type Options struct { |
| 145 | + // The Info logger. |
| 146 | + // If nil, no Info logging will be done. |
| 147 | + Infof func(format string, a ...interface{}) |
| 148 | + |
| 149 | + // The Dir to build from. |
| 150 | + Dir string |
| 151 | + |
| 152 | + // Developer ID Application + Developer ID Installer |
| 153 | + // https://developer.apple.com/account/resources/certificates/list |
| 154 | + SigningIdentity string |
| 155 | + |
| 156 | + // The result |
| 157 | + PackageOutputPath string |
| 158 | + |
| 159 | + // The staging directory where all your build artifacts are located. |
| 160 | + StagingDirectory string |
| 161 | + |
| 162 | + // E.g. io.gohugo.hugo |
| 163 | + Identifier string |
| 164 | + |
| 165 | + // E.g. 234 |
| 166 | + Version string |
| 167 | + |
| 168 | + // E.g. /Applications |
| 169 | + InstallLocation string |
| 170 | + |
| 171 | + // Scripts passed on the command line --scripts flag. |
| 172 | + // E.g. /mypkgscripts |
| 173 | + ScriptsDirectory string |
| 174 | + |
| 175 | + // Flags to enable skipping of build steps. |
| 176 | + SkipCodeSigning bool |
| 177 | + SkipInstallerSigning bool |
| 178 | + SkipNotarization bool |
| 179 | +} |
| 180 | + |
| 181 | +func (o *Options) init() error { |
| 182 | + if o.Infof == nil { |
| 183 | + o.Infof = func(format string, a ...interface{}) { |
| 184 | + } |
| 185 | + } |
| 186 | + if o.SigningIdentity == "" { |
| 187 | + return fmt.Errorf("opts: SigningIdentity is required") |
| 188 | + } |
| 189 | + |
| 190 | + if o.StagingDirectory == "" { |
| 191 | + return fmt.Errorf("opts: StagingDirectory not set") |
| 192 | + } |
| 193 | + |
| 194 | + if o.Identifier == "" { |
| 195 | + return fmt.Errorf("opts: Identifier not set") |
| 196 | + } |
| 197 | + |
| 198 | + if o.Version == "" { |
| 199 | + return fmt.Errorf("opts: Version not set") |
| 200 | + } |
| 201 | + |
| 202 | + if o.InstallLocation == "" { |
| 203 | + return fmt.Errorf("opts: InstallLocation not set") |
| 204 | + } |
| 205 | + |
| 206 | + if o.PackageOutputPath == "" { |
| 207 | + return fmt.Errorf("opts: PackageOutputPath not set") |
| 208 | + } |
| 209 | + |
| 210 | + return nil |
| 211 | +} |
0 commit comments