|
| 1 | +class Certloader < Formula |
| 2 | + # GitHubPrivateRepositoryReleaseDownloadStrategy downloads tarballs from GitHub |
| 3 | + # Release assets. To use it, add |
| 4 | + # `using: GitHubPrivateRepositoryReleaseDownloadStrategy` to the URL section of |
| 5 | + # your formula. This download strategy uses GitHub access tokens from `dev github print-auth` |
| 6 | + # to sign the request. |
| 7 | + class GitHubPrivateRepositoryReleaseDownloadStrategy < CurlDownloadStrategy |
| 8 | + require "utils/formatter" |
| 9 | + require "utils/github" |
| 10 | + |
| 11 | + def initialize(url, name, version, **meta) |
| 12 | + super |
| 13 | + set_github_token |
| 14 | + parse_url_pattern |
| 15 | + end |
| 16 | + |
| 17 | + def set_github_token |
| 18 | + creds_filepath = "/opt/dev/var/private/git_credential_store" |
| 19 | + |
| 20 | + unless File.file?(creds_filepath) |
| 21 | + raise CurlDownloadStrategyError, "No github auth credentials found. Please run `dev github auth`." |
| 22 | + end |
| 23 | + |
| 24 | + file = File.open(creds_filepath) |
| 25 | + contents = file.read.strip!.split("\n") |
| 26 | + latest_raw_creds = contents.last |
| 27 | + creds = URI.parse(latest_raw_creds) |
| 28 | + |
| 29 | + @github_token = creds.password |
| 30 | + |
| 31 | + unless @github_token |
| 32 | + raise CurlDownloadStrategyError, "No github auth found. Please run `dev github auth`." |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + def parse_url_pattern |
| 37 | + unless match = url.match(%r{https://github.com/([^/]+)/([^/]+)/releases/download/([^/]+)/(\S+)}) |
| 38 | + raise CurlDownloadStrategyError, "Invalid url pattern for GitHub Release." |
| 39 | + end |
| 40 | + |
| 41 | + _, @owner, @repo, @tag, @filename = *match |
| 42 | + end |
| 43 | + |
| 44 | + def download_url |
| 45 | + "https://#{@github_token}@api.github.com/repos/#{@owner}/#{@repo}/releases/assets/#{asset_id}" |
| 46 | + end |
| 47 | + |
| 48 | + private |
| 49 | + |
| 50 | + def _fetch(url:, resolved_url:, timeout:) |
| 51 | + # HTTP request header `Accept: application/octet-stream` is required. |
| 52 | + # Without this, the GitHub API will respond with metadata, not binary. |
| 53 | + curl_download download_url, "--header", "Accept: application/octet-stream", to: temporary_path |
| 54 | + end |
| 55 | + |
| 56 | + def asset_id |
| 57 | + @asset_id ||= resolve_asset_id |
| 58 | + end |
| 59 | + |
| 60 | + def resolve_asset_id |
| 61 | + release_metadata = fetch_release_metadata |
| 62 | + assets = release_metadata["assets"].select { |a| a["name"] == @filename } |
| 63 | + raise CurlDownloadStrategyError, "Asset file not found." if assets.empty? |
| 64 | + |
| 65 | + assets.first["id"] |
| 66 | + end |
| 67 | + |
| 68 | + def fetch_release_metadata |
| 69 | + release_url = "https://#{@github_token}@api.github.com/repos/#{@owner}/#{@repo}/releases/tags/#{@tag}" |
| 70 | + GitHub::API.open_rest(release_url) |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + @@version = "0.3.2" |
| 75 | + |
| 76 | + desc 'CLI for managing certificates' |
| 77 | + homepage 'https://github.com/Shopify/certloader' |
| 78 | + version @@version |
| 79 | + |
| 80 | + case |
| 81 | + when OS.mac? && Hardware::CPU.arm? |
| 82 | + url "https://github.com/Shopify/certloader/releases/download/#{@@version}/certloader_darwin_arm64.tar.gz", using: GitHubPrivateRepositoryReleaseDownloadStrategy |
| 83 | + sha256 "7eb27a38551073a2b58b9b7dc39469e043e02e947ce73e2cc242435ab794ba3f" |
| 84 | + else |
| 85 | + odie "Unexpected platform!" |
| 86 | + end |
| 87 | + |
| 88 | + def install |
| 89 | + bin.install({"certloader" => "certloader"}) |
| 90 | + mkdir_p var/"log/certloader" |
| 91 | + mkdir_p var/"certloader/certs" |
| 92 | + end |
| 93 | + |
| 94 | + test do |
| 95 | + system "#{bin}/certloader", "--help" |
| 96 | + end |
| 97 | +end |
0 commit comments