|
| 1 | +/* |
| 2 | + * jPOS Project [http://jpos.org] |
| 3 | + * Copyright (C) 2000-2026 jPOS Software SRL |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as |
| 7 | + * published by the Free Software Foundation, either version 3 of the |
| 8 | + * License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.jpos.util; |
| 20 | + |
| 21 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 22 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 23 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 24 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 25 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| 26 | +import org.jpos.core.Configurable; |
| 27 | +import org.jpos.core.Configuration; |
| 28 | +import org.jpos.core.ConfigurationException; |
| 29 | +import org.jpos.iso.ISOMsg; |
| 30 | +import org.jpos.iso.ISOUtil; |
| 31 | +import org.jpos.log.AuditLogEvent; |
| 32 | +import org.jpos.log.evt.LogEvt; |
| 33 | +import org.jpos.log.evt.LogMessage; |
| 34 | +import org.jpos.log.evt.ThrowableAuditLogEvent; |
| 35 | +import org.jpos.log.render.ThrowableSerializer; |
| 36 | + |
| 37 | +import java.io.ByteArrayOutputStream; |
| 38 | +import java.io.PrintStream; |
| 39 | +import java.net.InetAddress; |
| 40 | +import java.time.Duration; |
| 41 | +import java.util.*; |
| 42 | + |
| 43 | +/** |
| 44 | + * JSONL (one JSON object per line) LogEventWriter with built-in PCI protection. |
| 45 | + * |
| 46 | + * <p>When serializing ISOMsg objects in LogEvent payloads, sensitive fields are |
| 47 | + * masked or wiped inline — no upstream {@link ProtectedLogListener} required.</p> |
| 48 | + * |
| 49 | + * <p>Configuration properties (same convention as {@link ProtectedLogListener}):</p> |
| 50 | + * <ul> |
| 51 | + * <li>{@code protect} — space-separated field numbers to mask via {@link ISOUtil#protect(String)} (default: {@code "2"})</li> |
| 52 | + * <li>{@code wipe} — space-separated field numbers to replace with [WIPED] (default: {@code "35 45 48 52 55"})</li> |
| 53 | + * </ul> |
| 54 | + * |
| 55 | + * <p>Output is suitable for {@code jq}, Filebeat, and Elasticsearch ingestion.</p> |
| 56 | + * |
| 57 | + * @since 3.0.0 |
| 58 | + */ |
| 59 | +public class JsonlLogWriter extends BaseLogEventWriter implements Configurable { |
| 60 | + private static final String WIPED = "[WIPED]"; |
| 61 | + private static final Set<Integer> DEFAULT_SAFE_FIELDS = Set.of( |
| 62 | + 3, 4, 7, 11, 12, 13, 18, 22, 24, 25, 32, 37, 38, 39, 41, 42, 49, 90 |
| 63 | + ); |
| 64 | + |
| 65 | + private ObjectMapper mapper; |
| 66 | + private String host; |
| 67 | + private Set<Integer> protectFields = Set.of(2); |
| 68 | + private Set<Integer> wipeFields = Set.of(35, 45, 48, 52, 55); |
| 69 | + |
| 70 | + public JsonlLogWriter() { |
| 71 | + initMapper(); |
| 72 | + try { |
| 73 | + host = InetAddress.getLocalHost().getHostName(); |
| 74 | + } catch (Exception e) { |
| 75 | + host = "unknown"; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void setConfiguration(Configuration cfg) throws ConfigurationException { |
| 81 | + String protect = cfg.get("protect", null); |
| 82 | + if (protect != null) { |
| 83 | + protectFields = toIntSet(protect); |
| 84 | + } |
| 85 | + String wipe = cfg.get("wipe", null); |
| 86 | + if (wipe != null) { |
| 87 | + wipeFields = toIntSet(wipe); |
| 88 | + } |
| 89 | + initMapper(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void write(LogEvent ev) { |
| 94 | + if (p == null || ev == null) |
| 95 | + return; |
| 96 | + try { |
| 97 | + List<AuditLogEvent> events; |
| 98 | + synchronized (ev.getPayLoad()) { |
| 99 | + events = ev.getPayLoad() |
| 100 | + .stream() |
| 101 | + .map(this::toAuditLogEvent) |
| 102 | + .toList(); |
| 103 | + } |
| 104 | + long elapsed = Duration.between(ev.getCreatedAt(), ev.getDumpedAt()).toMillis(); |
| 105 | + LogEvt logEvt = new LogEvt( |
| 106 | + ev.getDumpedAt(), |
| 107 | + ev.getTag(), |
| 108 | + elapsed == 0L ? null : elapsed, |
| 109 | + buildTags(ev), |
| 110 | + events |
| 111 | + ); |
| 112 | + p.println(mapper.writeValueAsString(logEvt)); |
| 113 | + p.flush(); |
| 114 | + } catch (JsonProcessingException e) { |
| 115 | + p.println("{\"error\":\"" + e.getMessage().replace("\"", "'") + "\"}"); |
| 116 | + p.flush(); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private AuditLogEvent toAuditLogEvent(Object obj) { |
| 121 | + return switch (obj) { |
| 122 | + case AuditLogEvent ale -> ale; |
| 123 | + case ISOMsg m -> new LogMessage(protectAndDump(m)); |
| 124 | + case Throwable t -> new ThrowableAuditLogEvent(t); |
| 125 | + default -> new LogMessage(dump(obj)); |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + private Map<String,String> buildTags(LogEvent ev) { |
| 130 | + ev.getTraceId(); // ensure trace-id is generated |
| 131 | + Map<String,String> tags = new LinkedHashMap<>(); |
| 132 | + String realm = ev.getRealm(); |
| 133 | + if (realm != null && !realm.isEmpty()) |
| 134 | + tags.put("realm", realm); |
| 135 | + if (host != null) |
| 136 | + tags.put("host", host); |
| 137 | + tags.putAll(ev.getTags()); |
| 138 | + return tags; |
| 139 | + } |
| 140 | + |
| 141 | + private String protectAndDump(ISOMsg original) { |
| 142 | + ISOMsg m = (ISOMsg) original.clone(); |
| 143 | + for (int field : protectFields) { |
| 144 | + String v = m.getString(field); |
| 145 | + if (v != null) { |
| 146 | + m.set(field, ISOUtil.protect(v)); |
| 147 | + } |
| 148 | + } |
| 149 | + for (int field : wipeFields) { |
| 150 | + if (m.hasField(field)) { |
| 151 | + m.set(field, WIPED); |
| 152 | + } |
| 153 | + } |
| 154 | + return dump(m); |
| 155 | + } |
| 156 | + |
| 157 | + private String dump(Object obj) { |
| 158 | + if (obj instanceof Loggeable loggeable) { |
| 159 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 160 | + PrintStream ps = new PrintStream(baos); |
| 161 | + loggeable.dump(ps, ""); |
| 162 | + return baos.toString().trim(); |
| 163 | + } |
| 164 | + return obj.toString(); |
| 165 | + } |
| 166 | + |
| 167 | + private void initMapper() { |
| 168 | + mapper = new ObjectMapper(); |
| 169 | + mapper.registerModule(new JavaTimeModule()); |
| 170 | + mapper.disable(com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); |
| 171 | + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); |
| 172 | + SimpleModule module = new SimpleModule(); |
| 173 | + module.addSerializer(Throwable.class, new ThrowableSerializer()); |
| 174 | + mapper.registerModule(module); |
| 175 | + } |
| 176 | + |
| 177 | + private static Set<Integer> toIntSet(String spaceSeparated) { |
| 178 | + if (spaceSeparated == null || spaceSeparated.isBlank()) |
| 179 | + return Set.of(); |
| 180 | + Set<Integer> result = new HashSet<>(); |
| 181 | + for (String token : spaceSeparated.trim().split("\\s+")) { |
| 182 | + result.add(Integer.parseInt(token)); |
| 183 | + } |
| 184 | + return Collections.unmodifiableSet(result); |
| 185 | + } |
| 186 | +} |
0 commit comments