@@ -109,6 +109,117 @@ func (e *Entry) GetCredentialPrivateKeyData() (*EntryCredentialPrivateKeyData, b
109109 return data , ok
110110}
111111
112+ // ToCredentialMap flattens a credential entry into a map of fields keyed by a stable name.
113+ // It always includes "entry-id" and "entry-name" and then subtype-specific keys.
114+ func (e Entry ) ToCredentialMap () (map [string ]string , error ) {
115+ if e .GetType () != EntryCredentialType {
116+ return nil , fmt .Errorf ("unsupported entry type (%s). Only %s is supported" , e .GetType (), EntryCredentialType )
117+ }
118+
119+ secretMap := map [string ]string {
120+ "entry-id" : e .Id ,
121+ "entry-name" : e .Name ,
122+ }
123+
124+ switch e .SubType {
125+ case EntryCredentialSubTypeDefault :
126+ if data , ok := e .GetCredentialDefaultData (); ok {
127+ if data .Username != "" {
128+ secretMap ["username" ] = data .Username
129+ }
130+ if data .Password != "" {
131+ secretMap ["password" ] = data .Password
132+ }
133+ if data .Domain != "" {
134+ secretMap ["domain" ] = data .Domain
135+ }
136+ }
137+
138+ case EntryCredentialSubTypeAccessCode :
139+ if data , ok := e .GetCredentialAccessCodeData (); ok {
140+ if data .Password != "" {
141+ secretMap ["password" ] = data .Password
142+ }
143+ }
144+
145+ case EntryCredentialSubTypeApiKey :
146+ if data , ok := e .GetCredentialApiKeyData (); ok {
147+ if data .ApiId != "" {
148+ secretMap ["api-id" ] = data .ApiId
149+ }
150+ if data .ApiKey != "" {
151+ secretMap ["api-key" ] = data .ApiKey
152+ }
153+ if data .TenantId != "" {
154+ secretMap ["tenant-id" ] = data .TenantId
155+ }
156+ }
157+
158+ case EntryCredentialSubTypeAzureServicePrincipal :
159+ if data , ok := e .GetCredentialAzureServicePrincipalData (); ok {
160+ if data .ClientId != "" {
161+ secretMap ["client-id" ] = data .ClientId
162+ }
163+ if data .ClientSecret != "" {
164+ secretMap ["client-secret" ] = data .ClientSecret
165+ }
166+ if data .TenantId != "" {
167+ secretMap ["tenant-id" ] = data .TenantId
168+ }
169+ }
170+
171+ case EntryCredentialSubTypeConnectionString :
172+ if data , ok := e .GetCredentialConnectionStringData (); ok {
173+ if data .ConnectionString != "" {
174+ secretMap ["connection-string" ] = data .ConnectionString
175+ }
176+ }
177+
178+ case EntryCredentialSubTypePrivateKey :
179+ if data , ok := e .GetCredentialPrivateKeyData (); ok {
180+ if data .Username != "" {
181+ secretMap ["username" ] = data .Username
182+ }
183+ if data .Password != "" {
184+ secretMap ["password" ] = data .Password
185+ }
186+ if data .PrivateKey != "" {
187+ secretMap ["private-key" ] = data .PrivateKey
188+ }
189+ if data .PublicKey != "" {
190+ secretMap ["public-key" ] = data .PublicKey
191+ }
192+ if data .Passphrase != "" {
193+ secretMap ["passphrase" ] = data .Passphrase
194+ }
195+ }
196+
197+ default :
198+ return nil , fmt .Errorf ("unsupported credential subtype (%s)" , e .SubType )
199+ }
200+
201+ return secretMap , nil
202+ }
203+
204+ // SetCredentialSecret mutates the Entry data to update the secret value for supported subtypes.
205+ // It preserves the existing Type/SubType but overwrites Data for the relevant subtype.
206+ func (e * Entry ) SetCredentialSecret (secret string ) error {
207+ if e .GetType () != EntryCredentialType {
208+ return fmt .Errorf ("unsupported entry type (%s). Only %s is supported" , e .GetType (), EntryCredentialType )
209+ }
210+
211+ switch e .SubType {
212+ case EntryCredentialSubTypeDefault :
213+ e .Data = & EntryCredentialDefaultData {Password : secret }
214+ case EntryCredentialSubTypeAccessCode :
215+ e .Data = & EntryCredentialAccessCodeData {Password : secret }
216+ default :
217+ return fmt .Errorf ("cannot set secret for credential subtype (%s)" , e .SubType )
218+ }
219+
220+ return nil
221+ }
222+
112223// validateEntry checks if an Entry has the required fields and valid type/subtype.
113224func (c * EntryCredentialService ) validateEntry (entry * Entry ) error {
114225 if entry .VaultId == "" {
0 commit comments