Skip to content

Commit b453b17

Browse files
committed
fix anonymous user access with PAB defined and fix PAB tests
Signed-off-by: Utkarsh Srivastava <[email protected]>
1 parent 043d40c commit b453b17

File tree

5 files changed

+47
-38
lines changed

5 files changed

+47
-38
lines changed

src/api/bucket_api.js

+3
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,9 @@ module.exports = {
13211321
},
13221322
cors_configuration_rules: {
13231323
$ref: 'common_api#/definitions/bucket_cors_configuration',
1324+
},
1325+
public_access_block: {
1326+
$ref: 'common_api#/definitions/public_access_block',
13241327
}
13251328
}
13261329
},

src/endpoint/s3/s3_rest.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,21 @@ async function authorize_request_policy(req) {
230230
if (req.op_name === 'put_bucket') return;
231231

232232
// owner_account is { id: bucket.owner_account, email: bucket.bucket_owner };
233-
const { s3_policy, system_owner, bucket_owner, owner_account } = await req.object_sdk.read_bucket_sdk_policy_info(req.params.bucket);
233+
const {
234+
s3_policy,
235+
system_owner,
236+
bucket_owner,
237+
owner_account,
238+
public_access_block,
239+
} = await req.object_sdk.read_bucket_sdk_policy_info(req.params.bucket);
240+
234241
const auth_token = req.object_sdk.get_auth_token();
235242
const arn_path = _get_arn_from_req_path(req);
236243
const method = _get_method_from_req(req);
237244

238245
const is_anon = !(auth_token && auth_token.access_key);
239246
if (is_anon) {
240-
await authorize_anonymous_access(s3_policy, method, arn_path, req);
247+
await authorize_anonymous_access(s3_policy, method, arn_path, req, public_access_block);
241248
return;
242249
}
243250

@@ -283,21 +290,20 @@ async function authorize_request_policy(req) {
283290
let permission_by_id;
284291
let permission_by_name;
285292

286-
const public_access_block_cfg = await req.object_sdk.get_public_access_block({ name: req.params.bucket });
287293
// In NC, we allow principal to be:
288294
// 1. account name (for backwards compatibility)
289295
// 2. account id
290296
// we start the permission check on account identifier intentionally
291297
if (account_identifier_id) {
292298
permission_by_id = await s3_bucket_policy_utils.has_bucket_policy_permission(
293-
s3_policy, account_identifier_id, method, arn_path, req, public_access_block_cfg?.public_access_block?.restrict_public_buckets);
299+
s3_policy, account_identifier_id, method, arn_path, req, public_access_block?.restrict_public_buckets);
294300
dbg.log3('authorize_request_policy: permission_by_id', permission_by_id);
295301
}
296302
if (permission_by_id === "DENY") throw new S3Error(S3Error.AccessDenied);
297303

298304
if ((!account_identifier_id || permission_by_id !== "DENY") && account.owner === undefined) {
299305
permission_by_name = await s3_bucket_policy_utils.has_bucket_policy_permission(
300-
s3_policy, account_identifier_name, method, arn_path, req, public_access_block_cfg?.public_access_block?.restrict_public_buckets
306+
s3_policy, account_identifier_name, method, arn_path, req, public_access_block?.restrict_public_buckets
301307
);
302308
dbg.log3('authorize_request_policy: permission_by_name', permission_by_name);
303309
}
@@ -307,11 +313,11 @@ async function authorize_request_policy(req) {
307313
throw new S3Error(S3Error.AccessDenied);
308314
}
309315

310-
async function authorize_anonymous_access(s3_policy, method, arn_path, req) {
316+
async function authorize_anonymous_access(s3_policy, method, arn_path, req, public_access_block) {
311317
if (!s3_policy) throw new S3Error(S3Error.AccessDenied);
312318

313319
const permission = await s3_bucket_policy_utils.has_bucket_policy_permission(
314-
s3_policy, undefined, method, arn_path, req);
320+
s3_policy, undefined, method, arn_path, req, public_access_block?.restrict_public_buckets);
315321
if (permission === "ALLOW") return;
316322

317323
throw new S3Error(S3Error.AccessDenied);

src/sdk/object_sdk.js

+1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ class ObjectSDK {
219219
system_owner: bucket.system_owner, // note that bucketspace_fs currently doesn't return system_owner
220220
bucket_owner: bucket.bucket_owner,
221221
owner_account: bucket.owner_account, // in NC NSFS this is the account id that owns the bucket
222+
public_access_block: bucket.public_access_block,
222223
};
223224
return policy_info;
224225
}

src/server/system_services/bucket_server.js

+1
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@ async function read_bucket_sdk_info(req) {
716716
.then(get_bucket_info),
717717
notifications: bucket.notifications,
718718
cors_configuration_rules: bucket.cors_configuration_rules,
719+
public_access_block: bucket.public_access_block,
719720
};
720721

721722
if (bucket.namespace) {

src/test/unit_tests/test_public_access_block.js

+29-31
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async function run_on_random_bucket(s3, bucket_prefix, cb) {
4646
try {
4747
await cb(bucket);
4848
} finally {
49-
await s3.deleteBucket({ Bucket: bucket });
49+
await s3.deleteBucket({ Bucket: bucket }).catch(() => { /* nothing */ });
5050
}
5151
}
5252

@@ -110,29 +110,33 @@ mocha.describe('noobaa public access block test', function() {
110110

111111
mocha.it('put_public_access_block must throw when acls are used', async function() {
112112
await run_on_random_bucket(s3_owner, bucket_prefix, async bucket => {
113-
try {
114-
await s3_owner.putPublicAccessBlock({
113+
await assert.rejects(
114+
s3_owner.putPublicAccessBlock({
115115
Bucket: bucket,
116116
PublicAccessBlockConfiguration: {
117117
BlockPublicAcls: true,
118118
}
119-
});
120-
assert.fail("expected to fail");
121-
} catch (error) {
122-
assert(error.Code === S3Error.AccessControlListNotSupported.code);
123-
}
119+
}),
120+
error => {
121+
// @ts-ignore
122+
assert(error.Code === S3Error.AccessControlListNotSupported.code);
123+
return true;
124+
}
125+
);
124126

125-
try {
126-
await s3_owner.putPublicAccessBlock({
127+
await assert.rejects(
128+
s3_owner.putPublicAccessBlock({
127129
Bucket: bucket,
128130
PublicAccessBlockConfiguration: {
129131
IgnorePublicAcls: true,
130132
}
131-
});
132-
assert.fail("expected to fail");
133-
} catch (error) {
134-
assert(error.Code === S3Error.AccessControlListNotSupported.code);
135-
}
133+
}),
134+
error => {
135+
// @ts-ignore
136+
assert(error.Code === S3Error.AccessControlListNotSupported.code);
137+
return true;
138+
}
139+
);
136140
});
137141
});
138142

@@ -146,15 +150,12 @@ mocha.describe('noobaa public access block test', function() {
146150
});
147151

148152
// Ensure we cannot put a public policy on this bucket
149-
try {
150-
await s3_owner.putBucketPolicy({
153+
await assert.rejects(
154+
s3_owner.putBucketPolicy({
151155
Bucket: bucket,
152156
Policy: generate_public_policy(bucket),
153-
});
154-
assert.fail("expected to fail");
155-
} catch {
156-
assert.ok(true);
157-
}
157+
})
158+
);
158159

159160
await s3_owner.deletePublicAccessBlock({
160161
Bucket: bucket,
@@ -163,7 +164,7 @@ mocha.describe('noobaa public access block test', function() {
163164
});
164165

165166
mocha.it('public_access_block should work when restrict public buckets is used', async function() {
166-
await run_on_random_bucket(s3_owner, bucket_prefix, async bucket => {
167+
await run_on_random_bucket(s3_owner, bucket_prefix, async function(bucket) {
167168
const KEY = "key";
168169

169170
await s3_owner.putObject({
@@ -191,16 +192,13 @@ mocha.describe('noobaa public access block test', function() {
191192
}
192193
});
193194

194-
try {
195-
// Ensure anon can no longer access
196-
await s3_anon.getObject({
195+
// Ensure anon can no longer access
196+
await assert.rejects(
197+
s3_anon.getObject({
197198
Bucket: bucket,
198199
Key: KEY,
199-
});
200-
assert.fail("expected to fail after PublicAccessBlock");
201-
} catch {
202-
assert.ok(true);
203-
}
200+
}),
201+
);
204202

205203
await s3_owner.deletePublicAccessBlock({
206204
Bucket: bucket,

0 commit comments

Comments
 (0)