Browse code

Syslog Driver: RFC 5425 Framing should be used only when protocol is TCP+TLS

Signed-off-by: Ferran Rodenas <frodenas@gmail.com>

Ferran Rodenas authored on 2016/06/30 00:44:00
Showing 2 changed files
... ...
@@ -105,7 +105,7 @@ func New(ctx logger.Context) (logger.Logger, error) {
105 105
 		return nil, err
106 106
 	}
107 107
 
108
-	syslogFormatter, syslogFramer, err := parseLogFormat(ctx.Config["syslog-format"])
108
+	syslogFormatter, syslogFramer, err := parseLogFormat(ctx.Config["syslog-format"], proto)
109 109
 	if err != nil {
110 110
 		return nil, err
111 111
 	}
... ...
@@ -205,7 +205,7 @@ func ValidateLogOpt(cfg map[string]string) error {
205 205
 	if _, err := parseFacility(cfg["syslog-facility"]); err != nil {
206 206
 		return err
207 207
 	}
208
-	if _, _, err := parseLogFormat(cfg["syslog-format"]); err != nil {
208
+	if _, _, err := parseLogFormat(cfg["syslog-format"], ""); err != nil {
209 209
 		return err
210 210
 	}
211 211
 	return nil
... ...
@@ -241,16 +241,22 @@ func parseTLSConfig(cfg map[string]string) (*tls.Config, error) {
241 241
 	return tlsconfig.Client(opts)
242 242
 }
243 243
 
244
-func parseLogFormat(logFormat string) (syslog.Formatter, syslog.Framer, error) {
244
+func parseLogFormat(logFormat, proto string) (syslog.Formatter, syslog.Framer, error) {
245 245
 	switch logFormat {
246 246
 	case "":
247 247
 		return syslog.UnixFormatter, syslog.DefaultFramer, nil
248 248
 	case "rfc3164":
249 249
 		return syslog.RFC3164Formatter, syslog.DefaultFramer, nil
250 250
 	case "rfc5424":
251
-		return rfc5424formatterWithAppNameAsTag, syslog.RFC5425MessageLengthFramer, nil
251
+		if proto == secureProto {
252
+			return rfc5424formatterWithAppNameAsTag, syslog.RFC5425MessageLengthFramer, nil
253
+		}
254
+		return rfc5424formatterWithAppNameAsTag, syslog.DefaultFramer, nil
252 255
 	case "rfc5424micro":
253
-		return rfc5424microformatterWithAppNameAsTag, syslog.RFC5425MessageLengthFramer, nil
256
+		if proto == secureProto {
257
+			return rfc5424microformatterWithAppNameAsTag, syslog.RFC5425MessageLengthFramer, nil
258
+		}
259
+		return rfc5424microformatterWithAppNameAsTag, syslog.DefaultFramer, nil
254 260
 	default:
255 261
 		return nil, nil, errors.New("Invalid syslog format")
256 262
 	}
... ...
@@ -13,31 +13,43 @@ func functionMatches(expectedFun interface{}, actualFun interface{}) bool {
13 13
 }
14 14
 
15 15
 func TestParseLogFormat(t *testing.T) {
16
-	formatter, framer, err := parseLogFormat("rfc5424")
16
+	formatter, framer, err := parseLogFormat("rfc5424", "udp")
17
+	if err != nil || !functionMatches(rfc5424formatterWithAppNameAsTag, formatter) ||
18
+		!functionMatches(syslog.DefaultFramer, framer) {
19
+		t.Fatal("Failed to parse rfc5424 format", err, formatter, framer)
20
+	}
21
+
22
+	formatter, framer, err = parseLogFormat("rfc5424", "tcp+tls")
17 23
 	if err != nil || !functionMatches(rfc5424formatterWithAppNameAsTag, formatter) ||
18 24
 		!functionMatches(syslog.RFC5425MessageLengthFramer, framer) {
19 25
 		t.Fatal("Failed to parse rfc5424 format", err, formatter, framer)
20 26
 	}
21 27
 
22
-	formatter, framer, err = parseLogFormat("rfc5424micro")
28
+	formatter, framer, err = parseLogFormat("rfc5424micro", "udp")
29
+	if err != nil || !functionMatches(rfc5424microformatterWithAppNameAsTag, formatter) ||
30
+		!functionMatches(syslog.DefaultFramer, framer) {
31
+		t.Fatal("Failed to parse rfc5424 (microsecond) format", err, formatter, framer)
32
+	}
33
+
34
+	formatter, framer, err = parseLogFormat("rfc5424micro", "tcp+tls")
23 35
 	if err != nil || !functionMatches(rfc5424microformatterWithAppNameAsTag, formatter) ||
24 36
 		!functionMatches(syslog.RFC5425MessageLengthFramer, framer) {
25 37
 		t.Fatal("Failed to parse rfc5424 (microsecond) format", err, formatter, framer)
26 38
 	}
27 39
 
28
-	formatter, framer, err = parseLogFormat("rfc3164")
40
+	formatter, framer, err = parseLogFormat("rfc3164", "")
29 41
 	if err != nil || !functionMatches(syslog.RFC3164Formatter, formatter) ||
30 42
 		!functionMatches(syslog.DefaultFramer, framer) {
31 43
 		t.Fatal("Failed to parse rfc3164 format", err, formatter, framer)
32 44
 	}
33 45
 
34
-	formatter, framer, err = parseLogFormat("")
46
+	formatter, framer, err = parseLogFormat("", "")
35 47
 	if err != nil || !functionMatches(syslog.UnixFormatter, formatter) ||
36 48
 		!functionMatches(syslog.DefaultFramer, framer) {
37 49
 		t.Fatal("Failed to parse empty format", err, formatter, framer)
38 50
 	}
39 51
 
40
-	formatter, framer, err = parseLogFormat("invalid")
52
+	formatter, framer, err = parseLogFormat("invalid", "")
41 53
 	if err == nil {
42 54
 		t.Fatal("Failed to parse invalid format", err, formatter, framer)
43 55
 	}