-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #902 from InseeFrLab/feat-disable-policy
feat: disable policy feature when 403 or error when fetching and parsing bucket policy
- Loading branch information
Showing
15 changed files
with
201 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,43 @@ | ||
import { S3BucketPolicy } from "core/ports/S3Client"; | ||
import type { S3BucketPolicy } from "core/ports/S3Client"; | ||
import { assert, Equals } from "tsafe"; | ||
import { z } from "zod"; | ||
import { id } from "tsafe/id"; | ||
|
||
const s3ActionSchema = z.custom<`s3:${string}`>( | ||
const zS3Action = z.custom<`s3:${string}`>( | ||
val => typeof val === "string" && val.startsWith("s3:") | ||
); | ||
|
||
const s3PolicyStatementSchema = z.object({ | ||
Effect: z.enum(["Allow", "Deny"]), | ||
Principal: z.union([z.string(), z.object({ AWS: z.string().array() })]), | ||
Action: z.union([s3ActionSchema, s3ActionSchema.array()]), | ||
Resource: z.string().array(), | ||
Condition: z.record(z.any()).optional() | ||
}); | ||
type S3PolicyStatement = S3BucketPolicy["Statement"][number]; | ||
|
||
export const s3BucketPolicySchema = z.object({ | ||
Version: z.literal("2012-10-17"), | ||
Statement: z.array(s3PolicyStatementSchema) | ||
}); | ||
const zS3PolicyStatement = (() => { | ||
type TargetType = S3PolicyStatement; | ||
|
||
assert<Equals<z.infer<typeof s3BucketPolicySchema>, S3BucketPolicy>>(true); | ||
const zTargetType = z.object({ | ||
Effect: z.enum(["Allow", "Deny"]), | ||
Principal: z.union([z.string(), z.object({ AWS: z.string().array() })]), | ||
Action: z.union([zS3Action, zS3Action.array()]), | ||
Resource: z.string().array(), | ||
Condition: z.record(z.any()).optional() | ||
}); | ||
|
||
type InferredType = z.infer<typeof zTargetType>; | ||
|
||
assert<Equals<TargetType, InferredType>>; | ||
|
||
return id<z.ZodType<TargetType>>(zTargetType); | ||
})(); | ||
|
||
export const zS3BucketPolicy = (() => { | ||
type TargetType = S3BucketPolicy; | ||
|
||
const zTargetType = z.object({ | ||
Version: z.literal("2012-10-17"), | ||
Statement: z.array(zS3PolicyStatement) | ||
}); | ||
|
||
type InferredType = z.infer<typeof zTargetType>; | ||
|
||
assert<Equals<TargetType, InferredType>>; | ||
|
||
return id<z.ZodType<TargetType>>(zTargetType); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.