第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Go 中創(chuàng)建 KMS 密鑰策略

在 Go 中創(chuàng)建 KMS 密鑰策略

Go
Smart貓小萌 2023-02-21 16:41:28
我正在嘗試使用AWS SDK v2函數(shù)調(diào)用創(chuàng)建 KMS 密鑰:conn := kms.NewFromConfig(cfg)input := kms.CreateKeyInput{    KeySpec:     types.KeySpecEccNistP521,    KeyUsage:    types.KeyUsageTypeSignVerify,    MultiRegion: aws.Bool(true),    Policy:      aws.String("")}output, err := conn.CreateKey(ctx, &input)我遇到的問題是我不確定如何為密鑰生成策略。我假設(shè)我可以為 IAM 策略文檔創(chuàng)建 JSON,但我不覺得自己生成它的前景特別吸引人。是否有可用于生成此文檔的包或庫?
查看完整描述

1 回答

?
慕碼人2483693

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個贊

我最終創(chuàng)建了自己的策略結(jié)構(gòu):


// Policy describes a policy document that can be used to configure permissions in IAM

type Policy struct {

    Version    string       `json:"Version"`

    ID         string       `json:"Id"`

    Statements []*Statement `json:"Statement"`

}


// Statement describes a set of permissions that define what resources and users should have access

// to the resources described therein

type Statement struct {

    ID            string     `json:"Sid"`

    Effect        Effect     `json:"Effect"`

    PrincipalArns Principals `json:"Principal"`

    ActionArns    Actions    `json:"Action"`

    ResourceArns  Resources  `json:"Resource"`

}


// Principals describes a list of principals associated with a policy statement

type Principals []string


// MarhsalJSON converts a Principals collection to JSON

func (p Principals) MarshalJSON() ([]byte, error) {


    // First, get the inner string from the list of principals

    var inner string

    if len(p) > 1 {

        inner = marshal(p...)

    } else if len(p) == 1 {

        inner = strings.Quote(p[0], "\"")

    } else {

        return nil, fmt.Errorf("Principal must contain at least one element")

    }


    // Next, create the principal block and return it

    return []byte(fmt.Sprintf("{\"AWS\": %s}", inner)), nil

}


// Actions describes a list of actions that may or may not be taken by principals with regard to the

// resources described in a policy statement

type Actions []Action


// MarshalJSON converts an Actions collection to JSON

func (a Actions) MarshalJSON() ([]byte, error) {


    // First, get the inner string from the list of actions

    var inner string

    if len(a) > 1 {

        inner = marshal(a...)

    } else if len(a) == 1 {

        inner = strings.Quote(a[0], "\"")

    } else {

        return nil, fmt.Errorf("Action must contain at least one element")

    }


    // Next, create the action block and return it

    return []byte(inner), nil

}


// Resources describes a list of resources effected by the policy statement

type Resources []string


// MarshalJSON converts a Resources collection to JSON

func (r Resources) MarshalJSON() ([]byte, error) {


    // First, get the inner string from the list of actions

    var inner string

    if len(r) > 1 {

        inner = marshal(r...)

    } else if len(r) == 1 {

        inner = strings.Quote(r[0], "\"")

    } else {

        return nil, fmt.Errorf("Resource must contain at least one element")

    }


    // Next, create the action block and return it

    return []byte(inner), nil

}


// Helper function that converts a list of items to a JSON-string

func marshal[S ~string](items ...S) string {

    return "[" + strings.ModifyAndJoin(func(item string) string { return strings.Quote(item, "\"") }, ",", items...) + "]"

}


// Effect describes the effect a policy statement will have upon the resource and for the actions described

type Effect string


var (


    // Allow to grant access of the resource and actions to the principals described in the policy statement

    Allow = Effect("Allow")


    // Deny to deny access of the resource and actions from the principals described in the policy statement

    Deny = Effect("Deny")

)


// Action describes a valid operation that may be made against a particular AWS resource

type Action string


// Describes the various action types available to AWS

var (

    CancelKeyDeletion                   = Action("kms:CancelKeyDeletion")

    ConnectCustomKeyStore               = Action("kms:ConnectCustomKeyStore")

    CreateAlias                         = Action("kms:CreateAlias")

    CreateCustomKeyStore                = Action("kms:CreateCustomKeyStore")

    CreateGrant                         = Action("kms:CreateGrant")

    CreateKey                           = Action("kms:CreateKey")

    Decrypt                             = Action("kms:Decrypt")

    DeleteAlias                         = Action("kms:DeleteAlias")

    DeleteCustomKeyStore                = Action("kms:DeleteCustomKeyStore")

    DeleteImportedKeyMaterial           = Action("kms:DeleteImportedKeyMaterial")

    DescribeCustomKeyStores             = Action("kms:DescribeCustomKeyStores")

    DescribeKey                         = Action("kms:DescribeKey")

    DisableKey                          = Action("kms:DisableKey")

    DisableKeyRotation                  = Action("kms:DisableKeyRotation")

    DisconnectCustomKeyStore            = Action("kms:DisconnectCustomKeyStore")

    EnableKey                           = Action("kms:EnableKey")

    EnableKeyRotation                   = Action("kms:EnableKeyRotation")

    Encrypt                             = Action("kms:Encrypt")

    GenerateDataKey                     = Action("kms:GenerateDataKey")

    GenerateDataKeyPair                 = Action("kms:GenerateDataKeyPair")

    GenerateDataKeyPairWithoutPlaintext = Action("kms:GenerateDataKeyPairWithoutPlaintext")

    GenerateDataKeyWithoutPlaintext     = Action("kms:GenerateDataKeyWithoutPlaintext")

    GenerateMac                         = Action("kms:GenerateMac")

    GenerateRandom                      = Action("kms:GenerateRandom")

    GetKeyPolicy                        = Action("kms:GetKeyPolicy")

    GetKeyRotationStatus                = Action("kms:GetKeyRotationStatus")

    GetParametersForImport              = Action("kms:GetParametersForImport")

    GetPublicKey                        = Action("kms:GetPublicKey")

    ImportKeyMaterial                   = Action("kms:ImportKeyMaterial")

    ListAliases                         = Action("kms:ListAliases")

    ListGrants                          = Action("kms:ListGrants")

    ListKeyPolicies                     = Action("kms:ListKeyPolicies")

    ListKeys                            = Action("kms:ListKeys")

    ListResourceTags                    = Action("kms:ListResourceTags")

    ListRetirableGrants                 = Action("kms:ListRetirableGrants")

    PutKeyPolicy                        = Action("kms:PutKeyPolicy")

    ReEncryptFrom                       = Action("kms:ReEncryptFrom")

    ReEncryptTo                         = Action("kms:ReEncryptTo")

    ReplicateKey                        = Action("kms:ReplicateKey")

    RetireGrant                         = Action("kms:RetireGrant")

    RevokeGrant                         = Action("kms:RevokeGrant")

    ScheduleKeyDeletion                 = Action("kms:ScheduleKeyDeletion")

    Sign                                = Action("kms:Sign")

    TagResource                         = Action("kms:TagResource")

    UntagResource                       = Action("kms:UntagResource")

    UpdateAlias                         = Action("kms:UpdateAlias")

    UpdateCustomKeyStore                = Action("kms:UpdateCustomKeyStore")

    UpdateKeyDescription                = Action("kms:UpdateKeyDescription")

    UpdatePrimaryRegion                 = Action("kms:UpdatePrimaryRegion")

    Verify                              = Action("kms:Verify")

    VerifyMac                           = Action("kms:VerifyMac")

    KmsAll                              = Action("kms:*")

)

然后我可以像這樣在我的代碼中使用它:


conn := kms.NewFromConfig(cfg)


policy := Policy {

    Version: "2012-10-17",

    ID:      "test-key",

    Statements: []*policy.Statement{

        {

            ID:            "test-failure",

            Effect:        policy.Allow,

            PrincipalArns: []string{"arn:aws:kms:eu-west-2:111122223333:root"},

            ActionArns:    policy.Actions{policy.KmsAll},

            ResourceArns:  []string{"*"},

        },

    },

}


pData, err := json.Marshal(policy)

if err != nil {

   return err

}


input := kms.CreateKeyInput{

    KeySpec:     types.KeySpecEccNistP521,

    KeyUsage:    types.KeyUsageTypeSignVerify,

    MultiRegion: aws.Bool(true),

    Policy:      aws.String(string(pData)),

}


output, err := conn.CreateKey(ctx, &input)

我在可以在此處找到的開源包中添加了此代碼,以便其他人可以使用它。


查看完整回答
反對 回復(fù) 2023-02-21
  • 1 回答
  • 0 關(guān)注
  • 101 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號