--- policy: /ce/ca/aws/kms/key-policy-public logic: /ce/ca/aws/kms/key-policy-public/prod.logic.yaml executionTime: 2026-06-06T12:03:05.512254861Z generationMs: 52 executionMs: 1068 rows: - id: test1 match: true status: expected: DISAPPEARED actual: DISAPPEARED conditionIndex: expected: 99 actual: 99 conditionText: expected: isDisappeared(CA10__disappearanceTime__c) actual: isDisappeared(CA10__disappearanceTime__c) runtimeError: {} - id: test2 match: true status: expected: INAPPLICABLE actual: INAPPLICABLE conditionIndex: expected: 199 actual: 199 conditionText: expected: extract('CA10__state__c') != 'Enabled' actual: extract('CA10__state__c') != 'Enabled' runtimeError: {} - id: test3 match: true status: expected: INCOMPLIANT actual: INCOMPLIANT conditionIndex: expected: 299 actual: 299 conditionText: expected: CA10__AWS_KMS_Key_Policies__r.has(INCOMPLIANT) actual: CA10__AWS_KMS_Key_Policies__r.has(INCOMPLIANT) runtimeError: {} - id: test4 match: true status: expected: COMPLIANT actual: COMPLIANT conditionIndex: expected: 399 actual: 399 conditionText: expected: CA10__AWS_KMS_Key_Policies__r.has(COMPLIANT) actual: CA10__AWS_KMS_Key_Policies__r.has(COMPLIANT) runtimeError: {} - id: test5 match: true status: expected: UNDETERMINED actual: UNDETERMINED conditionIndex: expected: 400 actual: 400 conditionText: expected: otherwise actual: otherwise runtimeError: {} usedFiles: - path: /ce/ca/aws/kms/key-policy-public/policy.yaml md5Hash: 28575DC47E4893CAC7CB02392DD0A598 content: "---\nnames:\n full: \"AWS KMS Key Policy allows public access\"\n \ \ contextual: \"Key Policy allows public access\"\ndescription: >\n Ensure\ \ that AWS KMS key policies do not allow anonymous or public access. \n Allowing\ \ unrestricted access to cryptographic keys could enable unauthorized parties\ \ \n to encrypt, decrypt, or manage data, leading to severe data breaches.\n\ type: \"COMPLIANCE_POLICY\"\ncategories:\n - \"SECURITY\"\nframeworkMappings:\n\ \ - \"/frameworks/cloudaware/resource-security/secure-access\"\n - \"/frameworks/aws-fsbp-v1.0.0/kms/05\"\ \nsimilarPolicies:\n awsSecurityHub:\n - name: \"[KMS.5] KMS keys should\ \ not be publicly accessible\"\n url: \"https://docs.aws.amazon.com/securityhub/latest/userguide/kms-controls.html#kms-5\"\ \n" - path: /ce/ca/aws/kms/key-policy-public/prod.logic.yaml md5Hash: E5F8842573903AA85F81B046C9DA4088 content: | --- inputType: "CA10__CaAwsKmsKey__c" testData: - file: "test-data.json" importExtracts: - file: "/types/CA10__CaAwsKmsKey__c/object.extracts.yaml" conditions: - status: "INAPPLICABLE" currentStateMessage: "The key is not enabled." check: NOT_EQUAL: left: EXTRACT: CA10__state__c right: TEXT: "Enabled" - status: "INCOMPLIANT" currentStateMessage: "The KMS key policy allows anonymous access." remediationMessage: "Modify the key policy to remove statements where the Principal is '*'." check: RELATED_LIST_HAS: status: "INCOMPLIANT" relationshipName: "CA10__AWS_KMS_Key_Policies__r" - status: "COMPLIANT" currentStateMessage: "The KMS key policy does not allow anonymous access." check: RELATED_LIST_HAS: status: "COMPLIANT" relationshipName: "CA10__AWS_KMS_Key_Policies__r" otherwise: status: "UNDETERMINED" currentStateMessage: "The key policy is not present in the CMDB." relatedLists: - relationshipName: "CA10__AWS_KMS_Key_Policies__r" conditions: - status: "INCOMPLIANT" currentStateMessage: "The KMS key policy allows anonymous access." remediationMessage: "Modify the key policy to remove statements where the Principal is '*'." check: AWS_POLICY_ALLOWS: widestAcceptableAccessLevel: "EXTERNAL_PRINCIPAL" policyExtField: "CA10__policyDocumentExt__c" actions: - "kms:Encrypt" - "kms:Decrypt" - "kms:ReEncryptFrom" - "kms:ReEncryptTo" - "kms:GenerateDataKey" - "kms:CreateGrant" - "kms:DescribeKey" - "kms:EnableKey" - "kms:PutKeyPolicy" - "kms:GetKeyPolicy" - "kms:ScheduleKeyDeletion" - "kms:CancelKeyDeletion" - "kms:RotateKeyOnDemand" otherwise: status: "COMPLIANT" currentStateMessage: "The KMS key policy does not allow anonymous or public access." - path: /ce/ca/aws/kms/key-policy-public/test-data.json md5Hash: 933041B79B062F852616E063E3DFA08F content: |- [ { "expectedResult": { "status": "DISAPPEARED", "conditionIndex": "99", "conditionText": "isDisappeared(CA10__disappearanceTime__c)", "runtimeError": null }, "context": { "snapshotTime": "2026-01-11T19:40:46Z" }, "CA10__disappearanceTime__c": "2026-01-11T10:49:25Z", "CA10__state__c": "PendingDeletion", "Id": "test1", "CA10__AWS_KMS_Key_Policies__r": [ { "CA10__disappearanceTime__c": "2026-01-11T10:50:14Z", "CA10__policyDocumentExt__c": "{\"Id\":\"Id\",\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Sid\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::id:root\"},\"Action\":\"kms:*\",\"Resource\":\"*\",\"AnalyzedResult\":{\"accessLevel\":\"SAME_ACCOUNT\",\"reason\":\"reason\"}}]}", "CA10__kmsKey__c": "test1", "Id": "test1_1" } ] }, { "expectedResult": { "status": "INAPPLICABLE", "conditionIndex": "199", "conditionText": "extract('CA10__state__c') != 'Enabled'", "runtimeError": null }, "context": { "snapshotTime": "2026-01-11T19:40:46Z" }, "CA10__disappearanceTime__c": null, "CA10__state__c": "Disabled", "Id": "test2", "CA10__AWS_KMS_Key_Policies__r": [ { "CA10__disappearanceTime__c": null, "CA10__policyDocumentExt__c": "{\"Id\":\"Id\",\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Sid\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::id:root\"},\"Action\":\"kms:*\",\"Resource\":\"*\",\"AnalyzedResult\":{\"accessLevel\":\"SAME_ACCOUNT\",\"reason\":\"reason\"}}]}", "CA10__kmsKey__c": "test2", "Id": "test2_1" } ] }, { "expectedResult": { "status": "INCOMPLIANT", "conditionIndex": "299", "conditionText": "CA10__AWS_KMS_Key_Policies__r.has(INCOMPLIANT)", "runtimeError": null }, "context": { "snapshotTime": "2026-01-11T19:40:46Z" }, "CA10__disappearanceTime__c": null, "CA10__state__c": "Enabled", "Id": "test3", "CA10__AWS_KMS_Key_Policies__r": [ { "CA10__disappearanceTime__c": null, "CA10__policyDocumentExt__c": "{\"Id\":\"Id\",\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"kms:Get*\",\"Resource\":\"*\",\"AnalyzedResult\":{\"accessLevel\":\"ANONYMOUS_PRINCIPAL\",\"reason\":\"All principals '*' do not restrict access\"}}]}", "CA10__kmsKey__c": "test3", "Id": "test3_1" } ] }, { "expectedResult": { "status": "COMPLIANT", "conditionIndex": "399", "conditionText": "CA10__AWS_KMS_Key_Policies__r.has(COMPLIANT)", "runtimeError": null }, "context": { "snapshotTime": "2026-01-11T19:40:46Z" }, "CA10__disappearanceTime__c": null, "CA10__state__c": "Enabled", "Id": "test4", "CA10__AWS_KMS_Key_Policies__r": [ { "CA10__disappearanceTime__c": null, "CA10__policyDocumentExt__c": "{\"Id\":\"Id\",\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Sid\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"lightsail.amazonaws.com\"},\"Action\":[\"kms:Describe*\",\"kms:Get*\"],\"Resource\":\"*\",\"AnalyzedResult\":{\"accessLevel\":\"AWS_SERVICE\",\"reason\":\"Access allowed only for AWS Service: 'lightsail.amazonaws.com'\"}}]}", "CA10__kmsKey__c": "test4", "Id": "test4_1" } ] }, { "expectedResult": { "status": "UNDETERMINED", "conditionIndex": "400", "conditionText": "otherwise", "runtimeError": null }, "context": { "snapshotTime": "2026-01-11T19:40:46Z" }, "CA10__disappearanceTime__c": null, "CA10__state__c": "Enabled", "Id": "test5", "CA10__AWS_KMS_Key_Policies__r": [] } ] - path: /types/CA10__CaAwsKmsKey__c/object.extracts.yaml md5Hash: EC6D0EFD0BFEAB3447A84529724D4AFE content: "---\nextracts:\n# Values: Creating, Enabled, Disabled, PendingDeletion,\ \ PendingImport, PendingReplicaDeletion, Unavailable, Updating. Not nullable.\n\ \ - name: \"CA10__state__c\"\n value: \n FIELD:\n path: \"CA10__state__c\"\ \n undeterminedIf:\n noAccessDelegate: \n path: \"\ CA10__state__c\"\n currentStateMessage: \"Unable to determine Key\ \ state. Possible permission issue with kms:DescribeKey.\"\n isEmpty:\ \ \"KMS Key state is not populated yet.\"\n# Values: RSA_2048, RSA_3072, RSA_4096,\ \ ECC_NIST_P256, ECC_NIST_P384, ECC_NIST_P521, ECC_SECG_P256K1, SYMMETRIC_DEFAULT,\ \ HMAC_224, HMAC_256, HMAC_384, HMAC_512, SM2\n# Not nullable\n - name: \"\ CA10__keySpec__c\"\n value: \n FIELD:\n path: \"CA10__keySpec__c\"\ \n undeterminedIf:\n noAccessDelegate: \n path: \"\ CA10__keySpec__c\"\n currentStateMessage: \"Unable to determine Key\ \ state. Possible permission issue with kms:DescribeKey.\"\n isEmpty:\ \ \"KMS Key state is not populated yet.\"\n# Checkbox.\n - name: \"CA10__rotationEnabled__c\"\ \n value: \n FIELD:\n path: \"CA10__rotationEnabled__c\"\n \ \ undeterminedIf:\n noAccessDelegate:\n path: \"CA10__rotationEnabled__c\"\ \n currentStateMessage: \"Unable to determine Key Rotation status.\ \ Possible permission issue with kms:GetKeyRotationStatus.\"\n# Nullable.\n\ \ - name: \"CA10__deletionDate__c\"\n value: \n FIELD:\n path:\ \ \"CA10__deletionDate__c\"\n# Values: AWS | Customer. Not Nullable.\n - name:\ \ \"CA10__manager__c\"\n value: \n FIELD:\n path: \"CA10__manager__c\"\ \n undeterminedIf:\n noAccessDelegate: \n path: \"\ CA10__manager__c\"\n currentStateMessage: \"Unable to determine Key\ \ Manager. Possible permission issue with kms:DescribeKey.\"\n" script: |- CREATE TEMP FUNCTION mock_ExpectedResult() RETURNS ARRAY >> DETERMINISTIC LANGUAGE js AS r""" return [ { "Id" : "test1", "expectedResult" : { "status" : "DISAPPEARED", "conditionIndex" : "99", "conditionText" : "isDisappeared(CA10__disappearanceTime__c)", "runtimeError" : null } }, { "Id" : "test2", "expectedResult" : { "status" : "INAPPLICABLE", "conditionIndex" : "199", "conditionText" : "extract('CA10__state__c') != 'Enabled'", "runtimeError" : null } }, { "Id" : "test3", "expectedResult" : { "status" : "INCOMPLIANT", "conditionIndex" : "299", "conditionText" : "CA10__AWS_KMS_Key_Policies__r.has(INCOMPLIANT)", "runtimeError" : null } }, { "Id" : "test4", "expectedResult" : { "status" : "COMPLIANT", "conditionIndex" : "399", "conditionText" : "CA10__AWS_KMS_Key_Policies__r.has(COMPLIANT)", "runtimeError" : null } }, { "Id" : "test5", "expectedResult" : { "status" : "UNDETERMINED", "conditionIndex" : "400", "conditionText" : "otherwise", "runtimeError" : null } } ]; """; CREATE TEMP FUNCTION mock_CA10__CaAwsKmsKey__c() RETURNS ARRAY >> DETERMINISTIC LANGUAGE js AS r""" return [ { "context" : { "snapshotTime" : new Date("2026-01-11T19:40:46Z") }, "CA10__disappearanceTime__c" : new Date("2026-01-11T10:49:25Z"), "CA10__state__c" : "PendingDeletion", "Id" : "test1" }, { "context" : { "snapshotTime" : new Date("2026-01-11T19:40:46Z") }, "CA10__state__c" : "Disabled", "Id" : "test2" }, { "context" : { "snapshotTime" : new Date("2026-01-11T19:40:46Z") }, "CA10__state__c" : "Enabled", "Id" : "test3" }, { "context" : { "snapshotTime" : new Date("2026-01-11T19:40:46Z") }, "CA10__state__c" : "Enabled", "Id" : "test4" }, { "context" : { "snapshotTime" : new Date("2026-01-11T19:40:46Z") }, "CA10__state__c" : "Enabled", "Id" : "test5" } ]; """; CREATE TEMP FUNCTION mock_CA10__CaAwsKmsKeyPolicy__c() RETURNS ARRAY >> DETERMINISTIC LANGUAGE js AS r""" return [ { "context" : { "snapshotTime" : new Date("2026-01-11T19:40:46Z") }, "CA10__disappearanceTime__c" : new Date("2026-01-11T10:50:14Z"), "CA10__policyDocumentExt__c" : "{\"Id\":\"Id\",\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Sid\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::id:root\"},\"Action\":\"kms:*\",\"Resource\":\"*\",\"AnalyzedResult\":{\"accessLevel\":\"SAME_ACCOUNT\",\"reason\":\"reason\"}}]}", "CA10__kmsKey__c" : "test1", "Id" : "test1_1" }, { "context" : { "snapshotTime" : new Date("2026-01-11T19:40:46Z") }, "CA10__policyDocumentExt__c" : "{\"Id\":\"Id\",\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Sid\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::id:root\"},\"Action\":\"kms:*\",\"Resource\":\"*\",\"AnalyzedResult\":{\"accessLevel\":\"SAME_ACCOUNT\",\"reason\":\"reason\"}}]}", "CA10__kmsKey__c" : "test2", "Id" : "test2_1" }, { "context" : { "snapshotTime" : new Date("2026-01-11T19:40:46Z") }, "CA10__policyDocumentExt__c" : "{\"Id\":\"Id\",\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"kms:Get*\",\"Resource\":\"*\",\"AnalyzedResult\":{\"accessLevel\":\"ANONYMOUS_PRINCIPAL\",\"reason\":\"All principals '*' do not restrict access\"}}]}", "CA10__kmsKey__c" : "test3", "Id" : "test3_1" }, { "context" : { "snapshotTime" : new Date("2026-01-11T19:40:46Z") }, "CA10__policyDocumentExt__c" : "{\"Id\":\"Id\",\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Sid\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"lightsail.amazonaws.com\"},\"Action\":[\"kms:Describe*\",\"kms:Get*\"],\"Resource\":\"*\",\"AnalyzedResult\":{\"accessLevel\":\"AWS_SERVICE\",\"reason\":\"Access allowed only for AWS Service: 'lightsail.amazonaws.com'\"}}]}", "CA10__kmsKey__c" : "test4", "Id" : "test4_1" } ]; """; CREATE TEMP FUNCTION process_CA10__CaAwsKmsKey__c( obj STRUCT< CA10__disappearanceTime__c TIMESTAMP, CA10__state__c STRING, CA10__AWS_KMS_Key_Policies__r ARRAY >>, Id STRING >, snapshotTime TIMESTAMP ) RETURNS STRUCT DETERMINISTIC LANGUAGE js AS r""" var TextLib = new function () { this.normalize = function(arg) { return arg == null ? '' : arg.replace(/\s+/g, ' ').trim().toLowerCase(); }; this.isEmpty = function(arg) { return this.normalize(arg) == ''; }; this.isNotEmpty = function(arg) { return this.normalize(arg) != ''; }; this.equal = function(left, right) { return this.normalize(left) == this.normalize(right); }; this.notEqual = function(left, right) { return this.normalize(left) != this.normalize(right); }; this.startsWith = function(arg, substring) { return this.normalize(arg).startsWith(this.normalize(substring)); }; this.endsWith = function(arg, substring) { return this.normalize(arg).endsWith(this.normalize(substring)); }; this.contains = function(arg, substring) { return this.normalize(arg).includes(this.normalize(substring)); }; this.containsAll = function(arg, substrings) { if (substrings == null || substrings.length === 0) return false; let normalizedArg = this.normalize(arg); return substrings.every(sub => normalizedArg.includes(this.normalize(sub))); }; this.containsAny = function(arg, substrings) { if (substrings == null || substrings.length === 0) return false; let normalizedArg = this.normalize(arg); return substrings.some(sub => normalizedArg.includes(this.normalize(sub))); }; }(); var references1 = []; // condition[0], conditionIndex:[0..99] references1.push('Deleted From AWS [CA10__disappearanceTime__c]: ' + obj.CA10__disappearanceTime__c); if (obj.CA10__disappearanceTime__c != null) { return {status: 'DISAPPEARED', conditionIndex: 99, conditionText: "isDisappeared(CA10__disappearanceTime__c)", currentStateMessage: "Object is deleted in the source", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}; } // condition[1], conditionIndex:[100..199] function fieldChecked4() { if (TextLib.isEmpty(obj.CA10__state__c)) { throw new Error("UNDETERMINED condition:101", {cause: {status: 'UNDETERMINED', conditionIndex: 101, conditionText: "CA10__state__c.delegatedTo(CA10__state__c).isEmpty()", currentStateMessage: "Unable to determine Key state. Possible permission issue with kms:DescribeKey.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}}); } if (TextLib.isEmpty(obj.CA10__state__c)) { throw new Error("UNDETERMINED condition:102", {cause: {status: 'UNDETERMINED', conditionIndex: 102, conditionText: "CA10__state__c.isEmpty()", currentStateMessage: "KMS Key state is not populated yet.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}}); } return obj.CA10__state__c; } function extract3() { if (!this.out) { this.out = fieldChecked4(); } return this.out; }; references1.push('State [obj.CA10__state__c]: ' + obj.CA10__state__c); try { if (TextLib.notEqual(extract3.call(extract3), 'Enabled')) { return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "extract('CA10__state__c') != 'Enabled'", currentStateMessage: "The key is not enabled.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}; } } catch (err) { if (err.cause && err.cause.status) { return err.cause; } else { throw err; } } var count_CA10__AWS_KMS_Key_Policies__r_INCOMPLIANT5 = 0; if (obj.CA10__AWS_KMS_Key_Policies__r != null) { for (var i6 = 0; i6 < obj.CA10__AWS_KMS_Key_Policies__r.length; i6++) { if (typeof(obj.CA10__AWS_KMS_Key_Policies__r[i6].status) !== 'undefined') { if (obj.CA10__AWS_KMS_Key_Policies__r[i6].status == 'INCOMPLIANT') { count_CA10__AWS_KMS_Key_Policies__r_INCOMPLIANT5 += obj.CA10__AWS_KMS_Key_Policies__r[i6].count; } } else { if (obj.CA10__AWS_KMS_Key_Policies__r[i6].result.status == 'INCOMPLIANT') { count_CA10__AWS_KMS_Key_Policies__r_INCOMPLIANT5 += 1; } } } } // condition[2], conditionIndex:[200..299] references1.push('Related list [CA10__AWS_KMS_Key_Policies__r] ' + (count_CA10__AWS_KMS_Key_Policies__r_INCOMPLIANT5 > 0 ? 'has' : 'does not have') + ' objects in INCOMPLIANT status'); if (count_CA10__AWS_KMS_Key_Policies__r_INCOMPLIANT5 > 0) { return {status: 'INCOMPLIANT', conditionIndex: 299, conditionText: "CA10__AWS_KMS_Key_Policies__r.has(INCOMPLIANT)", currentStateMessage: "The KMS key policy allows anonymous access.", currentStateReferences: references1.join('\n'), remediation: "Modify the key policy to remove statements where the Principal is '*'.", runtimeError: null}; } var count_CA10__AWS_KMS_Key_Policies__r_COMPLIANT7 = 0; if (obj.CA10__AWS_KMS_Key_Policies__r != null) { for (var i8 = 0; i8 < obj.CA10__AWS_KMS_Key_Policies__r.length; i8++) { if (typeof(obj.CA10__AWS_KMS_Key_Policies__r[i8].status) !== 'undefined') { if (obj.CA10__AWS_KMS_Key_Policies__r[i8].status == 'COMPLIANT') { count_CA10__AWS_KMS_Key_Policies__r_COMPLIANT7 += obj.CA10__AWS_KMS_Key_Policies__r[i8].count; } } else { if (obj.CA10__AWS_KMS_Key_Policies__r[i8].result.status == 'COMPLIANT') { count_CA10__AWS_KMS_Key_Policies__r_COMPLIANT7 += 1; } } } } // condition[3], conditionIndex:[300..399] if (count_CA10__AWS_KMS_Key_Policies__r_COMPLIANT7 > 0) { return {status: 'COMPLIANT', conditionIndex: 399, conditionText: "CA10__AWS_KMS_Key_Policies__r.has(COMPLIANT)", currentStateMessage: "The KMS key policy does not allow anonymous access.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}; } return {status: 'UNDETERMINED', conditionIndex: 400, conditionText: "otherwise", currentStateMessage: "The key policy is not present in the CMDB.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}; """; CREATE TEMP FUNCTION process_CA10__AWS_KMS_Key_Policies__r( obj STRUCT< CA10__disappearanceTime__c TIMESTAMP, CA10__policyDocumentExt__c STRING, CA10__kmsKey__c STRING, Id STRING >, snapshotTime TIMESTAMP ) RETURNS STRUCT DETERMINISTIC LANGUAGE js AS r""" var AwsPolicyAllows = new function () { function accessLevelOrdinal(accessLevel) { if (accessLevel === 'NOBODY') { return 0; } else if (accessLevel === 'SAME_ACCOUNT') { return 1; } else if (accessLevel === 'SAME_ORG') { return 2; } else if (accessLevel === 'TRUSTED_PRINCIPAL') { return 3; } else if (accessLevel === 'AWS_SERVICE') { return 4; } else if (accessLevel === 'EXTERNAL_PRINCIPAL') { return 5; } else if (accessLevel === 'ANONYMOUS_PRINCIPAL') { return 6; } else { return 7; } } function ensureArray(obj) { if (obj == null) { return []; } else if (Array.isArray(obj)) { return obj; } else { return [obj]; } } function arrayExec(obj, func) { if (obj == null) { return false; } else if (Array.isArray(obj)) { for (var i = 0; i < obj.length; i++) { if (func(obj[i], i)) { return true; } } return false; } else { return func(obj, 0); } } function wildcardMatches(text, pattern) { if (pattern.indexOf("*") === -1) { return text.toLowerCase() === pattern.toLowerCase(); } var regex = new RegExp("^" + pattern.toLowerCase().replaceAll("*", ".*") + "$"); return regex.test(text.toLowerCase()); } function isActionMatches(action, statement) { if (statement.Action) { let matches = false; arrayExec(statement.Action, function (statementAction) { if (wildcardMatches(action, statementAction)) { matches = true; // break; return true; } }); return matches; } else if (statement.NotAction) { let matches = false; arrayExec(statement.NotAction, function (statementAction) { if (wildcardMatches(action, statementAction)) { matches = true; // break; return true; } }); return !matches; } } function allowResources(openedResources, statement, statementIndex) { if (statement.Resource) { arrayExec(statement.Resource, function (resource) { openedResources.push({ allowType: "R", allowResource: resource, allowStatement: statementIndex, accessLevel: statement.AnalyzedResult.accessLevel, accessLevelOrdinal: accessLevelOrdinal(statement.AnalyzedResult.accessLevel) }); }); } else if (statement.NotResource) { arrayExec(statement.NotResource, function (resource) { openedResources.push({ allowType: "NR", allowResource: resource, allowStatement: statementIndex, accessLevel: statement.AnalyzedResult.accessLevel, accessLevelOrdinal: accessLevelOrdinal(statement.AnalyzedResult.accessLevel) }); }); } } function denyResources(openedResources, statement, statementIndex) { var denyType, list; if (statement.Resource) { denyType = "R"; list = statement.Resource; } else if (statement.NotResource) { denyType = "NR"; list = statement.NotResource; } else { return; } var closingAccessLevelOrdinal = accessLevelOrdinal(statement.AnalyzedResult.accessLevel); arrayExec(list, function (resource) { for (var i = 0; i < openedResources.length; i++) { var openedResource = openedResources[i]; if (openedResource.accessLevelOrdinal <= closingAccessLevelOrdinal) { // there is no sense processing deny if it is more widely open than the original allow continue; } if (openedResource.allowType === denyType) { if (wildcardMatches(openedResource.allowResource, resource)) { // openedResource.denyStatement = statementIndex; openedResource.denyType = denyType; openedResource.denyResource = resource; openedResource.accessLevel = statement.AnalyzedResult.accessLevel; openedResource.accessLevelOrdinal = closingAccessLevelOrdinal; } } else { if (!wildcardMatches(openedResource.allowResource, resource)) { // openedResource.denyStatement = statementIndex; openedResource.denyType = denyType; openedResource.denyResource = resource; openedResource.accessLevel = statement.AnalyzedResult.accessLevel; openedResource.accessLevelOrdinal = closingAccessLevelOrdinal; } } } }); } function calculateSingleAccess(policyExt, action) { var openedResources = []; var result; arrayExec(policyExt.Statement, function (statement, statementIndex) { if (statement.Effect !== 'Allow') { // we're accumulating only what's allowed return false; // continue; } if (statement.AnalyzedResult == null) { // we can't analyze the policy without pre-analysis done on the CMDB side result = { accessLevelOrdinal: accessLevelOrdinal("ANONYMOUS_PRINCIPAL"), accessLevel: "ANONYMOUS_PRINCIPAL", reason: 'There is no analyzed result for a statement #' + (statementIndex + 1) }; return true; // break; } if (isActionMatches(action, statement)) { allowResources(openedResources, statement, statementIndex); } }); if (result) { return [result]; } if (openedResources.length == 0) { return [{ accessLevelOrdinal: accessLevelOrdinal("NOBODY"), accessLevel: "NOBODY", reason: "Action " + action + " is implicitly denied. None of the statements are matching this action." }]; } arrayExec(policyExt.Statement, function (statement, statementIndex) { if (statement.Effect !== 'Deny') { // we're accumulating only what's denied return false; // continue; } if (statement.AnalyzedResult == null) { // we can't analyze the policy without pre-analysis done on the CMDB side result = { accessLevelOrdinal: accessLevelOrdinal("ANONYMOUS_PRINCIPAL"), accessLevel: "ANONYMOUS_PRINCIPAL", reason: 'There is no analyzed result for a statement #' + (statementIndex + 1) }; return true; // break; } if (isActionMatches(action, statement)) { denyResources(openedResources, statement, statementIndex); } }); if (result) { return [result]; } /** * чтобы "закрыть" Allow тебе нужно * 1) иметь accessLevel уже чем у Allow * 2) закрыть все Resource у Allow своими, которые в Deny */ return openedResources; } function resourceAccessibleBy(openedResources, widestAcceptableAccessLevel) { var ordinal = accessLevelOrdinal(widestAcceptableAccessLevel); return openedResources.filter(openedResource => openedResource.accessLevelOrdinal > ordinal); } this.calculateAccess = function(policyExt, actions, widestAcceptableAccessLevel) { var openedResources = []; arrayExec(actions, function (action) { var actionResult = calculateSingleAccess(policyExt, action); if (Array.isArray(actionResult)) { var filtered = resourceAccessibleBy(actionResult, widestAcceptableAccessLevel); if (filtered.length > 0) { var lowestLevel = null; arrayExec(filtered, function (resource) { if (lowestLevel == null) { lowestLevel = resource.accessLevel; } else { var resourceAccessLevelOrdinal = accessLevelOrdinal(resource.accessLevel); var lowestLevelOrdinal = accessLevelOrdinal(lowestLevel); if (resourceAccessLevelOrdinal > lowestLevelOrdinal) { lowestLevel = resource.accessLevel; } } }); openedResources.push({ action: action, accessLevel: lowestLevel, accessLevelOrdinal: accessLevelOrdinal(lowestLevel), reason: 'Policy allows action ' + action + ' with access level up to ' + lowestLevel, openedResources: filtered }); } } else { actionResult.action = action; openedResources.push(actionResult); } }); return openedResources; } }(); var references1 = []; // condition[0], conditionIndex:[0..99] references1.push('Deleted From AWS [CA10__disappearanceTime__c]: ' + obj.CA10__disappearanceTime__c); if (obj.CA10__disappearanceTime__c != null) { return {status: 'DISAPPEARED', conditionIndex: 99, conditionText: "isDisappeared(CA10__disappearanceTime__c)", currentStateMessage: "Object is deleted in the source", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}; } // condition[1], conditionIndex:[100..199] function awsPolicyAllows4() { var policyExt3 = obj.CA10__policyDocumentExt__c; if (policyExt3 === '') { policyExt3 = '{}'; } var calculated2 = AwsPolicyAllows.calculateAccess(JSON.parse(policyExt3), ['kms:Encrypt', 'kms:Decrypt', 'kms:ReEncryptFrom', 'kms:ReEncryptTo', 'kms:GenerateDataKey', 'kms:CreateGrant', 'kms:DescribeKey', 'kms:EnableKey', 'kms:PutKeyPolicy', 'kms:GetKeyPolicy', 'kms:ScheduleKeyDeletion', 'kms:CancelKeyDeletion', 'kms:RotateKeyOnDemand'], 'EXTERNAL_PRINCIPAL'); return calculated2.length > 0; } try { if (awsPolicyAllows4()) { return {status: 'INCOMPLIANT', conditionIndex: 199, conditionText: "CA10__policyDocumentExt__c .allows(EXTERNAL_PRINCIPAL, [kms:Encrypt, kms:Decrypt, kms:ReEncryptFrom, ... 10 elements])", currentStateMessage: "The KMS key policy allows anonymous access.", currentStateReferences: references1.join('\n'), remediation: "Modify the key policy to remove statements where the Principal is '*'.", runtimeError: null}; } } catch (err) { if (err.cause && err.cause.status) { return err.cause; } else { throw err; } } return {status: 'COMPLIANT', conditionIndex: 200, conditionText: "otherwise", currentStateMessage: "The KMS key policy does not allow anonymous or public access.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null}; """; SELECT expectedResult.Id as Id, IF ( IFNULL(expectedResult.expectedResult.status, '') = IFNULL(sObject.result.status, '') AND IFNULL(expectedResult.expectedResult.conditionIndex, -1) = IFNULL(sObject.result.conditionIndex, -1) AND IFNULL(expectedResult.expectedResult.conditionText, '') = IFNULL(sObject.result.conditionText, '') AND IFNULL(expectedResult.expectedResult.runtimeError, '') = IFNULL(sObject.result.runtimeError, ''), "MATCH", "FAIL" ) as match, expectedResult.expectedResult.status as expectedStatus, sObject.result.status as actualStatus, expectedResult.expectedResult.conditionIndex as expectedConditionIndex, sObject.result.conditionIndex as actualConditionIndex, expectedResult.expectedResult.conditionText as expectedConditionText, sObject.result.conditionText as actualConditionText, expectedResult.expectedResult.runtimeError as expectedRuntimeError, sObject.result.runtimeError as actualRuntimeError FROM UNNEST(mock_ExpectedResult()) expectedResult LEFT JOIN ( SELECT sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c, sObject.CA10__state__c AS CA10__state__c, `CA10__AWS_KMS_Key_Policies__r`.arr AS CA10__AWS_KMS_Key_Policies__r, sObject.Id AS Id, process_CA10__CaAwsKmsKey__c( STRUCT( sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c, sObject.CA10__state__c AS CA10__state__c, `CA10__AWS_KMS_Key_Policies__r`.arr AS CA10__AWS_KMS_Key_Policies__r, sObject.Id AS Id ), sObject.context.snapshotTime ) as result FROM UNNEST(mock_CA10__CaAwsKmsKey__c()) AS sObject LEFT JOIN ( SELECT sObject.CA10__kmsKey__c, ARRAY_AGG( STRUCT( sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c, sObject.CA10__policyDocumentExt__c AS CA10__policyDocumentExt__c, sObject.CA10__kmsKey__c AS CA10__kmsKey__c, sObject.Id AS Id, process_CA10__AWS_KMS_Key_Policies__r( STRUCT( sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c, sObject.CA10__policyDocumentExt__c AS CA10__policyDocumentExt__c, sObject.CA10__kmsKey__c AS CA10__kmsKey__c, sObject.Id AS Id ), sObject.context.snapshotTime ) as result ) ) AS arr FROM UNNEST(mock_CA10__CaAwsKmsKeyPolicy__c()) AS sObject GROUP BY sObject.CA10__kmsKey__c ) AS `CA10__AWS_KMS_Key_Policies__r` ON sObject.Id = `CA10__AWS_KMS_Key_Policies__r`.CA10__kmsKey__c ) sObject ON sObject.Id = expectedResult.Id;