|
| 1 | +/* |
| 2 | + * Copyright 2024-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.function.utils; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | +import java.net.URL; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.util.Collection; |
| 25 | +import java.util.Enumeration; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Set; |
| 29 | +import java.util.TreeSet; |
| 30 | + |
| 31 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 32 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 33 | +import org.apache.commons.logging.Log; |
| 34 | +import org.apache.commons.logging.LogFactory; |
| 35 | + |
| 36 | +import org.springframework.cloud.function.json.JacksonMapper; |
| 37 | +import org.springframework.cloud.function.json.JsonMapper; |
| 38 | +import org.springframework.util.ClassUtils; |
| 39 | + |
| 40 | + |
| 41 | +/** |
| 42 | + * @author Oleg Zhurakousky |
| 43 | + */ |
| 44 | +public final class JsonMasker { |
| 45 | + |
| 46 | + private static final Log logger = LogFactory.getLog(JsonMasker.class); |
| 47 | + |
| 48 | + private static JsonMasker jsonMasker; |
| 49 | + |
| 50 | + private final JacksonMapper mapper; |
| 51 | + |
| 52 | + private final Set<String> keysToMask; |
| 53 | + |
| 54 | + private JsonMasker() { |
| 55 | + this.keysToMask = loadKeys(); |
| 56 | + this.mapper = new JacksonMapper(new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT)); |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + public synchronized static JsonMasker INSTANCE() { |
| 61 | + if (jsonMasker == null) { |
| 62 | + jsonMasker = new JsonMasker(); |
| 63 | + } |
| 64 | + return jsonMasker; |
| 65 | + } |
| 66 | + |
| 67 | + public synchronized static JsonMasker INSTANCE(Set<String> keysToMask) { |
| 68 | + INSTANCE().addKeys(keysToMask); |
| 69 | + return jsonMasker; |
| 70 | + } |
| 71 | + |
| 72 | + public String[] getKeysToMask() { |
| 73 | + return keysToMask.toArray(new String[0]); |
| 74 | + } |
| 75 | + |
| 76 | + public String mask(Object json) { |
| 77 | + if (!JsonMapper.isJsonString(json)) { |
| 78 | + return (String) json; |
| 79 | + } |
| 80 | + Object map = this.mapper.fromJson(json, Object.class); |
| 81 | + return this.iterate(map); |
| 82 | + } |
| 83 | + |
| 84 | + @SuppressWarnings({ "unchecked" }) |
| 85 | + private String iterate(Object json) { |
| 86 | + if (json instanceof Collection arrayValue) { |
| 87 | + for (Object element : arrayValue) { |
| 88 | + if (element instanceof Map mapElement) { |
| 89 | + for (Map.Entry<String, Object> entry : ((Map<String, Object>) mapElement).entrySet()) { |
| 90 | + this.doMask(entry.getKey(), entry); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + else if (json instanceof Map mapElement) { |
| 96 | + for (Map.Entry<String, Object> entry : ((Map<String, Object>) mapElement).entrySet()) { |
| 97 | + this.doMask(entry.getKey(), entry); |
| 98 | + } |
| 99 | + } |
| 100 | + return new String(this.mapper.toJson(json), StandardCharsets.UTF_8); |
| 101 | + } |
| 102 | + |
| 103 | + private void doMask(String key, Map.Entry<String, Object> entry) { |
| 104 | + if (this.keysToMask.contains(key)) { |
| 105 | + entry.setValue("*******"); |
| 106 | + } |
| 107 | + else if (entry.getValue() instanceof Map) { |
| 108 | + this.iterate(entry.getValue()); |
| 109 | + } |
| 110 | + else if (entry.getValue() instanceof Collection) { |
| 111 | + this.iterate(entry.getValue()); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static Set<String> loadKeys() { |
| 116 | + Set<String> finalKeysToMask = new TreeSet<>(); |
| 117 | + try { |
| 118 | + Enumeration<URL> resources = ClassUtils.getDefaultClassLoader().getResources("META-INF/mask.keys"); |
| 119 | + while (resources.hasMoreElements()) { |
| 120 | + URI uri = resources.nextElement().toURI(); |
| 121 | + List<String> lines = Files.readAllLines(Path.of(uri)); |
| 122 | + for (String line : lines) { |
| 123 | + // need to split in case if delimited |
| 124 | + String[] keys = line.split(","); |
| 125 | + for (int i = 0; i < keys.length; i++) { |
| 126 | + finalKeysToMask.add(keys[i].trim()); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + catch (Exception e) { |
| 132 | + logger.warn("Failed to load keys to mask. No keys will be masked", e); |
| 133 | + } |
| 134 | + return finalKeysToMask; |
| 135 | + } |
| 136 | + |
| 137 | + private void addKeys(Set<String> keys) { |
| 138 | + this.keysToMask.addAll(keys); |
| 139 | + } |
| 140 | +} |
0 commit comments