Skip to content

Commit 6d86e85

Browse files
committed
Support mirroring Liberty audit events to console log
1 parent dfc6be4 commit 6d86e85

File tree

2 files changed

+75
-6
lines changed

2 files changed

+75
-6
lines changed

cmd/runmqserver/logging.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,53 @@ func formatBasic(obj map[string]interface{}) string {
100100
// Convert time zone information from some logs (e.g. Liberty) for consistency
101101
obj["ibm_datetime"] = strings.Replace(obj["ibm_datetime"].(string), "+0000", "Z", 1)
102102
// Escape any new-line characters, so that we don't get multi-line messages messing up the output
103-
obj["message"] = strings.ReplaceAll(obj["message"].(string), "\n", "\\n")
103+
if message, found := obj["message"]; found {
104+
obj["message"] = strings.ReplaceAll(message.(string), "\n", "\\n")
105+
}
106+
107+
switch obj["type"] {
108+
case "liberty_audit":
109+
timestamp := obj["ibm_datetime"]
110+
checkFields := []string{
111+
"ibm_audit_eventName",
112+
"ibm_audit_eventSequenceNumber",
113+
"ibm_audit_outcome",
114+
"ibm_audit_reason.reasonCode",
115+
"ibm_audit_reason.reasonType",
116+
"ibm_audit_observer.id",
117+
"ibm_audit_observer.name",
118+
"ibm_audit_initiator.host.address",
119+
"ibm_audit_initiator.host.agent",
120+
"ibm_audit_target.id",
121+
"ibm_audit_target.host.address",
122+
"ibm_audit_target.credential.token",
123+
"ibm_audit_target.method",
124+
"ibm_audit_target.name",
125+
"ibm_audit_target.jmx.mbean.action",
126+
"ibm_audit_target.jmx.mbean.name",
127+
"ibm_audit_target.session",
128+
"ibm_audit_eventTime",
129+
}
130+
131+
msg := strings.Builder{}
132+
msg.WriteString(timestamp.(string))
133+
msg.WriteString(" AUDIT:")
134+
for _, field := range checkFields {
135+
fieldVal, found := obj[field]
136+
if !found {
137+
continue
138+
}
139+
displayName := strings.TrimPrefix(field, "ibm_audit_")
140+
141+
msg.WriteByte(' ')
142+
msg.WriteString(displayName)
143+
msg.WriteByte('=')
144+
msg.WriteString(fmt.Sprintf("%#v", fieldVal))
145+
}
146+
msg.WriteByte('\n')
104147

105-
if obj["type"] != nil && (obj["type"] == "liberty_trace") {
148+
return msg.String()
149+
case "liberty_trace":
106150
timeStamp := obj["ibm_datetime"]
107151
threadID := ""
108152
srtModuleName := ""

cmd/runmqserver/logging_test.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,49 @@ var formatBasicTests = []struct {
2828
outContains string
2929
}{
3030
{
31-
[]byte("{\"ibm_datetime\":\"2020/06/24 00:00:00\",\"message\":\"Hello world\"}"),
31+
[]byte(`{"ibm_datetime":"2020/06/24 00:00:00","message":"Hello world"}`),
3232
"Hello",
3333
},
3434
{
35-
[]byte("{\"ibm_datetime\":\"2020/06/24 00:00:00\",\"message\":\"Hello world\", \"ibm_commentInsert1\":\"foo\"}"),
35+
[]byte(`{"ibm_datetime":"2020/06/24 00:00:00","message":"Hello world", "ibm_commentInsert1":"foo"}`),
3636
"CommentInsert1(foo)",
3737
},
3838
{
39-
[]byte("{\"ibm_datetime\":\"2020/06/24 00:00:00\",\"message\":\"Hello world\", \"ibm_arithInsert1\":1}"),
39+
[]byte(`{"ibm_datetime":"2020/06/24 00:00:00","message":"Hello world", "ibm_arithInsert1":1}`),
4040
"ArithInsert1(1)",
4141
},
42+
{
43+
[]byte(`{
44+
"ibm_audit_eventName": "SECURITY_AUTHN",
45+
"ibm_audit_eventSequenceNumber": "47",
46+
"ibm_audit_eventTime": "2025-05-29T12:08:24.056+0000",
47+
"ibm_audit_initiator.host.address": "10.254.16.2",
48+
"ibm_audit_observer.id": "websphere: cp4i-audit-test-ibm-mq-0.qm.mq-test.svc.cluster.local:/mnt/mqm/data/web/installations/Installation1/:mqweb",
49+
"ibm_audit_observer.name": "SecurityService",
50+
"ibm_audit_outcome": "success",
51+
"ibm_audit_reason.reasonCode": "200",
52+
"ibm_audit_reason.reasonType": "HTTPS",
53+
"ibm_audit_target.credential.token": "mquser1",
54+
"ibm_audit_target.host.address": "10.254.16.109:9443",
55+
"ibm_audit_target.id": "websphere: cp4i-audit-test-ibm-mq-0.qm.mq-test.svc.cluster.local:/mnt/mqm/data/web/installations/Installation1/:mqweb",
56+
"ibm_audit_target.method": "GET",
57+
"ibm_audit_target.name": "/ibmmq/console/",
58+
"ibm_audit_target.session": "Vu5GA1hXQEyFRqmYH5mWlGS",
59+
"ibm_datetime": "2025-05-29T12:08:24.057Z",
60+
"type": "liberty_audit"
61+
}`),
62+
`AUDIT: eventName="SECURITY_AUTHN" eventSequenceNumber="47" outcome="success" reason.reasonCode="200" reason.reasonType="HTTPS" observer.id="websphere: cp4i-audit-test-ibm-mq-0.qm.mq-test.svc.cluster.local:/mnt/mqm/data/web/installations/Installation1/:mqweb" observer.name="SecurityService" initiator.host.address="10.254.16.2" target.id="websphere: cp4i-audit-test-ibm-mq-0.qm.mq-test.svc.cluster.local:/mnt/mqm/data/web/installations/Installation1/:mqweb" target.host.address="10.254.16.109:9443" target.credential.token="mquser1" target.method="GET" target.name="/ibmmq/console/" target.session="Vu5GA1hXQEyFRqmYH5mWlGS" eventTime="2025-05-29T12:08:24.056+0000"`,
63+
},
4264
}
4365

4466
func TestFormatBasic(t *testing.T) {
4567
for i, table := range formatBasicTests {
4668
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
4769
var inObj map[string]interface{}
48-
json.Unmarshal(table.in, &inObj)
70+
err := json.Unmarshal(table.in, &inObj)
71+
if err != nil {
72+
t.Fatalf("Error unmarshalling: %s", err.Error())
73+
}
4974
t.Logf("Unmarshalled: %+v", inObj)
5075
out := formatBasic(inObj)
5176
if !strings.Contains(out, table.outContains) {

0 commit comments

Comments
 (0)