Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Formula/cortex.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require_relative "../lib/github_private_release_download_strategy"

class Cortex < Formula
desc "Cerebral agent mesh CLI"
homepage "https://github.com/cerebral-work/cortex"
license :cannot_represent

on_macos do
on_intel do
url "https://github.com/cerebral-work/cortex/releases/download/v1.0.0-rc.46/cortex-v1.0.0-rc.46-x86_64-apple-darwin.tar.gz",
using: GitHubPrivateReleaseDownloadStrategy
sha256 "e5f0e606acd742ac1ef1e892f26f10888eb4460bf9a76003deb4bf56f1ff5062"
end
on_arm do
url "https://github.com/cerebral-work/cortex/releases/download/v1.0.0-rc.46/cortex-v1.0.0-rc.46-aarch64-apple-darwin.tar.gz",
using: GitHubPrivateReleaseDownloadStrategy
sha256 "899a5f24acb571184042fe985e1e0873c412d084a6a3f7c661b2bcefe0e93a36"
end
end

on_linux do
on_intel do
url "https://github.com/cerebral-work/cortex/releases/download/v1.0.0-rc.46/cortex-v1.0.0-rc.46-x86_64-unknown-linux-musl.tar.gz",
using: GitHubPrivateReleaseDownloadStrategy
sha256 "541ebf9e36a14d72b87a944afaa60b70a2cf2545be26759518b23fca0d1abed8"
end
on_arm do
url "https://github.com/cerebral-work/cortex/releases/download/v1.0.0-rc.46/cortex-v1.0.0-rc.46-aarch64-unknown-linux-musl.tar.gz",
using: GitHubPrivateReleaseDownloadStrategy
sha256 "53d4d0260124f0b65f7077e6051f5093786341afc1963c5a9c35294d388967d0"
end
end

def install
bin.install "cortex"
end

test do
assert_match "cortex #{version}", shell_output("#{bin}/cortex --version")
end
end
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,26 @@ brew install unsigned-gg/unsigned/<tool> # CLIs
| Tool | Homebrew | mise |
|---|---|---|
| herdr | `brew install unsigned-gg/unsigned/herdr` (Linux) | `mise use github:unsigned-gg/herdr@ug-v0.9.5.1` |
| cortex (private) | `brew install unsigned-gg/unsigned/cortex` | `mise use github:cerebral-work/cortex@v1.0.0-rc.46` |

mise's `ubi:` backend (`mise use ubi:unsigned-gg/herdr@ug-v0.9.5.1`) installs the
same binary, but mise has deprecated it and removes it in 2027.1.0.

### Private tools

Their release assets live in private repositories, so installing one needs a
read-only GitHub token in the **environment**: `HOMEBREW_GITHUB_API_TOKEN` for
Homebrew, `GITHUB_TOKEN` for mise. Export it from your secret store; never pass
it as a command-line argument or put it in a URL.

Homebrew fetches these assets with `lib/github_private_release_download_strategy.rb`,
which hands the token to curl through a 0600 header file, never on curl's
command line. It refuses to run with `HOMEBREW_CURL_VERBOSE` or
`HOMEBREW_CURLRC` set, since either can print request headers.

Pin private tools to a tag: mise's "latest" skips prereleases such as
`v1.0.0-rc.46`.

## Layout

- `Casks/` — GUI app casks (`.dmg`/`.zip`/`.pkg` artifacts)
Expand Down
82 changes: 82 additions & 0 deletions lib/github_private_release_download_strategy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# typed: false
# frozen_string_literal: true

require "download_strategy"
require "json"
require "tempfile"

# Downloads a release asset of a private GitHub repository through the
# release-asset API, authenticated by HOMEBREW_GITHUB_API_TOKEN.
#
# The token reaches curl only through a header file (`--header @file`, mode
# 0600, deleted after use). It is never on curl's argv, so it never shows in a
# process list or in the command line Homebrew prints when curl fails, and it is
# never part of a URL. curl does not resend the header after the cross-host
# redirect to the signed asset URL.
#
# A formula names the asset by its browser URL and selects this strategy:
# url "https://github.com/<owner>/<repo>/releases/download/<tag>/<asset>",
# using: GitHubPrivateReleaseDownloadStrategy
class GitHubPrivateReleaseDownloadStrategy < CurlDownloadStrategy
URL_PATTERN = %r{\Ahttps://github\.com/([^/]+)/([^/]+)/releases/download/([^/]+)/([^/]+)\z}

def initialize(url, name, version, **meta)
super
@owner, @repo, @tag, @asset = URL_PATTERN.match(url)&.captures
raise CurlDownloadStrategyError, "not a GitHub release-asset URL: #{url}" unless @asset
end

# The browser URL of a private asset is not readable without auth, so nothing
# is probed before the download: no unauthenticated HEAD request.
def resolve_url_basename_time_file_size(url, timeout: nil)
[url, @asset, nil, nil, nil, false]
end

private

def _fetch(url:, resolved_url:, timeout:)
with_auth_header do
curl_download "https://api.github.com/repos/#{@owner}/#{@repo}/releases/assets/#{asset_id}",
"--header", "Accept: application/octet-stream",
to: temporary_path, timeout:
end
end

def asset_id
release = JSON.parse(curl_output("--header", "Accept: application/vnd.github+json",
"https://api.github.com/repos/#{@owner}/#{@repo}/releases/tags/#{@tag}").stdout)
asset = release["assets"]&.find { |a| a["name"] == @asset }
return asset["id"] if asset

reason = release["message"] || "no asset named #{@asset}"
raise CurlDownloadStrategyError, "#{@owner}/#{@repo} release #{@tag}: #{reason}"
end

def with_auth_header
token = Homebrew::EnvConfig.github_api_token
if token.blank?
raise CurlDownloadStrategyError,
"#{@owner}/#{@repo} is private: set HOMEBREW_GITHUB_API_TOKEN to a read-only token"
end
# curl's --verbose, and options a user curlrc can add, print request headers.
if Homebrew::EnvConfig.curl_verbose? || Homebrew::EnvConfig.curlrc
raise CurlDownloadStrategyError,
"#{@owner}/#{@repo}: unset HOMEBREW_CURL_VERBOSE and HOMEBREW_CURLRC; they would print the token"
end

Tempfile.create("brew-github-auth") do |file|
file.write("Authorization: Bearer #{token}\n")
file.flush
@auth_header_file = file.path
yield
ensure
@auth_header_file = nil
end
end

def _curl_args
args = super
args += ["--header", "@#{@auth_header_file}"] if @auth_header_file
args
end
end
Loading