public class CidrRangeOperator extends FetchOperator<CidrRangeOperator> implements IResourceBuilder
CidrRangeOperator
is an efficient and more powerful
implementation of the CidrMatchOperator
.
It works with resource tuples. Here is a basic example. Say you have a CSV resource files containing the following :
range,city,user
192.168.1.0/16,london,dimi
194.168.1.0/16,paris,fred
The CidrRangeOperator expects CSV resource files, whose one of the filed is a CIDR notation IP range.
Here is how you can check that a given IP address is included in one of the range and use them as resource tuple in your punchlet. Here is a first example:
if (cidrRange("cidrs.csv")
.columns("range",""user","city")
.cidrField("range")
.on("192.168.1.3")
.into([match]) {
// there is a match. What you have in [match] is :
// "match": {
// "city": "london",
// "range": "192.168.1.0/16",
// "user": "dimi"
// }
//
}
Note that providing the cidr field name and the column names is mandatory. If you omit one, an exception will be reported.
The cidrRange operator has the same capabilities for fetching remote resource files than the FetchCsvOperator. That is, you can write:
if (cidrRange("https://your.resource.server/cidrs.csv")
.columns("range",""user","city")
.cidrField("range")
// fetch your resource every minute
.refresh("0/59 0/1 * 1/1 * ? *")
// watch out by default your resource is not loaded at startup
// you must explicitly request that if you need to.
.loadAtStartup()
.on("192.168.1.3")
.into([match]) {
// handle the match ...
}
This code will periodically reload your remote resource file. And will use it for the CIDR match.
Check the FetchOperator class for examples.
IResourceBuilder.CompactionType
compactionType, hashKey, logger, lowerCaseKeys, requiredResource, runtimeContext, s3Bucket, s3endpoint, s3KeyPath, s3Object, s3SecretPath, silent, url, uuid
Constructor and Description |
---|
CidrRangeOperator(RuntimeContext r)
Constructor
|
CidrRangeOperator(RuntimeContext r,
String url)
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
CidrRangeOperator |
cidrField(String cidrColumn)
Set the field providing the CIDR range.
|
CidrRangeOperator |
cidrTrie()
Use the CIDR trie.
|
CidrRangeOperator |
columns(List<String> columnNames)
Provide the expected column names as a list of strings
|
CidrRangeOperator |
columns(String... columnNames)
Provide the expected column names
|
CidrRangeOperator |
compactCidrTrie()
Use the compact CIDR trie.
|
CidrRangeOperator |
delimiter(String delimiter)
Set the CSV delimiter.
|
IResourceBuilder |
getResourceBuilder()
Implemented by subclasses to return the adequate resource builder.
|
CidrRangeOperator |
intervalTree()
Use the interval tree.
|
boolean |
into(Tuple result)
Fire the operator
|
CidrRangeOperator |
on(String ip)
Set the input value you want to match.
|
CidrRangeOperator |
on(Tuple ip)
Set the input ip value you want to match.
|
Tuple |
onEnd()
Callback that tells you the scan is done.
|
void |
onStart()
Callback to tell you you are about to receive new lines.
|
void |
onStream(Reader reader)
Callback for you to stream the remote resource file.
|
bestEffort, compact, compactDirect, compactInPlace, getResource, hashKey, loadAtStartup, lowerCaseKeys, refresh, required, s3AccessKey, s3AccessKeyPath, s3Bucket, s3Endpoint, s3Object, s3Secret, silent, url
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
prepare
@Deprecated public CidrRangeOperator(RuntimeContext r, String url)
r
- the punchlet runtime context.url
- an http or file url.public CidrRangeOperator(RuntimeContext r)
r
- the punchlet runtime contextpublic CidrRangeOperator intervalTree()
public CidrRangeOperator cidrTrie()
public CidrRangeOperator compactCidrTrie()
public CidrRangeOperator cidrField(String cidrColumn)
cidrColumn
- the field to be used as CIDR valuepublic CidrRangeOperator delimiter(String delimiter)
delimiter
- a CSV delimiter. By default "," is used.public CidrRangeOperator columns(String... columnNames)
columnNames
- the column namespublic CidrRangeOperator columns(List<String> columnNames)
columnNames
- the csv column name.public CidrRangeOperator on(String ip)
ip
- the ip input value as a Stringpublic CidrRangeOperator on(Tuple ip)
ip
- the ip as a Tuple. This only works if the tuple contains a String leaf value.public boolean into(Tuple result)
result
- the holder tuple that will contain the value.public void onStart()
IResourceBuilder
onStart
in interface IResourceBuilder
public void onStream(Reader reader) throws IOException
IResourceBuilder
onStream
in interface IResourceBuilder
reader
- the input stream to consume the resource fileIOException
- in case of IO errorpublic Tuple onEnd()
IResourceBuilder
You should typically free any temporary storage, if any, used to build your resource.
onEnd
in interface IResourceBuilder
public IResourceBuilder getResourceBuilder()
FetchOperator
FetchCsvOperator
returns a builder to construct a tuple from a CSV document
while the FetchJsonOperator
returns builders to deal with JSON files.
This is called only once at init time.
getResourceBuilder
in class FetchOperator<CidrRangeOperator>
Copyright © 2023. All rights reserved.