|
| 1 | +package armo_builtins |
| 2 | + |
| 3 | +import future.keywords.in |
| 4 | + |
| 5 | +deny[msg] { |
| 6 | + obj = input[_] |
| 7 | + is_api_server(obj) |
| 8 | + dontwanted = [ |
| 9 | + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", |
| 10 | + "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA", |
| 11 | + "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA", |
| 12 | + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", |
| 13 | + "TLS_ECDHE_RSA_WITH_RC4_128_SHA", |
| 14 | + "TLS_RSA_WITH_3DES_EDE_CBC_SHA", |
| 15 | + "TLS_RSA_WITH_AES_128_CBC_SHA", |
| 16 | + "TLS_RSA_WITH_AES_128_CBC_SHA256", |
| 17 | + "TLS_RSA_WITH_AES_128_GCM_SHA256", |
| 18 | + "TLS_RSA_WITH_AES_256_CBC_SHA", |
| 19 | + "TLS_RSA_WITH_AES_256_GCM_SHA384", |
| 20 | + "TLS_RSA_WITH_RC4_128_SHA" |
| 21 | + ] |
| 22 | + |
| 23 | + result = invalid_flag(obj.spec.containers[0].command, dontwanted) |
| 24 | + msg := { |
| 25 | + "alertMessage": "The API server is not configured to use strong cryptographic ciphers", |
| 26 | + "alertScore": 2, |
| 27 | + "reviewPaths": result.failed_paths, |
| 28 | + "failedPaths": result.failed_paths, |
| 29 | + "fixPaths": result.fix_paths, |
| 30 | + |
| 31 | + "packagename": "armo_builtins", |
| 32 | + "alertObject": {"k8sApiObjects": [obj]}, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +is_api_server(obj) { |
| 37 | + obj.apiVersion == "v1" |
| 38 | + obj.kind == "Pod" |
| 39 | + obj.metadata.namespace == "kube-system" |
| 40 | + count(obj.spec.containers) == 1 |
| 41 | + count(obj.spec.containers[0].command) > 0 |
| 42 | + endswith(obj.spec.containers[0].command[0], "kube-apiserver") |
| 43 | +} |
| 44 | + |
| 45 | +get_flag_values(cmd) = {"origin": origin, "values": values} { |
| 46 | + re := " ?--tls-cipher-suites=(.+?)(?: |$)" |
| 47 | + matchs := regex.find_all_string_submatch_n(re, cmd, -1) |
| 48 | + count(matchs) == 1 |
| 49 | + values := [val | val := split(matchs[0][1], ",")[j]; val != ""] |
| 50 | + origin := matchs[0][0] |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +# Assume flag set only once |
| 55 | +invalid_flag(cmd, dontwanted) = result { |
| 56 | + flag := get_flag_values(cmd[i]) |
| 57 | + |
| 58 | + # value check |
| 59 | + dontuse = [x | x = dontwanted[_]; x in flag.values] |
| 60 | + count(dontuse) > 0 |
| 61 | + |
| 62 | + |
| 63 | + # get fixed and failed paths |
| 64 | + fixed_values := array.concat(flag.values, dontuse) |
| 65 | + fixed_flag = sprintf("%s=%s", ["--tls-cipher-suites", concat(",", fixed_values)]) |
| 66 | + fixed_cmd = replace(cmd[i], flag.origin, fixed_flag) |
| 67 | + path := sprintf("spec.containers[0].command[%d]", [i]) |
| 68 | + |
| 69 | + |
| 70 | + result := { |
| 71 | + "failed_paths": [path], |
| 72 | + "fix_paths": [{ |
| 73 | + "path": path, |
| 74 | + "value": fixed_cmd, |
| 75 | + }], |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +invalid_flag(cmd, wanted) = result { |
| 80 | + full_cmd := concat(" ", cmd) |
| 81 | + not contains(full_cmd, "--tls-cipher-suites") |
| 82 | + |
| 83 | + path = sprintf("spec.containers[0].command[%d]", [count(cmd)]) |
| 84 | + result = { |
| 85 | + "failed_paths": [], |
| 86 | + "fix_paths": [{ |
| 87 | + "path": path, |
| 88 | + "value": sprintf("--tls-cipher-suites=%s", [concat(",", wanted)]), |
| 89 | + }], |
| 90 | + } |
| 91 | +} |
0 commit comments