Skip to content
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

Konflux(ROX-22195): Install RHEL RPMs with subscription manager workaround #1573

Merged
merged 114 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 95 commits
Commits
Show all changes
114 commits
Select commit Hold shift + click to select a range
7ff08c8
add custom Dockerfile for rhtap
tommartensen Oct 16, 2023
1c6a49d
Red Hat Trusted App Pipeline update collector (#1367)
red-hat-konflux[bot] Oct 16, 2023
69200d7
RHTAP: fix workspace size for checkouts (#1368)
tommartensen Oct 16, 2023
7410e30
broken dnf installs
tommartensen Oct 17, 2023
4399cad
Update RHTAP references (#1375)
red-hat-konflux[bot] Oct 19, 2023
37b0c74
Update RHTAP references (#1378)
red-hat-konflux[bot] Oct 23, 2023
80d6c38
use centos instead of ubi images
tommartensen Oct 25, 2023
8cc0a9a
Merge branch 'tm/rhtap-onboarding' of github.com:stackrox/collector i…
tommartensen Oct 25, 2023
8b4d052
Update RHTAP references (#1387)
red-hat-konflux[bot] Nov 6, 2023
3a55302
Merge branch 'master' into tm/rhtap-onboarding
tommartensen Nov 6, 2023
6945989
update pipelines
tommartensen Nov 6, 2023
a1811d5
increase storage for shared volume in pipeline
tommartensen Nov 6, 2023
157b8d0
falco submodule
tommartensen Nov 6, 2023
b26118c
update TODOs and LABELs
tommartensen Nov 6, 2023
807015c
restore falco
tommartensen Nov 6, 2023
6ade98a
clean up
tommartensen Nov 9, 2023
0d4cef0
Red Hat Trusted App Pipeline update collector-slim (#1414)
red-hat-konflux[bot] Nov 9, 2023
1ea2eb2
rename collector -> collector-slim
tommartensen Nov 9, 2023
3db110e
finish up
tommartensen Nov 13, 2023
56abbd3
Merge branch 'master' into tm/rhtap-onboarding
tommartensen Nov 13, 2023
346e7a4
fix build after rebase
tommartensen Nov 13, 2023
49f6bcd
Update RHTAP references (#1415)
red-hat-konflux[bot] Nov 13, 2023
f4d9d03
more oomph
tommartensen Nov 13, 2023
4f2e97e
Merge branch 'tm/rhtap-onboarding' of github.com:stackrox/collector i…
tommartensen Nov 13, 2023
a225b95
Update RHTAP references (#1420)
red-hat-konflux[bot] Nov 14, 2023
a8c55ee
attempt with default buildah size
tommartensen Nov 14, 2023
4f41bb5
fix task ref
tommartensen Nov 14, 2023
7ec670a
Update RHTAP references (#1421)
red-hat-konflux[bot] Nov 14, 2023
3957e05
Update RHTAP references (#1422)
red-hat-konflux[bot] Nov 15, 2023
a7bff4d
RHTAP Onboarding: 2nd attempt (#1425)
tommartensen Nov 17, 2023
e2bf0b3
move dockerfile
tommartensen Nov 20, 2023
b1eea97
emptyg
tommartensen Nov 20, 2023
780e997
disable prefetch-input
tommartensen Nov 20, 2023
bb8cfa8
remove guard on prefetch-dependencies task
tommartensen Nov 20, 2023
f4512eb
clean up Dockerfile
tommartensen Nov 20, 2023
119ff0a
add CODEOWNERS
tommartensen Nov 20, 2023
4c57a27
Merge branch 'master' into tm/rhtap-onboarding
tommartensen Nov 21, 2023
156c0e1
rename Dockerfile for slim
tommartensen Nov 21, 2023
6b393ab
Update RHTAP references (#1444)
red-hat-konflux[bot] Nov 27, 2023
3f5a901
Update .tekton/collector-slim-pull-request.yaml
tommartensen Nov 27, 2023
b867082
some recommendations from PR
tommartensen Nov 27, 2023
e3ea3cc
restrict pipeline for *rhtap* branches
tommartensen Nov 28, 2023
f62607e
cleanup
tommartensen Dec 4, 2023
ce0f293
Update RHTAP references (#1452)
red-hat-konflux[bot] Dec 4, 2023
fc214da
empty
tommartensen Dec 4, 2023
5adbce7
empty to check new pod limits
tommartensen Dec 4, 2023
9e2bcb1
Update RHTAP references (#1455)
red-hat-konflux[bot] Dec 5, 2023
ac2e9d8
empty commit
tommartensen Dec 5, 2023
8d41a38
test with CPU limits
tommartensen Dec 5, 2023
0710d12
Update RHTAP references (#1463)
red-hat-konflux[bot] Dec 6, 2023
cc2fd34
Update RHTAP references (#1464)
red-hat-konflux[bot] Dec 11, 2023
7a5b8a1
Update RHTAP references (#1467)
red-hat-konflux[bot] Dec 12, 2023
05a86d4
update Dockerfile with midstream changes
tommartensen Dec 12, 2023
bc4405f
Merge branch 'tm/rhtap-onboarding' of github.com:stackrox/collector i…
tommartensen Dec 12, 2023
bda0de0
Apply suggestions from code review
tommartensen Dec 12, 2023
bc5cf90
Merge branch 'master' into tm/rhtap-onboarding
tommartensen Dec 12, 2023
d44cd27
Merge branch 'tm/rhtap-onboarding' of github.com:stackrox/collector i…
tommartensen Dec 12, 2023
fb4ded4
fix comment
tommartensen Dec 12, 2023
1541146
Update RHTAP references (#1471)
red-hat-konflux[bot] Dec 13, 2023
0fbf75f
setup Snyk for collector
tommartensen Dec 13, 2023
be05ea1
bump to get green RHTAP CI
tommartensen Dec 13, 2023
4801067
empty commit for retrigger
tommartensen Dec 14, 2023
6b38abe
Update RHTAP references (#1474)
red-hat-konflux[bot] Dec 18, 2023
de9d423
Update RHTAP references (#1477)
red-hat-konflux[bot] Dec 20, 2023
9178b08
Update RHTAP references (#1478)
red-hat-konflux[bot] Jan 3, 2024
11e5321
add source image build task
tommartensen Jan 3, 2024
e66d192
test enabled builds
tommartensen Jan 3, 2024
ce6314a
fix workspaces
tommartensen Jan 3, 2024
ea19451
hack the rpm installation
tommartensen Jan 3, 2024
cbd01bc
skip cleanup
tommartensen Jan 3, 2024
86278b0
bodge
tommartensen Jan 3, 2024
f0099d9
add the three dependencies that are RHEL RPM only
tommartensen Jan 3, 2024
d6d4e6a
empty commit
tommartensen Jan 3, 2024
b3d216d
fix missing dependency installation
tommartensen Jan 3, 2024
8bd5588
give it a shot w/ the midstream Dockerfile
tommartensen Jan 4, 2024
242bfc0
fix installations in second stage by using ubi-nomrla
tommartensen Jan 4, 2024
fc6fc25
empty commit
tommartensen Jan 4, 2024
2a0117e
Merge branch 'master' into tm/rhtap-onboarding
tommartensen Jan 4, 2024
6681437
Merge branch 'tm/rhtap-onboarding' into tm/rhtap-dnf-enablement
tommartensen Jan 4, 2024
c84c61b
inject collector_version
tommartensen Jan 4, 2024
7bf4bdc
empty commit
tommartensen Jan 4, 2024
1fed215
empty to retrigger
tommartensen Jan 11, 2024
2acdd3e
format according to collector team's preferences
tommartensen Jan 11, 2024
8afcd6d
try with a ubi-minimal final stage
tommartensen Jan 11, 2024
902307e
fix last stage
tommartensen Jan 11, 2024
42207c9
empty
tommartensen Jan 15, 2024
e0d3640
Merge branch 'master' into tm/rhtap-dnf-enablement
tommartensen Jan 19, 2024
7bdaf04
Merge branch 'master' into tm/rhtap-dnf-enablement
tommartensen Feb 22, 2024
923b6f6
update subscription-manager-bro.sh script
tommartensen Feb 22, 2024
3572405
use scratch instead for final stage
tommartensen Feb 22, 2024
20050e5
fix
tommartensen Feb 22, 2024
a564841
2nd part
tommartensen Feb 22, 2024
ad7fb33
fix kernel-modules directory
tommartensen Feb 22, 2024
53b88d9
empty commit
tommartensen Feb 26, 2024
a00eb62
empty
tommartensen Feb 26, 2024
37dcf9e
add missing labels
tommartensen Feb 26, 2024
23d4abc
rename stage
tommartensen Feb 27, 2024
49c9860
cosmetic reorder
tommartensen Feb 27, 2024
5728876
rename script .rhtap -> .konflux
tommartensen Feb 27, 2024
e56e81a
port changes to all tekton pipelines
tommartensen Feb 27, 2024
82f3846
reduce builder to single stage
tommartensen Feb 27, 2024
cd34e5f
Revert "reduce builder to single stage"
tommartensen Feb 27, 2024
f1e1666
update full Dockerfile
tommartensen Feb 27, 2024
5520ffa
make the shfmt linter happy
tommartensen Feb 27, 2024
b447995
register
tommartensen Feb 27, 2024
08f069d
desperation
tommartensen Feb 27, 2024
ae2ff4f
remove gitignore
tommartensen Feb 27, 2024
dc1144e
Merge branch 'master' into tm/konflux-scratch-final-stage
tommartensen Feb 27, 2024
8aa4c0e
make rhtap maintainers owners of the konflux dir
tommartensen Feb 27, 2024
bb164dd
Update .tekton/collector-pull-request.yaml
tommartensen Feb 28, 2024
79d1c99
apply suggestion for workspace path
tommartensen Feb 28, 2024
c41368a
make prefetch and custom tasks run in parallel
tommartensen Feb 28, 2024
2eadede
link instead of copy; remove redundant info
tommartensen Feb 28, 2024
87f70d2
use a staging dir to avoid overriding existing content in builder image
tommartensen Feb 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rhtap/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
3 changes: 3 additions & 0 deletions .rhtap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# .rhtap

This is a directory with misc files needed for builds on RHTAP.
311 changes: 311 additions & 0 deletions .rhtap/scripts/subscription-manager-bro.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,311 @@
#!/usr/bin/env bash

# This script is for registering container with Red Hat subscription-manager during Konflux builds for getting access
# to RHEL RPMs during the build.
# The script was created as a workaround in absence of better options.
# TODO(ROX-20651): remove this script and switch to use content sets once available.

set -euo pipefail

SCRIPT_NAME="$(basename -- "${BASH_SOURCE[0]}")"
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

SECRET_NAME_IN_KONFLUX="subscription-manager-activation-key"
SECRET_KONFLUX_WORKSPACE_PATH="/workspace/${SECRET_NAME_IN_KONFLUX}"
SECRET_KEY="activation-key"
SECRET_LOCAL_PATH="${SCRIPT_DIR}/${SECRET_KEY}"
SECRET_INFO_URL='https://docs.engineering.redhat.com/pages/viewpage.action?pageId=407312060'

RED_HAT_ORG_ID="11009103"
TARGETS_LIST_FILE="/tmp/subscription-manager-bro-targets"

# These were figured experimentally with the help of self-test subcommand.
TARGET_BACKUP_PATHS=(
etc/pki/consumer
etc/pki/entitlement
etc/pki/product
etc/pki/product-default
etc/yum.repos.d
var/lib/rhsm
var/cache/ldconfig
)


function main {
if [[ "$#" == "0" ]] ; then
>&2 echo "Error: command is missing. See the usage below."
usage
exit 2
fi

local cmd="$1"
shift

local fn

case "$cmd" in
"help" | "--help" | "-h")
fn=usage ;;
"smuggle")
fn=smuggle ;;
"register")
fn=register ;;
"cleanup")
fn=cleanup ;;
"self-test")
fn=self_test ;;
"diff")
fn=assert_diff ;;
*)
>&2 echo "Error: unknown command '$1'; call '$SCRIPT_NAME help' to see the usage."
exit 2
;;
esac

if [[ "$#" -gt "0" && "$cmd" != "diff" && "$cmd" != "register" ]]; then
>&2 echo "Error: too many arguments; call '$SCRIPT_NAME help' to see the usage."
exit 2
fi

"$fn" "$@"
}

function usage {
local example_target_dir="/mnt"

echo "Usage: $SCRIPT_NAME smuggle|register|cleanup|self-test"
echo
echo "This script enables access to RHEL RPMs during Konflux builds. The intended usage is as follows."
echo

echo -n "1. Make sure there is a '$SECRET_NAME_IN_KONFLUX' secret in Konflux with key name '$SECRET_KEY' and "
echo "the actual activation key as a value."
echo " Find where to get the secret from ${SECRET_INFO_URL}"

echo -n "2. In a Tekton pipeline step before the container build, copy the subscription manager activation "
echo "key secret to the source workspace. Use:"
echo " \$ <source-workspace>/$SCRIPT_NAME smuggle"
echo -n " This expects the '$SECRET_NAME_IN_KONFLUX' secret to be mounted as a workspace with the same name "
echo "('$SECRET_KONFLUX_WORKSPACE_PATH')."

echo "3. Arrange Dockerfile stages to have UBI (normal) as an installer and other RHEL/UBI (any) as a target."
echo " Make sure to match major versions: 8/8 is ok but 9/8 or 8/9 will result in errors."
echo " Copy the target contents to some directory, e.g. ${example_target_dir}, in the installer stage."
echo " See self-test Dockerfiles as examples."

echo "4. In the installer stage, register the container with the subscription manager. Use:"
echo " \$ $SCRIPT_NAME register ${example_target_dir}"
echo -n " It is possible to provide multiple target directories as arguments if the script is used to prepare "
echo "multiple distinct stages."

echo -n "5. Use 'dnf --installroot=${example_target_dir} ...' to install RHEL RPMs, enable RHEL modules, etc. "
echo "in the target contents."

echo -n "6. In the same installer stage, deregister the container so that the end users can't use "
echo "our subscription on our behalf. Use:"
echo " \$ $SCRIPT_NAME cleanup"
echo " This step is mandatory because it cleans entitlements on the target in the right way."

echo -n "7. Copy out ${example_target_dir} contents from the installer stage to a new 'scratch' stage. "
echo "That's your target container."

echo
echo "When altering this script, use the 'self-test' command as a (regression) test tool:"
echo " \$ $SCRIPT_NAME self-test"
echo "For it to work, you need to put a valid activation key in ${SECRET_LOCAL_PATH} file."
echo "Find out where to get it from ${SECRET_INFO_URL}"
}

function smuggle {
mkdir -p "$(dirname "${SECRET_LOCAL_PATH}" )"
cp --verbose "${SECRET_KONFLUX_WORKSPACE_PATH}/${SECRET_KEY}" "${SECRET_LOCAL_PATH}"
}

function register {
if [[ ! -s "${SECRET_LOCAL_PATH}" ]]; then
>&2 echo "Error: it does not look like the activation key is present in ${SECRET_LOCAL_PATH}"
exit 3
fi
local secret
secret="$(cat "${SECRET_LOCAL_PATH}")"

if [[ "$#" -lt 1 ]]; then
>&2 echo "Error: target path(s) must be provided for the 'register' command."
exit 2
fi

local target_dirs=( "$@" )

check_targets_and_store_paths_for_cleanup "${target_dirs[@]}"

# Besides just installing packages and making the desired updates to rpmdb, the use of subscription-manager with
# the subsequent installation introduces some side-effects that seem undesired. Backup and restore is how I suggest
# maintaining the original state of the target.
for target_dir in "${target_dirs[@]}"; do
echo "Backing up the original artifacts in $target_dir"
mkdir -p "${target_dir}/tmp/restore"
tar --create -vf "${target_dir}/tmp/restore/backup.tar" --files-from /dev/null
for item in "${TARGET_BACKUP_PATHS[@]}"; do
if [[ -e "${target_dir}/${item}" ]]; then
tar --append -vf "${target_dir}/tmp/restore/backup.tar" -C "${target_dir}" "${item}"
fi
done
done

echo "Registering the installer container with the subscription manager"
subscription-manager register --org="$RED_HAT_ORG_ID" --activationkey="$secret"

# It is suggested in the following articles that certain files can be linked to $target_dir/run/secrets,
# but I was not able to make it work, therefore doing it differently.
# https://www.neteye-blog.com/2022/07/how-to-use-a-hosts-redhat-subscription-to-run-containers-using-docker-instead-of-podman/
# https://access.redhat.com/solutions/5870841
for target_dir in "${target_dirs[@]}"; do
echo "Enabling entitled rpm repos in $target_dir"
mkdir -p "${target_dir}/etc/pki/entitlement"
ln --verbose -s /etc/pki/entitlement/*.pem "${target_dir}/etc/pki/entitlement"
ln --verbose --force -s /etc/yum.repos.d/redhat.repo "${target_dir}/etc/yum.repos.d/"
done

echo "Looks like the registration succeeded. Don't forget to call '$SCRIPT_NAME cleanup' when done with rpms!"
}

function check_targets_and_store_paths_for_cleanup {
local target_dirs=( "$@" )

for target_dir in "${target_dirs[@]}"; do
if [[ ! -d "${target_dir}/etc" ]]; then
>&2 echo "Error: Looks like the target system is not placed at ${target_dir}"
exit 4
fi
done

if [[ -f "${TARGETS_LIST_FILE}" ]]; then
>&2 echo "Error: ${TARGETS_LIST_FILE} already exists. Are you trying to register again without doing a cleanup?"
exit 5
fi

printf "%s\n" "${target_dirs[@]}" > "${TARGETS_LIST_FILE}"
}

function cleanup {
local -a target_dirs
readarray -t target_dirs < "${TARGETS_LIST_FILE}"

for target_dir in "${target_dirs[@]}"; do
echo "Cleaning up entitlement artifacts in $target_dir"

echo "Restoring original artifacts"
for item in "${TARGET_BACKUP_PATHS[@]}"; do
rm --verbose -rf "${target_dir:?}/${item}"
done
tar --extract -vf "${target_dir}/tmp/restore/backup.tar" -C "${target_dir}"

echo "Removing original artifacts backups"
rm --verbose -rf "${target_dir:?}/tmp/restore"
done

# It should be good to unregister this installer container so that it's not left hanging in some Red Hat database.
echo "Unregistering the installer container"
subscription-manager unregister

rm --verbose "${TARGETS_LIST_FILE}"

echo "Cleanup complete."
}

function self_test {
local command="podman"

local targets=(
"registry.access.redhat.com/ubi8/ubi-micro:latest"
"registry.access.redhat.com/ubi8/ubi-minimal:latest"
"registry.access.redhat.com/ubi8/ubi:latest"
"registry.redhat.io/rhel8/toolbox:latest"

"registry.access.redhat.com/ubi9/ubi-micro:latest"
"registry.access.redhat.com/ubi9/ubi-minimal:latest"
"registry.access.redhat.com/ubi9/ubi:latest"
"registry.redhat.io/rhel9/toolbox:latest"
)

for target in "${targets[@]}"; do
[[ $target =~ /(ubi|rhel)([0-9]+)/ ]]
local major_version="${BASH_REMATCH[2]}"

echo
echo
echo "Testing against ${target} with the installer major version ${major_version}"
echo
echo

set -x
"${command}" build \
-f "${SCRIPT_DIR}/bro.self-test.Dockerfile" \
--build-arg TARGET_BASE="${target}" \
--build-arg INSTALLER_MAJOR_VERSION="${major_version}" \
"${SCRIPT_DIR}"
set +x
done

"${command}" build -f "${SCRIPT_DIR}/bro.self-test-demo.Dockerfile" "${SCRIPT_DIR}"
"${command}" build -f "${SCRIPT_DIR}/bro.self-test-multiple-targets.Dockerfile" "${SCRIPT_DIR}"
echo "Self-tests passed."
}

function assert_diff {
if [[ "$#" != "2" ]]; then
>&2 echo "Error: expecting two arguments: expected and actual paths"
exit 2
fi

local expected="$1"
local actual="$2"

local failed_check_file
failed_check_file="$(mktemp)"

echo "Comparing /etc"
if ! diff --brief --recursive --no-dereference --exclude='ld.so.cache' "$expected/etc" "$actual/etc" ; then
echo 1 >> "$failed_check_file"
fi

echo "Comparing /var"
local var_exclusions
var_exclusions="$(mktemp)"
{
# Before adding any exclusions here, make sure you check there's nothing sensitive in these files.
# If sensitive, they should be added to backup/restore (TARGET_BACKUP_PATHS) or cleanup.
echo '/var/lib: dnf'
echo '/var/lib/dnf/history\.sqlite'
echo '/var/lib/rpm(:|/)'
echo '/var/log: (dnf.*|hawkey)\.log'
echo '/var/log/(dnf.*|hawkey)\.log'
# /var/cache/dnf should be kept on Target, otherwise Konflux Enterprise Check fails not finding SBOM.
echo '/var/cache(: |/)dnf'
} >> "$var_exclusions"

if { diff --brief --recursive --no-dereference "$expected/var" "$actual/var" || true; } | \
grep -vEf "$var_exclusions" | { grep '.'; }; then
echo 2 >> "$failed_check_file"
fi

local other_dirs_to_compare=(bin home lib lib64 media mnt opt root sbin srv tmp usr)

for dir in "${other_dirs_to_compare[@]}"; do
echo "Comparing /$dir"
if ! diff --brief --recursive --no-dereference "$expected/$dir" "$actual/$dir"; then
echo 3 >> "$failed_check_file"
fi
done

if [[ -s "$failed_check_file" ]]; then
>&2 echo "Error: differences detected"
exit 6
fi

echo "Diff check for $expected and $actual passed."
}

if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
main "$@"
fi
21 changes: 20 additions & 1 deletion .tekton/collector-slim-pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ spec:
- name: git-auth
secret:
secretName: '{{ git_auth_secret }}'
- name: subscription-manager-activation-key
secret:
secretName: subscription-manager-activation-key

pipelineSpec:

Expand Down Expand Up @@ -167,6 +170,7 @@ spec:
workspaces:
- name: workspace
- name: git-auth
- name: subscription-manager-activation-key

tasks:

Expand Down Expand Up @@ -213,12 +217,27 @@ spec:
- name: basic-auth
workspace: git-auth

- name: download-external-content
msugakov marked this conversation as resolved.
Show resolved Hide resolved
runAfter:
- clone-repository
workspaces:
- name: source
workspace: workspace
- name: subscription-manager-activation-key
workspace: subscription-manager-activation-key
taskSpec:
steps:
# TODO(ROX-20651): use content sets instead of subscription manager for access to RHEL RPMs once available.
- name: smuggle-activation-key
image: registry.access.redhat.com/ubi8/ubi:latest
script: exec /workspace/source/source/.rhtap/scripts/subscription-manager-bro.sh smuggle

- name: prefetch-dependencies
params:
- name: input
value: $(params.prefetch-input)
runAfter:
- clone-repository
- download-external-content
taskRef:
params:
- name: name
Expand Down
Loading
Loading