@@ -15,6 +15,7 @@ import (
1515 "github.com/hashicorp/vault/helper/namespace"
1616 postgreshelper "github.com/hashicorp/vault/helper/testhelpers/postgresql"
1717 v5 "github.com/hashicorp/vault/sdk/database/dbplugin/v5"
18+ "github.com/hashicorp/vault/sdk/helper/testhelpers/snapshots"
1819 "github.com/hashicorp/vault/sdk/logical"
1920 "github.com/stretchr/testify/assert"
2021 "github.com/stretchr/testify/mock"
@@ -1494,3 +1495,90 @@ ALTER USER "{{name}}" WITH PASSWORD '{{password}}';
14941495const testRoleStaticUpdateRotation = `
14951496ALTER USER "{{name}}" WITH PASSWORD '{{password}}';GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "{{name}}";
14961497`
1498+
1499+ // TestStaticRole_Recover verifies that a role that exists in a snapshot can be
1500+ // read, listed, and recovered
1501+ func TestStaticRole_Recover (t * testing.T ) {
1502+ b , storage , mockDB := getBackend (t )
1503+ b2 , snapStorage , mockDBSnap := getBackend (t )
1504+ ctx := context .Background ()
1505+ defer b .Cleanup (ctx )
1506+ defer b2 .Cleanup (ctx )
1507+ tc := snapshots .NewSnapshotTestCaseWithStorages (t , b , storage , snapStorage )
1508+
1509+ configureDBMount (t , storage )
1510+ configureDBMount (t , snapStorage )
1511+
1512+ createRole (t , b2 , snapStorage , mockDBSnap , "hashicorp" )
1513+
1514+ tc .RunRead (t , "static-roles/hashicorp" )
1515+
1516+ tc .RunList (t , "static-roles" )
1517+
1518+ mockDB .On ("UpdateUser" , mock .Anything , mock .Anything ).
1519+ Return (v5.UpdateUserResponse {}, nil ).
1520+ Once ()
1521+ _ , err := tc .DoRecover (t , "static-roles/hashicorp" )
1522+ require .NoError (t , err )
1523+ readStaticCred (t , b , storage , mockDB , "hashicorp" )
1524+
1525+ mockDB .On ("UpdateUser" , mock .Anything , mock .Anything ).
1526+ Return (v5.UpdateUserResponse {}, nil ).
1527+ Once ()
1528+ _ , err = tc .DoRecover (t , "static-roles/hashicorp-copy" , snapshots .WithRecoverSourcePath ("static-roles/hashicorp" ))
1529+ require .NoError (t , err )
1530+ readStaticCred (t , b , storage , mockDB , "hashicorp-copy" )
1531+ }
1532+
1533+ // TestStaticRole_RecoverExists verifies that a static role cannot be updated
1534+ // via a recover operation, but can be copied to a new role
1535+ func TestStaticRole_RecoverExists (t * testing.T ) {
1536+ b , storage , mockDB := getBackend (t )
1537+ b2 , snapStorage , mockDBSnap := getBackend (t )
1538+ ctx := context .Background ()
1539+ defer b .Cleanup (ctx )
1540+ defer b2 .Cleanup (ctx )
1541+ tc := snapshots .NewSnapshotTestCaseWithStorages (t , b , storage , snapStorage )
1542+
1543+ configureDBMount (t , storage )
1544+ configureDBMount (t , snapStorage )
1545+
1546+ createRole (t , b2 , snapStorage , mockDBSnap , "hashicorp" )
1547+ createRole (t , b , storage , mockDB , "hashicorp" )
1548+
1549+ resp , err := tc .DoRecover (t , "static-roles/hashicorp" )
1550+ require .NoError (t , err )
1551+ require .True (t , resp .IsError ())
1552+ require .ErrorContains (t , resp .Error (), "cannot recover a static role that already exists" )
1553+
1554+ mockDB .On ("UpdateUser" , mock .Anything , mock .Anything ).
1555+ Return (v5.UpdateUserResponse {}, nil ).
1556+ Once ()
1557+ resp , err = tc .DoRecover (t , "static-roles/hashicorp-copy" , snapshots .WithRecoverSourcePath ("static-roles/hashicorp" ))
1558+ require .NoError (t , err )
1559+ require .False (t , resp .IsError ())
1560+ readStaticCred (t , b , storage , mockDB , "hashicorp-copy" )
1561+ }
1562+
1563+ // TestStaticCreds_Recover verifies that static credentials can be read from the
1564+ // snapshot without side effects, but they cannot be recovered
1565+ func TestStaticCreds_Recover (t * testing.T ) {
1566+ b , storage , mockDB := getBackend (t )
1567+ b2 , snapStorage , mockDBSnap := getBackend (t )
1568+ ctx := context .Background ()
1569+ defer b .Cleanup (ctx )
1570+ defer b2 .Cleanup (ctx )
1571+ tc := snapshots .NewSnapshotTestCaseWithStorages (t , b , storage , snapStorage )
1572+
1573+ configureDBMount (t , storage )
1574+ configureDBMount (t , snapStorage )
1575+
1576+ createRole (t , b2 , snapStorage , mockDBSnap , "hashicorp" )
1577+ createRole (t , b , storage , mockDB , "hashicorp" )
1578+
1579+ rotateRole (t , b , snapStorage , mockDB , "hashicorp" )
1580+ tc .RunRead (t , "static-creds/hashicorp" )
1581+
1582+ _ , err := tc .DoRecover (t , "static-creds/hashicorp" )
1583+ require .Error (t , err )
1584+ }
0 commit comments