-
Notifications
You must be signed in to change notification settings - Fork 10
feat: support XML policy fragment artifacts #293
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
Open
Chris Dayne (cdayne)
wants to merge
6
commits into
Azure:main
Choose a base branch
from
cdayne:feat/policy-fragment-xml
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
31f5362
feat: support XML policy fragment artifacts
ac8e090
fix: skip policy fragments without values
265a5ce
fix: validate policy fragment values after overrides
68a8187
Merge remote-tracking branch 'origin/main' into feat/policy-fragment-xml
5dd4f99
docs: minimize policy fragment documentation changes
a8c4ce3
fix: report policy fragment redaction sources
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| import type { IArtifactStore } from '../clients/iartifact-store.js'; | ||
| import type { ResourceDescriptor } from '../models/types.js'; | ||
| import { ResourceType } from '../models/resource-types.js'; | ||
| import { redactAndWarnPolicySecrets } from './secret-redactor.js'; | ||
|
|
||
| export type PolicyFragmentValueSource = | ||
| | 'policy.xml' | ||
| | 'policyFragmentInformation.json'; | ||
|
|
||
| export interface PolicyFragmentArtifactDetails { | ||
| payload: Record<string, unknown>; | ||
| valueSource?: PolicyFragmentValueSource; | ||
| } | ||
|
|
||
| function getProperties( | ||
| json: Record<string, unknown> | undefined | ||
| ): Record<string, unknown> { | ||
| const properties = json?.properties; | ||
| return properties !== null && typeof properties === 'object' && !Array.isArray(properties) | ||
| ? properties as Record<string, unknown> | ||
| : {}; | ||
| } | ||
|
|
||
| export function hasPolicyFragmentValue( | ||
| artifact: Record<string, unknown> | undefined | ||
| ): boolean { | ||
| return typeof getProperties(artifact).value === 'string'; | ||
| } | ||
|
|
||
| /** | ||
| * Read a policy fragment using the Azure APIops artifact contract. | ||
| * | ||
| * Either policyFragmentInformation.json or policy.xml may represent the | ||
| * fragment. When both exist, JSON metadata is retained and policy.xml supplies | ||
| * the authoritative value and format. | ||
| */ | ||
| export async function readPolicyFragmentArtifact( | ||
| store: IArtifactStore, | ||
| baseDir: string, | ||
| descriptor: ResourceDescriptor | ||
| ): Promise<Record<string, unknown> | undefined> { | ||
| return (await readPolicyFragmentArtifactDetails(store, baseDir, descriptor)) | ||
| ?.payload; | ||
| } | ||
|
|
||
| export async function readPolicyFragmentArtifactDetails( | ||
| store: IArtifactStore, | ||
| baseDir: string, | ||
| descriptor: ResourceDescriptor | ||
| ): Promise<PolicyFragmentArtifactDetails | undefined> { | ||
| if (descriptor.type !== ResourceType.PolicyFragment) { | ||
| throw new Error(`Expected PolicyFragment descriptor, got ${descriptor.type}`); | ||
| } | ||
|
|
||
| const [information, policyContent] = await Promise.all([ | ||
| store.readResource(baseDir, descriptor), | ||
| store.readContent(baseDir, descriptor, 'policy'), | ||
| ]); | ||
|
|
||
| if (!information && !policyContent) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (!policyContent) { | ||
| return { | ||
| payload: information!, | ||
| valueSource: hasPolicyFragmentValue(information) | ||
| ? 'policyFragmentInformation.json' | ||
| : undefined, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| payload: { | ||
| ...(information ?? {}), | ||
| properties: { | ||
| ...getProperties(information), | ||
| value: policyContent.content, | ||
| format: 'rawxml', | ||
| }, | ||
| }, | ||
| valueSource: 'policy.xml', | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Write an extracted policy fragment using the Azure APIops split layout: | ||
| * metadata in policyFragmentInformation.json and content in policy.xml. | ||
| */ | ||
| export async function writePolicyFragmentArtifact( | ||
| store: IArtifactStore, | ||
| baseDir: string, | ||
| descriptor: ResourceDescriptor, | ||
| json: Record<string, unknown> | ||
| ): Promise<Record<string, unknown>> { | ||
| if (descriptor.type !== ResourceType.PolicyFragment) { | ||
| throw new Error(`Expected PolicyFragment descriptor, got ${descriptor.type}`); | ||
| } | ||
|
|
||
| const properties = getProperties(json); | ||
| const { value, format: _format, ...metadataProperties } = properties; | ||
| const information = { | ||
| ...json, | ||
| properties: metadataProperties, | ||
| }; | ||
|
|
||
| await store.writeResource(baseDir, descriptor, information); | ||
|
|
||
| if (typeof value !== 'string') { | ||
| return json; | ||
| } | ||
|
|
||
| const redactedContent = redactAndWarnPolicySecrets(descriptor, value); | ||
| await store.writeContent(baseDir, descriptor, redactedContent, 'policy'); | ||
|
|
||
| return { | ||
| ...json, | ||
| properties: { | ||
| ...properties, | ||
| value: redactedContent, | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.