|
| 1 | +/** |
| 2 | + * Provides classes and predicates for working with serverless handlers. |
| 3 | + * E.g. [AWS](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html) or [serverless](https://npmjs.com/package/serverless) |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * Provides the input for the `ServerLess` module. |
| 8 | + * Most of these should be provided by the `yaml` library. |
| 9 | + */ |
| 10 | +signature module Input { |
| 11 | + // -------------------------------------------------- |
| 12 | + // The below should be provided by the `yaml` library. |
| 13 | + // -------------------------------------------------- |
| 14 | + class Container { |
| 15 | + string getAbsolutePath(); |
| 16 | + |
| 17 | + Container getParentContainer(); |
| 18 | + } |
| 19 | + |
| 20 | + class File extends Container; |
| 21 | + |
| 22 | + class YamlNode { |
| 23 | + File getFile(); |
| 24 | + |
| 25 | + YamlCollection getParentNode(); |
| 26 | + } |
| 27 | + |
| 28 | + class YamlValue extends YamlNode; |
| 29 | + |
| 30 | + class YamlCollection extends YamlValue; |
| 31 | + |
| 32 | + class YamlScalar extends YamlValue { |
| 33 | + string getValue(); |
| 34 | + } |
| 35 | + |
| 36 | + class YamlMapping extends YamlCollection { |
| 37 | + YamlValue lookup(string key); |
| 38 | + |
| 39 | + YamlValue getValue(int i); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Provides classes and predicates for working with serverless handlers. |
| 45 | + * Supports AWS, Alibaba, and serverless. |
| 46 | + * |
| 47 | + * Common usage is to interpret the handlers as functions and add the |
| 48 | + * first argument of these as remote flow sources. |
| 49 | + */ |
| 50 | +module ServerLess<Input I> { |
| 51 | + import I |
| 52 | + |
| 53 | + /** |
| 54 | + * Gets the looked up value as a convenience. |
| 55 | + */ |
| 56 | + pragma[inline] |
| 57 | + private string lookupValue(YamlMapping mapping, string property) { |
| 58 | + result = mapping.lookup(property).(YamlScalar).getValue() |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Gets a string where an ending "/." is simplified to "/" (if it exists). |
| 63 | + */ |
| 64 | + bindingset[base] |
| 65 | + private string removeTrailingDot(string base) { |
| 66 | + if base.regexpMatch(".*/\\.") |
| 67 | + then result = base.substring(0, base.length() - 1) |
| 68 | + else result = base |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Gets a string where a leading "./" is simplified to "" (if it exists). |
| 73 | + */ |
| 74 | + bindingset[base] |
| 75 | + private string removeLeadingDotSlash(string base) { |
| 76 | + if base.regexpMatch("\\./.*") then result = base.substring(2, base.length()) else result = base |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Gets a string suitable as part of a file path. |
| 81 | + */ |
| 82 | + bindingset[base] |
| 83 | + private string normalise(string base) { result = removeLeadingDotSlash(removeTrailingDot(base)) } |
| 84 | + |
| 85 | + /** |
| 86 | + * Holds if the `.yml` file `ymlFile` contains a serverless configuration fro `framework` with |
| 87 | + * `handler`, `codeURI`, and `runtime` properties. |
| 88 | + * `codeURI` and `runtime` default to the empty string if no explicit value is set in the configuration. |
| 89 | + * |
| 90 | + * `handler` should be interpreted in a language specific way, see `mapping.md`. |
| 91 | + */ |
| 92 | + predicate hasServerlessHandler( |
| 93 | + File ymlFile, string framework, string handler, string codeUri, string runtime |
| 94 | + ) { |
| 95 | + exists(YamlMapping resource | ymlFile = resource.getFile() | |
| 96 | + // There exists at least "AWS::Serverless::Function" and "Aliyun::Serverless::Function" |
| 97 | + resource.lookup("Type").(YamlScalar).getValue().regexpMatch(".*::Serverless::Function") and |
| 98 | + framework = lookupValue(resource, "Type") and |
| 99 | + exists(YamlMapping properties | properties = resource.lookup("Properties") | |
| 100 | + ( |
| 101 | + handler = lookupValue(properties, "Handler") and |
| 102 | + ( |
| 103 | + if exists(properties.lookup("CodeUri")) |
| 104 | + then codeUri = normalise(lookupValue(properties, "CodeUri")) |
| 105 | + else codeUri = "" |
| 106 | + ) and |
| 107 | + ( |
| 108 | + if exists(properties.lookup("Runtime")) |
| 109 | + then runtime = lookupValue(properties, "Runtime") |
| 110 | + else runtime = "" |
| 111 | + ) |
| 112 | + ) |
| 113 | + ) |
| 114 | + or |
| 115 | + // The `serverless` library, which specifies a top-level `functions` property |
| 116 | + framework = "Serverless" and |
| 117 | + exists(YamlMapping functions | |
| 118 | + functions = resource.lookup("functions") and |
| 119 | + not exists(resource.getParentNode()) and |
| 120 | + handler = lookupValue(functions.getValue(_), "handler") and |
| 121 | + codeUri = "" and |
| 122 | + ( |
| 123 | + if exists(functions.lookup("Runtime")) |
| 124 | + then runtime = lookupValue(functions, "Runtime") |
| 125 | + else runtime = "" |
| 126 | + ) |
| 127 | + ) |
| 128 | + ) |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Holds if `handler` = `filePart . astPart` and `filePart` does not contain a `.`. |
| 133 | + * This is a convenience predicate, as in many cases the first part of the handler property |
| 134 | + * should be interpreted as (the stem of) a file name. |
| 135 | + */ |
| 136 | + bindingset[handler] |
| 137 | + predicate splitHandler(string handler, string filePart, string astPart) { |
| 138 | + exists(string pattern | pattern = "(.*?)\\.(.*)" | |
| 139 | + filePart = handler.regexpCapture(pattern, 1) and |
| 140 | + astPart = handler.regexpCapture(pattern, 2) |
| 141 | + ) |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Holds if a file with stem `fileStem` has a serverless handler denoted by `func`. |
| 146 | + * |
| 147 | + * This is a convenience predicate for the common case where the first part of the |
| 148 | + * handler property is the file name. |
| 149 | + * |
| 150 | + * `func` should be interpreted in a language specific way, see `mapping.md`. |
| 151 | + */ |
| 152 | + predicate hasServerlessHandler(string fileStem, string func, string framework, string runtime) { |
| 153 | + exists(File ymlFile, string handler, string codeUri, string filePart | |
| 154 | + hasServerlessHandler(ymlFile, framework, handler, codeUri, runtime) |
| 155 | + | |
| 156 | + splitHandler(handler, filePart, func) and |
| 157 | + fileStem = ymlFile.getParentContainer().getAbsolutePath() + "/" + codeUri + filePart |
| 158 | + ) |
| 159 | + } |
| 160 | +} |
0 commit comments