@@ -375,13 +375,13 @@ const (
375
375
376
376
// CreateKey creates a new key in the specified transit secret engine
377
377
// See: https://developer.hashicorp.com/vault/api-docs/secret/transit#create-key
378
- func (c * Client ) CreateKey (ctx context.Context , spireKeyID string , keyType TransitKeyType ) error {
378
+ func (c * Client ) CreateKey (ctx context.Context , keyName string , keyType TransitKeyType ) error {
379
379
arguments := map [string ]interface {}{
380
380
"type" : keyType ,
381
381
"exportable" : "false" , // SPIRE keys are never exportable
382
382
}
383
383
384
- _ , err := c .vaultClient .Logical ().WriteWithContext (ctx , fmt .Sprintf ("/%s/keys/%s" , c .clientParams .TransitEnginePath , spireKeyID ), arguments )
384
+ _ , err := c .vaultClient .Logical ().WriteWithContext (ctx , fmt .Sprintf ("/%s/keys/%s" , c .clientParams .TransitEnginePath , keyName ), arguments )
385
385
if err != nil {
386
386
return status .Errorf (codes .Internal , "failed to create transit engine key: %v" , err )
387
387
}
@@ -391,7 +391,7 @@ func (c *Client) CreateKey(ctx context.Context, spireKeyID string, keyType Trans
391
391
392
392
// SignData signs the data using the transit engine key with the provided spire key id.
393
393
// See: https://developer.hashicorp.com/vault/api-docs/secret/transit#sign-data
394
- func (c * Client ) SignData (ctx context.Context , spireKeyID string , data []byte , hashAlgo TransitHashAlgorithm , signatureAlgo TransitSignatureAlgorithm ) ([]byte , error ) {
394
+ func (c * Client ) SignData (ctx context.Context , keyName string , data []byte , hashAlgo TransitHashAlgorithm , signatureAlgo TransitSignatureAlgorithm ) ([]byte , error ) {
395
395
encodedData := base64 .StdEncoding .EncodeToString (data )
396
396
397
397
body := map [string ]interface {}{
@@ -401,7 +401,7 @@ func (c *Client) SignData(ctx context.Context, spireKeyID string, data []byte, h
401
401
"prehashed" : "true" ,
402
402
}
403
403
404
- sigResp , err := c .vaultClient .Logical ().WriteWithContext (ctx , fmt .Sprintf ("/%s/sign/%s/%s" , c .clientParams .TransitEnginePath , spireKeyID , hashAlgo ), body )
404
+ sigResp , err := c .vaultClient .Logical ().WriteWithContext (ctx , fmt .Sprintf ("/%s/sign/%s/%s" , c .clientParams .TransitEnginePath , keyName , hashAlgo ), body )
405
405
if err != nil {
406
406
return nil , status .Errorf (codes .Internal , "transit engine sign call failed: %v" , err )
407
407
}
@@ -449,18 +449,18 @@ func (c *Client) GetKeys(ctx context.Context) ([]*keyEntry, error) {
449
449
return nil , status .Errorf (codes .Internal , "transit engine list keys call was successful but keys are missing" )
450
450
}
451
451
452
- keyIds , ok := keys .([]interface {})
452
+ keyNames , ok := keys .([]interface {})
453
453
if ! ok {
454
- return nil , status .Errorf (codes .Internal , "expected keys data type %T but got %T" , keyIds , keys )
454
+ return nil , status .Errorf (codes .Internal , "expected keys data type %T but got %T" , keyNames , keys )
455
455
}
456
456
457
- for _ , keyId := range keyIds {
458
- keyIdStr , ok := keyId .(string )
457
+ for _ , keyName := range keyNames {
458
+ keyNameStr , ok := keyName .(string )
459
459
if ! ok {
460
- return nil , status .Errorf (codes .Internal , "expected key id data type %T but got %T" , keyIdStr , keyId )
460
+ return nil , status .Errorf (codes .Internal , "expected key id data type %T but got %T" , keyNameStr , keyName )
461
461
}
462
462
463
- keyEntry , err := c .getKeyEntry (ctx , keyIdStr )
463
+ keyEntry , err := c .getKeyEntry (ctx , keyNameStr )
464
464
if err != nil {
465
465
return nil , err
466
466
}
@@ -471,9 +471,14 @@ func (c *Client) GetKeys(ctx context.Context) ([]*keyEntry, error) {
471
471
return keyEntries , nil
472
472
}
473
473
474
- // getKeyEntry gets the transit engine key with the specified spire key id and converts it into a key entry.
475
- func (c * Client ) getKeyEntry (ctx context.Context , spireKeyID string ) (* keyEntry , error ) {
476
- keyData , err := c .getKey (ctx , spireKeyID )
474
+ // getKeyEntry gets the transit engine key with the specified key name and converts it into a key entry.
475
+ func (c * Client ) getKeyEntry (ctx context.Context , keyName string ) (* keyEntry , error ) {
476
+ spireKeyID , ok := spireKeyIDFromKeyName (keyName )
477
+ if ! ok {
478
+ return nil , status .Errorf (codes .Internal , "unable to get SPIRE key ID from key %s" , keyName )
479
+ }
480
+
481
+ keyData , err := c .getKey (ctx , keyName )
477
482
if err != nil {
478
483
return nil , err
479
484
}
@@ -519,6 +524,7 @@ func (c *Client) getKeyEntry(ctx context.Context, spireKeyID string) (*keyEntry,
519
524
}
520
525
521
526
return & keyEntry {
527
+ KeyName : keyName ,
522
528
PublicKey : & keymanagerv1.PublicKey {
523
529
Id : spireKeyID ,
524
530
Type : keyType ,
@@ -530,8 +536,8 @@ func (c *Client) getKeyEntry(ctx context.Context, spireKeyID string) (*keyEntry,
530
536
531
537
// getKey returns a specific key from the transit engine.
532
538
// See: https://developer.hashicorp.com/vault/api-docs/secret/transit#read-key
533
- func (c * Client ) getKey (ctx context.Context , spireKeyID string ) (map [string ]interface {}, error ) {
534
- res , err := c .vaultClient .Logical ().ReadWithContext (ctx , fmt .Sprintf ("/%s/keys/%s" , c .clientParams .TransitEnginePath , spireKeyID ))
539
+ func (c * Client ) getKey (ctx context.Context , keyName string ) (map [string ]interface {}, error ) {
540
+ res , err := c .vaultClient .Logical ().ReadWithContext (ctx , fmt .Sprintf ("/%s/keys/%s" , c .clientParams .TransitEnginePath , keyName ))
535
541
if err != nil {
536
542
return nil , status .Errorf (codes .Internal , "failed to get transit engine key: %v" , err )
537
543
}
@@ -558,3 +564,17 @@ func (c *Client) getKey(ctx context.Context, spireKeyID string) (map[string]inte
558
564
559
565
return currentKeyMap , nil
560
566
}
567
+
568
+ // spireKeyIDFromKeyName parses a Key Vault key name to get the
569
+ // SPIRE Key ID. This Key ID is used in the Server KeyManager interface.
570
+ func spireKeyIDFromKeyName (keyName string ) (string , bool ) {
571
+ // A key name would have the format <UUID>-<SPIRE-KEY-ID>.
572
+ // first we find the position where the SPIRE Key ID starts.
573
+ spireKeyIDIndex := 37 // 36 is the UUID length plus one '-' separator
574
+ if spireKeyIDIndex >= len (keyName ) {
575
+ // The index is out of range.
576
+ return "" , false
577
+ }
578
+ spireKeyID := keyName [spireKeyIDIndex :]
579
+ return spireKeyID , true
580
+ }
0 commit comments