使用来自 base64 的不同级别的相同密钥解密值

Decrypt values with the same key at different levels from base64

我的输入如下。我想搜索 SearchString 密钥(你可以看到我们不能为它使用固定索引)并且当密钥出现时从 base64 解密它的值(可能使用 @base64d 过滤器)。 JQ可以吗?如果是,怎么做?

[
  {
    "Name": "searchblock",
    "Priority": 3,
    "Statement": {
      "RateBasedStatement": {
        "Limit": 100,
        "AggregateKeyType": "IP",
        "ScopeDownStatement": {
          "ByteMatchStatement": {
            "SearchString": "Y2F0YWxvZ3NlYXJjaA==",
            "FieldToMatch": {
              "UriPath": {}
            },
            "TextTransformations": [
              {
                "Priority": 0,
                "Type": "LOWERCASE"
              }
            ],
            "PositionalConstraint": "CONTAINS"
          }
        }
      }
    },
    "Action": {
      "Block": {}
    },
    "VisibilityConfig": {
      "SampledRequestsEnabled": true,
      "CloudWatchMetricsEnabled": true,
      "MetricName": "searchblock"
    }
  },
  {
    "Name": "bot-block",
    "Priority": 4,
    "Statement": {
      "ByteMatchStatement": {
        "SearchString": "Ym90",
        "FieldToMatch": {
          "SingleHeader": {
            "Name": "user-agent"
          }
        },
        "TextTransformations": [
          {
            "Priority": 0,
            "Type": "LOWERCASE"
          }
        ],
        "PositionalConstraint": "CONTAINS"
      }
    },
    "Action": {
      "Allow": {}
    },
    "VisibilityConfig": {
      "SampledRequestsEnabled": true,
      "CloudWatchMetricsEnabled": true,
      "MetricName": "user-agent"
    }
  }
]

当固定路径不可用时,我们使用 pathpathsgetpathsetpath 内置函数进行此类操作。

getpath(paths | select(.[-1] == "SearchString")) |= @base64d

Online demo

walk 对于这种任务来说非常直观:

walk(if type == "object" and .SearchString 
     then .SearchString |= @base64d else . end)

使用这种方法,修改程序以使其更健壮也很简单,例如检查 .SearchString 是否为字符串:

walk(if type == "object" and (.SearchString|type) == "string" 
     then .SearchString |= @base64d else . end)

注意:如果您的 jq 不包含 walk,您只需从任何信誉良好的网站或 https://github.com/stedolan/jq/blob/master/src/builtin.jq

复制其 def