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

replace ambient authority in net package by explicit Root authority #301

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
92b0388
require Root authority when creating TCP/UDP sockets
dckc Aug 30, 2015
681f39a
require Root capability to resolve DNS addresses
dckc Aug 30, 2015
16cf3ea
require Root capability to do reverse DNS lookups
dckc Aug 30, 2015
01033b1
update net/http to use explicit authority for net access
dckc Sep 1, 2015
651079c
update httpget test to use explicit authority for net access
dckc Sep 1, 2015
a434d19
reduce prereq for http Client from Root to TCPConnector
dckc Sep 4, 2015
56131f0
use explicit authority in httpget test
dckc Sep 4, 2015
ad5bab9
explicit file access authority for SSLContext.set_authority()
dckc Sep 4, 2015
924641c
use explicit authority (FilePath) in SSLContext.set_cert
dckc Sep 7, 2015
8ae781d
update ping/pong net test to use explicit authority
dckc Sep 7, 2015
20ad196
factor out network constructors as NetworkInterface
dckc Sep 11, 2015
31c7e1b
update httpget, net tests w.r.t. NetworkInterface
dckc Sep 11, 2015
00c9262
thinner networking interfaces: client, server, endpoint
dckc Sep 13, 2015
b49040c
update integration test: Pong only needs UDPEndpoint
dckc Sep 13, 2015
e1aea81
Merge branch 'master' of https://github.com/CausalityLtd/ponyc.git in…
dckc Nov 6, 2015
a801b11
rename Root to AmbientAuth in net package
dckc Nov 6, 2015
3c8a292
update httpget w.r.t. Root rename
dckc Nov 8, 2015
7c3cf13
Merge branch 'master' of https://github.com/CausalityLtd/ponyc.git in…
dckc Nov 8, 2015
954fd3b
update Root to AmbientAuth in examples/net
dckc Nov 8, 2015
12ed678
prune unused reference to files
dckc Nov 8, 2015
473e10b
Merge branch 'master' of https://github.com/CausalityLtd/ponyc.git in…
dckc Feb 21, 2016
56bac65
DNS API uses DNSClient interface on NetworkInterface
dckc Feb 22, 2016
795dfa4
shorten env.root check
dckc Feb 22, 2016
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: 8 additions & 1 deletion examples/echo/echo.pony
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ use "net"

actor Main
new create(env: Env) =>
let listener = TCPListener.ip4(Listener(env))
match env.root
| let a: AmbientAuth =>
let net = NetworkInterface(a)
let listener = net.listen(Listener(env)
where v=IPv4)
else
env.err.print("no root")
end

class Listener is TCPListenNotify
let _env: Env
Expand Down
18 changes: 14 additions & 4 deletions examples/httpget/httpget.pony
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
use "assert"
use "collections"
use "files"
use "net"
use "net/http"
use "net/ssl"

actor Main

new create(env: Env) =>
match env.root
| None => env.out.print("No root!")
| let r: AmbientAuth => Go(env, r)
end


actor Go
let _env: Env
let _client: Client

new create(env: Env) =>
new create(env: Env, root: AmbientAuth) =>
_env = env

let sslctx = try
recover
SSLContext
.set_client_verify(true)
.set_authority("./test/pony/httpget/cacert.pem")
.set_authority(FilePath(root, "./test/pony/httpget/cacert.pem"))
end
end

_client = Client(consume sslctx)
_client = Client(recover NetworkInterface(root) end, consume sslctx)

for i in Range(1, env.args.size()) do
try
Expand Down
20 changes: 15 additions & 5 deletions examples/httpserver/httpserver.pony
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
use "net"
use "net/http"

actor Main
new create(env: Env) =>
let service = try env.args(1) else "50000" end
let limit = try env.args(2).usize() else 100 end
Server(Info(env), Handle, CommonLog(env.out) where service = service,
match env.root
| let a: AmbientAuth =>
let service = try env.args(1) else "50000" end
let limit = try env.args(2).usize() else 100 end
let net: NetworkInterface val = recover NetworkInterface(a) end
Server(net, Info(env, net), Handle, CommonLog(env.out, net) where service = service,
limit = limit)
// Server(Info(env), Handle, ContentsLog(env.out) where service = service,
// limit = limit)
// Server(Info(env), Handle, DiscardLog where service = service,
// limit = limit)
else
env.err.print("cannot use network: no root")
end


class Info
let _env: Env
let _dns: DNSClient val

new iso create(env: Env) =>
new iso create(env: Env, dns: DNSClient val) =>
_env = env
_dns = dns

fun ref listening(server: Server ref) =>
try
(let host, let service) = server.local_address().name()
(let host, let service) = server.local_address().name(_dns)
_env.out.print("Listening on " + host + ":" + service)
else
_env.out.print("Couldn't get local address.")
Expand Down
19 changes: 13 additions & 6 deletions examples/net/listener.pony
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use "files"
use "net"
use "net/ssl"

Expand All @@ -8,17 +9,22 @@ class Listener is TCPListenNotify
var _host: String = ""
var _service: String = ""
var _count: USize = 0
let _root: AmbientAuth

new create(env: Env, ssl: Bool, limit: USize) =>
new create(env: Env, ssl: Bool, limit: USize) ? =>
_env = env
_root = env.root as AmbientAuth
_limit = limit

let cert = try FilePath(_root, "./test/pony/net/cert.pem") else error end
let key = try FilePath(_root, "./test/pony/net/key.pem") else error end

_sslctx = if ssl then
try
recover
SSLContext
.set_authority("./test/pony/net/cert.pem")
.set_cert("./test/pony/net/cert.pem", "./test/pony/net/key.pem")
.set_authority(cert)
.set_cert(cert, key)
.set_client_verify(true)
.set_server_verify(true)
end
Expand Down Expand Up @@ -70,13 +76,14 @@ class Listener is TCPListenNotify
try
let env = _env

let network = NetworkInterface(_root)
match _sslctx
| let ctx: SSLContext =>
let ssl = ctx.client()
TCPConnection(SSLConnection(ClientSide(env), consume ssl), _host,
_service)
network.connect(SSLConnection(ClientSide(env), consume ssl),
_host, _service)
else
TCPConnection(ClientSide(env), _host, _service)
network.connect(ClientSide(env), _host, _service)
end
else
_env.out.print("couldn't create client side")
Expand Down
12 changes: 10 additions & 2 deletions examples/net/net.pony
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,13 @@ actor Main
1
end

TCPListener(recover Listener(env, ssl, limit) end)
UDPSocket(recover Pong(env) end)
match env.root
| let r: AmbientAuth =>
let network = NetworkInterface(r)
try
network.listen(recover Listener(env, ssl, limit) end)
network.udp_socket(recover Pong(env) end)
end
else
env.out.print("no root in Env!")
end
16 changes: 8 additions & 8 deletions examples/net/pong.pony
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ use "net"

class Pong is UDPNotify
let _env: Env
let _network: UDPEndpoint val

new create(env: Env) =>
new create(env: Env) ? =>
_env = env
_network = match env.root
| let r: AmbientAuth => recover NetworkInterface(r) end
else
error
end

fun ref listening(sock: UDPSocket ref) =>
try
Expand All @@ -14,13 +20,7 @@ class Pong is UDPNotify

let env = _env

if ip.ip4() then
UDPSocket.ip4(recover Ping(env, ip) end)
elseif ip.ip6() then
UDPSocket.ip6(recover Ping(env, ip) end)
else
error
end
_network.udp_socket(recover Ping(env, ip) end where v=ip.version())
else
_env.out.print("Pong: couldn't get local name")
sock.dispose()
Expand Down
18 changes: 6 additions & 12 deletions packages/net/dns.pony
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,29 @@ primitive DNS
"""
Helper functions for resolving DNS queries.
"""
fun apply(host: String, service: String): Array[IPAddress] iso^ =>
"""
Gets all IPv4 and IPv6 addresses for a host and service.
"""
_resolve(0, host, service)

fun ip4(host: String, service: String): Array[IPAddress] iso^ =>
fun _ip4(host: String, service: String): Array[IPAddress] iso^ =>
"""
Gets all IPv4 addresses for a host and service.
"""
_resolve(1, host, service)

fun ip6(host: String, service: String): Array[IPAddress] iso^ =>
fun _ip6(host: String, service: String): Array[IPAddress] iso^ =>
"""
Gets all IPv6 addresses for a host and service.
"""
_resolve(2, host, service)

fun broadcast_ip4(service: String): Array[IPAddress] iso^ =>
fun _broadcast_ip4(service: String): Array[IPAddress] iso^ =>
"""
Link-local IP4 broadcast address.
"""
ip4("255.255.255.255", service)
_ip4("255.255.255.255", service)

fun broadcast_ip6(service: String): Array[IPAddress] iso^ =>
fun _broadcast_ip6(service: String): Array[IPAddress] iso^ =>
"""
Link-local IP6 broadcast address.
"""
ip6("FF02::1", service)
_ip6("FF02::1", service)

fun is_ip4(host: String): Bool =>
"""
Expand Down
11 changes: 8 additions & 3 deletions packages/net/http/client.pony
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ actor Client
let _sslctx: SSLContext
let _pipeline: Bool
let _clients: Map[_HostService, _ClientConnection] = _clients.create()
let _tcp: TCPClient val

new create(sslctx: (SSLContext | None) = None, pipeline: Bool = true) =>
new create(tcp: TCPClient val,
sslctx: (SSLContext | None) = None,
pipeline: Bool = true)
=>
"""
Create a client for the given host and service.
"""
Expand All @@ -21,6 +25,7 @@ actor Client
end

_pipeline = pipeline
_tcp = tcp

be apply(request: Payload val) =>
"""
Expand Down Expand Up @@ -50,8 +55,8 @@ actor Client
_clients(hs)
else
let client = match url.scheme
| "http" => _ClientConnection(hs.host, hs.service, None, _pipeline)
| "https" => _ClientConnection(hs.host, hs.service, _sslctx, _pipeline)
| "http" => _ClientConnection(_tcp, hs.host, hs.service, None, _pipeline)
| "https" => _ClientConnection(_tcp, hs.host, hs.service, _sslctx, _pipeline)
else
error
end
Expand Down
9 changes: 6 additions & 3 deletions packages/net/http/clientconnection.pony
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ actor _ClientConnection
let _unsent: List[Payload val] = _unsent.create()
let _sent: List[Payload val] = _sent.create()
var _conn: (TCPConnection | None) = None
let _tcp: TCPClient val

new create(host: String, service: String, sslctx: (SSLContext | None) = None,
new create(tcp: TCPClient val,
host: String, service: String, sslctx: (SSLContext | None) = None,
pipeline: Bool = true)
=>
"""
Expand All @@ -24,6 +26,7 @@ actor _ClientConnection
_service = service
_sslctx = sslctx
_pipeline = pipeline
_tcp = tcp

be apply(request: Payload val) =>
"""
Expand Down Expand Up @@ -130,10 +133,10 @@ actor _ClientConnection
_conn = try
let ctx = _sslctx as SSLContext
let ssl = ctx.client(_host)
TCPConnection(SSLConnection(_ResponseBuilder(this), consume ssl),
_tcp.connect(SSLConnection(_ResponseBuilder(this), consume ssl),
_host, _service)
else
TCPConnection(_ResponseBuilder(this), _host, _service)
_tcp.connect(_ResponseBuilder(this), _host, _service)
end

fun ref _cancel_all() =>
Expand Down
13 changes: 12 additions & 1 deletion packages/net/http/commonlog.pony
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
use "net"
use "time"

class CommonLog is Logger
"""
Logs HTTP requests in the common log format.
"""
let _out: OutStream
let _dns: DNSClient val

new val create(out: OutStream) =>
new val create(out: OutStream, dns: DNSClient val) =>
_out = out
_dns = dns

fun val client_ip(addr: IPAddress): String ? =>
try
(let host, let port) = addr.name(_dns)
host
else
error
end

fun val apply(ip: String, request: Payload val, response: Payload val) =>
let list = recover Array[String](24) end
Expand Down
5 changes: 5 additions & 0 deletions packages/net/http/discardlog.pony
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use "net"

primitive DiscardLog
"""
Doesn't log anything.
"""
fun val apply(ip: String, request: Payload val, response: Payload val) =>
None

fun val client_ip(addr: IPAddress): String ? =>
error
7 changes: 7 additions & 0 deletions packages/net/http/notify.pony
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use "net"

interface val ResponseHandler
"""
Handles responses to HTTP requests.
Expand Down Expand Up @@ -26,6 +28,11 @@ interface val Logger
Called after the server has handled a request.
"""

fun val client_ip(addr: IPAddress): String ? =>
"""
Convert client IP address to string.
"""

interface ServerNotify
"""
Notifications for HTTP servers.
Expand Down
2 changes: 1 addition & 1 deletion packages/net/http/requestbuilder.pony
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class _RequestBuilder is TCPConnectionNotify
"""
Create a server connection to handle response ordering.
"""
(let host, let port) = try conn.remote_address().name() else ("-", "-") end
let host = try _logger.client_ip(conn.remote_address()) else "-" end
_server = _ServerConnection(_handler, _logger, conn, host)

fun ref received(conn: TCPConnection ref, data: Array[U8] iso) =>
Expand Down
5 changes: 3 additions & 2 deletions packages/net/http/server.pony
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ actor Server
var _address: IPAddress
var _dirty_routes: Bool = false

new create(notify: ServerNotify iso, handler: RequestHandler,
new create(tcp: TCPServer val,
notify: ServerNotify iso, handler: RequestHandler,
logger: Logger = DiscardLog, host: String = "", service: String = "0",
limit: USize = 0, sslctx: (SSLContext | None) = None)
=>
Expand All @@ -26,7 +27,7 @@ actor Server
_handler = handler
_logger = logger
_sslctx = sslctx
_listen = TCPListener(_ServerListener(this, sslctx, _handler, _logger),
_listen = tcp.listen(_ServerListener(this, sslctx, _handler, _logger),
host, service, limit)
_address = recover IPAddress end

Expand Down
Loading