LuciferYang commented on code in PR #14926:
URL: https://github.com/apache/gravitino/pull/14926#discussion_r4546643007
##########
core/src/main/java/org/apache/gravitino/secret/SecretManager.java:
##########
@@ -484,23 +547,56 @@ public String alterSetProperty(
}
/**
- * Deletes write-through secret if owned by this entity; removes key from
properties.
+ * Collects a write-through secret for post-commit deletion and removes the
key from properties.
*
* @param properties mutable properties map updated as changes are applied
* @param entityType {@code catalog}, {@code schema}, or {@code fileset}
* @param entityId stable numeric entity id
* @param property property key to remove
+ * @param replacedVersions list that receives the current version to delete
after commit
*/
public void alterRemoveProperty(
- Map<String, String> properties, String entityType, long entityId, String
property) {
+ Map<String, String> properties,
+ String entityType,
+ long entityId,
+ String property,
+ List<SecretVersion> replacedVersions) {
Preconditions.checkArgument(StringUtils.isNotBlank(property), "property
must not be blank");
String current = properties.get(property);
if (SecretPropertyUtils.isWriteThroughForEntity(property, current,
entityType, entityId)) {
- deleteSecretsFromProperties(Map.of(property, current));
+ collectCurrentVersion(SecretUrn.parse(current), replacedVersions);
}
properties.remove(property);
}
+ private void collectCurrentVersion(SecretUrn urn, List<SecretVersion>
replacedVersions) {
+ String version =
registry.getProvider(urn.providerName()).currentSecretVersion(urn);
Review Comment:
Rotating, `setProperty` and removing a write-through secret now look up its
current version first (`collectCurrentVersion`), and that throws `Secret not
found` when the URN has no material. So an entity whose material is gone can no
longer be repaired or cleaned up: a same-provider rebind, `setProperty` or
`removeProperty` on that key fails on every retry. That covers every
write-through secret after a restart with the in-memory provider, material
deleted outside Gravitino, and entities already left dangling by #13439. This
is the Fork CI failure in `testCatalogManageableWhenSecretMissing`; on main the
rebind overwrote the URN and the remove was best effort.
When there is no current version, collect nothing. A remove then has nothing
to delete, and for a rebind or `setProperty` the new version can simply become
current, which `writeSecretVersion` already does when the URN is empty.
##########
core/src/main/java/org/apache/gravitino/catalog/SchemaOperationDispatcher.java:
##########
@@ -551,10 +552,11 @@ private Pair<Schema, SchemaChange[]>
alterExternalSchemaUnderLock(
c -> c.doWithSchemaOps(s -> s.alterSchema(ident,
effectiveChanges)),
NoSuchSchemaException.class);
alterCommitted = true;
+ writtenSecretMaterials.commit(secretManager);
Review Comment:
On the external schema path `commit()` runs right after the external
`alterSchema`, but the SchemaEntity is only updated afterwards, in
`alterSchema` through `operateOnEntity`, which swallows failures. If that
update fails after a remove or a switch to a reference, the alter still
succeeds and the SchemaEntity keeps a URN whose version was just deleted, and
this path reads URNs from the SchemaEntity first.
Returning the holder to `alterSchema` and running the deletes only after
`operateOnEntity` succeeded (or when there is no entity) would close it. The
promote can stay where it is.
##########
core/src/test/java/org/apache/gravitino/secret/TestSecretManagerAlter.java:
##########
@@ -35,47 +37,201 @@ void testAlterSetSecretBindingWritesAndReturnsUrn() {
try (SecretManager secretManager = memorySecretManager()) {
Map<String, String> props = new HashMap<>(Map.of("jdbc-user", "root"));
List<SecretMaterial> written = new ArrayList<>();
+ List<SecretVersion> pending = new ArrayList<>();
+ List<SecretVersion> replacedVersions = new ArrayList<>();
+ List<SecretUrn> replacedUrns = new ArrayList<>();
String urn =
secretManager.alterSetSecretBinding(
props,
"catalog",
42L,
"jdbc-password",
new SecretBinding("memory", "s3cr3t"),
- written);
+ written,
+ pending,
+ replacedVersions,
+ replacedUrns);
Assertions.assertTrue(
SecretPropertyUtils.isWriteThroughForEntity("jdbc-password", urn,
"catalog", 42L));
Assertions.assertEquals(urn, props.get("jdbc-password"));
Assertions.assertEquals(1, written.size());
+ Assertions.assertTrue(pending.isEmpty());
Assertions.assertEquals(
"s3cr3t",
secretManager.getRegistry().getProvider("memory").readSecret(written.get(0).urn()));
}
}
@Test
- void testAlterRemovePropertyDeletesWriteThroughSecret() {
+ void testAlterRemovePropertyDefersWriteThroughSecretDeletion() {
try (SecretManager secretManager = memorySecretManager()) {
Map<String, String> props = new HashMap<>();
List<SecretMaterial> written = new ArrayList<>();
+ List<SecretVersion> pending = new ArrayList<>();
+ List<SecretVersion> replacedVersions = new ArrayList<>();
+ List<SecretUrn> replacedUrns = new ArrayList<>();
String urn =
secretManager.alterSetSecretBinding(
- props, "catalog", 7L, "jdbc-password", new
SecretBinding("memory", "old"), written);
+ props,
+ "catalog",
+ 7L,
+ "jdbc-password",
+ new SecretBinding("memory", "old"),
+ written,
+ pending,
+ replacedVersions,
+ replacedUrns);
- secretManager.alterRemoveProperty(props, "catalog", 7L, "jdbc-password");
+ List<SecretVersion> toDelete = new ArrayList<>();
+ secretManager.alterRemoveProperty(props, "catalog", 7L, "jdbc-password",
toDelete);
Assertions.assertFalse(props.containsKey("jdbc-password"));
+ Assertions.assertEquals(1, toDelete.size());
+ Assertions.assertEquals(
+ "old",
+
secretManager.getRegistry().getProvider("memory").readSecret(SecretUrn.parse(urn)));
+
+ SecretMaterialsHolder holder = new SecretMaterialsHolder();
+ holder.setReplacedVersions(toDelete);
+ holder.commit(secretManager);
Assertions.assertThrows(
IllegalArgumentException.class,
() ->
secretManager.getRegistry().getProvider("memory").readSecret(SecretUrn.parse(urn)));
}
}
+ @Test
+ void testFailedSameProviderRotationKeepsOldValue() {
+ try (SecretManager secretManager = memorySecretManager()) {
+ Map<String, String> props = new HashMap<>();
+ Pair<CatalogChange[], SecretMaterialsHolder> first =
+ SecretAlterChanges.prepareCatalogChanges(
+ secretManager,
+ props,
+ 3L,
+ CatalogChange.setSecretBinding("jdbc-password", new
SecretBinding("memory", "old")));
+ first.getRight().commit(secretManager);
+ String urn =
+ first.getLeft()[0] instanceof CatalogChange.SetProperty
+ ? ((CatalogChange.SetProperty) first.getLeft()[0]).getValue()
+ : null;
+ Assertions.assertNotNull(urn);
+ props.put("jdbc-password", urn);
+
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ SecretAlterChanges.prepareCatalogChanges(
+ secretManager,
+ props,
+ 3L,
+ CatalogChange.setSecretBinding(
+ "jdbc-password", new SecretBinding("memory", "new")),
+ CatalogChange.setProperty("jdbc-password", "******")));
+
+ Assertions.assertEquals(
+ "old",
+
secretManager.getRegistry().getProvider("memory").readSecret(SecretUrn.parse(urn)));
+ }
+ }
+
+ @Test
+ void testFailedPrepareKeepsReplacedSecretReadable() {
+ try (SecretManager secretManager = memorySecretManager()) {
+ Map<String, String> props = new HashMap<>();
+ Pair<CatalogChange[], SecretMaterialsHolder> first =
+ SecretAlterChanges.prepareCatalogChanges(
+ secretManager,
+ props,
+ 5L,
+ CatalogChange.setSecretBinding("jdbc-password", new
SecretBinding("memory", "keep")));
+ first.getRight().commit(secretManager);
+ String urn = ((CatalogChange.SetProperty) first.getLeft()[0]).getValue();
+ props.put("jdbc-password", urn);
+
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ SecretAlterChanges.prepareCatalogChanges(
+ secretManager,
+ props,
+ 5L,
+ CatalogChange.removeProperty("jdbc-password"),
+ CatalogChange.setProperty("other", "******")));
+
+ Assertions.assertEquals(
+ "keep",
+
secretManager.getRegistry().getProvider("memory").readSecret(SecretUrn.parse(urn)));
+ }
+ }
+
+ @Test
+ void testRotationWritesPendingVersionAndPromotesOnCommit() {
+ try (SecretManager secretManager = memorySecretManager()) {
+ Map<String, String> props = new HashMap<>();
+ Pair<CatalogChange[], SecretMaterialsHolder> create =
+ SecretAlterChanges.prepareCatalogChanges(
+ secretManager,
+ props,
+ 9L,
+ CatalogChange.setSecretBinding("jdbc-password", new
SecretBinding("memory", "old")));
+ create.getRight().commit(secretManager);
+ String urn = ((CatalogChange.SetProperty)
create.getLeft()[0]).getValue();
+ props.put("jdbc-password", urn);
+
+ Pair<CatalogChange[], SecretMaterialsHolder> rotate =
+ SecretAlterChanges.prepareCatalogChanges(
+ secretManager,
+ props,
+ 9L,
+ CatalogChange.setSecretBinding("jdbc-password", new
SecretBinding("memory", "new")));
+ Assertions.assertEquals(
+ "old",
+
secretManager.getRegistry().getProvider("memory").readSecret(SecretUrn.parse(urn)));
+ Assertions.assertEquals(1,
rotate.getRight().getPendingVersions().size());
+
+ rotate.getRight().commit(secretManager);
+ Assertions.assertEquals(
+ "new",
+
secretManager.getRegistry().getProvider("memory").readSecret(SecretUrn.parse(urn)));
+ Assertions.assertEquals(urn, ((CatalogChange.SetProperty)
rotate.getLeft()[0]).getValue());
+ }
+ }
+
+ @Test
+ void testRemoveAndRebindKeepsRewrittenSecretAfterCommit() {
+ try (SecretManager secretManager = memorySecretManager()) {
+ Map<String, String> props = new HashMap<>();
+ Pair<CatalogChange[], SecretMaterialsHolder> create =
+ SecretAlterChanges.prepareCatalogChanges(
+ secretManager,
+ props,
+ 11L,
+ CatalogChange.setSecretBinding("jdbc-password", new
SecretBinding("memory", "old")));
+ create.getRight().commit(secretManager);
+ String urn = ((CatalogChange.SetProperty)
create.getLeft()[0]).getValue();
+ props.put("jdbc-password", urn);
+
+ Pair<CatalogChange[], SecretMaterialsHolder> rebind =
+ SecretAlterChanges.prepareCatalogChanges(
+ secretManager,
+ props,
+ 11L,
+ CatalogChange.removeProperty("jdbc-password"),
+ CatalogChange.setSecretBinding(
+ "jdbc-password", new SecretBinding("memory", "rebound")));
+ rebind.getRight().commit(secretManager);
Review Comment:
The new tests call `holder.commit()` by hand and fake the entity commit with
`props.put`, and every failure case fails inside prepare on a `"******"` value.
So nothing covers `rollback()`, `copyFrom()`, the five call sites, a store
update failing after prepare, a failed promote, or the "a later alter on the
same URN is not wiped" claim, and the remove-and-rebind test only checks the
success path.
Could you add dispatcher-level cases, for example through
`CatalogManager.alterCatalog` with a store update that throws after prepare,
for a rotation, a remove, and a remove plus rebind?
##########
core/src/main/java/org/apache/gravitino/secret/SecretMaterialsHolder.java:
##########
@@ -39,4 +46,85 @@ public List<SecretMaterial> get() {
public void set(List<SecretMaterial> materials) {
this.materials = materials;
}
+
+ /**
+ * Returns pending versions written without changing the current readable
value. Promoted after
+ * commit; deleted on rollback.
+ */
+ public List<SecretVersion> getPendingVersions() {
+ return pendingVersions;
+ }
+
+ /** Records pending secret versions. */
+ public void setPendingVersions(List<SecretVersion> pendingVersions) {
+ this.pendingVersions = pendingVersions;
+ }
+
+ /**
+ * Returns versions replaced or removed while the URN may be rewritten in
the same or a later
+ * alter. Deleted with {@code deleteSecretVersion} after commit so a newer
current version is
+ * kept.
+ */
+ public List<SecretVersion> getReplacedVersions() {
+ return replacedVersions;
+ }
+
+ /** Records replaced secret versions for post-commit deletion. */
+ public void setReplacedVersions(List<SecretVersion> replacedVersions) {
+ this.replacedVersions = replacedVersions;
+ }
+
+ /**
+ * Returns write-through URNs replaced by a different URN (for example a
cross-provider bind).
+ * Deleted with {@code deleteSecret} after commit.
+ */
+ public List<SecretUrn> getReplacedUrns() {
+ return replacedUrns;
+ }
+
+ /** Records replaced write-through URNs for post-commit deletion. */
+ public void setReplacedUrns(List<SecretUrn> replacedUrns) {
+ this.replacedUrns = replacedUrns;
+ }
+
+ /**
+ * Copies bookkeeping from a prepare result into this holder (for use inside
{@code store.update}
+ * lambdas).
+ *
+ * @param prepared holder returned by prepare
+ */
+ public void copyFrom(SecretMaterialsHolder prepared) {
+ this.materials = prepared.materials;
+ this.pendingVersions = prepared.pendingVersions;
+ this.replacedVersions = prepared.replacedVersions;
+ this.replacedUrns = prepared.replacedUrns;
+ }
+
+ /**
+ * After a successful alter commit: promotes pending versions to current,
then deletes replaced
+ * versions and URNs. Best effort; must not fail the already-committed alter.
+ *
+ * @param secretManager secret manager
+ */
+ public void commit(SecretManager secretManager) {
+ secretManager.promoteSecretVersions(pendingVersions);
Review Comment:
For a same-URN rotation the entity property doesn't change, so the real
switch is the promote in `commit()`. That promote is best effort, and the next
line deletes the replaced version anyway. If the promote fails, the alter still
returns success and the old version, which is still current, is deleted, so the
entity points at a secret that can't be read. The entity version check also
can't order promotes across servers, so an earlier alter that promotes late
overwrites a later one.
At minimum, delete a replaced version only after the promote for its URN
succeeded. A sturdier option is to store the version id with the URN and read
by version, which Vault KV v2 (`?version=N`), AWS (`VersionId`) and GCP
(`versions/N`) support natively. The entity commit then becomes the switch, its
version check orders concurrent rotations, and no promote is needed.
##########
common/src/main/java/org/apache/gravitino/secret/SecretProvider.java:
##########
@@ -53,12 +53,67 @@ public interface SecretProvider {
* SecretConstants#ATTR_ENTITY_TYPE}, {@link
SecretConstants#ATTR_ENTITY_ID}, and {@link
* SecretConstants#ATTR_PROPERTY_KEY}.
*
+ * <p>For versioned providers this creates a new version and makes it the
current readable value
+ * (create path). Prefer {@link #writeSecretVersion} when an alter must keep
the previous current
+ * value readable until commit.
+ *
* @param plaintext the secret plaintext
* @param attributes provider-specific write attributes
* @return the secret URN
*/
SecretUrn writeSecret(String plaintext, Map<String, String> attributes);
+ /**
+ * Writes a new secret version under the URN built from {@code attributes}
without changing the
+ * currently readable value when material already exists.
+ *
+ * <p>Aligns with multi-version secret stores (Vault KV v2, AWS Secrets
Manager, GCP Secret
+ * Manager): the logical name / URN stays fixed; each write allocates a
version id. When the URN
+ * has no material yet, the new version becomes current.
+ *
+ * @param plaintext the secret plaintext
+ * @param attributes provider-specific write attributes
+ * @return the URN and the new version id
+ * @throws UnsupportedOperationException if this provider does not support
versions
+ */
+ default SecretVersion writeSecretVersion(String plaintext, Map<String,
String> attributes) {
Review Comment:
The four version methods default to `UnsupportedOperationException`, but
every same-provider rotation, write-through `setProperty` and `removeProperty`
calls them, so a provider that implements only the original SPI can no longer
rotate or remove the secrets it wrote. In practice the methods are mandatory.
The pending/promote model also maps natively only to AWS. In Vault KV v2
every write becomes the version an unversioned read returns, and GCP's `latest`
is always the newest version, so both would need a provider-side pointer.
Reading a pinned version is native in all three.
Since the SPI isn't released yet, could the contract be made explicit now:
abstract methods, or a capability flag that keeps the old overwrite/delete path
for providers without versions? The "aligns with Vault KV v2, AWS Secrets
Manager, GCP Secret Manager" wording in the Javadoc and the design doc needs
the same correction.
##########
core/src/main/java/org/apache/gravitino/secret/SecretManager.java:
##########
@@ -385,47 +435,58 @@ public String alterSetSecretBinding(
long entityId,
String property,
SecretBinding binding,
- List<SecretMaterial> written) {
+ List<SecretMaterial> written,
+ List<SecretVersion> pendingVersions,
+ List<SecretVersion> replacedVersions,
+ List<SecretUrn> replacedUrns) {
Preconditions.checkArgument(StringUtils.isNotBlank(property), "property
must not be blank");
Preconditions.checkArgument(binding != null, "binding must not be null");
SecretPropertyUtils.validateAlterSecretBindingPlaintext(binding.plaintext());
Map<String, SecretBinding> bindings = ImmutableMap.of(property, binding);
List<SecretUrn> urns = buildSecretBindingUrns(entityType, entityId,
bindings);
- String newUrn = urns.get(0).toString();
+ SecretUrn newUrn = urns.get(0);
String current = properties.get(property);
- if (current != null
- && !current.equals(newUrn)
- && SecretPropertyUtils.isWriteThroughForEntity(property, current,
entityType, entityId)) {
- deleteSecretsFromProperties(Map.of(property, current));
+ if (SecretPropertyUtils.isWriteThroughForEntity(property, current,
entityType, entityId)) {
Review Comment:
The bookkeeping is decided per change from the batch-local properties map,
so a batch that touches one key twice gets the wrong commit or rollback. After
`removeProperty(P)` or `setSecretReference(P)`, a same-provider
`setSecretBinding(P)` no longer sees P's URN and takes the first-bind branch:
`writeSecret` makes the new value current during prepare, and a failed alter's
`rollbackSecrets` deletes the whole URN, old version included, while the entity
still points at it.
Two shapes go wrong even when the alter succeeds. `[setSecretReference(P),
setSecretBinding(P, same provider)]` leaves P's final URN in `replacedUrns`, so
`commit()` deletes the secret the entity references. `[setSecretBinding(P),
removeProperty(P)]` deletes only the old version, so the removed plaintext
stays readable.
Same-key batches aren't rejected
(`testRemoveAndRebindKeepsRewrittenSecretAfterCommit` uses one). Could the
actions be derived per key from the batch's start and end values instead?
##########
core/src/main/java/org/apache/gravitino/secret/SecretMaterialsHolder.java:
##########
@@ -39,4 +46,85 @@ public List<SecretMaterial> get() {
public void set(List<SecretMaterial> materials) {
this.materials = materials;
}
+
+ /**
+ * Returns pending versions written without changing the current readable
value. Promoted after
+ * commit; deleted on rollback.
+ */
+ public List<SecretVersion> getPendingVersions() {
+ return pendingVersions;
+ }
+
+ /** Records pending secret versions. */
+ public void setPendingVersions(List<SecretVersion> pendingVersions) {
+ this.pendingVersions = pendingVersions;
+ }
+
+ /**
+ * Returns versions replaced or removed while the URN may be rewritten in
the same or a later
+ * alter. Deleted with {@code deleteSecretVersion} after commit so a newer
current version is
+ * kept.
+ */
+ public List<SecretVersion> getReplacedVersions() {
+ return replacedVersions;
+ }
+
+ /** Records replaced secret versions for post-commit deletion. */
+ public void setReplacedVersions(List<SecretVersion> replacedVersions) {
+ this.replacedVersions = replacedVersions;
+ }
+
+ /**
+ * Returns write-through URNs replaced by a different URN (for example a
cross-provider bind).
+ * Deleted with {@code deleteSecret} after commit.
+ */
+ public List<SecretUrn> getReplacedUrns() {
+ return replacedUrns;
+ }
+
+ /** Records replaced write-through URNs for post-commit deletion. */
+ public void setReplacedUrns(List<SecretUrn> replacedUrns) {
+ this.replacedUrns = replacedUrns;
+ }
+
+ /**
+ * Copies bookkeeping from a prepare result into this holder (for use inside
{@code store.update}
+ * lambdas).
+ *
+ * @param prepared holder returned by prepare
+ */
+ public void copyFrom(SecretMaterialsHolder prepared) {
+ this.materials = prepared.materials;
+ this.pendingVersions = prepared.pendingVersions;
+ this.replacedVersions = prepared.replacedVersions;
+ this.replacedUrns = prepared.replacedUrns;
+ }
+
+ /**
+ * After a successful alter commit: promotes pending versions to current,
then deletes replaced
+ * versions and URNs. Best effort; must not fail the already-committed alter.
+ *
+ * @param secretManager secret manager
+ */
+ public void commit(SecretManager secretManager) {
+ secretManager.promoteSecretVersions(pendingVersions);
+ secretManager.rollbackSecretVersions(replacedVersions);
+ for (SecretUrn urn : replacedUrns) {
+ try {
+
secretManager.getRegistry().getProvider(urn.providerName()).deleteSecret(urn);
Review Comment:
Replaced versions are now deleted by id so that a later alter writing the
same URN keeps its value, but `replacedUrns` still goes through `deleteSecret`,
which drops every version. yuqi1129's interleaving from #13440 still works on
these paths: A switches P to a reference and commits, B binds P back to the
same provider (writing into the same URN) and commits, then A's commit deletes
the URN and B's entity references nothing.
Collecting the current version of the replaced URN during prepare and
deleting that version, as the rotation path already does, would close it.
--
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]