0

在哪里可以找到 AWS 策略的类型(结构)定义?例如,对于这样的政策

firstly := "{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "s3:ListBucket",
            "Resource": "arn:aws:s3:::amit",
            "Condition": {
                "StringLike": {
                    "s3:prefix": "Development/*"
                }
            }
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject"
            ],
            "Resource": "arn:aws:s3:::amit/Development/*"
        }
    ]
}

我目前正在执行以下操作来访问里面的字段:

var temp interface{}
json.Unmarshal([]byte(firstKey), &temp)
c := temp.(map[string]interface{})
fmt.Println(c)
#
map[Version:2012-10-17 Statement:[map[Resource:arn:aws:s3:::amit Condition:map[StringLike:map[s3:prefix:Development/*]] Sid:VisualEditor0 Effect:Allow Action:s3:ListBucket] map[Sid:VisualEditor1 Effect:Allow Action:[s3:PutObject s3:GetObject] Resource:arn:aws:s3:::amit/Development/*]]]

我想要一个像这样的结构

type Policy {
       Version string,
       Statement []blah
} 

并且在解组时,能够像 Policy.Version 一样访问。

4

2 回答 2

0

使用分离的结构使您的代码更有条理。

condition := struct {
        StringLike map[string]string
}

statement := []struct {
        Sid       string
        Effect    string
        Action    []string
        Resource  []string
        Condition interface{}
}

policyStruct, _ := json.Marshal(struct {
        Version   string
        Statement interface{}
}

更新:如果您需要在 Swagger 上记录它,我强烈建议使用OpenAPi。它生成服务器/客户端代码以及结构定义。

例子:

---
openapi: 3.0.3
info:
  version: "1.0.0"
  title: OpenAPI AWS Policy Map
security:
  - bearerAuth: []
paths:
  /policy-statements:
    get:
      operationId: listPolicyStatements
      description: Returns all existing policy statements
      responses:
        "200":
          description: A list of policy statements
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/PolicyStatement"

components:
  schemas:
    PolicyStatement:
      type: object
      required:
        - sid
        - effect
        - action
        - resource
      properties:
        sid:
          type: string
        effect:
          type: string
        action:
          type: array
          items:
            type: string
        resource:
          type: array
          items:
            type: string
        example:
          sid: bucketWrite
          effect: Allow
          action:
            - s3:List*
            - s3:Get*
            - s3:Put*
          resource:
            - arn:aws:s3:::xxxxxx/*
            - arn:aws:s3:::yyyyyy/*

于 2020-10-10T02:58:32.373 回答
0

查看https://mholt.github.io/json-to-go/。使用您的示例,它给了我(我将结构名称更改为 Policy;请原谅格式):

类型政策结构{

Version   string `json:"Version"`
Statement []struct {
    Sid       string `json:"Sid"`
    Effect    string `json:"Effect"`
    Action    string `json:"Action"`
    Resource  string `json:"Resource"`
    Condition struct {
        StringLike struct {
            S3Prefix string `json:"s3:prefix"`
        } `json:"StringLike"`
    } `json:"Condition,omitempty"`
} `json:"Statement"`

}

我将您的策略​​保存在一个文件 policy.json 中,然后运行:

contents, err := ioutil.ReadFile("policy.json")
if err != nil {
    os.Exit(1)
}

var thePolicy Policy
json.Unmarshal(contents, &thePolicy)
fmt.Println("Version: ", thePolicy.Version)

并得到:

Version:  2012-10-17
于 2018-01-30T16:30:06.630 回答