Azure / Azure/azure-cli

'az policy assignment update' needs to validate '--not-scopes' value

Open
#18,896 2 comments 0 reactions 1 assignee Claimed by @zhoxing-ms View on GitHub
act-identity-squad ARM feature-request
Dominant language
Python
Stars
4.6k
Forks
3.5k
Avg merge
3d 2h
Merged PRs (30d)
60

Description

**Describe the bug**

The `az policy assignment update` option's `--not-scopes` option, or the underlying API call, are not validating their input.

**To Reproduce**

Assuming the policy already has two or more exclusions...

```
EXCLUSIONS="$(
az policy assignment show -o tsv --query notScopes \
--name "$POLICY_NAME"
)"

az policy assignment update \
--name "$POLICY_NAME" \
--not-scopes "$EXCLUSIONS $NIC_ID"
```

The first command returns a value with embedded newlines, so the second command submits a value with that long newline-separated string, followed by a space, followed by the new ID. (The policy in this case is "NICs cannot have public IPs", and the exclusions are the NICs which have been "approved" to have public IPs.)

**Observed behavior**

The policy started with 15 exclusions. The command and the API call were accepted. After the call was processed, the policy (as shown in the portal) had 2 exclusions - one containing *all* of the existing IDs as a string with embedded newlines (which the portal didn't understand, but didn't show any kind of error message for) and one containing the new ID.

**Expected behavior**

EITHER the `az` command should convert newlines to spaces, OR the `az` command should throw an error message before the API call is made.

In addition, if the value with embedded newlines is submitted to the API, the API call should fail without making any changes.

**Environment summary**

`azure-cli 2.26.0` on macOS 11.3, installed using Homebrew. `bash` shell.

**Additional context**

Worked around the problem by fixing the input data. (The extra `echo` around the first command converted the newlines to spaces.) Note that this is the same fix I would have done if the command or the API call had thrown an error - the difference is, the policy database within Azure wouldn't have contained an invalid policy exclusion for the short time it took me to figure out what the problem was.

```
EXCLUSIONS="$( echo $(
az policy assignment show -o tsv --query notScopes \
--name "$POLICY_NAME"
))"

az policy assignment update \
--name "$POLICY_NAME" \
--not-scopes "$EXCLUSIONS $NIC_ID"
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.