MQL Built-in Functions Reference
Complete reference for all MQL built-in functions
Built-in functions are the core language features that let you filter, transform, and make assertions across collections of data. They are the difference between just retrieving raw data and turning that data into meaningful answers.
Think of built-ins as the verbs of MQL:
.where()narrows down what you're looking at..map()transforms the results..all(),.any(),.none(),.one()let you assert truth over collections..length, and.containsOnlyhelp you structure and compare results.
Overview
Array Functions
| Function | Summary |
|---|---|
| all | Assert all elements satisfy predicate |
| any | Assert at least one element satisfies predicate |
| contains | Check if array contains a value or matches predicate |
| containsAll | Check that array contains all specified values |
| containsNone | Check that array contains none of the specified values |
| containsOnly | Check that array contains only specified values |
| duplicates | Return duplicate values (optionally by key) |
| first | Return the first element of an array |
| flat | Flatten nested arrays |
| in | Check if a string is in an array |
| join | Join all values in an array into a single string |
| last | Return the last element of an array |
| length | Return the number of elements in the array |
| map | Transform each element |
| none | Assert no elements satisfy predicate |
| notIn | Check if a string is not in an array |
| one | Assert exactly one element satisfies predicate |
| reverse | Reverse the order of the array |
| sample | Return a random sample of elements |
| unique | Return unique values (optionally by key) |
| where | Filter by predicate |
Map Functions
| Function | Summary |
|---|---|
| all | Assert all entries satisfy predicate |
| any | Assert at least one entry satisfies predicate |
| contains | Check if any entry matches a value or predicate |
| keys | Return the keys of the map |
| length | Number of entries in the map |
| none | Assert no entry satisfies predicate |
| one | Assert exactly one entry satisfies predicate |
| sample | Return a random sample of entries |
| values | Return the values of the map |
| where | Filter map entries by predicate |
String Functions
| Function | Summary |
|---|---|
| camelcase | Convert string to camelCase |
| contains | Check if string contains a substring or matches regex |
| downcase | Convert string to lowercase |
| find | Find all regex matches in the string |
| in | Check if string is in a list |
| inRange | Check if string represents a number within a range |
| length | Number of characters in the string |
| lines | Split string into lines |
| notIn | Check if string is not in a list |
| split | Split string by a delimiter |
| trim | Trim whitespace or specified characters |
| upcase | Convert string to uppercase |
Number Functions
| Function | Summary |
|---|---|
| inRange | Check if a number is within a range |
Time Functions
| Function | Summary |
|---|---|
| days | Extract days or convert to days |
| hours | Extract hours or convert to hours |
| inRange | Check if time is within a range |
| minutes | Extract minutes or convert to minutes |
| seconds | Extract seconds or convert to seconds |
| unix | Convert to Unix epoch seconds |
IP / Network Functions
| Function | Summary |
|---|---|
| address | Return network address of CIDR |
| cidr | Return the CIDR block |
| inRange | Check if IP/network is within another |
| isUnspecified | Check if IP is unspecified (0.0.0.0 or ::) |
| prefix | Return the network prefix |
| prefixLength | Return prefix length |
| subnet | Return subnet from CIDR |
| suffix | Return suffix (host bits) |
| version | Return IP version (4 or 6) |
Array Functions
array.all
Assert that all elements in an array satisfy the condition. Empty arrays return true.
Ensure all EC2 instances do not have a public IP address:
aws.ec2.instances.all( publicIp == empty )Example output:
aws.ec2.instances.all( publicIp == empty )
[failed] [].all()
actual: [
0: aws.ec2.instance region="us-east-1" state="running" instanceType="t2.large" instanceId="i-0684b0bfd64641234" architecture="x86_64" platformDetails="Linux/UNIX" {
publicIp: "54.144.152.249"
}
]array.any
Assert that at least one element in an array satisfies the condition. Empty arrays return false.
Check if at least one SSH service is running:
services.any(name == "sshd" && running == true)Example output:
services.any(name == "sshd" && running == true)
[ok] value: truearray.contains
Check if an array contains a specific value or if any element matches a predicate. Returns true if found.
Check if any package is named openssl:
packages.map(name).contains("openssl")Example output:
packages.map(name).contains("openssl")
[failed] [].contains()
expected: > 0
actual: 0array.containsAll
Check that an array contains all specified values. Returns true only if all are present.
Ensure both root and ec2-user exist:
users.map(name).containsAll(["root","ec2-user"])Example output:
users.map(name).containsAll(["root","ec2-user"])
[failed] [].containsAll()
expected: == []
actual: [
0: "ec2-user"
]array.containsNone
Check that an array contains none of the specified values. Returns true if no matches are found.
Ensure no insecure packages are installed:
packages.map(name).containsNone(["telnet","rsh"])Example output:
packages.map(name).containsNone(["telnet","rsh"])
[failed] [].containsNone()
expected: == []
actual: [
0: "telnet"
]array.containsOnly
Check that all elements in an array are from an allowlist. Returns true if no other values appear.
Ensure only root and shadow groups own /etc/gshadow:
["/etc/gshadow"].where(file(_).exists) {
file(_) {
group.name.lines.containsOnly(["root","shadow"])
}
}Example output:
["/etc/gshadow"].where(file(_).exists) {
} } group.name.lines.containsOnly(["root","shadow"])
where: [
0: {
file: {
[].containsOnly(): true
}
}
]array.duplicates
Return duplicate values from an array.
Find duplicate package names:
packages.map(name).duplicatesarray.first
Return the first element of an array.
Get the first package installed on the system:
packages.map(name).firstarray.flat
Flatten nested arrays into a single array.
Flatten a deeply nested array:
[[[1,2,3]]].flatarray.in
Check if a string is in an array
Is the string 'term' in the array ['a','b','c']
> 'term'.in(['a','b','c'])
falseIs the string 'term' in the array ['term']:
> 'term'.in(['term'])
truearray.join
Join all values in an array into a single string
Join the values ["a", "b", "c"] into "abc"
["a", "b", "c"].join
"abc"array.last
Return the last element of an array.
Get the last installed package:
packages.map(name).lastarray.length
Return the number of elements in an array.
Count the number of installed packages:
packages.lengtharray.map
Transform each element of an array. Returns a new array with the transformed values.
List all package names:
packages.map(name)array.none
Assert that no elements in an array satisfy the condition.
Ensure no X11-related packages are installed:
packages.none(name == /^x(org|server|11)/i)array.notIn
Check if a string is not in an array
Is the string 'term' not in the array ['a','b','c']
> 'term'.notIn(['a','b','c'])
trueIs the string 'term' not in the array ['term']:
> 'term'.notIn(['term'])
falsearray.one
Assert that exactly one element in an array satisfies the condition.
Ensure exactly one default route exists:
routes.one(destination == "0.0.0.0/0")array.reverse
Reverse the order of elements in an array.
Reverse the order of values in an array
> [1, 2, 3].reverse
[3, 2, 1]Reverse the order of period separated values using split, reverse, and join
> "123.456.789.10".split(".").reverse.join(".")
"10.789.456.123"array.sample
Return a random sample of elements from an array.
Return one random package:
packages.sample(5)Example output:
packages.sample(5)
packages.sample: [
0: package id = macos://Notes/4.12.7/arm64
1: package id = macos://Calculator/11.0/arm64
2: package id = macos://UAD Updater/11.7.0/arm64
3: package id = macos://MakePDF/10.1/arm64
4: package id = macos://AirDrop/15.5/arm64
]array.unique
Return only unique elements from an array.
List unique package names:
packages.map(name).uniquearray.where
Filter an array by a boolean condition. Returns a new array of elements that satisfy the predicate.
List all packages that start with "openssl":
packages.where(name == /^openssl/)Map Functions
map.all
Assert that all entries in the map satisfy a condition. Empty maps return true.
All net.* kernel parameters are non-zero:
kernel.parameters.where(key == /^net\./).all(value != 0)map.any
Assert that at least one entry in the map satisfies a condition. Empty maps return false.
Any kernel parameter is named kernel.kptr_restrict:
kernel.parameters.any(key == "kernel.kptr_restrict")map.contains
Check whether any entry in the map satisfies a condition. Returns true if at least one match is found.
At least one kernel parameter vm.swappiness is set to 60 or less:
kernel.parameters.contains(key == "vm.swappiness" && value <= 60)map.keys
Return the keys of a map as an array.
List kernel parameter names:
kernel.parameters.keysmap.length
Return the number of entries in a map.
Count the number of installed packages:
packages.lengthmap.none
Assert that no entries in the map satisfy a condition.
No kernel parameter named net.ipv4.ip_forward is set to 1:
kernel.parameters.where(key == "net.ipv4.ip_forward").none(value == 1)map.one
Assert that exactly one entry in the map satisfies a condition.
Exactly one label sets the environment to prod:
asset.labels.one(key == "env" && value == "prod")map.sample
Return a random sample of entries from a map. When a number is provided, returns that many entries.
Return a single random kernel parameter:
kernel.parameters.samplemap.values
Return the values of a map as an array.
List kernel parameter values:
kernel.parameters.valuesmap.where
Filter map entries by a boolean condition. Returns a new map with only the matching key/value pairs.
Kernel parameters with names starting with net.:
kernel.parameters.where(key == /^net\./)String Functions
string.camelcase
Convert a string to camelCase.
Convert a phrase to camelCase:
"hello world".camelcasestring.contains
Check if a string contains a substring or matches a regex. Returns true if found.
Check if /etc/passwd content contains root:
file("/etc/passwd").content.contains("root")string.downcase
Convert a string to lowercase.
Convert a username to lowercase:
"ROOT".downcasestring.find
Find all regex matches in a string. Returns an array of matches.
Find all numeric substrings:
"error codes: 123 456 789".find(/[0-9]+/)string.in
Check if the string is contained in a list. Returns true if found.
Check if prod is in the list of environments:
"prod".in(["dev","test","prod"])string.inrange
Check if the string (when interpreted as a number) falls within a range. Returns true if within bounds.
Check if "42" is between 10 and 100:
"42".inRange(10, 100)string.length
Return the number of characters in a string.
Count the characters in a package name:
"openssl".lengthstring.lines
Split a string into an array of lines.
Split /etc/passwd into lines:
file("/etc/passwd").content.linesstring.notin
Check if the string is not contained in a list. Returns true if not found.
Check if qa is not in the list of environments:
"qa".notIn(["dev","test","prod"])string.split
Split a string by a delimiter. Returns an array.
Split a comma-separated string:
"dev,test,prod".split(",")string.trim
Trim whitespace (or optionally specified characters) from the beginning and end of a string.
Trim spaces from a string:
" hello ".trimstring.upcase
Convert a string to uppercase.
Convert a username to uppercase:
"root".upcaseNumber Functions
number.inrange
Check if a number falls within a specified inclusive range. Returns true if within bounds.
Check if a number is between 10 and 100:
42.inRange(10, 100)Time Functions
time.days
Return the days component of a time or duration.
Get the days value of the current time:
now.daysExtract days from a parsed duration:
parse.duration("2d3h").daystime.hours
Return the hours component of a time or duration.
Get the hours value of the current time:
now.hoursExtract hours from a parsed duration:
parse.duration("1h30m15s").hourstime.inrange
Check whether a time falls between two other times (inclusive).
Check if the current time is in a maintenance window:
now.inRange(
parse.date("2006-01-02 15:04", "2025-05-01 00:00"),
parse.date("2006-01-02 15:04", "2025-05-01 06:00")
)Check if a certificate expiry date is within the next 30 days:
cert.notAfter.inRange(now, now + parse.duration("720h"))time.minutes
Return the minutes component of a time or duration.
Get the minutes value of the current time:
now.minutesExtract minutes from a parsed duration:
parse.duration("1h30m15s").minutestime.seconds
Return the seconds component of a time or duration.
Get the seconds value of the current time:
now.secondstime.unix
Convert a time into Unix epoch seconds.
Get the current Unix epoch:
now.unixConvert a parsed date into Unix epoch:
parse.date("2006-01-02", "2025-01-01").unixIP / Network Functions
ip.address
Return the network address of a CIDR.
Get the network address from a CIDR:
ip("192.168.1.10/24").addressIPv6 example:
ip("2001:db8:abcd:1::123/64").addressip.cidr
Return the CIDR block as a string.
CIDR for an IPv4 address with mask:
ip("10.0.5.7/16").cidrCIDR for an IPv6 network:
ip("2001:db8::1/48").cidrip.inrange
Check whether an IP (or network) is within another network (inclusive). Returns true if contained.
IPv4 address in a CIDR:
ip("10.0.5.7/32").inRange("10.0.0.0/16")IPv6 address in a CIDR:
ip("2001:db8::123/128").inRange("2001:db8::/48")ip.isunspecified
Return true if the IP is unspecified (0.0.0.0 for IPv4 or :: for IPv6).
Unspecified IPv4:
ip("0.0.0.0/0").isUnspecifiedUnspecified IPv6:
ip("::/0").isUnspecifiedip.prefix
Return the network prefix (network portion) of the IP.
IPv4 example:
ip("192.168.0.1").prefixAnother IPv4 example:
ip("10.0.5.123").prefixip.prefixlength
Return the prefix length of a CIDR.
Prefix length for IPv4:
ip("192.168.1.10/24").prefixLengthPrefix length for IPv6:
ip("2001:db8::1/56").prefixLengthip.subnet
Return the subnet derived from the CIDR.
IPv4 example (to be confirmed):
ip("192.168.0.1/24").subnetIPv6 example:
ip("2001:db8::123/64").subnetip.suffix
Return the host suffix (host bits) of the IP within its network.
IPv4 example:
ip("192.168.0.1").suffixip.version
Return the IP version (4 or 6).
IPv4 example:
ip("192.168.1.10/24").versionMondoo Policy Checks for Terraform (HCL, Plan, and State) with MQL
Learn how to write Mondoo MQL checks for Terraform HCL, Plan, and State. Use the cnspec shell, Mondoo Terraform provider, filters, and variants to build production-grade Policy as Code across your IaC and runtime.
MQL Providers and Resources
Complete reference of all MQL resources for querying infrastructure