@@ -51,6 +51,25 @@ func decodeSecret(blob []byte, secret store.Secret) error {
5151 return secret .Unmarshal (val )
5252}
5353
54+ func mapToWindowsAttributes (attributes map [string ]string ) []wincred.CredentialAttribute {
55+ winAttrs := make ([]wincred.CredentialAttribute , 0 , len (attributes ))
56+ for k , v := range attributes {
57+ winAttrs = append (winAttrs , wincred.CredentialAttribute {
58+ Keyword : k ,
59+ Value : []byte (v ),
60+ })
61+ }
62+ return winAttrs
63+ }
64+
65+ func mapFromWindowsAttributes (winAttrs []wincred.CredentialAttribute ) map [string ]string {
66+ attributes := make (map [string ]string , len (winAttrs ))
67+ for _ , attr := range winAttrs {
68+ attributes [attr .Keyword ] = string (attr .Value )
69+ }
70+ return attributes
71+ }
72+
5473func (k * keychainStore [T ]) Delete (_ context.Context , id store.ID ) error {
5574 if err := id .Valid (); err != nil {
5675 return err
@@ -74,10 +93,7 @@ func (k *keychainStore[T]) Get(_ context.Context, id store.ID) (store.Secret, er
7493 return nil , mapWindowsCredentialError (err )
7594 }
7695
77- attributes := make (map [string ]string )
78- for _ , attr := range gc .Attributes {
79- attributes [attr .Keyword ] = string (attr .Value )
80- }
96+ attributes := cleanSecretAttributes (mapFromWindowsAttributes (gc .Attributes ))
8197
8298 secret := k .factory ()
8399 if err := secret .SetMetadata (attributes ); err != nil {
@@ -148,12 +164,9 @@ func (k *keychainStore[T]) GetAll(context.Context) (map[store.ID]store.Secret, e
148164 continue
149165 }
150166
151- secretAttr := make (map [string ]string , len (cred .Attributes ))
152- for _ , attr := range cred .Attributes {
153- secretAttr [attr .Keyword ] = string (attr .Value )
154- }
167+ attributes := cleanSecretAttributes (mapFromWindowsAttributes (cred .Attributes ))
155168 secret := k .factory ()
156- if err := secret .SetMetadata (secretAttr ); err != nil {
169+ if err := secret .SetMetadata (attributes ); err != nil {
157170 return nil , err
158171 }
159172 secrets [id ] = secret
@@ -172,44 +185,19 @@ func (k *keychainStore[T]) Save(_ context.Context, id store.ID, secret store.Sec
172185 return err
173186 }
174187
175- attributes := []wincred.CredentialAttribute {}
176- parts := strings .SplitSeq (id .String (), "/" )
177- for p := range parts {
178- if p == "" {
179- continue
180- }
181- attributes = append (attributes , wincred.CredentialAttribute {
182- Keyword : p ,
183- Value : []byte (p ),
184- })
185- }
186- for k , v := range secret .Metadata () {
187- attributes = append (attributes , wincred.CredentialAttribute {
188- Keyword : k ,
189- Value : []byte (v ),
190- })
191- }
188+ attributes := make (map [string ]string )
189+ maps .Copy (attributes , secret .Metadata ())
190+ maps .Copy (attributes , convertSecretID (id ))
192191
193- attributes = append (attributes ,
194- wincred.CredentialAttribute {
195- Keyword : "id" ,
196- Value : []byte (id .String ()),
197- },
198- wincred.CredentialAttribute {
199- Keyword : "service:group" ,
200- Value : []byte (k .serviceGroup ),
201- },
202- wincred.CredentialAttribute {
203- Keyword : "service:name" ,
204- Value : []byte (k .serviceName ),
205- },
206- )
192+ attributes ["id" ] = id .String ()
193+ attributes ["service:group" ] = k .serviceGroup
194+ attributes ["service:name" ] = k .serviceName
207195
208196 g := wincred .NewGenericCredential (k .itemLabel (id ))
209197 g .UserName = id .String ()
210198 g .CredentialBlob = blob
211199 g .Persist = wincred .PersistLocalMachine
212- g .Attributes = attributes
200+ g .Attributes = mapToWindowsAttributes ( attributes )
213201 return mapWindowsCredentialError (g .Write ())
214202}
215203
@@ -223,16 +211,9 @@ func (k *keychainStore[T]) Filter(_ context.Context, id store.ID, attributes map
223211
224212 attrs := make (map [string ]string )
225213 maps .Copy (attrs , attributes )
214+ maps .Copy (attrs , convertSecretID (id ))
226215
227- parts := strings .SplitSeq (id .String (), "/" )
228- for p := range parts {
229- if p == "" {
230- continue
231- }
232- attrs [p ] = p
233- }
234-
235- secrets := make (map [store.ID ]store.Secret , len (credentials ))
216+ secrets := make (map [store.ID ]store.Secret )
236217 for cred := range findServiceCredentials (k , attrs , credentials ) {
237218 id , err := store .ParseID (strings .ReplaceAll (cred .TargetName , onlyLabelPrefix , "" ))
238219 if err != nil {
@@ -250,13 +231,10 @@ func (k *keychainStore[T]) Filter(_ context.Context, id store.ID, attributes map
250231 return nil , err
251232 }
252233
253- gcAttr := make (map [string ]string )
254- for _ , attr := range gc .Attributes {
255- gcAttr [attr .Keyword ] = string (attr .Value )
256- }
234+ gcAttributes := cleanSecretAttributes (mapFromWindowsAttributes (gc .Attributes ))
257235
258236 secret := k .factory ()
259- if err := secret .SetMetadata (gcAttr ); err != nil {
237+ if err := secret .SetMetadata (gcAttributes ); err != nil {
260238 return nil , err
261239 }
262240 if err := secret .Unmarshal (blob ); err != nil {
0 commit comments