@@ -17,6 +17,10 @@ func credsStaticPath(name string) string {
1717 return fmt .Sprintf ("%s/%s" , "static-creds" , name )
1818}
1919
20+ func rotateStaticCreds (name string ) string {
21+ return fmt .Sprintf ("%s/%s" , "rotate-role" , name )
22+ }
23+
2024func TestStaticCredentialsRead_ok (t * testing.T ) {
2125 userID , _ := uuid .GenerateUUID ()
2226 secret , _ := uuid .GenerateUUID ()
@@ -45,7 +49,7 @@ func TestStaticCredentialsRead_ok(t *testing.T) {
4549 t .Run ("user_token" , func (t * testing.T ) {
4650 require .NoError (t , s .Put (context .Background (), cloudEntry ))
4751
48- roleName := createSaveRandomStaticRole (t , s , projectName , "token" , secret )
52+ roleName := createSaveRandomStaticRole (t , s , projectName , "token" , secret , "" )
4953
5054 res , err := b .HandleRequest (context .Background (), & logical.Request {
5155 Operation : logical .ReadOperation ,
@@ -58,7 +62,7 @@ func TestStaticCredentialsRead_ok(t *testing.T) {
5862 t .Run ("user_password" , func (t * testing.T ) {
5963 require .NoError (t , s .Put (context .Background (), cloudEntry ))
6064
61- roleName := createSaveRandomStaticRole (t , s , projectName , "password" , secret )
65+ roleName := createSaveRandomStaticRole (t , s , projectName , "password" , secret , "" )
6266
6367 res , err := b .HandleRequest (context .Background (), & logical.Request {
6468 Operation : logical .ReadOperation ,
@@ -78,7 +82,7 @@ func TestStaticCredentialsRead_error(t *testing.T) {
7882
7983 b , s := testBackend (t , failVerbRead )
8084
81- roleName := createSaveRandomStaticRole (t , s , "" , "token" , secret )
85+ roleName := createSaveRandomStaticRole (t , s , "" , "token" , secret , "" )
8286
8387 _ , err := b .HandleRequest (context .Background (), & logical.Request {
8488 Path : credsStaticPath (roleName ),
@@ -120,7 +124,7 @@ func TestStaticCredentialsRead_error(t *testing.T) {
120124
121125 b , s := testBackend (t )
122126
123- roleName := createSaveRandomStaticRole (t , s , data .ProjectName , data .ServiceType , secret )
127+ roleName := createSaveRandomStaticRole (t , s , data .ProjectName , data .ServiceType , secret , "" )
124128
125129 testClient := thClient .ServiceClient ()
126130 authURL := testClient .Endpoint + "v3"
@@ -146,7 +150,127 @@ func TestStaticCredentialsRead_error(t *testing.T) {
146150 }
147151}
148152
149- func createSaveRandomStaticRole (t * testing.T , s logical.Storage , projectName , sType string , secret string ) string {
153+ func TestRotateStaticCredentials_ok (t * testing.T ) {
154+ userID , _ := uuid .GenerateUUID ()
155+ secret , _ := uuid .GenerateUUID ()
156+ projectName := tools .RandomString ("p" , 5 )
157+
158+ fixtures .SetupKeystoneMock (t , userID , projectName , fixtures.EnabledMocks {
159+ TokenPost : true ,
160+ TokenGet : true ,
161+ PasswordChange : true ,
162+ })
163+
164+ testClient := thClient .ServiceClient ()
165+ authURL := testClient .Endpoint + "v3"
166+
167+ b , s := testBackend (t )
168+ cloudEntry , err := logical .StorageEntryJSON (storageCloudKey (testCloudName ), & OsCloud {
169+ Name : testCloudName ,
170+ AuthURL : authURL ,
171+ UserDomainName : testUserDomainName ,
172+ Username : testUsername ,
173+ Password : testPassword1 ,
174+ UsernameTemplate : testTemplate1 ,
175+ })
176+ require .NoError (t , err )
177+
178+ t .Run ("user_token" , func (t * testing.T ) {
179+ require .NoError (t , s .Put (context .Background (), cloudEntry ))
180+
181+ roleName := createSaveRandomStaticRole (t , s , projectName , "token" , secret , userID )
182+
183+ _ , err := b .HandleRequest (context .Background (), & logical.Request {
184+ Operation : logical .CreateOperation ,
185+ Path : rotateStaticCreds (roleName ),
186+ Storage : s ,
187+ })
188+ require .NoError (t , err )
189+ })
190+ t .Run ("user_password" , func (t * testing.T ) {
191+ require .NoError (t , s .Put (context .Background (), cloudEntry ))
192+
193+ roleName := createSaveRandomStaticRole (t , s , projectName , "password" , secret , userID )
194+
195+ res , err := b .HandleRequest (context .Background (), & logical.Request {
196+ Operation : logical .ReadOperation ,
197+ Path : credsStaticPath (roleName ),
198+ Storage : s ,
199+ })
200+ require .NoError (t , err )
201+ require .NotEmpty (t , res .Data )
202+ })
203+ }
204+
205+ func TestRotateStaticCredentials_error (t * testing.T ) {
206+ t .Parallel ()
207+
208+ t .Run ("read-fail" , func (t * testing.T ) {
209+ userID , _ := uuid .GenerateUUID ()
210+ projectName := tools .RandomString ("p" , 5 )
211+ fixtures .SetupKeystoneMock (t , userID , projectName , fixtures.EnabledMocks {})
212+
213+ b , s := testBackend (t , failVerbRead )
214+
215+ roleName := createSaveRandomStaticRole (t , s , projectName , "password" , "" , "" )
216+
217+ _ , err := b .HandleRequest (context .Background (), & logical.Request {
218+ Path : "rotate-role/" + roleName ,
219+ Operation : logical .CreateOperation ,
220+ Storage : s ,
221+ })
222+ require .Error (t , err )
223+ })
224+
225+ cases := map [string ]fixtures.EnabledMocks {
226+ "no-change" : {
227+ TokenPost : true , TokenGet : true ,
228+ },
229+ "no-post" : {
230+ TokenGet : true , PasswordChange : true ,
231+ },
232+ "no-get" : {
233+ TokenPost : true , PasswordChange : true ,
234+ },
235+ }
236+
237+ for name , data := range cases {
238+ t .Run (name , func (t * testing.T ) {
239+ data := data
240+ userID , _ := uuid .GenerateUUID ()
241+ secret , _ := uuid .GenerateUUID ()
242+ projectName := tools .RandomString ("p" , 5 )
243+
244+ fixtures .SetupKeystoneMock (t , userID , projectName , data )
245+
246+ testClient := thClient .ServiceClient ()
247+ authURL := testClient .Endpoint + "v3"
248+
249+ b , s := testBackend (t )
250+ cloudEntry , err := logical .StorageEntryJSON (storageCloudKey (testCloudName ), & OsCloud {
251+ Name : testCloudName ,
252+ AuthURL : authURL ,
253+ UserDomainName : testUserDomainName ,
254+ Username : testUsername ,
255+ Password : testPassword1 ,
256+ UsernameTemplate : testTemplate1 ,
257+ })
258+ require .NoError (t , err )
259+ require .NoError (t , s .Put (context .Background (), cloudEntry ))
260+
261+ roleName := createSaveRandomStaticRole (t , s , projectName , "token" , secret , userID )
262+
263+ _ , err = b .HandleRequest (context .Background (), & logical.Request {
264+ Path : "rotate-role/" + roleName ,
265+ Operation : logical .CreateOperation ,
266+ Storage : s ,
267+ })
268+ require .Error (t , err )
269+ })
270+ }
271+ }
272+
273+ func createSaveRandomStaticRole (t * testing.T , s logical.Storage , projectName , sType string , secret string , userId string ) string {
150274 roleName := randomRoleName ()
151275 role := map [string ]interface {}{
152276 "name" : roleName ,
@@ -156,6 +280,7 @@ func createSaveRandomStaticRole(t *testing.T, s logical.Storage, projectName, sT
156280 "secret_type" : sType ,
157281 "secret" : secret ,
158282 "username" : roleName ,
283+ "user_id" : userId ,
159284 }
160285 saveRawStaticRole (t , roleName , role , s )
161286
0 commit comments