Skip to content

Commit 40229e6

Browse files
authored
fix(datastore): remove namespace from Key.String() (#10684) (#10823)
* fix(datastore): remove namespace from Key.String() (#10684) Datastore namespaces may be sensitive, and it's best not to emit them. Provide a `datastore.Key.StringWithNamespace()` as an alternative, and use this version internally, which preserves the fix from #7829. * amend! fix(datastore): remove namespace from Key.String() (#10684) fix(datastore): remove namespace from Key.String() Fixes #10684. Datastore namespaces may be sensitive, and it's best not to emit them. Restores the behavior of `Key.String` prior to #8363, but maintains the fix for #7829 by providing an internal implementation that does provide the namespace.
1 parent d50a370 commit 40229e6

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

datastore/datastore.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ func (c *Client) get(ctx context.Context, keys []*Key, dst interface{}, opts *pb
540540
multiErr[i] = fmt.Errorf("datastore: can't get the incomplete key: %v", k)
541541
any = true
542542
} else {
543-
ks := k.String()
543+
ks := k.stringInternal()
544544
if _, ok := keyMap[ks]; !ok {
545545
pbKeys = append(pbKeys, keyToProto(k))
546546
}
@@ -584,8 +584,8 @@ func (c *Client) get(ctx context.Context, keys []*Key, dst interface{}, opts *pb
584584
if err != nil {
585585
return txnID, errors.New("datastore: internal error: server returned an invalid key")
586586
}
587-
filled += len(keyMap[k.String()])
588-
for _, index := range keyMap[k.String()] {
587+
filled += len(keyMap[k.stringInternal()])
588+
for _, index := range keyMap[k.stringInternal()] {
589589
elem := v.Index(index)
590590
if multiArgType == multiArgTypePropertyLoadSaver || multiArgType == multiArgTypeStruct {
591591
elem = elem.Addr()
@@ -604,8 +604,8 @@ func (c *Client) get(ctx context.Context, keys []*Key, dst interface{}, opts *pb
604604
if err != nil {
605605
return txnID, errors.New("datastore: internal error: server returned an invalid key")
606606
}
607-
filled += len(keyMap[k.String()])
608-
for _, index := range keyMap[k.String()] {
607+
filled += len(keyMap[k.stringInternal()])
608+
for _, index := range keyMap[k.stringInternal()] {
609609
multiErr[index] = ErrNoSuchEntity
610610
}
611611
any = true
@@ -798,7 +798,7 @@ func deleteMutations(keys []*Key) ([]*pb.Mutation, error) {
798798
multiErr[i] = fmt.Errorf("datastore: can't delete the incomplete key: %v", k)
799799
hasErr = true
800800
} else {
801-
ks := k.String()
801+
ks := k.stringInternal()
802802
if !set[ks] {
803803
mutations = append(mutations, &pb.Mutation{
804804
Operation: &pb.Mutation_Delete{Delete: keyToProto(k)},

datastore/key.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ func (k *Key) Equal(o *Key) bool {
9393
}
9494

9595
// marshal marshals the key's string representation to the buffer.
96-
func (k *Key) marshal(b *bytes.Buffer) {
96+
// If includeSensitive is true, it will include the namespace when creating the string.
97+
func (k *Key) marshal(b *bytes.Buffer, includeSensitive bool) {
9798
if k.Parent != nil {
98-
k.Parent.marshal(b)
99+
k.Parent.marshal(b, includeSensitive)
99100
}
100101
b.WriteByte('/')
101102
b.WriteString(k.Kind)
@@ -105,19 +106,29 @@ func (k *Key) marshal(b *bytes.Buffer) {
105106
} else {
106107
b.WriteString(strconv.FormatInt(k.ID, 10))
107108
}
108-
if k.Namespace != "" {
109+
if k.Namespace != "" && includeSensitive {
109110
b.WriteByte(',')
110111
b.WriteString(k.Namespace)
111112
}
112113
}
113114

114-
// String returns a string representation of the key.
115+
// String returns a string representation of the key. It does not include
116+
// the namespace in case that the Namespace's name is sensitive information.
115117
func (k *Key) String() string {
118+
return k.string(false)
119+
}
120+
121+
// stringInternal is identical to String(), but appends the namespace.
122+
func (k *Key) stringInternal() string {
123+
return k.string(true)
124+
}
125+
126+
func (k *Key) string(includeSensitive bool) string {
116127
if k == nil {
117128
return ""
118129
}
119130
b := bytes.NewBuffer(make([]byte, 0, 512))
120-
k.marshal(b)
131+
k.marshal(b, includeSensitive)
121132
return b.String()
122133
}
123134

datastore/mutation.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func mutationProtos(muts []*Mutation) ([]*pb.Mutation, error) {
122122
seen := map[string]bool{}
123123
for _, m := range muts {
124124
if m.isDelete() {
125-
ks := m.key.String()
125+
ks := m.key.stringInternal()
126126
if seen[ks] {
127127
continue
128128
}

0 commit comments

Comments
 (0)