forked from skydive-project/skydive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TLS communitcation between agents and analyzer
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
Showing
8 changed files
with
483 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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,230 @@ | ||
/* | ||
* 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) { | ||
var err error | ||
if a.mode == TLS { | ||
a.tlsConn, err = a.tlsListen.Accept() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
tlsConn, ok := a.tlsConn.(*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: a.tlsConn, | ||
}, nil | ||
} | ||
if a.mode == UDP { | ||
return a, ErrAgentAnalyzerFakeUDPTimeout | ||
} | ||
return nil, errors.New("Connection mode is not set properly") | ||
} | ||
|
||
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") | ||
|
||
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(certpem) | ||
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{ | ||
RootCAs: roots, | ||
ClientAuth: tls.RequireAndVerifyClientCert, | ||
Certificates: []tls.Certificate{cert}, | ||
} | ||
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") | ||
|
||
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(certpem) | ||
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{ | ||
Certificates: []tls.Certificate{cert}, | ||
RootCAs: roots, | ||
} | ||
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 | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.