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 40af2f8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
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
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

0 comments on commit 40af2f8

Please sign in to comment.