How to add parameters to CFLint rule - json

I want to set specific parameters for the built-in CFLint rules using CFLint 1.2.3. Unfortunately, there is currently no clear description how to do that.
So I tried to set them in different ways within the configuration having a look at the project test files and the provided JSON schema:
As defined in one of the test files:
{
"rule" : [
{
"name": "VariableNameChecker",
"className": "VariableNameChecker",
"message": [
{
"code": "VAR_TOO_SHORT",
"severity": "INFO",
"messageText": "Variable ${variable} SHORTER THAN ${MinLength}!"
}
],
"parameter": [
{
"name": "MinLength",
"value": "5"
}
]
}
],
"inheritParent" : true
}
Within the rule object:
{
"rule": [ ],
"excludes": [ ],
"includes": [
{
"code": "VAR_TOO_SHORT",
{
"parameter": {
"MinLength": "5"
}
}
}
],
"inheritParent": false
}
As separate global property:
{
"rule": [ ],
"excludes": [ ],
"includes": [
{
"code": "VAR_TOO_SHORT",
}
],
"parameter": {
"MinLength": "5"
}
"inheritParent": false
}
I also tried different naming conventions as parameter name like VariableNameChecker.MinLength and also writing parameters instead of parameter, though without luck.
What is the correct syntax to specify the parameters?

The only ways to override a plugin param prior to CFLint 1.3.0 are
(1) replace the cflint.definition.json file with your own
(2) set a system property in the form ClassName DOT parameter. for example:
java -DVariableNameChecker.MinLength=5 cflint-1.2.3-all.jar -file
In CFLint 1.3.0 the following will work:
{
"parameters" : {
"VariableNameChecker.MinLength": "5"
}
}

Related

jmespath :select json object element based on other (array) element in the object

I have this JSON
{
"srv_config": [{
"name": "db1",
"servers": ["srv1", "srv2"],
"prop": [{"source":"aa"},"destination":"bb"},{"source":"cc"},"destination":"cc"},]
}, {
"name": "db2",
"servers": ["srv2", "srv2"],
"prop": [{"source":"dd"},"destination":"dd"},{"source":"ee"},"destination":"ee"},]
}
]
}
I try to build a JMESPath expression to select the prop application in each object in the main array, but based on the existence of a string in the servers element.
To select all props, I can do:
*.props [*]
But how do I add condition that says "select only if srv1 is in servers list"?
You can use the contains function in order to filter based on a array containing something.
Given the query:
*[?contains(servers, `srv1`)].prop | [][]
This gives us:
[
{
"source": "aa",
"destination": "bb"
},
{
"source": "cc",
"destination": "cc"
}
]
Please mind that I am also using a bit of flattening here.
All this run towards a corrected version of you JSON:
{
"srv_config":[
{
"name":"db1",
"servers":[
"srv1",
"srv2"
],
"prop":[
{
"source":"aa",
"destination":"bb"
},
{
"source":"cc",
"destination":"cc"
}
]
},
{
"name":"db2",
"servers":[
"srv2",
"srv2"
],
"prop":[
{
"source":"dd",
"destination":"dd"
},
{
"source":"ee",
"destination":"ee"
}
]
}
]
}

JSON Schema - Allow only specific enum values for a property and reject the rest

Say I have the following JSON that I'd like validated.
[
{
"UpStatus":"Closed"
},
{
"UpStatus":"Open"
}
]
I want the json to pass validation only if there is at least one 'UpStatus' in the array defined to either 'Open' or 'Locked'.
If 'UpStatus' is not found as set to 'Open' or 'Locked' in the array, and is set to something else that is arbitrary say "Closed", I want the validation to fail.
I tinkered around with anyOf and came up with the following schema.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": [
{
"type": "object",
"properties": {
"UpStatus": {
"type": "string"
}
},
"minItems": 1,
"anyOf": [
{
"properties": {
"UpStatus": {
"const": "Open"
}
},
"required": [
"UpStatus"
]
},
{
"properties": {
"UpStatus": {
"const": "Locked"
}
},
"required": [
"UpStatus"
]
}
]
}
]
}
The above does not work correctly as it allows the following to pass which I thought it should fail to validate.
[
{
"UpStatus": "Closed"
},
{
"UpStatus": "Closed"
}
]
I played with the json schema for a long time and looked at examples and read some docs but could not get it to work. Any help is appreciated. Thank you.
In your schema above, you put the "minItems" keyword inside "items", which does nothing -- it needs to be adjacent to "items". But using "items" also means that all items must match, not just one.
Instead, use "contains":
{
"type: "array",
"contains": {
"type": "object",
"required": ["UpStatus"],
"properties": {
"UpStatus": {
"enum": ["Open","Locked"],
}
}
}
}
Translation: the data must be an array, where at least one element must be an object, which has the property "UpStatus" with value either "Open" or "Locked".
You may want all items in the array to conform to something specific, in which case you use "items" to specify that. The difference between "items" and "contains" is that the "items" schema must match all items, whereas the "contains" schema only has to match one.
HOWEVER, "contains" is not available in the draft 4 version of the spec. Is there any chance you can upgrade? There is a list of implementations in various languages here. Alternatively, you can simulate the "contains" keyword with "not": { "items": { "not": { ... schema ... } } } (courtesy Jason Desrosiers).
addendum: When I evaluate your schema and data, it does not pass, but rather produces these errors, so perhaps your implementation is buggy (or you mispasted something):
{
"errors" : [
{
"error" : "value does not match",
"instanceLocation" : "/0/UpStatus",
"keywordLocation" : "/items/0/anyOf/0/properties/UpStatus/const"
},
{
"error" : "not all properties are valid",
"instanceLocation" : "/0",
"keywordLocation" : "/items/0/anyOf/0/properties"
},
{
"error" : "value does not match",
"instanceLocation" : "/0/UpStatus",
"keywordLocation" : "/items/0/anyOf/1/properties/UpStatus/const"
},
{
"error" : "not all properties are valid",
"instanceLocation" : "/0",
"keywordLocation" : "/items/0/anyOf/1/properties"
},
{
"error" : "no subschemas are valid",
"instanceLocation" : "/0",
"keywordLocation" : "/items/0/anyOf"
},
{
"error" : "not all items are valid",
"instanceLocation" : "",
"keywordLocation" : "/items"
}
],
"valid" : false
}

How can i insert object which got arrays into arrays using jq?

I may not discribed it properly, I tried many times and looked up the manaual on jq Manaual , and i got no idea how to insert object which contains array into a json file by jq command, anyway, here is the origin test.json:
[
{
"gate": [
{
"pro1": "1"
}
],
"home": [
{
"mem1": "1"
}
],
"holder": "1"
}
]
And i wish to be like this after insert:
[
{
"gate": [
{
"pro1": "1"
}
],
"home": [
{
"mem1": "1"
}
],
"holder": "1"
},
{
"gate": [
{
"pro1": "2"
}
],
"home": [
{
"mem1": "2"
}
],
"holder": "1"
}
]
Could it possibly done by jq?
Since you haven't provided more detail I am going to assume that you simply want to append a hard-coded object to an existing array. (If that's not what you mean you'll need to be more precise in your question.)
You can add items to the end of a list with the + operator. So in your case:
jq '.+[{"gate":[{"pro1": "2"}], "home":[{"mem1": "2"}],"holder": "1"}]' input.json
[
{
"gate": [
{
"pro1": "1"
}
],
"home": [
{
"mem1": "1"
}
],
"holder": "1"
},
{
"gate": [
{
"pro1": "2"
}
],
"home": [
{
"mem1": "2"
}
],
"holder": "1"
}
]
The . takes the existing input, which is an array. The +[ {...} ] concatenates it with another array of one object. If you want to put the new item(s) at the start instead of the end, swap it round: [ {...} ]+.

Geeting error in adding ssl certificate in cloudfront using cloudformation (needs to be specified)

{
"AWSTemplateFormatVersion" : "2010-09-09",
"Parameters": {
"AlternateDomainNames": {
"Description": "CNAMEs (alternate domain names), if any, for the distribution. Example. test.codavel.com",
"Type": "String",
"Default": "test.example.com"
}
},
"Resources" : {
"myDistribution" : {
"Type" : "AWS::CloudFront::Distribution",
"Properties" : {
"DistributionConfig" : {
"Origins" : [ {
"DomainName" : "ELBfor-1234.region.elb.amazonaws.com",
"Id" : "myCustomOrigin",
"CustomOriginConfig" : {
"HTTPPort" : "80",
"HTTPSPort" : "443",
"OriginProtocolPolicy" : "match-viewer",
"OriginSSLProtocols" : [
"TLSv1",
"TLSv1.1",
"TLSv1.2",
"SSLv3"
]
}
} ],
"HttpVersion": "http2",
"Aliases": [
{
"Ref": "AlternateDomainNames"
}
],
"Enabled" : "true",
"Comment" : "example-cdn",
"DefaultCacheBehavior" : {
"TargetOriginId" : "myCustomOrigin",
"SmoothStreaming" : "false",
"AllowedMethods": [
"HEAD",
"GET",
"OPTIONS"
],
"MaxTTL": "31536000",
"MinTTL": "0",
"Compress" : "true",
"ForwardedValues" : {
"QueryString" : "false",
"Cookies" : { "Forward" : "all" }
},
"ViewerProtocolPolicy" : "allow-all"
},
"PriceClass" : "PriceClass_All",
"Restrictions" : {
"GeoRestriction": {
"RestrictionType": "none",
"Locations": []
}
},
"ViewerCertificate": {
"SslSupportMethod": "sni-only",
"AcmCertificateArn" : {
"Fn::Sub": "arn:aws:acm:us-east-1:<ID>:certificate/2345f-534234"
}
}
}
}
}
}
}
Hi Team,
I am using this in my cloudfront template to add my custom SSL on that and it is showing me some an error:- Exactly one of [AcmCertificateArn,CloudFrontDefaultCertificate,IamCertificateId] needs to be specified.
So please let me know how will i add this or if there is any option to add in parameter so that it will list that certificate. Please guide me for the same. This is my certificate ARN - arn:aws:acm:us-east-1::certificate/2345f-534234
ViewerCertificate block should look like this in your case:
"ViewerCertificate": {
"SslSupportMethod": "sni-only",
"AcmCertificateArn": " arn:aws:acm:us-east-1::certificate/2345f-534234"
}
Also what you should always take care is that certificate is provisioned in us-east1 region (yours is, based on the ARN :)
The property you need to use is ViewerCertificate. The configuration within the CloudFormation documentation should help you identify any options you might want to add.
You can add a parameter if you would like to specify the ACM certificate, the type will be a string.
Below is an updated template. You will need to ensure th ACM certificate includes your account id. I have ran this to validate that it builds successfully.
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"AlternateDomainNames": {
"Description": "CNAMEs (alternate domain names), if any, for the distribution. Example. test.codavel.com",
"Type": "String",
"Default": "test.example.com"
}
},
"Resources": {
"myDistribution": {
"Type": "AWS::CloudFront::Distribution",
"Properties": {
"DistributionConfig": {
"Origins": [{
"DomainName": "ELBfor-1234.region.elb.amazonaws.com",
"Id": "myCustomOrigin",
"CustomOriginConfig": {
"HTTPPort": "80",
"HTTPSPort": "443",
"OriginProtocolPolicy": "match-viewer",
"OriginSSLProtocols": [
"TLSv1",
"TLSv1.1",
"TLSv1.2",
"SSLv3"
]
}
}],
"ViewerCertificate": {
"SslSupportMethod": "sni-only",
"AcmCertificateArn": "arn:aws:acm:us-east-1::certificate/2345f-534234"
},
"HttpVersion": "http2",
"Aliases": [{
"Ref": "AlternateDomainNames"
}],
"Enabled": "true",
"Comment": "example-cdn",
"DefaultCacheBehavior": {
"TargetOriginId": "myCustomOrigin",
"SmoothStreaming": "false",
"AllowedMethods": [
"HEAD",
"GET",
"OPTIONS"
],
"MaxTTL": "31536000",
"MinTTL": "0",
"Compress": "true",
"ForwardedValues": {
"QueryString": "false",
"Cookies": {
"Forward": "all"
}
},
"ViewerProtocolPolicy": "allow-all"
},
"PriceClass": "PriceClass_All",
"Restrictions": {
"GeoRestriction": {
"RestrictionType": "none",
"Locations": []
}
}
}
}
}
}
}

Mongodb query on triple nested array of object

I'm having some problem to write a query to return a triple nested value from a document. The documents I'm using are structured like this
{
"areaname": "name1",
"places": [
{
"placename": "place1",
"objects": [
{
"objname": "obj1",
"tags": [
"tag1",
"tag2"
]
},
{
"objname": "obj2",
"tags": [
"tag6",
"tag7"
]
}
]
},
{
"placename": "place2",
"objects": [
{
"objname": "obj45",
"tags": [
"tag46",
"tag34"
]
},
{
"objname": "obj77",
"tags": [
"tag56",
"tag11"
]
}
]
}
]
}
It is quite simple actually but I can't find a solution to a simple query like:
"return the objname of the object that contains tag1 inside their tag"
So for the give document if I use "tag1" as a parameter it is expected for the query to return "obj1"
It should give me the same result if I use "tag2" as a parameter
Other example: using "tag56" it should return only "obj77"
Right now i have no problem returning the whole document using the dot-notation or top level field such as areaname or others
db.users.find( {"places.objects.tags":"tag1"}, { areaname: 1, _id:0 } )
Is this even possible?
Keeping it simple:
[
{
"$match" : {
"places.objects.tags" : "tag1"
}
},
{
"$unwind" : "$places"
},
{
"$unwind" : "$places.objects"
},
{
"$match" : {
"places.objects.tags" : "tag1"
}
},
{
"$group" : {
"_id" : "$_id",
"obj_names" : {
"$push" : "$places.objects.objname"
}
}
}
],
You should add any other fields you want to keep to the group stage,
this can also be done without the double $unwind stage but i choose this for read-ability.