-
Notifications
You must be signed in to change notification settings - Fork 21
Update openshift/snc cloud-init commands #633
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
kpouget
wants to merge
2
commits into
redhat-developer:main
Choose a base branch
from
kpouget:snc
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
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
126 changes: 126 additions & 0 deletions
126
pkg/provider/aws/action/openshift-snc/mapt-crc-aws-fetch-secrets.sh
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 @@ | ||
#!/bin/bash | ||
|
||
set -o pipefail | ||
set -o errexit | ||
set -o nounset | ||
set -o errtrace | ||
set -x | ||
|
||
# set -x is safe, the secrets are passed via stdin | ||
|
||
AWS_CLI_IMG=docker.io/amazon/aws-cli | ||
MIN_CHAR_COUNT=8 # minimum number of chars for the secret to be | ||
# assumed valid | ||
|
||
umask 0077 # 0600 file permission for secrets | ||
|
||
PULL_SECRETS_KEY=${1:-} | ||
KUBEADM_PASS_KEY=${2:-} | ||
DEVELOPER_PASS_KEY=${3:-} | ||
|
||
if [[ -z "$PULL_SECRETS_KEY" || -z "$KUBEADM_PASS_KEY" || -z "$DEVELOPER_PASS_KEY" ]]; then | ||
echo "ERROR: expected to receive 3 parameters: PULL_SECRETS_KEY KUBEADM_PASS_KEY DEVELOPER_PASS_KEY" | ||
exit 1 | ||
fi | ||
|
||
SECONDS=0 | ||
podman pull --quiet "$AWS_CLI_IMG" | ||
echo "Took $SECONDS seconds to pull the $AWS_CLI_IMG" | ||
|
||
wait_imds_available_and_get_region() { | ||
total_timeout_minutes=5 | ||
retry_interval_seconds=5 | ||
|
||
IMDS_TOKEN_COMMAND=( | ||
curl | ||
--connect-timeout 1 | ||
-X PUT | ||
"http://169.254.169.254/latest/api/token" | ||
-H "X-aws-ec2-metadata-token-ttl-seconds: 21600" | ||
-Ssf | ||
) | ||
success=false | ||
deadline=$(( $(date +%s) + (total_timeout_minutes * 60) )) | ||
while [[ $(date +%s) -lt $deadline ]]; do | ||
# By placing the command in an 'if' condition, we can test its exit code | ||
# without triggering 'set -e'. The output is still captured. | ||
if TOKEN=$("${IMDS_TOKEN_COMMAND[@]}"); then | ||
# This block only runs if the curl command succeeds (exit code 0) | ||
success=true | ||
echo "Successfully fetched token." >&2 | ||
break # Exit the loop on success | ||
fi | ||
|
||
# This block runs if the curl command fails | ||
echo "Failed to connect. Retrying in $retry_interval_seconds seconds..." >&2 | ||
sleep "$retry_interval_seconds" | ||
done | ||
|
||
if [[ "$success" != "true" ]]; then | ||
echo "ERROR: Could not fetch token after $total_timeout_minutes minutes." >&2 | ||
return 1 | ||
fi | ||
|
||
# Then, use the token to get the region | ||
echo "Fetching the AWS region ..." | ||
curl -Ssf -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/region > /tmp/aws-region | ||
echo >> /tmp/aws-region # add EOL at EOF, for consistency | ||
echo "AWS region: $(< /tmp/aws-region)" | ||
} | ||
|
||
( | ||
set +x # disable the xtrace as the token would be leaked | ||
echo "Waiting for the AWS IMDS service to be available ..." | ||
SECONDS=0 | ||
wait_imds_available_and_get_region | ||
echo "Took $SECONDS for the IMDS service to become available." | ||
) | ||
|
||
missing_secrets=0 | ||
|
||
save_secret() { | ||
name=$1 | ||
key=$2 | ||
dest=$3 | ||
|
||
# --log-driver=none avoids that the journal captures the stdout | ||
# logs of podman and leaks the passwords in the journal ... | ||
if ! podman run \ | ||
--name "cloud-init-fetch-$name" \ | ||
--env AWS_REGION="$(< /tmp/aws-region)" \ | ||
--log-driver=none \ | ||
--rm \ | ||
"$AWS_CLI_IMG" \ | ||
ssm get-parameter \ | ||
--name "$key" \ | ||
--with-decryption \ | ||
--query "Parameter.Value" \ | ||
--output text \ | ||
> "${dest}.tmp" | ||
then | ||
rm -f "${dest}.tmp" | ||
echo "ERROR: failed to get the '$name' secret ... (fetched from $key)" | ||
((missing_secrets += 1)) | ||
return | ||
fi | ||
char_count=$(wc -c < "${dest}.tmp") | ||
if (( char_count < MIN_CHAR_COUNT )); then | ||
echo "ERROR: the content of the '$name' secret is too short ... (fetched from $key)" | ||
rm -f "${dest}.tmp" | ||
((missing_secrets += 1)) | ||
return | ||
fi | ||
|
||
mv "${dest}.tmp" "${dest}" # atomic creation of the file | ||
} | ||
|
||
save_secret "pull-secrets" "$PULL_SECRETS_KEY" /opt/crc/pull-secret | ||
save_secret "kubeadmin-pass" "$KUBEADM_PASS_KEY" /opt/crc/pass_kubeadmin | ||
save_secret "developer-pass" "$DEVELOPER_PASS_KEY" /opt/crc/pass_developer | ||
|
||
if (( missing_secrets != 0 )); then | ||
echo "ERROR: failed to fetch $missing_secrets secrets ..." | ||
exit 1 | ||
fi | ||
|
||
exit 0 |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it was resized some how... AFAIK the disk was over the original 32 / 42 snc image disk size no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this part (the disk resize), we'll have to rethink it, this part was broken, and another script was broken
I think we'll try to let cloud-init handle it all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok that is pretty important...as with 34G and not resize to requested disk size... users can not use it..actually we already hit that issue and this is why we introduced this here