From 3958c70ecf968e5d3dfcba942573a104c5cb0159 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Martins?= Date: Wed, 3 Jun 2020 20:03:16 +0200 Subject: [PATCH] pkg/identity: protect LabelsSHA256 against concurrent initializations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit LabelsSHA256 can be protected with a sync.Once and it makes sure this field is only initialized once. Fixes: a77ba963cecc ("create identity package") Signed-off-by: André Martins --- pkg/identity/identity.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/identity/identity.go b/pkg/identity/identity.go index 7b220110b157..1eba1cb553cb 100644 --- a/pkg/identity/identity.go +++ b/pkg/identity/identity.go @@ -17,6 +17,7 @@ package identity import ( "fmt" "net" + "sync" "github.com/cilium/cilium/pkg/labels" ) @@ -28,6 +29,9 @@ type Identity struct { ID NumericIdentity `json:"id"` // Set of labels that belong to this Identity. Labels labels.Labels `json:"labels"` + + // onceLabelSHA256 makes sure LabelsSHA256 is only set once + onceLabelSHA256 sync.Once // SHA256 of labels. LabelsSHA256 string `json:"labelsSHA256"` @@ -92,9 +96,11 @@ func (id *Identity) Sanitize() { // GetLabelsSHA256 returns the SHA256 of the labels associated with the // identity. The SHA is calculated if not already cached. func (id *Identity) GetLabelsSHA256() string { - if id.LabelsSHA256 == "" { - id.LabelsSHA256 = id.Labels.SHA256Sum() - } + id.onceLabelSHA256.Do(func() { + if id.LabelsSHA256 == "" { + id.LabelsSHA256 = id.Labels.SHA256Sum() + } + }) return id.LabelsSHA256 }