This repository was archived by the owner on Aug 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
add action for pushing policies #8
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
96e9ff5
add action for pushing policies
matt-phylum b7e9b52
Merge remote-tracking branch 'origin/main' into matt/action
matt-phylum 746ad60
point to prod
matt-phylum 9235fd2
restrict token permissions
matt-phylum a3991a8
connect secret to step
matt-phylum 7048d08
python script is not an executable
matt-phylum e3b5a7e
rename secret
matt-phylum d602778
fix lints
matt-phylum 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| """ | ||
| Script to upload a set of policy files to Phylum. | ||
| """ | ||
|
|
||
| import contextlib | ||
| import io | ||
| import json | ||
| import os | ||
| from pathlib import Path | ||
| import shutil | ||
| from urllib.error import HTTPError | ||
| from urllib.parse import quote | ||
| from urllib.request import Request, urlopen | ||
| import uuid | ||
| import sys | ||
|
|
||
| # Set the group name if you are uploading policies for a group. | ||
| GROUP_NAME = None | ||
| token = os.environ["PHYLUM_TOKEN"] | ||
| version = os.environ.get("GITHUB_SHA", None) | ||
| BASE_ADDRESS = "https://api.phylum.io/api/" | ||
|
|
||
| # Build a multipart form body. | ||
| boundary = uuid.uuid4().hex | ||
| body = io.BytesIO() | ||
|
|
||
| for file in Path.cwd().glob("*.rego"): | ||
| body.write(f"--{boundary}\r\n".encode("ascii")) | ||
| body.write( | ||
| f'Content-Disposition: form-data; name="{file.stem}"; filename="{file.name}"\r\n'.encode( | ||
| "ascii" | ||
| ) | ||
| ) | ||
| body.write(b"Content-Type: text/plain\r\n\r\n") | ||
|
|
||
| with file.open("rb") as f: | ||
| shutil.copyfileobj(f, body) | ||
|
|
||
| body.write(b"\r\n") | ||
|
|
||
| body.write(f"--{boundary}--".encode("ascii")) | ||
|
|
||
| # Send the request. | ||
| if GROUP_NAME is None: | ||
| endpoint = f"{BASE_ADDRESS}v0/available-policies" | ||
| else: | ||
| endpoint = ( | ||
| f"{BASE_ADDRESS}v0/groups/{quote(GROUP_NAME, safe='')}/available-policies" | ||
| ) | ||
|
|
||
| if version is not None: | ||
| endpoint = f"{endpoint}?version={quote(version, safe='')}" | ||
|
|
||
| request = Request( | ||
| endpoint, | ||
| data=body.getbuffer(), | ||
| headers={ | ||
| "Authorization": f"Bearer {token}", | ||
| "Content-Type": f'multipart/form-data;boundary="{boundary}"', | ||
| }, | ||
| method="PUT", | ||
| ) | ||
|
|
||
| try: | ||
| with urlopen(request) as f: | ||
| pass | ||
| except HTTPError as error: | ||
| response = error.read().decode("utf8", errors="replace") | ||
| with contextlib.suppress(json.JSONDecodeError): | ||
| # Phylum's API returns JSON error descriptions. | ||
| # If that's what we got, reformat it to be more readable. | ||
| response = json.dumps(json.loads(response), indent=2) | ||
| print(response) | ||
| sys.exit(1) |
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.