From 98735a6d1247fd72cc09af7c9833a4b631220c4d Mon Sep 17 00:00:00 2001 From: Dhia Ayachi Date: Mon, 8 Nov 2021 11:43:21 -0500 Subject: [PATCH] KV refactoring, part 2 (#11512) * add partition to the kv get pretty print * fix failing test * add test for kvs RPC endpoint --- agent/consul/acl_endpoint_test.go | 40 ++++++++++++++++++++++++------- api/kv.go | 4 ++++ api/txn_test.go | 3 +++ command/kv/get/kv_get.go | 3 +++ command/kv/impexp/kvimpexp.go | 2 ++ 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/agent/consul/acl_endpoint_test.go b/agent/consul/acl_endpoint_test.go index bfe8a02200..54105fe19c 100644 --- a/agent/consul/acl_endpoint_test.go +++ b/agent/consul/acl_endpoint_test.go @@ -5248,14 +5248,18 @@ func TestValidateBindingRuleBindName(t *testing.T) { } // upsertTestToken creates a token for testing purposes -func upsertTestToken(codec rpc.ClientCodec, masterToken string, datacenter string, - tokenModificationFn func(token *structs.ACLToken)) (*structs.ACLToken, error) { +func upsertTestTokenInEntMeta(codec rpc.ClientCodec, masterToken string, datacenter string, + tokenModificationFn func(token *structs.ACLToken), entMeta *structs.EnterpriseMeta) (*structs.ACLToken, error) { + if entMeta == nil { + entMeta = structs.DefaultEnterpriseMetaInDefaultPartition() + } arg := structs.ACLTokenSetRequest{ Datacenter: datacenter, ACLToken: structs.ACLToken{ - Description: "User token", - Local: false, - Policies: nil, + Description: "User token", + Local: false, + Policies: nil, + EnterpriseMeta: *entMeta, }, WriteRequest: structs.WriteRequest{Token: masterToken}, } @@ -5279,15 +5283,21 @@ func upsertTestToken(codec rpc.ClientCodec, masterToken string, datacenter strin return &out, nil } -func upsertTestTokenWithPolicyRules(codec rpc.ClientCodec, masterToken string, datacenter string, rules string) (*structs.ACLToken, error) { - policy, err := upsertTestPolicyWithRules(codec, masterToken, datacenter, rules) +func upsertTestToken(codec rpc.ClientCodec, masterToken string, datacenter string, + tokenModificationFn func(token *structs.ACLToken)) (*structs.ACLToken, error) { + return upsertTestTokenInEntMeta(codec, masterToken, datacenter, + tokenModificationFn, structs.DefaultEnterpriseMetaInDefaultPartition()) +} + +func upsertTestTokenWithPolicyRulesInEntMeta(codec rpc.ClientCodec, masterToken string, datacenter string, rules string, entMeta *structs.EnterpriseMeta) (*structs.ACLToken, error) { + policy, err := upsertTestPolicyWithRulesInEntMeta(codec, masterToken, datacenter, rules, entMeta) if err != nil { return nil, err } - token, err := upsertTestToken(codec, masterToken, datacenter, func(token *structs.ACLToken) { + token, err := upsertTestTokenInEntMeta(codec, masterToken, datacenter, func(token *structs.ACLToken) { token.Policies = []structs.ACLTokenPolicyLink{{ID: policy.ID}} - }) + }, entMeta) if err != nil { return nil, err } @@ -5295,6 +5305,10 @@ func upsertTestTokenWithPolicyRules(codec rpc.ClientCodec, masterToken string, d return token, nil } +func upsertTestTokenWithPolicyRules(codec rpc.ClientCodec, masterToken string, datacenter string, rules string) (*structs.ACLToken, error) { + return upsertTestTokenWithPolicyRulesInEntMeta(codec, masterToken, datacenter, rules, nil) +} + func retrieveTestTokenAccessorForSecret(codec rpc.ClientCodec, masterToken string, datacenter string, id string) (string, error) { arg := structs.ACLTokenGetRequest{ TokenID: id, @@ -5402,8 +5416,16 @@ func upsertTestPolicy(codec rpc.ClientCodec, masterToken string, datacenter stri } func upsertTestPolicyWithRules(codec rpc.ClientCodec, masterToken string, datacenter string, rules string) (*structs.ACLPolicy, error) { + return upsertTestPolicyWithRulesInEntMeta(codec, masterToken, datacenter, rules, structs.DefaultEnterpriseMetaInDefaultPartition()) +} + +func upsertTestPolicyWithRulesInEntMeta(codec rpc.ClientCodec, masterToken string, datacenter string, rules string, entMeta *structs.EnterpriseMeta) (*structs.ACLPolicy, error) { return upsertTestCustomizedPolicy(codec, masterToken, datacenter, func(policy *structs.ACLPolicy) { + if entMeta == nil { + entMeta = structs.DefaultEnterpriseMetaInDefaultPartition() + } policy.Rules = rules + policy.EnterpriseMeta = *entMeta }) } diff --git a/api/kv.go b/api/kv.go index 47dfa66944..85a9d7750c 100644 --- a/api/kv.go +++ b/api/kv.go @@ -44,6 +44,10 @@ type KVPair struct { // Namespace is the namespace the KVPair is associated with // Namespacing is a Consul Enterprise feature. Namespace string `json:",omitempty"` + + // Partition is the partition the KVPair is associated with + // Admin Partition is a Consul Enterprise feature. + Partition string `json:",omitempty"` } // KVPairs is a list of KVPair objects diff --git a/api/txn_test.go b/api/txn_test.go index 3d69baff10..e418062e0e 100644 --- a/api/txn_test.go +++ b/api/txn_test.go @@ -153,6 +153,7 @@ func TestAPI_ClientTxn(t *testing.T) { CreateIndex: ret.Results[0].KV.CreateIndex, ModifyIndex: ret.Results[0].KV.ModifyIndex, Namespace: ret.Results[0].KV.Namespace, + Partition: defaultPartition, }, }, &TxnResult{ @@ -164,6 +165,7 @@ func TestAPI_ClientTxn(t *testing.T) { CreateIndex: ret.Results[1].KV.CreateIndex, ModifyIndex: ret.Results[1].KV.ModifyIndex, Namespace: ret.Results[0].KV.Namespace, + Partition: defaultPartition, }, }, &TxnResult{ @@ -264,6 +266,7 @@ func TestAPI_ClientTxn(t *testing.T) { CreateIndex: ret.Results[0].KV.CreateIndex, ModifyIndex: ret.Results[0].KV.ModifyIndex, Namespace: ret.Results[0].KV.Namespace, + Partition: defaultPartition, }, }, &TxnResult{ diff --git a/command/kv/get/kv_get.go b/command/kv/get/kv_get.go index 136202b6c3..099aedb9fc 100644 --- a/command/kv/get/kv_get.go +++ b/command/kv/get/kv_get.go @@ -199,6 +199,9 @@ func prettyKVPair(w io.Writer, pair *api.KVPair, base64EncodeValue bool) error { } else { fmt.Fprintf(tw, "Session\t%s\n", pair.Session) } + if pair.Partition != "" { + fmt.Fprintf(tw, "Partition\t%s\n", pair.Partition) + } if pair.Namespace != "" { fmt.Fprintf(tw, "Namespace\t%s\n", pair.Namespace) } diff --git a/command/kv/impexp/kvimpexp.go b/command/kv/impexp/kvimpexp.go index ed1472785d..4f4c7e87e1 100644 --- a/command/kv/impexp/kvimpexp.go +++ b/command/kv/impexp/kvimpexp.go @@ -11,6 +11,7 @@ type Entry struct { Flags uint64 `json:"flags"` Value string `json:"value"` Namespace string `json:"namespace,omitempty"` + Partition string `json:"partition,omitempty"` } func ToEntry(pair *api.KVPair) *Entry { @@ -19,5 +20,6 @@ func ToEntry(pair *api.KVPair) *Entry { Flags: pair.Flags, Value: base64.StdEncoding.EncodeToString(pair.Value), Namespace: pair.Namespace, + Partition: pair.Partition, } }