Skip to content

Commit

Permalink
feat: adds GitHub Action to create a new profile with inputs
Browse files Browse the repository at this point in the history
Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Jul 17, 2023
1 parent 66f35d5 commit 9fa2cc3
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 5 deletions.
21 changes: 21 additions & 0 deletions .github/actions/setup-trestle/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: "setup-trestle"
description: "Composite action to setup trestle."

inputs:
python-version:
required: false
description: "The python version to use"
default: "3.11"

runs:
using: "composite"
steps:

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ inputs.python-version }}

- name: Install dependencies
run: make trestle-install
shell: bash
64 changes: 64 additions & 0 deletions .github/workflows/create-new.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Profile Create
on:
workflow_dispatch:
inputs:
import_type:
type: choice
description: Import type
options:
- catalog
- profile
import_name:
required: true
description: Name of profile or catalog in trestle workspace to be imported
output:
required: true
description: Name of the profile to create


jobs:
create-profile:
name: Create profile
runs-on: ubuntu-latest
steps:
- name: Generate app token
uses: tibdex/github-app-token@v1
id: get_installation_token
with:
app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.PRIVATE_KEY }}
permissions: >-
{"contents": "write", "pull_requests": "write"}
- name: Clone
uses: actions/checkout@v3
with:
token: ${{ steps.get_installation_token.outputs.token }}
- name: Setup trestle
uses: ./.github/actions/setup-trestle
with:
python-version: "3.8"
- name: Create new profile with imports
run: |
echo "Creating profile with name ${OUTPUT}"
trestle create --type profile --output "${OUTPUT}"
trestle href --name "${OUTPUT}" -hr "trestle://${IMPORT_TYPE}s/${IMPORT_NAME}/${IMPORT_TYPE}.json"
echo "Normalizing profile fields"
trestle create --type profile -e profile.imports.0.include-all -f "profiles/${OUTPUT}/profile.json"
python3 scripts/set_default_profile.py --profile_name "${OUTPUT}" --trestle_root .
env:
OUTPUT: ${{ github.event.inputs.output }}
IMPORT_NAME: ${{ github.event.inputs.import_name }}
IMPORT_TYPE: ${{ github.event.inputs.import_type }}
- name: Generate and PR new profile
id: generate-profile
uses: RedHatProductSecurity/trestle-bot@main
with:
markdown_path: "markdown/profiles"
oscal_model: "profile"
branch: "profile-create-${{ github.run_id }}"
target_branch: "main"
file_pattern: "*.json,markdown/*"
skip_items: "fedramp_rev5_high"
skip_assemble: true
github_token: ${{ steps.get_installation_token.outputs.token }}
8 changes: 3 additions & 5 deletions .github/workflows/update-upstream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ jobs:
uses: actions/checkout@v3
with:
token: ${{ steps.get_installation_token.outputs.token }}
- name: Set up Python 3.8
uses: actions/setup-python@v2
- name: Setup trestle
uses: ./.github/actions/setup-trestle
with:
python-version: 3.8
- name: Install Trestle
run: make trestle-install
python-version: "3.8"
- name: Update catalogs
run: |
rm -rf "catalogs/${NIST_CATALOG_NAME}"
Expand Down
60 changes: 60 additions & 0 deletions scripts/set_default_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python3
# set_default_profile.py

# Copyright 2023 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""
Read in a profile created by trestle and set up default profile fields
Author: Jenn Power <[email protected]>
"""

import argparse
import pathlib

import trestle.core.generators as gens
import trestle.oscal.profile as prof
from trestle.common.load_validate import load_validate_model_name
from trestle.common.model_utils import ModelUtils
from trestle.core.models.file_content_type import FileContentType

def main():
p = argparse.ArgumentParser(description="Set default profile fields")
p.add_argument("--profile_name", required=True)
p.add_argument("--trestle_root", required=True)
args = p.parse_args()

trestle_root: pathlib.Path = pathlib.Path(args.trestle_root)
profile_data, _ = load_validate_model_name(
trestle_root, args.profile_name, prof.Profile
)

# Set up default values for merge settings.
merge_object: prof.Merge = gens.generate_sample_model(prof.Merge)
combine_object: prof.Combine = gens.generate_sample_model(prof.Combine)
combine_object.method = prof.Method.merge
merge_object.combine = combine_object
merge_object.as_is = True

profile_data.merge = merge_object

ModelUtils.update_last_modified(profile_data)
ModelUtils.save_top_level_model(
profile_data, trestle_root, args.profile_name, FileContentType.JSON
)


if __name__ == "__main__":
main()

0 comments on commit 9fa2cc3

Please sign in to comment.