2021-01-26 14:37:10 -05:00
package kms
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
wrapping "github.com/hashicorp/go-kms-wrapping"
)
// CreateOidcKeyVersion inserts into the repository and returns the new key
// version with its PrivateId. There are no valid options at this time.
func ( r * Repository ) CreateOidcKeyVersion ( ctx context . Context , rkvWrapper wrapping . Wrapper , oidcKeyId string , key [] byte , _ ... Option ) ( * OidcKeyVersion , error ) {
const op = "kms.(Repository).CreateOidcKeyVersion"
if rkvWrapper == nil {
2021-07-29 18:52:56 -06:00
return nil , errors . New ( ctx , errors . InvalidParameter , op , "missing root key version wrapper" )
2021-01-26 14:37:10 -05:00
}
rootKeyVersionId := rkvWrapper . KeyID ()
switch {
case ! strings . HasPrefix ( rootKeyVersionId , RootKeyVersionPrefix ):
2021-07-29 18:52:56 -06:00
return nil , errors . New ( ctx , errors . InvalidParameter , op , fmt . Sprintf ( "root key version id %s doesn't start with prefix %s" , rootKeyVersionId , RootKeyVersionPrefix ))
2021-01-26 14:37:10 -05:00
case rootKeyVersionId == "" :
2021-07-29 18:52:56 -06:00
return nil , errors . New ( ctx , errors . InvalidParameter , op , "missing root key version id" )
2021-01-26 14:37:10 -05:00
}
if oidcKeyId == "" {
2021-07-29 18:52:56 -06:00
return nil , errors . New ( ctx , errors . InvalidParameter , op , "missing oidc key id" )
2021-01-26 14:37:10 -05:00
}
if len ( key ) == 0 {
2021-07-29 18:52:56 -06:00
return nil , errors . New ( ctx , errors . InvalidParameter , op , "missing key" )
2021-01-26 14:37:10 -05:00
}
kv := AllocOidcKeyVersion ()
id , err := newOidcKeyVersionId ()
if err != nil {
2021-07-29 18:52:56 -06:00
return nil , errors . Wrap ( ctx , err , op )
2021-01-26 14:37:10 -05:00
}
kv . PrivateId = id
kv . RootKeyVersionId = rootKeyVersionId
kv . Key = key
kv . OidcKeyId = oidcKeyId
if err := kv . Encrypt ( ctx , rkvWrapper ); err != nil {
2021-07-29 18:52:56 -06:00
return nil , errors . Wrap ( ctx , err , op )
2021-01-26 14:37:10 -05:00
}
var returnedKey interface {}
_ , err = r . writer . DoTx (
ctx ,
db . StdRetryCnt ,
db . ExpBackoff {},
func ( _ db . Reader , w db . Writer ) error {
returnedKey = kv . Clone ()
// no oplog entries for root key version
if err := w . Create ( ctx , returnedKey ); err != nil {
2021-07-29 18:52:56 -06:00
return errors . Wrap ( ctx , err , op )
2021-01-26 14:37:10 -05:00
}
return nil
},
)
if err != nil {
2021-07-29 18:52:56 -06:00
return nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( fmt . Sprintf ( "failed for %s oidc key id" , kv . OidcKeyId )))
2021-01-26 14:37:10 -05:00
}
return returnedKey .( * OidcKeyVersion ), nil
}
// LookupOidcKeyVersion will look up a key version in the repository. If
// the key version is not found, it will return nil, nil.
func ( r * Repository ) LookupOidcKeyVersion ( ctx context . Context , keyWrapper wrapping . Wrapper , privateId string , _ ... Option ) ( * OidcKeyVersion , error ) {
const op = "kms.(Repository).LookupOidcKeyVersion"
if privateId == "" {
2021-07-29 18:52:56 -06:00
return nil , errors . New ( ctx , errors . InvalidParameter , op , "missing private id" )
2021-01-26 14:37:10 -05:00
}
if keyWrapper == nil {
2021-07-29 18:52:56 -06:00
return nil , errors . New ( ctx , errors . InvalidParameter , op , "missing key wrapper" )
2021-01-26 14:37:10 -05:00
}
k := AllocOidcKeyVersion ()
k . PrivateId = privateId
if err := r . reader . LookupById ( ctx , & k ); err != nil {
2021-07-29 18:52:56 -06:00
return nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( fmt . Sprintf ( "failed for %s" , privateId )))
2021-01-26 14:37:10 -05:00
}
if err := k . Decrypt ( ctx , keyWrapper ); err != nil {
2021-07-29 18:52:56 -06:00
return nil , errors . Wrap ( ctx , err , op )
2021-01-26 14:37:10 -05:00
}
return & k , nil
}
// DeleteOidcKeyVersion deletes the key version for the provided id from the
// repository returning a count of the number of records deleted. All options
// are ignored.
func ( r * Repository ) DeleteOidcKeyVersion ( ctx context . Context , privateId string , _ ... Option ) ( int , error ) {
const op = "kms.(Repository).DeleteOidcKeyVersion"
if privateId == "" {
2021-07-29 18:52:56 -06:00
return db . NoRowsAffected , errors . New ( ctx , errors . InvalidParameter , op , "missing private id" )
2021-01-26 14:37:10 -05:00
}
k := AllocOidcKeyVersion ()
k . PrivateId = privateId
if err := r . reader . LookupById ( ctx , & k ); err != nil {
2021-07-29 18:52:56 -06:00
return db . NoRowsAffected , errors . Wrap ( ctx , err , op , errors . WithMsg ( fmt . Sprintf ( "failed for %s" , privateId )))
2021-01-26 14:37:10 -05:00
}
var rowsDeleted int
_ , err := r . writer . DoTx (
ctx ,
db . StdRetryCnt ,
db . ExpBackoff {},
func ( _ db . Reader , w db . Writer ) ( err error ) {
dk := k . Clone ()
// no oplog entries for the key version
rowsDeleted , err = w . Delete ( ctx , dk )
if err != nil {
2021-07-29 18:52:56 -06:00
return errors . Wrap ( ctx , err , op )
2021-01-26 14:37:10 -05:00
}
if rowsDeleted > 1 {
2021-07-29 18:52:56 -06:00
return errors . New ( ctx , errors . MultipleRecords , op , "more than 1 resource would have been deleted" )
2021-01-26 14:37:10 -05:00
}
return nil
},
)
if err != nil {
2021-07-29 18:52:56 -06:00
return db . NoRowsAffected , errors . Wrap ( ctx , err , op , errors . WithMsg ( fmt . Sprintf ( "failed for %s" , privateId )))
2021-01-26 14:37:10 -05:00
}
return rowsDeleted , nil
}
// LatestOidcKeyVersion searches for the key version with the highest
// version number. When no results are found, it returns nil with an
// errors.RecordNotFound error.
func ( r * Repository ) LatestOidcKeyVersion ( ctx context . Context , rkvWrapper wrapping . Wrapper , OidcKeyId string , _ ... Option ) ( * OidcKeyVersion , error ) {
const op = "kms.(Repository).LatestOidcKeyVersion"
if OidcKeyId == "" {
2021-07-29 18:52:56 -06:00
return nil , errors . New ( ctx , errors . InvalidParameter , op , "missing oidc key id" )
2021-01-26 14:37:10 -05:00
}
if rkvWrapper == nil {
2021-07-29 18:52:56 -06:00
return nil , errors . New ( ctx , errors . InvalidParameter , op , "missing root key version wrapper" )
2021-01-26 14:37:10 -05:00
}
var foundKeys [] * OidcKeyVersion
if err := r . reader . SearchWhere ( ctx , & foundKeys , "oidc_key_id = ?" , [] interface {}{ OidcKeyId }, db . WithLimit ( 1 ), db . WithOrder ( "version desc" )); err != nil {
2021-07-29 18:52:56 -06:00
return nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( fmt . Sprintf ( "failed for %s" , OidcKeyId )))
2021-01-26 14:37:10 -05:00
}
if len ( foundKeys ) == 0 {
2021-07-29 18:52:56 -06:00
return nil , errors . E ( ctx , errors . WithCode ( errors . RecordNotFound ), errors . WithOp ( op ))
2021-01-26 14:37:10 -05:00
}
if err := foundKeys [ 0 ]. Decrypt ( ctx , rkvWrapper ); err != nil {
2021-07-29 18:52:56 -06:00
return nil , errors . Wrap ( ctx , err , op )
2021-01-26 14:37:10 -05:00
}
return foundKeys [ 0 ], nil
}
// ListOidcKeyVersions will lists versions of a key. Supports the WithLimit option.
func ( r * Repository ) ListOidcKeyVersions ( ctx context . Context , rkvWrapper wrapping . Wrapper , OidcKeyId string , opt ... Option ) ([] DekVersion , error ) {
const op = "kms.(Repository).ListOidcKeyVersions"
if OidcKeyId == "" {
2021-07-29 18:52:56 -06:00
return nil , errors . New ( ctx , errors . InvalidParameter , op , "missing oidc key id" )
2021-01-26 14:37:10 -05:00
}
if rkvWrapper == nil {
2021-07-29 18:52:56 -06:00
return nil , errors . New ( ctx , errors . InvalidParameter , op , "missing root key version wrapper" )
2021-01-26 14:37:10 -05:00
}
var versions [] * OidcKeyVersion
err := r . list ( ctx , & versions , "oidc_key_id = ?" , [] interface {}{ OidcKeyId }, opt ... )
if err != nil {
2021-07-29 18:52:56 -06:00
return nil , errors . Wrap ( ctx , err , op )
2021-01-26 14:37:10 -05:00
}
for i , k := range versions {
if err := k . Decrypt ( ctx , rkvWrapper ); err != nil {
2021-07-29 18:52:56 -06:00
return nil , errors . Wrap ( ctx , err , op , errors . WithMsg ( fmt . Sprintf ( "error decrypting key num %d" , i )))
2021-01-26 14:37:10 -05:00
}
}
dekVersions := make ([] DekVersion , 0 , len ( versions ))
for _ , version := range versions {
dekVersions = append ( dekVersions , version )
}
return dekVersions , nil
}