Skip to content
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

Use 'cert.install' instead of passing in root certificates. #1116

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 3 additions & 6 deletions src/cli/brokers/http/base.toit
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class BrokerCliHttp implements BrokerCli:
client_/http.Client? := null

constructor .server-config_ --.id:
// We are on the host. Just install all certificate roots.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do this earlier?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

certificate-roots.install-all-trusted-roots
network_ = net.open
add-finalizer this:: close

Expand Down Expand Up @@ -125,13 +127,8 @@ class BrokerCliHttp implements BrokerCli:

send-request_ encoded/ByteArray -> http.Response:
if not client_:
root-names := server-config_.root-certificate-names
if root-names:
root-certificates := root-names.map:
der/tls.RootCertificate := certificate-roots.MAP[it]
x509.Certificate.parse der.raw
if server-config_.root-certificate-names:
client_ = http.Client.tls network_
--root-certificates=root-certificates
else:
client_ = http.Client network_

Expand Down
2 changes: 1 addition & 1 deletion src/cli/utils/utils.toit
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ download-url url/string --out-path/string --ui/Ui -> none:
ui.info "Downloading $url."

network := net.open
certificate-roots.install-all-trusted-roots
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda want to this a bit earlier. That's typically how we deal with installed certificates.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

try:
client := http.Client.tls network
--root-certificates=certificate-roots.ALL

response := client.get --uri=url
if response.status-code != http.STATUS-OK:
Expand Down
3 changes: 1 addition & 2 deletions src/service/brokers/broker.toit
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,12 @@ interface BrokerService:
if colon-pos >= 0:
port = int.parse host[colon-pos + 1..]
host = host[..colon-pos]
// TODO(florian): get the path from the config.
der := supabase-config.root-certificate-der
http-config := ServerConfigHttp
server-config.name
--host=host
--port=port
--path="/functions/v1/b"
--path="/functions/v1/b" // TODO(florian): get the path from the config.
--poll-interval=supabase-config.poll-interval
--root-certificate-names=null
--root-certificate-ders=der ? [der] : null
Expand Down
11 changes: 5 additions & 6 deletions src/service/brokers/http/connection.toit
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import encoding.json
import encoding.base64
import http
import net
import net.x509
import tls
import reader show Reader
import system.storage
import certificate-roots
Expand All @@ -18,8 +18,9 @@ class HttpConnection_:

constructor .network_ .config_:
if config_.root-certificate-ders:
root-certificates_ = config_.root-certificate-ders.map:
x509.Certificate.parse it
config_.root-certificate-ders.do:
certificate := tls.RootCertificate it
certificate.install
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this early when setting up the config instead? There is no hash for this one (I think), so we're going to parse it every single time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to do this earlier, as the Broker is constructed relatively early, but we don't want to spend time installing roots if they won't be needed.
I added a flag that keeps track of whether they are already installed or not.

create-fresh-client_

create-fresh-client_ -> none:
Expand All @@ -28,9 +29,7 @@ class HttpConnection_:
client_ = null

if config_.root-certificate-ders:
client_ = http.Client.tls network_
--root-certificates=root-certificates_
--security-store=HttpSecurityStore_
client_ = http.Client.tls network_ --security-store=HttpSecurityStore_
else:
client_ = http.Client network_

Expand Down