Skip to content

Commit

Permalink
Modify RulesetProvider to take resource parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
tohhsinpei committed Mar 9, 2022
1 parent 323f382 commit aa0ebeb
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 9 deletions.
4 changes: 1 addition & 3 deletions src/emulator/storage/apis/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,7 @@ export function createFirebaseEndpoints(emulator: StorageEmulator): Router {
authorization: req.header("authorization"),
file: rulesFiles,
});
if (
!(permitted)
) {
if (!permitted) {
return res.status(403).json({
error: {
code: 403,
Expand Down
2 changes: 2 additions & 0 deletions src/emulator/storage/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export class StorageLayer {
if (!authorized) {
authorized = await this._validator.validate(
["b", request.bucketId, "o", request.decodedObjectId].join("/"),
request.bucketId,
RulesetOperationMethod.GET,
{ before: metadata?.asRulesResource() },
request.authorization
Expand Down Expand Up @@ -309,6 +310,7 @@ export class StorageLayer {
skipAuth ||
(await this._validator.validate(
["b", upload.bucketId, "o", upload.objectId].join("/"),
upload.bucketId,
RulesetOperationMethod.CREATE,
{ after: metadata?.asRulesResource() },
upload.authorization
Expand Down
2 changes: 1 addition & 1 deletion src/emulator/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class StorageEmulator implements EmulatorInstance {
this._persistence = new Persistence(this.getPersistenceTmpDir());
this._storageLayer = new StorageLayer(
args.projectId,
getRulesValidator(() => this.getRules("default")), // TODO(hsinpei): Fix
getRulesValidator((resource: string) => this.getRules(resource)),
this._persistence
);
this._uploadService = new UploadService(this._persistence);
Expand Down
4 changes: 2 additions & 2 deletions src/emulator/storage/rules/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { StorageRulesIssues, StorageRulesRuntime, StorageRulesetInstance } from
import { readFile } from "../../../fsutils";
import { RulesConfig, RulesType } from "..";

/**
/**
* Keeps track of the rules source file and maintains a generated ruleset for one or more storage
* resources.
* resources.
* */
export interface StorageRulesManager {
/** Sets source file for each resource using the rules previously passed in the constructor. */
Expand Down
6 changes: 4 additions & 2 deletions src/emulator/storage/rules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ export type RulesVariableOverrides = {
export interface RulesValidator {
validate(
path: string,
bucketId: string,
method: RulesetOperationMethod,
variableOverrides: RulesVariableOverrides,
authorization?: string
): Promise<boolean>;
}

/** Provider for Storage security rules. */
export type RulesetProvider = () => StorageRulesetInstance | undefined;
export type RulesetProvider = (resource: string) => StorageRulesetInstance | undefined;

/**
* Returns a {@link RulesValidator} that pulls a Ruleset from a
Expand All @@ -30,12 +31,13 @@ export function getRulesValidator(rulesetProvider: RulesetProvider): RulesValida
return {
validate: async (
path: string,
bucketId: string,
method: RulesetOperationMethod,
variableOverrides: RulesVariableOverrides,
authorization?: string
) => {
return await isPermitted({
ruleset: rulesetProvider(),
ruleset: rulesetProvider(bucketId),
file: variableOverrides,
path,
method,
Expand Down
2 changes: 1 addition & 1 deletion src/test/emulators/storage/rules/manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe("Storage Rules Manager", function () {
await rulesManager.close();
});

it("should load multiple rulesets on start", async () => {
it("should load multiple rulesets on start", () => {
expect(rulesManager.getRuleset("bucket_1")).not.to.be.undefined;
expect(rulesManager.getRuleset("bucket_2")).not.to.be.undefined;
});
Expand Down

0 comments on commit aa0ebeb

Please sign in to comment.