@@ -5142,6 +5142,122 @@ func TestGetUsername(t *testing.T) {
5142
5142
})
5143
5143
}
5144
5144
5145
+ func TestAuthorizationMountBlob (t * testing.T ) {
5146
+ Convey ("Make a new controller" , t , func () {
5147
+ port := test .GetFreePort ()
5148
+ baseURL := test .GetBaseURL (port )
5149
+
5150
+ conf := config .New ()
5151
+ conf .HTTP .Port = port
5152
+ // have two users: one for user Policy, and another for default policy
5153
+ username1 , _ := test .GenerateRandomString ()
5154
+ password1 , _ := test .GenerateRandomString ()
5155
+ username2 , _ := test .GenerateRandomString ()
5156
+ password2 , _ := test .GenerateRandomString ()
5157
+ username1 = strings .ToLower (username1 )
5158
+ username2 = strings .ToLower (username2 )
5159
+
5160
+ content := test .GetCredString (username1 , password1 ) + test .GetCredString (username2 , password2 )
5161
+ htpasswdPath := test .MakeHtpasswdFileFromString (content )
5162
+ defer os .Remove (htpasswdPath )
5163
+
5164
+ conf .HTTP .Auth = & config.AuthConfig {
5165
+ HTPasswd : config.AuthHTPasswd {
5166
+ Path : htpasswdPath ,
5167
+ },
5168
+ }
5169
+
5170
+ user1Repo := fmt .Sprintf ("%s/**" , username1 )
5171
+ user2Repo := fmt .Sprintf ("%s/**" , username2 )
5172
+
5173
+ // config with all policy types, to test that the correct one is applied in each case
5174
+ conf .HTTP .AccessControl = & config.AccessControlConfig {
5175
+ Repositories : config.Repositories {
5176
+ user1Repo : config.PolicyGroup {
5177
+ Policies : []config.Policy {
5178
+ {
5179
+ Users : []string {username1 },
5180
+ Actions : []string {
5181
+ constants .ReadPermission ,
5182
+ constants .CreatePermission ,
5183
+ },
5184
+ },
5185
+ },
5186
+ },
5187
+ user2Repo : config.PolicyGroup {
5188
+ Policies : []config.Policy {
5189
+ {
5190
+ Users : []string {username2 },
5191
+ Actions : []string {
5192
+ constants .ReadPermission ,
5193
+ constants .CreatePermission ,
5194
+ },
5195
+ },
5196
+ },
5197
+ },
5198
+ },
5199
+ }
5200
+
5201
+ dir := t .TempDir ()
5202
+
5203
+ ctlr := api .NewController (conf )
5204
+ ctlr .Config .Storage .RootDirectory = dir
5205
+
5206
+ cm := test .NewControllerManager (ctlr )
5207
+ cm .StartAndWait (port )
5208
+ defer cm .StopServer ()
5209
+
5210
+ userClient1 := resty .New ()
5211
+ userClient1 .SetBasicAuth (username1 , password1 )
5212
+
5213
+ userClient2 := resty .New ()
5214
+ userClient2 .SetBasicAuth (username2 , password2 )
5215
+
5216
+ img := CreateImageWith ().RandomLayers (1 , 2 ).DefaultConfig ().Build ()
5217
+
5218
+ repoName1 := username1 + "/" + "myrepo"
5219
+ tag := "1.0"
5220
+
5221
+ // upload image with user1 on repoName1
5222
+ err := UploadImageWithBasicAuth (img , baseURL , repoName1 , tag , username1 , password1 )
5223
+ So (err , ShouldBeNil )
5224
+
5225
+ repoName2 := username2 + "/" + "myrepo"
5226
+
5227
+ blobDigest := img .Manifest .Layers [0 ].Digest
5228
+
5229
+ /* a HEAD request by user2 on blob digest (found in user1Repo) should return 404
5230
+ because user2 doesn't have permissions to read user1Repo */
5231
+ resp , err := userClient2 .R ().Head (baseURL + fmt .Sprintf ("/v2/%s/blobs/%s" , repoName2 , blobDigest ))
5232
+ So (err , ShouldBeNil )
5233
+ So (resp .StatusCode (), ShouldEqual , http .StatusNotFound )
5234
+
5235
+ params := make (map [string ]string )
5236
+ params ["mount" ] = blobDigest .String ()
5237
+
5238
+ // trying to mount a blob which can be found in cache, but user doesn't have permission
5239
+ // should return 202 instead of 201
5240
+ resp , err = userClient2 .R ().SetQueryParams (params ).Post (baseURL + "/v2/" + repoName2 + "/blobs/uploads/" )
5241
+ So (err , ShouldBeNil )
5242
+ So (resp .StatusCode (), ShouldEqual , http .StatusAccepted )
5243
+
5244
+ /* a HEAD request by user1 on blob digest (found in user1Repo) should return 200
5245
+ because user1 has permission to read user1Repo */
5246
+ resp , err = userClient1 .R ().Head (baseURL + fmt .Sprintf ("/v2/%s/blobs/%s" , username1 + "/" + "mysecondrepo" , blobDigest ))
5247
+ So (err , ShouldBeNil )
5248
+ So (resp .StatusCode (), ShouldEqual , http .StatusOK )
5249
+
5250
+ // user2 can upload without dedupe
5251
+ err = UploadImageWithBasicAuth (img , baseURL , repoName2 , tag , username2 , password2 )
5252
+ So (err , ShouldBeNil )
5253
+
5254
+ // trying to mount a blob which can be found in cache and user has permission should return 201 instead of 202
5255
+ resp , err = userClient2 .R ().SetQueryParams (params ).Post (baseURL + "/v2/" + repoName2 + "/blobs/uploads/" )
5256
+ So (err , ShouldBeNil )
5257
+ So (resp .StatusCode (), ShouldEqual , http .StatusCreated )
5258
+ })
5259
+ }
5260
+
5145
5261
func TestAuthorizationWithOnlyAnonymousPolicy (t * testing.T ) {
5146
5262
Convey ("Make a new controller" , t , func () {
5147
5263
const TestRepo = "my-repos/repo"
0 commit comments