Copilot commented on code in PR #11976:
URL: https://github.com/apache/cloudstack/pull/11976#discussion_r4677326388
##########
server/src/main/java/com/cloud/api/dispatch/ParamProcessWorker.java:
##########
@@ -496,27 +497,36 @@ private Date messageDate(final Date date, final int
hourOfDay, final int minute,
return cal.getTime();
}
- private Long translateUuidToInternalId(final String uuid, final Parameter
annotation) {
+ protected boolean isInternalIdDisallowed() {
+ return Boolean.TRUE.equals(ApiServer.ApiDisallowInternalIds.value());
+ }
+
+ protected Long translateUuidToInternalId(final String uuid, final
Parameter annotation) {
Review Comment:
Changing `translateUuidToInternalId` from `private` to `protected` (and
adding a `protected` config accessor) increases the overridable surface area of
this class primarily to support tests. Prefer keeping production visibility
minimal: use package-private visibility (if tests are in the same package), or
annotate/package-scope the helper with a clear testing intent (e.g.,
`@VisibleForTesting`), or refactor the config lookup behind an injected
collaborator so the method can remain `private` while still being testable.
##########
server/src/main/java/com/cloud/api/ApiServer.java:
##########
@@ -369,6 +369,19 @@ public class ApiServer extends ManagerBase implements
HttpRequestHandler, ApiSer
, ConfigKey.Scope.Global, null, null, null, null, null,
ConfigKey.Kind.Select,
EnumSet.allOf(ApiSessionKeyCheckOption.class).stream().map(Enum::toString).collect(Collectors.joining(",
")));
+ public static final ConfigKey<Boolean> ApiDisallowInternalIds = new
ConfigKey<>(
+ ConfigKey.CATEGORY_ADVANCED,
+ Boolean.class,
+ "api.disallow.internal.ids",
+ "true",
+ "When true (default), only UUIDs are accepted as entity
identifiers and requests passing internal database IDs "
+ + "(or any other non-UUID value) for UUID parameters are
rejected with a parameter error. "
+ + "When false, legacy pre-3.x APIs also accept internal
database IDs for backward compatibility.",
+ true,
+ ConfigKey.Scope.Global
+ );
Review Comment:
With the default set to `true`, this changes behavior for existing
deployments that still pass internal DB IDs to legacy pre-3.x APIs (they will
now be rejected). That is effectively a breaking behavior change unless
existing callers already only use UUIDs. Consider either (a) marking this PR as
a breaking change, or (b) changing the default to `false` and documenting a
migration path to enable it, or (c) gating the new default behind an explicit
upgrade/compatibility flag.
##########
server/src/main/java/com/cloud/api/dispatch/ParamProcessWorker.java:
##########
@@ -496,27 +497,36 @@ private Date messageDate(final Date date, final int
hourOfDay, final int minute,
return cal.getTime();
}
- private Long translateUuidToInternalId(final String uuid, final Parameter
annotation) {
+ protected boolean isInternalIdDisallowed() {
+ return Boolean.TRUE.equals(ApiServer.ApiDisallowInternalIds.value());
+ }
+
+ protected Long translateUuidToInternalId(final String uuid, final
Parameter annotation) {
if (uuid.equals("-1")) {
// FIXME: This is to handle a lot of hardcoded special cases where
-1 is sent
// APITODO: Find and get rid of all hardcoded params in API Cmds
and service layer
return -1L;
}
Long internalId = null;
- // If annotation's empty, the cmd existed before 3.x try conversion to
long
- final boolean isPre3x = annotation.since().isEmpty();
+ final boolean internalIdDisallowed = isInternalIdDisallowed();
+ // Internal IDs are only honoured for pre-3.x APIs (empty since) when
not disallowed
+ final boolean internalIdAllowed = annotation.since().isEmpty() &&
!internalIdDisallowed;
// Match against Java's UUID regex to check if input is uuid string
final boolean isUuid = UuidUtils.isUuid(uuid);
- // Enforce that it's uuid for newly added apis from version 3.x
- if (!isPre3x && !isUuid)
+ if (!internalIdAllowed && !isUuid) {
+ if (internalIdDisallowed) {
+ throw new
InvalidParameterValueException(String.format("Invalid parameter %s value=%s, a
valid UUID is expected", annotation.name(), uuid));
+ }
+ // Legacy behaviour: non-UUID values for 3.x+ APIs are ignored
return null;
+ }
Review Comment:
The new error message is generic and doesn’t explain why the same value
might have worked previously (legacy/internal ID behavior) or how to remediate.
Consider including clearer guidance such as: parameter name, that internal IDs
are disallowed by `api.disallow.internal.ids`, and that a UUID must be provided
(or that the setting can be changed for legacy compatibility). This will reduce
operator confusion when older scripts start failing.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]