-
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.
- Loading branch information
Showing
6 changed files
with
174 additions
and
56 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package syslogsidecar | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/g41797/go-syslog/format" | ||
) | ||
|
||
func Test_PackUnpackBadlyFormatted(t *testing.T) { | ||
|
||
in := map[string]string{ | ||
FormerMessage: "FormerMessage", | ||
} | ||
|
||
logparts, err := toLogParts(in, formerMessage[:]) | ||
if err != nil { | ||
t.Fatalf("toLogParts error %v", err) | ||
} | ||
|
||
msgparts := new(syslogmsgparts) | ||
|
||
err = msgparts.pack(logparts, fmt.Errorf("bad formatted message")) | ||
if err != nil { | ||
t.Errorf("pack error %v", err) | ||
} | ||
|
||
hlp := newpuhelper() | ||
|
||
err = msgparts.Unpack(hlp.put) | ||
if err != nil { | ||
t.Errorf("Unpack error %v", err) | ||
} | ||
|
||
if !reflect.DeepEqual(in, hlp.slmparts) { | ||
t.Errorf("Expected %v Actual %v", in, hlp.slmparts) | ||
} | ||
} | ||
|
||
type puhelper struct { | ||
slmparts map[string]string | ||
} | ||
|
||
func newpuhelper() puhelper { | ||
var result puhelper | ||
result.slmparts = make(map[string]string) | ||
return result | ||
} | ||
|
||
func (hlp *puhelper) put(name, value string) error { | ||
if hlp.slmparts == nil { | ||
return fmt.Errorf("nil slmparts") | ||
} | ||
|
||
if _, present := hlp.slmparts[name]; present { | ||
return fmt.Errorf("%s already exists", name) | ||
} | ||
|
||
hlp.slmparts[name] = value | ||
return nil | ||
} | ||
|
||
func toLogParts(in map[string]string, parts []partType) (format.LogParts, error) { | ||
if len(in) == 0 { | ||
return nil, fmt.Errorf("empty in") | ||
} | ||
|
||
logParts := make(format.LogParts) | ||
|
||
for _, part := range parts { | ||
val, exists := in[part.name] | ||
if !exists { | ||
return nil, fmt.Errorf("%s does not exist", part.name) | ||
} | ||
logParts[part.name] = toValue(val, part.kind) | ||
} | ||
|
||
return logParts, nil | ||
} | ||
|
||
func toValue(str string, typ string) any { | ||
|
||
switch typ { | ||
case "string": | ||
return str | ||
case "int": | ||
result, _ := strconv.Atoi(str) | ||
return result | ||
case "time.Time": | ||
result, _ := time.Parse(time.RFC3339, str) | ||
return result | ||
} | ||
|
||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package syslogsidecar | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"os" | ||
) | ||
|
||
func prepareTLS(CLIENT_CERT_PATH, CLIENT_KEY_PATH, ROOT_CA_PATH string) (*tls.Config, error) { | ||
|
||
if CLIENT_CERT_PATH == "" || CLIENT_KEY_PATH != "" || ROOT_CA_PATH != "" { | ||
return nil, nil | ||
} | ||
|
||
cert, err := tls.LoadX509KeyPair(CLIENT_CERT_PATH, CLIENT_KEY_PATH) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cert.Leaf, err = x509.ParseCertificate(cert.Certificate[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
TLSConfig := &tls.Config{MinVersion: tls.VersionTLS12} | ||
TLSConfig.Certificates = []tls.Certificate{cert} | ||
certs := x509.NewCertPool() | ||
|
||
pemData, err := os.ReadFile(ROOT_CA_PATH) | ||
if err != nil { | ||
return nil, err | ||
} | ||
certs.AppendCertsFromPEM(pemData) | ||
TLSConfig.RootCAs = certs | ||
|
||
return TLSConfig, nil | ||
} |