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

frontier: add frontlas bound logics #36

Merged
merged 2 commits into from
Apr 4, 2024
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
3 changes: 3 additions & 0 deletions cmd/frontctl/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

func main() {}
6 changes: 6 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ type Listen struct {
Addr string `yaml:"addr"`
TLS TLS `yaml:"tls"`
}

type Dial struct {
Network string `yaml:"network"`
Addr string `yaml:"addr"`
TLS TLS `yaml:"tls"`
}
14 changes: 6 additions & 8 deletions pkg/frontier/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ type Bypass struct {
TLS config.TLS `yaml:"tls"` // certs to dial or ca to auth
}
type Edgebound struct {
Listen config.Listen `yaml:"listen"`
Bypass Bypass `yaml:"bypass"`
Listen config.Listen `yaml:"listen"`
Bypass config.Dial `yaml:"bypass"`
BypassEnable bool `yaml:"bypass_enable"`
// alloc edgeID when no get_id function online
EdgeIDAllocWhenNoIDServiceOn bool `yaml:"edgeid_alloc_when_no_idservice_on"`
}
Expand Down Expand Up @@ -243,10 +244,7 @@ type Dao struct {

// frontlas
type Frontlas struct {
Enable bool `yaml:"enable"`
Network string `yaml:"network"`
Addr string `yaml:"addr"` // addr to dial
TLS config.TLS `yaml:"tls"` // certs to dial or ca to auth
Dial config.Dial
}

type Configuration struct {
Expand Down Expand Up @@ -386,8 +384,8 @@ func genDefaultConfig(writer io.Writer) error {
},
},
EdgeIDAllocWhenNoIDServiceOn: true,
Bypass: Bypass{
Enable: false,
BypassEnable: false,
Bypass: config.Dial{
Network: "tcp",
Addr: "192.168.1.10:8443",
TLS: config.TLS{
Expand Down
76 changes: 4 additions & 72 deletions pkg/frontier/edgebound/edge_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package edgebound

import (
"context"
"crypto/tls"
"crypto/x509"
"net"
"os"
"strings"
"sync"

Expand Down Expand Up @@ -87,7 +84,7 @@ func newEdgeManager(conf *config.Configuration, repo apis.Repo, informer apis.Ed
}

geminioLn := ln
bypass := conf.Edgebound.Bypass.Enable
bypass := conf.Edgebound.BypassEnable
if bypass {
// multiplexer
cm := cmux.New(ln)
Expand All @@ -108,76 +105,12 @@ func newEdgeManager(conf *config.Configuration, repo apis.Repo, informer apis.Ed
}

func (em *edgeManager) bypassDial(_ net.Addr, _ interface{}) (net.Conn, error) {
bypass := &em.conf.Edgebound.Bypass
var (
network string = bypass.Network
addr string = bypass.Addr
)

if !bypass.TLS.Enable {
conn, err := net.Dial(network, addr)
if err != nil {
return nil, err
}
return conn, err
} else {
// load all certs to dial
certs := []tls.Certificate{}
for _, certFile := range bypass.TLS.Certs {
cert, err := tls.LoadX509KeyPair(certFile.Cert, certFile.Key)
if err != nil {
klog.Errorf("edge manager bypass tls load x509 cert err: %s, cert: %s, key: %s", err, certFile.Cert, certFile.Key)
continue
}
certs = append(certs, cert)
}

if !bypass.TLS.MTLS {
// tls
conn, err := tls.Dial(network, addr, &tls.Config{
Certificates: certs,
// it's user's call to verify the server certs or not.
InsecureSkipVerify: bypass.TLS.InsecureSkipVerify,
})
if err != nil {
klog.Errorf("edge manager bypass tls dial err: %s, network: %s, addr: %s", err, network, addr)
return nil, err
}
return conn, nil
} else {
// mtls, dial with our certs
// load all ca certs to pool
caPool := x509.NewCertPool()
for _, caFile := range bypass.TLS.CACerts {
ca, err := os.ReadFile(caFile)
if err != nil {
klog.Errorf("edge manager bypass read ca cert err: %s, file: %s", err, caFile)
return nil, err
}
if !caPool.AppendCertsFromPEM(ca) {
klog.Warningf("edge manager bypass append ca cert to ca pool err: %s, file: %s", err, caFile)
continue
}
}
conn, err := tls.Dial(network, addr, &tls.Config{
Certificates: certs,
// we should not skip the verify.
InsecureSkipVerify: bypass.TLS.InsecureSkipVerify,
RootCAs: caPool,
})
if err != nil {
klog.Errorf("edge manager bypass tls dial err: %s, network: %s, addr: %s", err, network, addr)
return nil, err
}
return conn, nil
}
}
return utils.Dial(&em.conf.Edgebound.Bypass)
}

// Serve blocks until the Accept error
func (em *edgeManager) Serve() error {
bypass := &em.conf.Edgebound.Bypass
if bypass.Enable {
if em.conf.Edgebound.BypassEnable {
go em.cm.Serve()
go em.rp.Proxy(context.TODO())
}
Expand Down Expand Up @@ -264,8 +197,7 @@ func (em *edgeManager) DelEdgeByID(edgeID uint64) error {

// Close all edges and manager
func (em *edgeManager) Close() error {
bypass := &em.conf.Edgebound.Bypass
if bypass.Enable {
if em.conf.Edgebound.BypassEnable {
em.cm.Close()
em.rp.Close()
}
Expand Down
77 changes: 77 additions & 0 deletions pkg/utils/dial.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package utils

import (
"crypto/tls"
"crypto/x509"
"net"
"os"

"github.com/singchia/frontier/pkg/config"
"k8s.io/klog/v2"
)

func Dial(dial *config.Dial) (net.Conn, error) {
var (
network string = dial.Network
addr string = dial.Addr
)

if dial.TLS.Enable {
conn, err := net.Dial(network, addr)
if err != nil {
return nil, err
}
return conn, err
} else {
// load all certs to dial
certs := []tls.Certificate{}
for _, certFile := range dial.TLS.Certs {
cert, err := tls.LoadX509KeyPair(certFile.Cert, certFile.Key)
if err != nil {
klog.Errorf("dial, tls load x509 cert err: %s, cert: %s, key: %s", err, certFile.Cert, certFile.Key)
continue
}
certs = append(certs, cert)
}

if !dial.TLS.MTLS {
// tls
conn, err := tls.Dial(network, addr, &tls.Config{
Certificates: certs,
// it's user's call to verify the server certs or not.
InsecureSkipVerify: dial.TLS.InsecureSkipVerify,
})
if err != nil {
klog.Errorf("tls dial err: %s, network: %s, addr: %s", err, network, addr)
return nil, err
}
return conn, nil
} else {
// mtls, dial with our certs
// load all ca certs to pool
caPool := x509.NewCertPool()
for _, caFile := range dial.TLS.CACerts {
ca, err := os.ReadFile(caFile)
if err != nil {
klog.Errorf("dial read ca cert err: %s, file: %s", err, caFile)
return nil, err
}
if !caPool.AppendCertsFromPEM(ca) {
klog.Warningf("dial append ca cert to ca pool err: %s, file: %s", err, caFile)
continue
}
}
conn, err := tls.Dial(network, addr, &tls.Config{
Certificates: certs,
// we should not skip the verify.
InsecureSkipVerify: dial.TLS.InsecureSkipVerify,
RootCAs: caPool,
})
if err != nil {
klog.Errorf("dial tls dial err: %s, network: %s, addr: %s", err, network, addr)
return nil, err
}
return conn, nil
}
}
}