Skip to content
This repository was archived by the owner on Dec 7, 2023. It is now read-only.

Commit 6c2a89e

Browse files
committed
WIP Switch to Rustls
1 parent 00ad604 commit 6c2a89e

File tree

5 files changed

+152
-30
lines changed

5 files changed

+152
-30
lines changed

cabal.project

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
11
packages: .
2+
3+
source-repository-package
4+
type: git
5+
location: https://github.com/amesgen/hs-rustls
6+
tag: af95cd7d3f79913d2864f46931bbfe339f9c396d
7+
--sha256: 0a2drcy9893r06ghh7cpj21lg2n8ma737x0mhqh0spsf3znsd4db
8+
subdir: rustls http-client-rustls
9+
10+
constraints: rustls -derive-storable-plugin

flake.lock

Lines changed: 106 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,23 @@
99
inputs.flake-utils.follows = "flake-utils";
1010
};
1111
flake-utils.url = "github:numtide/flake-utils";
12+
nix-rustls = {
13+
url = "github:amesgen/hs-rustls?dir=nix-rustls";
14+
inputs.nixpkgs.follows = "nixpkgs";
15+
inputs.flake-utils.follows = "flake-utils";
16+
};
1217
};
13-
outputs = { self, nixpkgs, flake-utils, haskellNix, nur, pre-commit-hooks }:
18+
outputs = { self, nixpkgs, flake-utils, haskellNix, nur, pre-commit-hooks, nix-rustls }:
1419
flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
1520
let
1621
pkgs = import nixpkgs {
1722
inherit system;
1823
inherit (haskellNix) config;
19-
overlays = [ haskellNix.overlay nur.overlay ];
24+
overlays = [
25+
haskellNix.overlay
26+
nur.overlay
27+
nix-rustls.overlays.default
28+
];
2029
};
2130
inherit (pkgs) lib;
2231
hsPkgs = pkgs.haskell-nix.cabalProject {
@@ -53,28 +62,23 @@
5362
--hie-directory ${hellsmack.components.tests.tasty.hie}
5463
'';
5564
pre-commit-check =
56-
let ormolu = pkgs.nur.repos.amesgen.ormolu; in
5765
pre-commit-hooks.lib.${system}.run {
5866
src = ./.;
5967
hooks = {
6068
nixpkgs-fmt.enable = true;
61-
ormolu = {
62-
enable = true;
63-
entry = lib.mkForce "${ormolu}/bin/ormolu -i";
64-
};
69+
ormolu.enable = true;
6570
hlint.enable = true;
6671
};
6772
tools = {
68-
inherit ormolu;
69-
hlint = pkgs.nur.repos.amesgen.hlint;
73+
inherit (pkgs.nur.repos.amesgen) ormolu hlint;
7074
};
7175
};
7276
};
7377
devShells.default = hsPkgs.shellFor {
7478
tools = { cabal = { }; };
7579
buildInputs = [ pkgs.nur.repos.amesgen.cabal-docspec ];
7680
withHoogle = false;
77-
exactDeps = true;
81+
exactDeps = false;
7882
inherit (self.checks.${system}.pre-commit-check) shellHook;
7983
};
8084

hellsmack.cabal

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ library
4141
, temporary >= 1.3
4242
, http-types >= 0.12
4343
, http-client >= 0.7
44+
, rustls >= 0.0
45+
, http-client-rustls >= 0.0
4446
, network-uri >= 2.6
4547
, aeson >= 2.0
4648
, deriving-aeson >= 0.2.7
@@ -68,11 +70,6 @@ library
6870
, semigroups >= 0.19
6971
, optparse-applicative >= 0.16
7072
, th-env >= 0.1
71-
if os(windows)
72-
build-depends: http-client-tls >= 0.3
73-
cpp-options: -DUSE_HASKELL_TLS
74-
else
75-
build-depends: http-client-openssl >= 0.3.3
7673

7774
exposed-modules:
7875
Prelude

src/HellSmack/Http.hs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
{-# LANGUAGE CPP #-}
1+
module HellSmack.Http (newTLSManager) where
22

3-
module HellSmack.Http (newTLSManager, Manager) where
3+
import Network.HTTP.Client qualified as HTTP
4+
import Network.HTTP.Client.Rustls (rustlsManagerSettings)
5+
import Rustls qualified
6+
import UnliftIO.Exception
47

5-
import Network.HTTP.Client
6-
#if USE_HASKELL_TLS
7-
import Network.HTTP.Client.TLS
8-
#else
9-
import Network.HTTP.Client.OpenSSL
10-
#endif
11-
12-
newTLSManager :: MonadIO m => m Manager
13-
#if USE_HASKELL_TLS
14-
newTLSManager = newTlsManager
15-
#else
16-
newTLSManager = liftIO $ withOpenSSL newOpenSSLManager
17-
#endif
8+
newTLSManager :: MonadIO m => m HTTP.Manager
9+
newTLSManager = liftIO do
10+
roots <-
11+
fmap (Rustls.ClientRootsInMemory . pure . Rustls.PEMCertificatesStrict) $
12+
defaultCertFile `onException` envCertFile
13+
clientConfig <- Rustls.buildClientConfig $ Rustls.defaultClientConfigBuilder roots
14+
HTTP.newManager $ rustlsManagerSettings clientConfig
15+
where
16+
defaultCertFile = readFileBS "/etc/ssl/certs/ca-certificates.crt"
17+
envCertFile =
18+
lookupEnv envKey >>= \case
19+
Just file | not (null file) -> readFileBS file
20+
_ -> throwString [i|default SSL certs not found, please set $envKey|]
21+
where
22+
envKey :: String
23+
envKey = "SSL_CERT_FILE"

0 commit comments

Comments
 (0)