@@ -2,12 +2,10 @@ package db
22
33import (
44 "context"
5- "encoding/base64"
65 "fmt"
76 "strings"
87
98 "github.com/opentdf/platform/protocol/go/common"
10- "github.com/opentdf/platform/protocol/go/policy"
119 "github.com/opentdf/platform/protocol/go/policy/attributes"
1210 "github.com/opentdf/platform/protocol/go/policy/namespaces"
1311 "github.com/opentdf/platform/service/pkg/db"
@@ -112,102 +110,27 @@ func (c *PolicyDBClient) GetAttributesByValueFqns(ctx context.Context, r *attrib
112110 }
113111 }
114112
115- // map keys to grants
116113 for _ , pair := range list {
117- if pair == nil {
118- continue
119- }
120- // Loop through all keys in the attribute and create grants
121- for _ , key := range pair .Attribute .GetKasKeys () {
122- grant := & policy.KeyAccessServer {}
123- grant .Uri = key .GetKasUri ()
124-
125- kasKeyInfo := key .GetKey () // *policy.KasPublicKeyInfo
126- // Assuming kasKeyInfo is not nil based on SQL construction and panic location.
127- // If kasKeyInfo could be nil, an additional check would be needed here.
128-
129- kasPubKey := & policy.KasPublicKey {
130- Kid : kasKeyInfo .GetKeyId (),
131- Alg : policy .KasPublicKeyAlgEnum (kasKeyInfo .GetKeyAlgorithm ()),
132- }
133- // Check if PublicKeyCtx is available before accessing Pem
134- if pubKeyCtx := kasKeyInfo .GetPublicKeyCtx (); pubKeyCtx != nil {
135- // Grant pem isn't expected to be b64 encoded.
136- pem , err := base64 .StdEncoding .DecodeString (pubKeyCtx .GetPem ())
137- if err != nil {
138- return nil , fmt .Errorf ("failed to decode PEM for key %s: %w" , kasPubKey .Kid , err )
139- }
140- kasPubKey .Pem = string (pem )
141- }
142-
143- grant .PublicKey = & policy.PublicKey {
144- PublicKey : & policy.PublicKey_Cached {
145- Cached : & policy.KasPublicKeySet {
146- Keys : []* policy.KasPublicKey {kasPubKey },
147- },
148- },
149- }
150- pair .Attribute .Grants = append (pair .Attribute .Grants , grant )
151- }
152- // Loop through all keys on values and create grants
153- for _ , key := range pair .GetValue ().GetKasKeys () {
154- grant := & policy.KeyAccessServer {}
155- grant .Uri = key .GetKasUri ()
156-
157- kasKeyInfo := key .GetKey () // *policy.KasPublicKeyInfo
158- kasPubKey := & policy.KasPublicKey {
159- Kid : kasKeyInfo .GetKeyId (),
160- Alg : policy .KasPublicKeyAlgEnum (kasKeyInfo .GetKeyAlgorithm ()),
161- }
162- // Check if PublicKeyCtx is available before accessing Pem
163- if pubKeyCtx := kasKeyInfo .GetPublicKeyCtx (); pubKeyCtx != nil {
164- // Grant pem isn't expected to be b64 encoded.
165- pem , err := base64 .StdEncoding .DecodeString (pubKeyCtx .GetPem ())
166- if err != nil {
167- return nil , fmt .Errorf ("failed to decode PEM for key %s: %w" , kasPubKey .Kid , err )
168- }
169- kasPubKey .Pem = string (pem )
114+ if pair != nil {
115+ attrGrants , err := mapKasKeysToGrants (pair .GetAttribute ().GetKasKeys ())
116+ if err != nil {
117+ return nil , fmt .Errorf ("could not map KAS attribute keys to grants: %w" , err )
170118 }
171-
172- grant .PublicKey = & policy.PublicKey {
173- PublicKey : & policy.PublicKey_Cached {
174- Cached : & policy.KasPublicKeySet {
175- Keys : []* policy.KasPublicKey {kasPubKey },
176- },
177- },
119+ // Update the response map with the attribute grants
120+ pair .GetAttribute ().Grants = append (pair .GetAttribute ().Grants , attrGrants ... )
121+ // Update the value grants
122+ valGrants , err := mapKasKeysToGrants (pair .GetValue ().GetKasKeys ())
123+ if err != nil {
124+ return nil , fmt .Errorf ("could not map KAS value keys to grants: %w" , err )
178125 }
179- pair .Value .Grants = append (pair .Value .Grants , grant )
180- }
181-
182- // Map Namespace Keys to Grants
183- if ns := pair .Attribute .GetNamespace (); ns != nil {
184- for _ , key := range ns .GetKasKeys () {
185- grant := & policy.KeyAccessServer {}
186- grant .Uri = key .GetKasUri ()
187-
188- kasKeyInfo := key .GetKey () // *policy.KasPublicKeyInfo
189- kasPubKey := & policy.KasPublicKey {
190- Kid : kasKeyInfo .GetKeyId (),
191- Alg : policy .KasPublicKeyAlgEnum (kasKeyInfo .GetKeyAlgorithm ()),
192- }
193- // Check if PublicKeyCtx is available before accessing Pem
194- if pubKeyCtx := kasKeyInfo .GetPublicKeyCtx (); pubKeyCtx != nil {
195- pem , err := base64 .StdEncoding .DecodeString (pubKeyCtx .GetPem ())
196- if err != nil {
197- return nil , fmt .Errorf ("failed to decode PEM for key %s: %w" , kasPubKey .Kid , err )
198- }
199- kasPubKey .Pem = string (pem )
200- }
201-
202- grant .PublicKey = & policy.PublicKey {
203- PublicKey : & policy.PublicKey_Cached {
204- Cached : & policy.KasPublicKeySet {
205- Keys : []* policy.KasPublicKey {kasPubKey },
206- },
207- },
208- }
209- ns .Grants = append (ns .Grants , grant )
126+ pair .GetValue ().Grants = append (pair .GetValue ().Grants , valGrants ... )
127+ // Update Namespace grants
128+ nsGrants , err := mapKasKeysToGrants (pair .GetAttribute ().GetNamespace ().GetKasKeys ())
129+ if err != nil {
130+ return nil , fmt .Errorf ("could not map KAS namespace keys to grants: %w" , err )
210131 }
132+ // Update the response map with the namespace grants
133+ pair .GetAttribute ().Grants = append (pair .GetAttribute ().Grants , nsGrants ... )
211134 }
212135 }
213136
0 commit comments