mirror of
https://github.com/rustfs/console.git
synced 2026-09-01 15:17:45 +08:00
40f9deb646
Co-authored-by: 季宏伟 <jihongwei@jihongweis-MacBook-Pro.local>
28 lines
960 B
TypeScript
28 lines
960 B
TypeScript
type PolicyStatement = Record<string, unknown>
|
|
|
|
export function normalizePolicyStatements(policyName: string, rawPolicy: unknown): PolicyStatement[] {
|
|
const policy = typeof rawPolicy === "string" ? (JSON.parse(rawPolicy) as unknown) : rawPolicy
|
|
|
|
if (typeof policy !== "object" || policy === null || Array.isArray(policy)) {
|
|
throw new TypeError("Policy must be a valid JSON object")
|
|
}
|
|
|
|
const statements = (policy as { Statement?: unknown }).Statement
|
|
if (statements === undefined) return []
|
|
if (!Array.isArray(statements)) {
|
|
throw new TypeError("Policy Statement must be an array")
|
|
}
|
|
|
|
return statements.map((statement) => {
|
|
if (typeof statement !== "object" || statement === null || Array.isArray(statement)) {
|
|
throw new TypeError("Policy Statement entries must be JSON objects")
|
|
}
|
|
|
|
const normalized = statement as PolicyStatement
|
|
return {
|
|
...normalized,
|
|
Sid: normalized.Sid ?? policyName,
|
|
}
|
|
})
|
|
}
|