-
Notifications
You must be signed in to change notification settings - Fork 8
Add HTTP proxy module #56
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| { | ||
| config, | ||
| lib, | ||
| ... | ||
| }: let | ||
| cfg = config.cardano.http; | ||
| inherit (lib) mkIf mkEnableOption optional; | ||
| in { | ||
| options.cardano.http = { | ||
| enable = | ||
| mkEnableOption "HTTP SSL proxy and load balancer for cardano services" | ||
| // {default = config.cardano.enable or false;}; | ||
| }; | ||
| config = mkIf cfg.enable { | ||
| networking.firewall.allowedTCPPorts = [80] ++ optional config.services.http-proxy.https.enable 443; | ||
|
|
||
| services.http-proxy = { | ||
| enable = true; | ||
| servers = mkIf config.cardano.enable ["127.0.0.1"]; | ||
| services = { | ||
| cardano-node = { | ||
| inherit (config.services.cardano-node) port; | ||
| inherit (config.services.cardano-node.package.passthru.identifier) version; | ||
| }; | ||
| ogmios = { | ||
| inherit (config.services.ogmios) port; | ||
| inherit (config.services.ogmios.package) version; | ||
| }; | ||
| }; | ||
| }; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| { | ||
| config, | ||
| lib, | ||
| ... | ||
| }: let | ||
| cfg = config.services.http-proxy; | ||
| inherit (lib) types listToAttrs mkOption mkEnableOption mapAttrs mkIf optionalString; | ||
| in { | ||
| options.services.http-proxy = { | ||
| enable = mkEnableOption "HTTP proxy, TLS endpoint and load balancer"; | ||
| domainName = mkOption { | ||
| description = "Domain name. For each service a virtualHost is configured as a subdomain."; | ||
| type = types.str; | ||
| default = ""; | ||
| }; | ||
| https.enable = mkOption { | ||
| description = "Enable TLS and redirect all connections to HTTPS. Requires certificates. Supports ACME."; | ||
| type = types.bool; | ||
| default = false; | ||
| }; | ||
| https.acme.enable = mkOption { | ||
| description = "Enable Let's Encrypt ACME TLS certificates. Requires public DNS records pointing to server and `security.acme` configured."; | ||
| type = types.bool; | ||
| default = cfg.https.enable; | ||
| }; | ||
| servers = mkOption { | ||
| description = "List of upstream server host names used for all services."; | ||
| type = types.listOf types.str; | ||
| default = []; | ||
| }; | ||
| services = mkOption { | ||
| description = "Configuraiton for each upstream service."; | ||
| type = types.attrsOf (types.submodule ({name, ...}: { | ||
| options = { | ||
| name = mkOption { | ||
| description = "Name of the service."; | ||
| type = types.str; | ||
| default = name; | ||
| }; | ||
| servers = mkOption { | ||
| description = "List of upstream server host names."; | ||
| type = types.listOf types.str; | ||
| default = cfg.servers; | ||
| }; | ||
| port = mkOption { | ||
| description = "Upstream server port."; | ||
| type = types.port; | ||
| }; | ||
| version = mkOption { | ||
| description = "This string will be served at path '/version'."; | ||
| type = types.nullOr types.str; | ||
| default = null; | ||
| }; | ||
| }; | ||
| })); | ||
| }; | ||
| _mkUpstream = mkOption { | ||
| type = types.functionTo types.attrs; | ||
| internal = true; | ||
| default = service: { | ||
| servers = listToAttrs (map | ||
| (server: { | ||
| name = "${server}:${toString service.port}"; | ||
| value = {}; | ||
| }) | ||
| service.servers); | ||
| }; | ||
| }; | ||
| _mkVirtualHost = mkOption { | ||
| type = types.functionTo types.attrs; | ||
| internal = true; | ||
| default = service: { | ||
| serverName = "${service.name}${optionalString (cfg.domainName != "") ".${cfg.domainName}"}"; | ||
| forceSSL = cfg.https.enable; | ||
| enableACME = cfg.https.acme.enable; | ||
| locations = { | ||
| "=/version" = mkIf (service.version != null) { | ||
brainrake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return = "200 ${service.version}"; | ||
| extraConfig = "add_header Content-Type text/plain;"; | ||
| }; | ||
| "/" = { | ||
| proxyWebsockets = true; | ||
| proxyPass = "http://${service.name}"; | ||
| }; | ||
| }; | ||
| extraConfig = " | ||
| proxy_hide_header Access-Control-Allow-Origin; | ||
| add_header Access-Control-Allow-Origin * always; | ||
| proxy_hide_header Access-Control-Allow-Headers; | ||
| add_header Access-Control-Allow-Headers * always; | ||
| proxy_hide_header Access-Control-Allow-Methods; | ||
| add_header Access-Control-Allow-Methods * always; | ||
| "; | ||
| }; | ||
| }; | ||
| }; | ||
| config = mkIf cfg.enable { | ||
| services.nginx = { | ||
| enable = true; | ||
| recommendedGzipSettings = true; | ||
| recommendedOptimisation = true; | ||
| recommendedProxySettings = true; | ||
| recommendedTlsSettings = true; | ||
| serverNamesHashBucketSize = 128; | ||
|
|
||
| statusPage = true; | ||
|
|
||
| upstreams = mapAttrs (_: cfg._mkUpstream) cfg.services; | ||
| virtualHosts = mapAttrs (_: cfg._mkVirtualHost) cfg.services; | ||
| }; | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,5 +3,6 @@ | |
| ./cardano-cli.nix | ||
| ./cardano-node.nix | ||
| ./ogmios.nix | ||
| ./http.nix | ||
| ]; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| { | ||
| perSystem.vmTests.tests.http = { | ||
| impure = true; | ||
| module = { | ||
| nodes.node = {config, ...}: { | ||
| cardano = { | ||
| network = "preview"; | ||
| node.enable = true; | ||
| ogmios.enable = true; | ||
| }; | ||
| services.ogmios.host = "0.0.0.0"; | ||
| networking.firewall.allowedTCPPorts = [config.services.ogmios.port]; | ||
| }; | ||
|
|
||
| nodes.proxy = { | ||
| cardano = { | ||
| http.enable = true; | ||
| }; | ||
| services.http-proxy.servers = ["node"]; | ||
| }; | ||
|
|
||
| nodes.client = {pkgs, ...}: { | ||
| environment.systemPackages = [pkgs.curl]; | ||
| }; | ||
|
|
||
| testScript = {nodes, ...}: '' | ||
| start_all() | ||
| node.wait_for_unit("ogmios") | ||
| node.wait_until_succeeds('curl --fail http://127.0.0.1:1337/health') | ||
| proxy.wait_for_unit("nginx") | ||
| client.wait_until_succeeds('curl --fail -H "Host: ogmios" http://proxy/health') | ||
| client.succeed('[ "${nodes.node.services.ogmios.package.version}" == "$(curl --silent --fail -H "Host: ogmios" http://proxy/version)" ]') | ||
| ''; | ||
| }; | ||
| }; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.