Skip to content

Commit 1d52d68

Browse files
committed
fix(audit-log): remove constant agent register from audit logging (#163)
1 parent 989ebf7 commit 1d52d68

3 files changed

Lines changed: 11 additions & 29 deletions

File tree

openaev-model/src/main/java/io/openaev/database/audit/AuditLogContext.java

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.openaev.database.audit;
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import java.util.Collections;
45
import java.util.LinkedHashMap;
56
import java.util.Map;
67
import org.springframework.web.context.request.RequestAttributes;
@@ -24,17 +25,6 @@ public final class AuditLogContext {
2425
private static final String REQUEST_ATTR_CLEANUP_REGISTERED = "openaev.audit.cleanupRegistered";
2526
private static final String REQUEST_ATTR_ENABLED = "openaev.audit.enabled";
2627

27-
private static final ThreadLocal<Map<String, Map<String, Object>>> BEFORE_SNAPSHOTS_TL =
28-
ThreadLocal.withInitial(LinkedHashMap::new);
29-
30-
private static final ThreadLocal<Map<String, EntitySnapshot>> SNAPSHOTS_TL =
31-
ThreadLocal.withInitial(LinkedHashMap::new);
32-
33-
private static final ThreadLocal<Boolean> CLEANUP_REGISTERED_TL =
34-
ThreadLocal.withInitial(() -> false);
35-
36-
private static final ThreadLocal<Boolean> ENABLED_TL = ThreadLocal.withInitial(() -> true);
37-
3828
private AuditLogContext() {}
3929

4030
// -- Before snapshot --
@@ -48,9 +38,7 @@ public static void setEnabled(boolean enabled) {
4838
if (hasRequestContext()) {
4939
requestAttributes()
5040
.setAttribute(REQUEST_ATTR_ENABLED, enabled, RequestAttributes.SCOPE_REQUEST);
51-
return;
5241
}
53-
ENABLED_TL.set(enabled);
5442
}
5543

5644
/** Returns {@code true} if audit logging is enabled for the current request/thread. */
@@ -60,7 +48,7 @@ public static boolean isEnabled() {
6048
requestAttributes().getAttribute(REQUEST_ATTR_ENABLED, RequestAttributes.SCOPE_REQUEST);
6149
return val == null || Boolean.TRUE.equals(val);
6250
}
63-
return Boolean.TRUE.equals(ENABLED_TL.get());
51+
return true;
6452
}
6553

6654
public static void storeBefore(String entityId, Map<String, Object> snapshot) {
@@ -92,17 +80,15 @@ public static boolean isCleanupRegistered() {
9280
.getAttribute(REQUEST_ATTR_CLEANUP_REGISTERED, RequestAttributes.SCOPE_REQUEST);
9381
return Boolean.TRUE.equals(val);
9482
}
95-
return Boolean.TRUE.equals(CLEANUP_REGISTERED_TL.get());
83+
return true;
9684
}
9785

9886
public static void markCleanupRegistered() {
9987
if (hasRequestContext()) {
10088
requestAttributes()
10189
.setAttribute(
10290
REQUEST_ATTR_CLEANUP_REGISTERED, Boolean.TRUE, RequestAttributes.SCOPE_REQUEST);
103-
return;
10491
}
105-
CLEANUP_REGISTERED_TL.set(true);
10692
}
10793

10894
public static void clear() {
@@ -112,11 +98,6 @@ public static void clear() {
11298
attrs.removeAttribute(REQUEST_ATTR_ENTITY_SNAPSHOTS, RequestAttributes.SCOPE_REQUEST);
11399
attrs.removeAttribute(REQUEST_ATTR_CLEANUP_REGISTERED, RequestAttributes.SCOPE_REQUEST);
114100
attrs.removeAttribute(REQUEST_ATTR_ENABLED, RequestAttributes.SCOPE_REQUEST);
115-
} else {
116-
BEFORE_SNAPSHOTS_TL.remove();
117-
SNAPSHOTS_TL.remove();
118-
CLEANUP_REGISTERED_TL.remove();
119-
ENABLED_TL.remove();
120101
}
121102
}
122103

@@ -132,10 +113,6 @@ public static void clearAfterTransactionCompletion() {
132113
attrs.removeAttribute(REQUEST_ATTR_BEFORE_SNAPSHOTS, RequestAttributes.SCOPE_REQUEST);
133114
attrs.removeAttribute(REQUEST_ATTR_CLEANUP_REGISTERED, RequestAttributes.SCOPE_REQUEST);
134115
}
135-
136-
BEFORE_SNAPSHOTS_TL.remove();
137-
SNAPSHOTS_TL.remove();
138-
CLEANUP_REGISTERED_TL.remove();
139116
}
140117

141118
// -- Request/thread storage helpers --
@@ -153,7 +130,7 @@ private static Map<String, Map<String, Object>> beforeSnapshots() {
153130
attrs.setAttribute(REQUEST_ATTR_BEFORE_SNAPSHOTS, created, RequestAttributes.SCOPE_REQUEST);
154131
return created;
155132
}
156-
return BEFORE_SNAPSHOTS_TL.get();
133+
return Collections.emptyMap();
157134
}
158135

159136
@SuppressWarnings("unchecked")
@@ -169,10 +146,10 @@ private static Map<String, EntitySnapshot> snapshots() {
169146
attrs.setAttribute(REQUEST_ATTR_ENTITY_SNAPSHOTS, created, RequestAttributes.SCOPE_REQUEST);
170147
return created;
171148
}
172-
return SNAPSHOTS_TL.get();
149+
return Collections.emptyMap();
173150
}
174151

175-
private static boolean hasRequestContext() {
152+
public static boolean hasRequestContext() {
176153
return RequestContextHolder.getRequestAttributes() != null;
177154
}
178155

openaev-model/src/main/java/io/openaev/database/model/Agent.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public enum DEPLOYMENT_MODE {
6565
@JsonProperty("agent_asset")
6666
@Schema(implementation = String.class)
6767
@NotNull
68+
@AuditStateIgnore
6869
private Asset asset;
6970

7071
@Queryable(sortable = true)
@@ -145,6 +146,7 @@ public boolean isActive() {
145146
@Column(name = "agent_created_at")
146147
@JsonProperty("agent_created_at")
147148
@NotNull
149+
@AuditStateIgnore
148150
private Instant createdAt = now();
149151

150152
@AuditStateIgnore
@@ -155,6 +157,7 @@ public boolean isActive() {
155157

156158
@Column(name = "agent_cleared_at")
157159
@JsonProperty("agent_cleared_at")
160+
@AuditStateIgnore
158161
private Instant clearedAt = now();
159162

160163
@Getter(onMethod_ = @JsonIgnore)

openaev-model/src/main/java/io/openaev/database/model/Asset.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public void setTags(Set<Tag> tags) {
9494
@JsonIgnore
9595
@ManyToMany(mappedBy = "assets")
9696
@Queryable(filterable = true, dynamicValues = true, path = "assetGroups.id")
97+
@AuditStateIgnore
9798
private Set<AssetGroup> assetGroups = new HashSet<>();
9899

99100
// -- AUDIT --
@@ -102,6 +103,7 @@ public void setTags(Set<Tag> tags) {
102103
@JsonProperty("asset_created_at")
103104
@NotNull
104105
@CreationTimestamp
106+
@AuditStateIgnore
105107
private Instant createdAt = now();
106108

107109
@AuditStateIgnore

0 commit comments

Comments
 (0)