AWS策略的类型定义

时间:2018-01-19 23:52:52

标签: aws-sdk-go

在哪里可以找到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一样访问。

2 个答案:

答案 0 :(得分:0)

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

输入Policy struct {

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

答案 1 :(得分: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/*