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

Enable anonymous access in Nexus configuration script #4387

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
32 changes: 27 additions & 5 deletions devops/terraform/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ else
az storage account show --resource-group "$TF_VAR_mgmt_resource_group_name" --name "$TF_VAR_mgmt_storage_account_name" --output table
fi

# shellcheck disable=SC1091
source ../scripts/mgmtstorage_enable_public_access.sh

# Grant user blob data contributor permissions
echo -e "\n\e[34m»»» 🔑 \e[96mGranting Storage Blob Data Contributor role to the current user\e[0m..."
Expand All @@ -38,23 +36,47 @@ if [ -n "${ARM_CLIENT_ID:-}" ]; then
else
USER_OBJECT_ID=$(az ad signed-in-user show --query id --output tsv)
fi

az role assignment create --assignee "$USER_OBJECT_ID" \
--role "Storage Account Contributor" \
--scope "/subscriptions/$ARM_SUBSCRIPTION_ID/resourceGroups/$TF_VAR_mgmt_resource_group_name/providers/Microsoft.Storage/storageAccounts/$TF_VAR_mgmt_storage_account_name"


az role assignment create --assignee "$USER_OBJECT_ID" \
--role "Storage Blob Data Contributor" \
--scope "/subscriptions/$ARM_SUBSCRIPTION_ID/resourceGroups/$TF_VAR_mgmt_resource_group_name/providers/Microsoft.Storage/storageAccounts/$TF_VAR_mgmt_storage_account_name"

# Function to check if the role assignment exists
check_role_assignment() {
az role assignment list --assignee "$USER_OBJECT_ID" --role "Storage Blob Data Contributor" --scope "/subscriptions/$ARM_SUBSCRIPTION_ID/resourceGroups/$TF_VAR_mgmt_resource_group_name/providers/Microsoft.Storage/storageAccounts/$TF_VAR_mgmt_storage_account_name" --query "[].id" --output tsv
check_role_assignments() {
local sbdc=$(az role assignment list \
--assignee "$USER_OBJECT_ID" \
--role "Storage Blob Data Contributor" \
--scope "/subscriptions/$ARM_SUBSCRIPTION_ID/resourceGroups/$TF_VAR_mgmt_resource_group_name/providers/Microsoft.Storage/storageAccounts/$TF_VAR_mgmt_storage_account_name" \
--query "[].id" --output tsv)

local sac=$(az role assignment list \
--assignee "$USER_OBJECT_ID" \
--role "Storage Account Contributor" \
--scope "/subscriptions/$ARM_SUBSCRIPTION_ID/resourceGroups/$TF_VAR_mgmt_resource_group_name/providers/Microsoft.Storage/storageAccounts/$TF_VAR_mgmt_storage_account_name" \
--query "[].id" --output tsv)

# Return a non-empty value only if both roles are assigned
if [[ -n "$sbdc" && -n "$sac" ]]; then
echo "both"
fi
}

# Wait for the role assignment to be applied
echo -e "\n\e[34m»»» ⏳ \e[96mWaiting for role assignment to be applied\e[0m..."
while [ -z "$(check_role_assignment)" ]; do
while [ -z "$(check_role_assignments)" ]; do
echo "Waiting for role assignment..."
sleep 10
done
echo "Role assignment applied."

# shellcheck disable=SC1091
source ../scripts/mgmtstorage_enable_public_access.sh

# Blob container
# shellcheck disable=SC2154
az storage container create --account-name "$TF_VAR_mgmt_storage_account_name" --name "$TF_VAR_terraform_state_container_name" --auth-mode login -o table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ while [ ! -d "$(dirname "${BASH_SOURCE[0]}")"/nexus_repos_config ]; do
((timeout--))
done

echo 'Enabling anonymous access in Nexus...'
anon_status_code=$(curl -iu admin:"$1" -XPUT \
'http://localhost/service/rest/v1/security/anonymous' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"enabled": true,
"userId": "anonymous",
"realmName": "NexusAuthorizingRealm"
}' \
-k -s -w "%{http_code}" -o /dev/null)
echo "Response received from Nexus for anonymous access: $anon_status_code"
if [ "$anon_status_code" -ne 200 ]; then
echo "ERROR - Failed to enable anonymous access."
exit 1
fi

# Create proxy for each .json file
for filename in "$(dirname "${BASH_SOURCE[0]}")"/nexus_repos_config/*.json; do
echo "Found config file: $filename. Sending to Nexus..."
Expand Down
Loading