Skip to content

remove using __init__ by either using OncePerProcess or recomputing #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 26, 2025
Merged
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
28 changes: 0 additions & 28 deletions .ci/test_and_change_uuid.jl

This file was deleted.

3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ jobs:
fail-fast: false
matrix:
version:
- '1.3'
- '1' # automatically expands to the latest stable 1.x release of Julia.
- 'nightly'
os:
- ubuntu-latest
Expand All @@ -44,7 +42,6 @@ jobs:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: julia-actions/cache@v2
- run: julia --color=yes .ci/test_and_change_uuid.jl
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ authors = ["Stefan Karpinski <[email protected]> and contributors"]
version = "1.3.0"

[compat]
julia = "1"
julia = "1.12"

[extras]
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
Expand Down
6 changes: 0 additions & 6 deletions src/NetworkOptions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,4 @@ include("ca_roots.jl")
include("ssh_options.jl")
include("verify_host.jl")

function __init__()
SYSTEM_CA_ROOTS[] = nothing
BUNDLED_KNOWN_HOSTS_FILE[] = nothing
empty!(ENV_HOST_PATTERN_CACHE)
end

end # module
28 changes: 10 additions & 18 deletions src/ca_roots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,22 @@ const BSD_CA_ROOTS = [
"/usr/local/etc/ssl/cert.pem" # FreeBSD
]

const SYSTEM_CA_ROOTS_LOCK = ReentrantLock()
const SYSTEM_CA_ROOTS = Ref{Union{Nothing, String}}(nothing)

const BEGIN_CERT_REGULAR = "-----BEGIN CERTIFICATE-----"
const BEGIN_CERT_OPENSSL = "-----BEGIN TRUSTED CERTIFICATE-----"

function system_ca_roots()
lock(SYSTEM_CA_ROOTS_LOCK) do
SYSTEM_CA_ROOTS[] !== nothing && return # from lock()
search_path = Sys.islinux() ? LINUX_CA_ROOTS :
Sys.isbsd() && !Sys.isapple() ? BSD_CA_ROOTS : String[]
for path in search_path
ispath(path) || continue
for line in eachline(path)
if line in [BEGIN_CERT_REGULAR, BEGIN_CERT_OPENSSL]
SYSTEM_CA_ROOTS[] = path
return # from lock()
end
const system_ca_roots = OncePerProcess{String}() do
search_path = Sys.islinux() ? LINUX_CA_ROOTS :
Sys.isbsd() && !Sys.isapple() ? BSD_CA_ROOTS : String[]
for path in search_path
ispath(path) || continue
for line in eachline(path)
if line in [BEGIN_CERT_REGULAR, BEGIN_CERT_OPENSSL]
return path
end
end
# TODO: extract system certs on Windows & macOS
SYSTEM_CA_ROOTS[] = bundled_ca_roots()
end
return SYSTEM_CA_ROOTS[]
# TODO: extract system certs on Windows & macOS
return bundled_ca_roots()
end

const CA_ROOTS_VARS = [
Expand Down
19 changes: 5 additions & 14 deletions src/ssh_options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,11 @@ end

## helper functions

const BUNDLED_KNOWN_HOSTS_LOCK = ReentrantLock()
const BUNDLED_KNOWN_HOSTS_FILE = Ref{Union{Nothing, String}}(nothing)

function bundled_known_hosts()
lock(BUNDLED_KNOWN_HOSTS_LOCK) do
file = BUNDLED_KNOWN_HOSTS_FILE[]
if file === nothing
file, io = mktemp()
BUNDLED_KNOWN_HOSTS_FILE[] = file
write(io, BUNDLED_KNOWN_HOSTS)
close(io)
end
return file::String
end
const bundled_known_hosts = OncePerProcess{String}() do
file, io = mktemp()
write(io, BUNDLED_KNOWN_HOSTS)
close(io)
return file
end

const BUNDLED_KNOWN_HOSTS = """
Expand Down
18 changes: 5 additions & 13 deletions src/verify_host.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,14 @@ env_host_pattern_match(var::AbstractString, host::AbstractString) =
env_host_pattern_match(var::AbstractString, host::Nothing) =
env_host_pattern_regex(var) === MATCH_ANY_RE

const ENV_HOST_PATTERN_LOCK = ReentrantLock()
const ENV_HOST_PATTERN_CACHE = Dict{String,Tuple{String,Regex}}()

function env_host_pattern_regex(var::AbstractString)
lock(ENV_HOST_PATTERN_LOCK) do
value = get(ENV, var, nothing)
if value === nothing
delete!(ENV_HOST_PATTERN_CACHE, var)
return MATCH_NONE_RE
end
old_value, regex = get(ENV_HOST_PATTERN_CACHE, var, (nothing, nothing))
old_value == value && return regex
regex = host_pattern_regex(value, var)
ENV_HOST_PATTERN_CACHE[var] = (value, regex)
return regex
value = get(ENV, var, nothing)
if value === nothing
return MATCH_NONE_RE
end
regex = host_pattern_regex(value, var)
return regex
end

if !@isdefined(contains)
Expand Down
Loading