Skip to content

Fix anonymous user access with public_access defined and fix public_access tests #9007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/api/bucket_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,9 @@ module.exports = {
},
cors_configuration_rules: {
$ref: 'common_api#/definitions/bucket_cors_configuration',
},
public_access_block: {
$ref: 'common_api#/definitions/public_access_block',
}
}
},
Expand Down
20 changes: 13 additions & 7 deletions src/endpoint/s3/s3_rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,21 @@ async function authorize_request_policy(req) {
if (req.op_name === 'put_bucket') return;

// owner_account is { id: bucket.owner_account, email: bucket.bucket_owner };
const { s3_policy, system_owner, bucket_owner, owner_account } = await req.object_sdk.read_bucket_sdk_policy_info(req.params.bucket);
const {
s3_policy,
system_owner,
bucket_owner,
owner_account,
public_access_block,
} = await req.object_sdk.read_bucket_sdk_policy_info(req.params.bucket);

const auth_token = req.object_sdk.get_auth_token();
const arn_path = _get_arn_from_req_path(req);
const method = _get_method_from_req(req);

const is_anon = !(auth_token && auth_token.access_key);
if (is_anon) {
await authorize_anonymous_access(s3_policy, method, arn_path, req);
await authorize_anonymous_access(s3_policy, method, arn_path, req, public_access_block);
return;
}

Expand Down Expand Up @@ -283,21 +290,20 @@ async function authorize_request_policy(req) {
let permission_by_id;
let permission_by_name;

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

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

async function authorize_anonymous_access(s3_policy, method, arn_path, req) {
async function authorize_anonymous_access(s3_policy, method, arn_path, req, public_access_block) {
if (!s3_policy) throw new S3Error(S3Error.AccessDenied);

const permission = await s3_bucket_policy_utils.has_bucket_policy_permission(
s3_policy, undefined, method, arn_path, req);
s3_policy, undefined, method, arn_path, req, public_access_block?.restrict_public_buckets);
if (permission === "ALLOW") return;

throw new S3Error(S3Error.AccessDenied);
Expand Down
1 change: 1 addition & 0 deletions src/sdk/object_sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ class ObjectSDK {
system_owner: bucket.system_owner, // note that bucketspace_fs currently doesn't return system_owner
bucket_owner: bucket.bucket_owner,
owner_account: bucket.owner_account, // in NC NSFS this is the account id that owns the bucket
public_access_block: bucket.public_access_block,
};
return policy_info;
}
Expand Down
1 change: 1 addition & 0 deletions src/server/system_services/bucket_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ async function read_bucket_sdk_info(req) {
.then(get_bucket_info),
notifications: bucket.notifications,
cors_configuration_rules: bucket.cors_configuration_rules,
public_access_block: bucket.public_access_block,
};

if (bucket.namespace) {
Expand Down
60 changes: 29 additions & 31 deletions src/test/unit_tests/test_public_access_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async function run_on_random_bucket(s3, bucket_prefix, cb) {
try {
await cb(bucket);
} finally {
await s3.deleteBucket({ Bucket: bucket });
await s3.deleteBucket({ Bucket: bucket }).catch(() => { /* nothing */ });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would delete bucket fail? shouldn't we handle such failure?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because sometimes the cb code might forget to delete all the objects (and their versions) from the bucket but that doesn't effect the correctness.

Failing to delete should be OK if the cb deems so. We can obviously write code to handle those cases but I think that would be more fragile without adding anything to the correctness of the respective tests. WDYT?

}
}

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

mocha.it('put_public_access_block must throw when acls are used', async function() {
await run_on_random_bucket(s3_owner, bucket_prefix, async bucket => {
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the difference between the old tests and the new? can you add a short explanation in the PR of why did the test not work, and what you changed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, actually assert.fail throws but the finally runs anyway so basically its like .catch(_.noop). Which gives us false positives.

await s3_owner.putPublicAccessBlock({
await assert.rejects(
s3_owner.putPublicAccessBlock({
Bucket: bucket,
PublicAccessBlockConfiguration: {
BlockPublicAcls: true,
}
});
assert.fail("expected to fail");
} catch (error) {
assert(error.Code === S3Error.AccessControlListNotSupported.code);
}
}),
error => {
// @ts-ignore
assert(error.Code === S3Error.AccessControlListNotSupported.code);
return true;
}
);

try {
await s3_owner.putPublicAccessBlock({
await assert.rejects(
s3_owner.putPublicAccessBlock({
Bucket: bucket,
PublicAccessBlockConfiguration: {
IgnorePublicAcls: true,
}
});
assert.fail("expected to fail");
} catch (error) {
assert(error.Code === S3Error.AccessControlListNotSupported.code);
}
}),
error => {
// @ts-ignore
assert(error.Code === S3Error.AccessControlListNotSupported.code);
return true;
}
);
});
});

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

// Ensure we cannot put a public policy on this bucket
try {
await s3_owner.putBucketPolicy({
await assert.rejects(
s3_owner.putBucketPolicy({
Bucket: bucket,
Policy: generate_public_policy(bucket),
});
assert.fail("expected to fail");
} catch {
assert.ok(true);
}
})
);

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

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

await s3_owner.putObject({
Expand Down Expand Up @@ -191,16 +192,13 @@ mocha.describe('noobaa public access block test', function() {
}
});

try {
// Ensure anon can no longer access
await s3_anon.getObject({
// Ensure anon can no longer access
await assert.rejects(
s3_anon.getObject({
Bucket: bucket,
Key: KEY,
});
assert.fail("expected to fail after PublicAccessBlock");
} catch {
assert.ok(true);
}
}),
);

await s3_owner.deletePublicAccessBlock({
Bucket: bucket,
Expand Down