Skip to content

Commit

Permalink
TLS communitcation between agents and analyzer
Browse files Browse the repository at this point in the history
UDP and TLS can be used as communication socket
DTLS in golang is not ready yet, but the most promising implementation :
github.com/pixelbender/go-dtls/dtls
will be compatible (API) with go crypto/tls implementation.

Close skydive-project#121

Change-Id: I91b0d1e1374c336b0febe63f2b9ebc338f1712dc
Signed-off-by: Nicolas PLANEL <nplanel@redhat.com>
  • Loading branch information
nplanel committed Nov 17, 2016
1 parent 1dbec78 commit 55a0191
Show file tree
Hide file tree
Showing 8 changed files with 504 additions and 16 deletions.
4 changes: 2 additions & 2 deletions analyzer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Client struct {
Addr string
Port int

connection net.Conn
connection *AgentAnalyzerClientConn
}

func (c *Client) SendFlow(f *flow.Flow) error {
Expand Down Expand Up @@ -65,7 +65,7 @@ func NewClient(addr string, port int) (*Client, error) {
return nil, err
}

connection, err := net.DialUDP("udp", nil, srv)
connection, err := NewAgentAnalyzerClientConn(srv)
if err != nil {
return nil, err
}
Expand Down
244 changes: 244 additions & 0 deletions analyzer/connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

package analyzer

import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"net"
"time"

"github.com/skydive-project/skydive/common"
"github.com/skydive-project/skydive/config"
"github.com/skydive-project/skydive/logging"
)

var ErrAgentAnalyzerFakeUDPTimeout = errors.New("UDP connection fake timeout")

type AgentAnalyzerConnectionType int

const (
UDP AgentAnalyzerConnectionType = 1 + iota
TLS
)

type AgentAnalyzerServerConn struct {
mode AgentAnalyzerConnectionType
udpConn *net.UDPConn
tlsConn net.Conn
tlsListen net.Listener
}

func (a *AgentAnalyzerServerConn) Accept() (*AgentAnalyzerServerConn, error) {
if a.mode == TLS {
acceptedTLSConn, err := a.tlsListen.Accept()
if err != nil {
return nil, err
}

tlsConn, ok := acceptedTLSConn.(*tls.Conn)
if !ok {
logging.GetLogger().Fatalf("This is not a TLS connection %v", a.tlsConn)
}
err = tlsConn.Handshake()
if err != nil {
return nil, err
}
state := tlsConn.ConnectionState()
if state.HandshakeComplete == false {
logging.GetLogger().Warning("TLS Handshake is not complete")
}
return &AgentAnalyzerServerConn{
mode: TLS,
tlsConn: acceptedTLSConn,
}, nil
}
if a.mode == UDP {
return a, ErrAgentAnalyzerFakeUDPTimeout
}
return nil, errors.New("Connection mode is not set properly")
}

func (a *AgentAnalyzerServerConn) Cleanup() {
if a.mode == TLS {
a.tlsListen.Close()
}
}

func (a *AgentAnalyzerServerConn) Close() {
switch a.mode {
case TLS:
err := a.tlsConn.Close()
if err != nil {
logging.GetLogger().Errorf("Close error %v", err)
}
case UDP:
err := a.udpConn.Close()
if err != nil {
logging.GetLogger().Errorf("Close error %v", err)
}
}
}

func (a *AgentAnalyzerServerConn) SetDeadline(t time.Time) {
switch a.mode {
case TLS:
err := a.tlsConn.SetReadDeadline(t)
if err != nil {
logging.GetLogger().Errorf("SetReadDeadline %v", err)
}
case UDP:
err := a.udpConn.SetDeadline(t)
if err != nil {
logging.GetLogger().Errorf("SetDeadline %v", err)
}
}
}

func (a *AgentAnalyzerServerConn) Read(data []byte) (int, error) {
switch a.mode {
case TLS:
n, err := a.tlsConn.Read(data)
return n, err
case UDP:
n, _, err := a.udpConn.ReadFromUDP(data)
return n, err
}
return 0, errors.New("Mode didn't exist")
}

func NewAgentAnalyzerServerConn(addr *net.UDPAddr) (a *AgentAnalyzerServerConn, err error) {
a = &AgentAnalyzerServerConn{mode: UDP}
certpem := config.GetConfig().GetString("analyzer.X509_cert")
keypem := config.GetConfig().GetString("analyzer.X509_key")
clientcertpem := config.GetConfig().GetString("agent.X509_cert")

if len(certpem) > 0 && len(keypem) > 0 {
cert, err := tls.LoadX509KeyPair(certpem, keypem)
if err != nil {
logging.GetLogger().Fatalf("Can't read X509 key pair set in config : cert '%s' key '%s'", certpem, keypem)
return nil, err
}
rootPEM, err := ioutil.ReadFile(clientcertpem)
if err != nil {
logging.GetLogger().Fatalf("Failed to open root certificate '%s' : %s", certpem, err.Error())
}
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM([]byte(rootPEM))
if !ok {
logging.GetLogger().Fatal("Failed to parse root certificate " + certpem)
}
cfgTLS := &tls.Config{
ClientCAs: roots,
ClientAuth: tls.RequireAndVerifyClientCert,
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12,
PreferServerCipherSuites: true,
// CipherSuites: []uint16{tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
}
cfgTLS.BuildNameToCertificate()
a.tlsListen, err = tls.Listen("tcp", fmt.Sprintf("%s:%d", common.IPToString(addr.IP), addr.Port+1), cfgTLS)
if err != nil {
return nil, err
}
a.mode = TLS
logging.GetLogger().Info("Analyzer listen agents on TLS socket")
return a, nil
}
a.udpConn, err = net.ListenUDP("udp", addr)
logging.GetLogger().Info("Analyzer listen agents on UDP socket")
return a, err
}

type AgentAnalyzerClientConn struct {
udpConn *net.UDPConn
tlsConnClient *tls.Conn
}

func (a *AgentAnalyzerClientConn) Close() {
if a.tlsConnClient != nil {
a.tlsConnClient.Close()
return
}
a.udpConn.Close()
}

func (a *AgentAnalyzerClientConn) Write(b []byte) (int, error) {
if a.tlsConnClient != nil {
return a.tlsConnClient.Write(b)
}
return a.udpConn.Write(b)
}

func NewAgentAnalyzerClientConn(addr *net.UDPAddr) (a *AgentAnalyzerClientConn, err error) {
a = &AgentAnalyzerClientConn{}
certpem := config.GetConfig().GetString("agent.X509_cert")
keypem := config.GetConfig().GetString("agent.X509_key")
servercertpem := config.GetConfig().GetString("analyzer.X509_cert")

if len(certpem) > 0 && len(keypem) > 0 {
///////
time.Sleep(time.Second)
/////////
cert, err := tls.LoadX509KeyPair(certpem, keypem)
if err != nil {
logging.GetLogger().Fatalf("Can't read X509 key pair set in config : cert '%s' key '%s'", certpem, keypem)
return nil, err
}
rootPEM, err := ioutil.ReadFile(servercertpem)
if err != nil {
logging.GetLogger().Fatalf("Failed to open root certificate '%s' : %s", certpem, err.Error())
}
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(rootPEM)
if !ok {
logging.GetLogger().Fatal("Failed to parse root certificate " + certpem)
}
cfgTLS := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: roots,
}
cfgTLS.BuildNameToCertificate()
logging.GetLogger().Debug("TLS client connection ... Dial " + fmt.Sprintf("%s:%d", common.IPToString(addr.IP), addr.Port+1))
a.tlsConnClient, err = tls.Dial("tcp", fmt.Sprintf("%s:%d", common.IPToString(addr.IP), addr.Port+1), cfgTLS)
if err != nil {
logging.GetLogger().Errorf("TLS error %s %v %v %s", fmt.Sprintf("%s:%d", common.IPToString(addr.IP), addr.Port+1), cfgTLS, err, err.Error())
return nil, err
}
state := a.tlsConnClient.ConnectionState()
if state.HandshakeComplete == false {
logging.GetLogger().Warning("TLS Handshake is not complete")
}
logging.GetLogger().Debug("TLS Handshake is complete")
return a, nil
}
a.udpConn, err = net.DialUDP("udp", nil, addr)
if err != nil {
return nil, err
}
logging.GetLogger().Debug("UDP client dialup done")
return a, nil
}
42 changes: 35 additions & 7 deletions analyzer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ type Server struct {
Storage storage.Storage
FlowTable *flow.Table
TableClient *flow.TableClient
conn *net.UDPConn
conn *AgentAnalyzerServerConn
EmbeddedEtcd *etcd.EmbeddedEtcd
EtcdClient *etcd.EtcdClient
running atomic.Value
wgServers sync.WaitGroup
wgFlowsHandlers sync.WaitGroup
}

func (s *Server) flowExpireUpdate(flows []*flow.Flow) {
Expand All @@ -76,15 +77,19 @@ func (s *Server) AnalyzeFlows(flows []*flow.Flow) {
logging.GetLogger().Debugf("%d flows received", len(flows))
}

func (s *Server) handleUDPFlowPacket() {
s.conn.SetDeadline(time.Now().Add(200 * time.Millisecond))
/* Conn must be local, as it can be a TCP socket connection */
func (s *Server) handleUDPFlowPacket(conn *AgentAnalyzerServerConn) {
defer s.wgFlowsHandlers.Done()
defer conn.Close()

conn.SetDeadline(time.Now().Add(200 * time.Millisecond))
data := make([]byte, 4096)

for s.running.Load() == true {
n, _, err := s.conn.ReadFromUDP(data)
n, err := conn.Read(data)
if err != nil {
if err.(net.Error).Timeout() == true {
s.conn.SetDeadline(time.Now().Add(200 * time.Millisecond))
conn.SetDeadline(time.Now().Add(200 * time.Millisecond))
continue
}
if s.running.Load() == false {
Expand All @@ -101,6 +106,7 @@ func (s *Server) handleUDPFlowPacket() {

s.AnalyzeFlows([]*flow.Flow{f})
}
logging.GetLogger().Critical("out of loop")
}

func (s *Server) ListenAndServe() {
Expand Down Expand Up @@ -128,13 +134,34 @@ func (s *Server) ListenAndServe() {

host := s.HTTPServer.Addr + ":" + strconv.FormatInt(int64(s.HTTPServer.Port), 10)
addr, err := net.ResolveUDPAddr("udp", host)
s.conn, err = net.ListenUDP("udp", addr)
s.conn, err = NewAgentAnalyzerServerConn(addr)
if err != nil {
panic(err)
}
defer s.conn.Close()

s.handleUDPFlowPacket()
for s.running.Load() == true {
conn, err := s.conn.Accept()
if s.running.Load() == false {
break
}
if err != nil && err != ErrAgentAnalyzerFakeUDPTimeout {
logging.GetLogger().Errorf("Accept error : %s", err.Error())
time.Sleep(200 * time.Millisecond)
continue
}

s.wgFlowsHandlers.Add(1)
go s.handleUDPFlowPacket(conn)

if err == ErrAgentAnalyzerFakeUDPTimeout {
/* We using an UDP socket, we can't have multiple connction handler */
break
}
}
logging.GetLogger().Debug("wait for opened connection ...")
s.wgFlowsHandlers.Wait()
logging.GetLogger().Debug("wait for opened connection done")
}()

s.FlowTable.Start()
Expand All @@ -153,6 +180,7 @@ func (s *Server) Stop() {
}
s.AlertServer.AlertManager.Stop()
s.EtcdClient.Stop()
s.conn.Cleanup()
s.wgServers.Wait()
if tr, ok := http.DefaultTransport.(interface {
CloseIdleConnections()
Expand Down
8 changes: 8 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"strings"
"time"
Expand Down Expand Up @@ -255,3 +256,10 @@ func IPv6Supported() bool {

return true
}

func IPToString(ip net.IP) string {
/* if len(ip) == 16 {
return "[" + ip.String() + "]"
}
*/return ip.String()
}
7 changes: 3 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (

"github.com/spf13/viper"
_ "github.com/spf13/viper/remote"

"github.com/skydive-project/skydive/common"
)

var cfg *viper.Viper
Expand Down Expand Up @@ -182,10 +184,7 @@ func validateIPPort(addressPort string) (string, int, error) {

addr := "127.0.0.1"
if IPaddr != nil {
addr = IPaddr.String()
if len(IPaddr) == 16 {
addr = "[" + IPaddr.String() + "]"
}
addr = common.IPToString(IPaddr)
}
return addr, port, nil
}
Expand Down
Loading

0 comments on commit 55a0191

Please sign in to comment.