Copilot commented on code in PR #12132: URL: https://github.com/apache/gravitino/pull/12132#discussion_r3642492941
########## api/src/main/java/org/apache/gravitino/encryption/kms/KmsReference.java: ########## @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.encryption.kms; + +import com.google.common.base.Preconditions; +import java.util.Locale; +import java.util.Objects; +import javax.annotation.Nullable; +import org.apache.commons.lang3.StringUtils; +import org.apache.gravitino.annotation.DeveloperApi; + +/** + * Identifies a key owned by a configured KMS source. + * + * <p>Contains no credentials or key material. + */ +@DeveloperApi +public final class KmsReference { + + private final String api; + private final String source; + private final String keyId; + + /** + * Creates a structurally valid key reference without contacting the provider. + * + * @param api explicitly selected KMS API identifier + * @param source configured KMS client-instance name + * @param keyId provider-native key identifier + * @throws IllegalArgumentException if any argument is null or blank + */ + public KmsReference(String api, String source, String keyId) { + Preconditions.checkArgument(StringUtils.isNotBlank(api), "KMS API cannot be blank"); + Preconditions.checkArgument(StringUtils.isNotBlank(source), "KMS source cannot be blank"); + Preconditions.checkArgument(StringUtils.isNotBlank(keyId), "KMS key ID cannot be blank"); + + this.api = api.trim().toLowerCase(Locale.ROOT); + this.source = source; + this.keyId = keyId; + } Review Comment: `KmsReference` canonicalizes `api` (trim + lower-case) but stores `source` untrimmed. This allows structurally-valid references with leading/trailing whitespace in `source`, which is likely to cause surprising mismatches when routing by configured source name (and breaks symmetry with `api`). Consider trimming `source` when constructing the reference. ########## common/src/main/java/org/apache/gravitino/encryption/kms/KmsClient.java: ########## @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.encryption.kms; + +import java.util.Optional; +import org.apache.gravitino.annotation.DeveloperApi; +import org.apache.gravitino.exceptions.ConnectionFailedException; + +/** + * Inspects keys managed by a configured KMS. + * + * <p>This is a server-side operation client, not a credential-vending API. Provider credentials + * authenticate calls made by the client and must never be returned to callers. This client does not + * perform cryptographic operations. + */ +@DeveloperApi +public interface KmsClient extends AutoCloseable { + + /** + * Reads the provider-reported properties of a key. + * + * <p>An empty result means the provider authoritatively reported that the key does not exist. + * Authentication, authorization, timeout, availability, and other indeterminate failures are + * reported as exceptions, never as an empty result. + * + * @param reference key to inspect + * @return normalized key properties, or empty when the key authoritatively does not exist; never + * null + * @throws IllegalArgumentException if the reference does not belong to this client + * @throws ConnectionFailedException if the provider cannot be queried Review Comment: The reusable contract tests require `getKeyProperties(null)` to throw `IllegalArgumentException`, but the interface Javadoc doesn't state that `reference` must be non-null. Explicitly documenting the null/ownership precondition makes the contract clearer for provider implementations. ########## common/src/testFixtures/java/org/apache/gravitino/encryption/kms/FakeKmsClient.java: ########## @@ -0,0 +1,141 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.encryption.kms; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import java.util.Optional; +import org.apache.commons.lang3.StringUtils; + +/** In-memory KMS client for contract and consumer tests. */ +public final class FakeKmsClient implements KmsClient { + + private final String api; + private final String source; + private final Map<String, KeyState> keys = new HashMap<>(); + + /** + * Creates an empty fake client. + * + * @param api canonical KMS API identifier accepted by the client + * @param source configured source accepted by the client + */ + public FakeKmsClient(String api, String source) { + if (StringUtils.isBlank(api)) { + throw new IllegalArgumentException("KMS API cannot be blank"); + } + this.api = api.trim().toLowerCase(Locale.ROOT); + this.source = source; + } Review Comment: FakeKmsClient doesn't validate `source` (it can be null/blank). This can lead to a NullPointerException in `requireReference` (`source.equals(...)`), which makes failures harder to diagnose in contract/consumer tests. Validate (and ideally trim) `source` the same way `api` is handled. -- 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]
