Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
WafActionOpenIntelligence.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
5using System.Xml;
6using Waher.Content;
13using Waher.Script;
14
16{
21 {
22 private readonly DurationAttribute duration;
23 private readonly StringAttribute vector;
24 private readonly StringAttribute protocol;
25 private readonly StringAttribute classification;
26 private readonly StringAttribute code;
27 private readonly StringAttribute message;
28
33 : base()
34 {
35 }
36
44 : base(Xml, Parent, Document)
45 {
46 this.duration = new DurationAttribute(Xml, "duration");
47 this.vector = new StringAttribute(Xml, "vector");
48 this.protocol = new StringAttribute(Xml, "protocol");
49 this.classification = new StringAttribute(Xml, "classification");
50 this.code = new StringAttribute(Xml, "code");
51 this.message = new StringAttribute(Xml, "message");
52 }
53
59 public async Task<GenericObject> EvaluateObject(ProcessingState State)
60 {
62 DateTime UtcNow = DateTime.UtcNow;
63 DateTime Expires = UtcNow + await this.duration.EvaluateAsync(Variables, Duration.Zero);
64
65 KeyValuePair<string, object>[] Properties = new KeyValuePair<string, object>[]
66 {
67 new KeyValuePair<string, object>("Domain", new CaseInsensitiveString(State.Request.Host)),
68 new KeyValuePair<string, object>("RemoteEndpoint", new CaseInsensitiveString(State.Request.RemoteEndPoint.RemovePortNumber())),
69 new KeyValuePair<string, object>("Timestamp", UtcNow),
70 new KeyValuePair<string, object>("Expires", Expires),
71 new KeyValuePair<string, object>("Vector", await this.vector.EvaluateAsync(Variables,string.Empty)),
72 new KeyValuePair<string, object>("Protocol", await this.protocol.EvaluateAsync(Variables,string.Empty)),
73 new KeyValuePair<string, object>("Classification", await this.classification.EvaluateAsync(Variables, string.Empty)),
74 new KeyValuePair<string, object>("Code", await this.code.EvaluateAsync(Variables, string.Empty)),
75 new KeyValuePair<string, object>("Message", await this.message.EvaluateAsync(Variables, string.Empty)),
76 new KeyValuePair<string, object>("AgentJid", new CaseInsensitiveString(State.Request.Host))
77 };
78
79 GenericObject Result = new GenericObject("OpenIntelligence",
80 "Waher.Service.IoTBroker.WebServices.Agent.Intelligence.Information",
81 Guid.NewGuid(), Properties);
82
84
85 foreach (KeyValuePair<string, object> P in await this.EvaluateTags(State))
86 {
87 Tags.Add(new GenericObject(string.Empty, string.Empty, Guid.Empty,
88 new KeyValuePair<string, object>[]
89 {
90 new KeyValuePair<string, object>("Name", P.Key),
91 new KeyValuePair<string, object>("Value", P.Value)
92 }));
93 }
94
95 Result["Tags"] = Tags.ToArray();
96
97 return Result;
98 }
99
105 public async Task<GenericObject[]> FindObjects(ProcessingState State)
106 {
108 DateTime UtcNow = DateTime.UtcNow;
109 DateTime From = UtcNow - await this.duration.EvaluateAsync(Variables, Duration.Zero);
110 StringBuilder sb = new StringBuilder();
111 string RemoteEndpoint = State.Request.RemoteEndPoint.RemovePortNumber();
112 string s;
113
114 sb.AppendLine(RemoteEndpoint);
115
117 {
118 new FilterFieldEqualTo("RemoteEndpoint", new CaseInsensitiveString(RemoteEndpoint)),
119 new FilterFieldGreaterOrEqualTo("Timestamp", From)
120 };
121
122 s = await this.vector.EvaluateAsync(Variables, string.Empty);
123 sb.AppendLine(s);
124 if (!string.IsNullOrEmpty(s))
125 Filters.Add(new FilterFieldEqualTo("Vector", s));
126
127 s = await this.protocol.EvaluateAsync(Variables, string.Empty);
128 sb.AppendLine(s);
129 if (!string.IsNullOrEmpty(s))
130 Filters.Add(new FilterFieldEqualTo("Protocol", s));
131
132 s = await this.classification.EvaluateAsync(Variables, string.Empty);
133 sb.AppendLine(s);
134 if (!string.IsNullOrEmpty(s))
135 Filters.Add(new FilterFieldEqualTo("Classification", s));
136
137 s = await this.code.EvaluateAsync(Variables, string.Empty);
138 sb.AppendLine(s);
139 if (!string.IsNullOrEmpty(s))
140 Filters.Add(new FilterFieldEqualTo("Code", s));
141
142 s = await this.message.EvaluateAsync(Variables, string.Empty);
143 sb.AppendLine(s);
144 if (!string.IsNullOrEmpty(s))
145 Filters.Add(new FilterFieldEqualTo("Message", s));
146
147 string Key = sb.ToString();
148
149 if (State.TryGetCachedObject(Key, out GenericObject[] Result))
150 return Result;
151
153 KeyValuePair<string, object>[] Tags = await this.EvaluateTags(State);
154 IEnumerable<GenericObject> Objects = await Database.Find<GenericObject>("OpenIntelligence",
155 new FilterAnd(Filters.ToArray()));
156
157 if (Tags.Length > 0)
158 {
159 foreach (GenericObject Obj in Objects)
160 {
161 if (!Obj.TryGetFieldValue("Tags", out object TagsObj) ||
162 !(TagsObj is Array TagsArray))
163 {
164 continue;
165 }
166
167 Dictionary<string, object> ObjTags = new Dictionary<string, object>();
168
169 foreach (object Item in TagsArray)
170 {
171 if (Item is GenericObject ItemObj &&
172 ItemObj.TryGetFieldValue("Name", out object NameObj) &&
173 !(NameObj is null) &&
174 ItemObj.TryGetFieldValue("Value", out object ValueObj))
175 {
176 ObjTags[NameObj.ToString()] = ValueObj;
177 }
178 }
179
180 bool Match = true;
181
182 foreach (KeyValuePair<string, object> P in Tags)
183 {
184 if (!ObjTags.TryGetValue(P.Key, out object Value))
185 {
186 Match = false;
187 break;
188 }
189
190 if (P.Value is null)
191 {
192 if (!(Value is null))
193 {
194 Match = false;
195 break;
196 }
197 }
198 else if (!P.Value.Equals(Value))
199 {
200 Match = false;
201 break;
202 }
203 }
204
205 if (!Match)
206 continue;
207
208 Results.Add(Obj);
209 }
210 }
211 else
212 Results.AddRange(Objects);
213
214 Result = Results.ToArray();
215 State.AddToCache(Key, Result, fiveMinutes);
216
217 return Result;
218 }
219 }
220}
Durationing point (Duration) attribute
Represents a case-insensitive string.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
Definition: Database.cs:238
This filter selects objects that conform to all child-filters provided.
Definition: FilterAnd.cs:10
This filter selects objects that have a named field equal to a given value.
This filter selects objects that have a named field greater or equal to a given value.
Generic object. Contains a sequence of properties.
bool TryGetFieldValue(string PropertyName, out object Value)
Gets the value of a field or property of the object, given its name.
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void AddRange(IEnumerable< T > Collection)
Adds a range of elements (last) to the list.
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
T[] ToArray()
Returns an array containing all elements of the collection.
Collection of variables.
Definition: Variables.cs:25
Abstract base class for Web Application Firewall Open Intelligence actions.
async Task< GenericObject[]> FindObjects(ProcessingState State)
Finds any Open Intelligence records matching the parameters of the action.
WafActionOpenIntelligence(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document)
Abstract base class for Web Application Firewall Open Intelligence actions.
WafActionOpenIntelligence()
Abstract base class for Web Application Firewall Open Intelligence actions.
async Task< GenericObject > EvaluateObject(ProcessingState State)
Evaluates an object reference from the attributes of the action.
Abstract base class for WAF actions that has tags.
async Task< KeyValuePair< string, object >[]> EvaluateTags(ProcessingState State)
Evaluates available tags.
Contains the current state of a review process.
Variables Variables
Set of variables.
void AddToCache(string Key, object Value)
Adds a value to the cache.
HttpRequest Request
Current HTTP Request
Abstract base class for Web Application Firewall actions.
Definition: WafAction.cs:17
WebApplicationFirewall Document
Web Application Firewall document.
Definition: WafAction.cs:60
static readonly TimeSpan fiveMinutes
Five minutes time span.
Definition: WafAction.cs:131
Web Application Firewall for HttpServer.
Represents a duration value, as defined by the xsd:duration data type: http://www....
Definition: Duration.cs:14
static readonly Duration Zero
Zero value
Definition: Duration.cs:577