feat(glue): catalog resource policy and Data Catalog encryption settings (#4027)

* feat(glue): catalog resource policy and Data Catalog encryption settings

Add PutResourcePolicy, GetResourcePolicy, GetResourcePolicies and
DeleteResourcePolicy with the PolicyExistsCondition and
PolicyHashCondition checks Terraform relies on, and Get/Put
DataCatalogEncryptionSettings with the documented defaults.

ConnectionPasswordEncryption is applied rather than stored: while it is
on, CreateConnection and UpdateConnection encrypt PASSWORD and the four
Kafka passwords with the configured KMS key and keep them under their
documented ENCRYPTED_* names, so reads return the ciphertext and a
missing key is GlueEncryptionException.

Closes #4023

* docs(glue): cite the source for connection password encryption timing and drop an unsourced claim

The developer guide states that whether a password is encrypted was
decided when the connection was created or updated, which is the
source for connections keeping their plaintext; the missing-key error
is described as the error CreateConnection and UpdateConnection list
rather than as AWS behaviour. Also replaces a var in the compat test
with the explicit type.
This commit is contained in:
David authored and GitHub committed 2026-09-20 17:32:36 -04:00
1 parent 2d56d85e9e
commit 628952ecf0
12 files changed
+899 -12

No files matched your search

@@ -0,0 +1,182 @@
package io.github.hectorvent.floci.services.glue;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.UUID;
import static io.github.hectorvent.floci.testing.RestAssuredJsonUtils.awsAction;
import static io.github.hectorvent.floci.testing.RestAssuredJsonUtils.awsActionJson;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* The catalog's resource policy and encryption settings over JSON 1.1, in the order Terraform
* drives them, plus the observable effect of ConnectionPasswordEncryption: a connection created
* while it is on stores its password KMS-encrypted and GetConnection returns it that way.
* Both settings are one per catalog, so the class runs in order and puts the catalog back at
* the end.
*/
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class GlueCatalogPolicyAndEncryptionIntegrationTest {
private static final String GLUE = "AWSGlue";
private static final String KMS = "TrentService";
private static final String POLICY = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Principal\":{\"AWS\":\"arn:aws:iam::111122223333:root\"},\"Action\":\"glue:GetTable\",\"Resource\":\"*\"}]}";
private static final String POLICY_V2 = POLICY.replace("glue:GetTable", "glue:GetTables");
private static String policyHash;
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
@Order(1)
void resourcePolicy_absentAtFirst_isEntityNotFound() {
// The catalog may carry a policy from another class; start from a known state.
awsAction(GLUE, "DeleteResourcePolicy", "{}");
awsAction(GLUE, "GetResourcePolicy", "{}")
.then()
.statusCode(400)
.body("__type", equalTo("EntityNotFoundException"));
awsAction(GLUE, "GetResourcePolicies", "{}")
.then()
.statusCode(200)
.body("GetResourcePoliciesResponseList.size()", equalTo(0));
}
@Test
@Order(2)
void putResourcePolicy_notExistThenMustExist_createsThenUpdates() throws Exception {
JsonNode created = awsActionJson(GLUE, "PutResourcePolicy",
"{\"PolicyInJson\": " + quoted(POLICY) + ", \"PolicyExistsCondition\": \"NOT_EXIST\"}");
policyHash = created.get("PolicyHash").asText();
awsAction(GLUE, "GetResourcePolicy", "{}")
.then()
.statusCode(200)
.body("PolicyInJson", equalTo(POLICY))
.body("PolicyHash", equalTo(policyHash))
.body("CreateTime", notNullValue())
.body("UpdateTime", notNullValue());
// Terraform's second create against an existing policy must be refused.
awsAction(GLUE, "PutResourcePolicy",
"{\"PolicyInJson\": " + quoted(POLICY_V2) + ", \"PolicyExistsCondition\": \"NOT_EXIST\"}")
.then()
.statusCode(400)
.body("__type", equalTo("ConditionCheckFailureException"));
JsonNode updated = awsActionJson(GLUE, "PutResourcePolicy",
"{\"PolicyInJson\": " + quoted(POLICY_V2) + ", \"PolicyExistsCondition\": \"MUST_EXIST\","
+ " \"PolicyHashCondition\": " + quoted(policyHash) + ", \"EnableHybrid\": \"TRUE\"}");
policyHash = updated.get("PolicyHash").asText();
awsAction(GLUE, "GetResourcePolicies", "{}")
.then()
.statusCode(200)
.body("GetResourcePoliciesResponseList.size()", equalTo(1))
.body("GetResourcePoliciesResponseList[0].PolicyInJson", equalTo(POLICY_V2));
}
@Test
@Order(3)
void deleteResourcePolicy_wrongHashRefused_rightHashDeletes() {
awsAction(GLUE, "DeleteResourcePolicy", "{\"PolicyHashCondition\": \"stale\"}")
.then()
.statusCode(400)
.body("__type", equalTo("ConditionCheckFailureException"));
awsAction(GLUE, "DeleteResourcePolicy", "{\"PolicyHashCondition\": " + quoted(policyHash) + "}")
.then()
.statusCode(200);
awsAction(GLUE, "DeleteResourcePolicy", "{}")
.then()
.statusCode(400)
.body("__type", equalTo("EntityNotFoundException"));
}
@Test
@Order(4)
void encryptionSettings_defaultOff_thenEncryptConnectionPasswordsWithARealKmsKey() throws Exception {
awsAction(GLUE, "GetDataCatalogEncryptionSettings", "{}")
.then()
.statusCode(200)
.body("DataCatalogEncryptionSettings.EncryptionAtRest.CatalogEncryptionMode", equalTo("DISABLED"))
.body("DataCatalogEncryptionSettings.ConnectionPasswordEncryption.ReturnConnectionPasswordEncrypted", equalTo(false));
String keyId = awsActionJson(KMS, "CreateKey", "{\"Description\": \"glue connection passwords\"}")
.path("KeyMetadata").path("KeyId").asText();
awsAction(GLUE, "PutDataCatalogEncryptionSettings", """
{"DataCatalogEncryptionSettings": {
"EncryptionAtRest": {"CatalogEncryptionMode": "DISABLED"},
"ConnectionPasswordEncryption": {"ReturnConnectionPasswordEncrypted": true, "AwsKmsKeyId": "%s"}
}}
""".formatted(keyId))
.then().statusCode(200);
awsAction(GLUE, "GetDataCatalogEncryptionSettings", "{}")
.then()
.statusCode(200)
.body("DataCatalogEncryptionSettings.ConnectionPasswordEncryption.ReturnConnectionPasswordEncrypted", equalTo(true))
.body("DataCatalogEncryptionSettings.ConnectionPasswordEncryption.AwsKmsKeyId", equalTo(keyId));
String name = "enc-" + UUID.randomUUID().toString().substring(0, 8);
awsAction(GLUE, "CreateConnection", """
{"ConnectionInput": {"Name": "%s", "ConnectionType": "JDBC",
"ConnectionProperties": {"JDBC_CONNECTION_URL": "jdbc:postgresql://db:5432/orders", "USERNAME": "app", "PASSWORD": "s3cret"}}}
""".formatted(name))
.then().statusCode(200);
JsonNode connection = awsActionJson(GLUE, "GetConnection", "{\"Name\": \"" + name + "\"}").path("Connection");
JsonNode properties = connection.path("ConnectionProperties");
assertEquals("app", properties.path("USERNAME").asText());
assertEquals(false, properties.has("PASSWORD"));
String encrypted = properties.path("ENCRYPTED_PASSWORD").asText();
// The stored value is a real KMS ciphertext: KMS itself decrypts it back to the password.
JsonNode decrypted = awsActionJson(KMS, "Decrypt", "{\"CiphertextBlob\": " + quoted(encrypted) + "}");
assertEquals("s3cret", new String(Base64.getDecoder().decode(decrypted.path("Plaintext").asText()), StandardCharsets.UTF_8));
awsAction(GLUE, "GetConnection", "{\"Name\": \"" + name + "\", \"HidePassword\": true}")
.then()
.statusCode(200)
.body("Connection.ConnectionProperties", not(hasKey("ENCRYPTED_PASSWORD")))
.body("Connection.ConnectionProperties", not(hasKey("PASSWORD")));
awsAction(GLUE, "DeleteConnection", "{\"ConnectionName\": \"" + name + "\"}").then().statusCode(200);
// Put the catalog back so other classes see the default.
awsAction(GLUE, "PutDataCatalogEncryptionSettings", """
{"DataCatalogEncryptionSettings": {
"EncryptionAtRest": {"CatalogEncryptionMode": "DISABLED"},
"ConnectionPasswordEncryption": {"ReturnConnectionPasswordEncrypted": false}
}}
""")
.then().statusCode(200);
}
@Test
@Order(5)
void putDataCatalogEncryptionSettings_unknownMode_isInvalidInput() {
awsAction(GLUE, "PutDataCatalogEncryptionSettings",
"{\"DataCatalogEncryptionSettings\": {\"EncryptionAtRest\": {\"CatalogEncryptionMode\": \"AES\"}}}")
.then()
.statusCode(400)
.body("__type", equalTo("InvalidInputException"));
}
private static String quoted(String value) {
return "\"" + value.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
}
}
@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.glue.schemaregistry.GlueSchemaRegistryService;
import io.github.hectorvent.floci.services.kms.KmsService;
import io.github.hectorvent.floci.services.resourcegroupstagging.ResourceGroupsTaggingService;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
@@ -41,7 +42,8 @@ class GlueJsonHandlerEmptyListTest {
GlueSchemaRegistryService schemaRegistryService =
new GlueSchemaRegistryService(storageFactory, regionResolver);
GlueService glueService = new GlueService(
storageFactory, schemaRegistryService, regionResolver, new ResourceGroupsTaggingService(storageFactory));
storageFactory, schemaRegistryService, regionResolver, new ResourceGroupsTaggingService(storageFactory),
new KmsService(storageFactory, regionResolver));
handler = new GlueJsonHandler(glueService, schemaRegistryService, mapper);
}
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.glue.schemaregistry.GlueSchemaRegistryService;
import io.github.hectorvent.floci.services.kms.KmsService;
import io.github.hectorvent.floci.services.resourcegroupstagging.ResourceGroupsTaggingService;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
@@ -41,7 +42,8 @@ class GlueJsonHandlerTest {
GlueSchemaRegistryService schemaRegistryService =
new GlueSchemaRegistryService(storageFactory, regionResolver);
GlueService glueService = new GlueService(
storageFactory, schemaRegistryService, regionResolver, new ResourceGroupsTaggingService(storageFactory));
storageFactory, schemaRegistryService, regionResolver, new ResourceGroupsTaggingService(storageFactory),
new KmsService(storageFactory, regionResolver));
handler = new GlueJsonHandler(glueService, schemaRegistryService, mapper);
}
@@ -737,4 +739,58 @@ class GlueJsonHandlerTest {
inline.putObject("ConnectionProperties").put("JDBC_CONNECTION_URL", "jdbc:mysql://h:3306/d");
assertEquals(200, handler.handle("TestConnection", test, REGION).getStatus());
}
@Test
void resourcePolicyOperationsAnswerWithTheDocumentedBodies() throws Exception {
String policy = "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
ObjectNode put = mapper.createObjectNode();
put.put("PolicyInJson", policy);
put.put("PolicyExistsCondition", "NOT_EXIST");
JsonNode putBody = mapper.valueToTree(handler.handle("PutResourcePolicy", put, REGION).getEntity());
String hash = putBody.get("PolicyHash").asText();
assertFalse(hash.isBlank());
JsonNode got = mapper.valueToTree(handler.handle("GetResourcePolicy", mapper.createObjectNode(), REGION).getEntity());
assertEquals(policy, got.get("PolicyInJson").asText());
assertEquals(hash, got.get("PolicyHash").asText());
assertTrue(got.get("CreateTime").isNumber());
assertTrue(got.get("UpdateTime").isNumber());
JsonNode list = mapper.valueToTree(handler.handle("GetResourcePolicies", mapper.createObjectNode(), REGION).getEntity());
assertEquals(1, list.get("GetResourcePoliciesResponseList").size());
assertEquals(hash, list.get("GetResourcePoliciesResponseList").get(0).get("PolicyHash").asText());
assertFalse(list.has("NextToken"));
AwsException conflict = assertThrows(AwsException.class, () -> handler.handle("PutResourcePolicy", put, REGION));
assertEquals("ConditionCheckFailureException", conflict.getErrorCode());
Response deleted = handler.handle("DeleteResourcePolicy", mapper.createObjectNode(), REGION);
assertEquals(0, mapper.valueToTree(deleted.getEntity()).size());
AwsException gone = assertThrows(AwsException.class,
() -> handler.handle("GetResourcePolicy", mapper.createObjectNode(), REGION));
assertEquals("EntityNotFoundException", gone.getErrorCode());
}
@Test
void encryptionSettingsReportBothBlocksBeforeAndAfterAPut() throws Exception {
JsonNode defaults = mapper.valueToTree(
handler.handle("GetDataCatalogEncryptionSettings", mapper.createObjectNode(), REGION).getEntity());
JsonNode settings = defaults.get("DataCatalogEncryptionSettings");
assertEquals("DISABLED", settings.get("EncryptionAtRest").get("CatalogEncryptionMode").asText());
assertFalse(settings.get("EncryptionAtRest").has("SseAwsKmsKeyId"));
assertFalse(settings.get("ConnectionPasswordEncryption").get("ReturnConnectionPasswordEncrypted").asBoolean());
ObjectNode put = mapper.createObjectNode();
ObjectNode block = put.putObject("DataCatalogEncryptionSettings").putObject("ConnectionPasswordEncryption");
block.put("ReturnConnectionPasswordEncrypted", true);
block.put("AwsKmsKeyId", "alias/glue");
assertEquals(0, mapper.valueToTree(handler.handle("PutDataCatalogEncryptionSettings", put, REGION).getEntity()).size());
JsonNode after = mapper.valueToTree(
handler.handle("GetDataCatalogEncryptionSettings", mapper.createObjectNode(), REGION).getEntity())
.get("DataCatalogEncryptionSettings");
assertTrue(after.get("ConnectionPasswordEncryption").get("ReturnConnectionPasswordEncrypted").asBoolean());
assertEquals("alias/glue", after.get("ConnectionPasswordEncryption").get("AwsKmsKeyId").asText());
assertEquals("DISABLED", after.get("EncryptionAtRest").get("CatalogEncryptionMode").asText());
}
}
@@ -12,6 +12,10 @@ import io.github.hectorvent.floci.services.glue.model.Column;
import io.github.hectorvent.floci.services.glue.model.Database;
import io.github.hectorvent.floci.services.glue.model.Connection;
import io.github.hectorvent.floci.services.glue.model.ConnectionInput;
import io.github.hectorvent.floci.services.glue.model.ConnectionPasswordEncryption;
import io.github.hectorvent.floci.services.glue.model.DataCatalogEncryptionSettings;
import io.github.hectorvent.floci.services.glue.model.EncryptionAtRest;
import io.github.hectorvent.floci.services.glue.model.GluePolicy;
import io.github.hectorvent.floci.services.glue.model.Crawler;
import io.github.hectorvent.floci.services.glue.model.AuthenticationConfiguration;
import io.github.hectorvent.floci.services.glue.model.PhysicalConnectionRequirements;
@@ -28,6 +32,7 @@ import io.github.hectorvent.floci.services.glue.model.StorageDescriptor;
import io.github.hectorvent.floci.services.glue.model.Table;
import io.github.hectorvent.floci.services.glue.model.UserDefinedFunction;
import io.github.hectorvent.floci.services.glue.schemaregistry.GlueSchemaRegistryService;
import io.github.hectorvent.floci.services.kms.KmsService;
import io.github.hectorvent.floci.services.glue.schemaregistry.model.RegistryId;
import io.github.hectorvent.floci.services.glue.schemaregistry.model.SchemaId;
import io.github.hectorvent.floci.services.resourcegroupstagging.ResourceGroupsTaggingService;
@@ -35,6 +40,8 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -44,6 +51,7 @@ import java.util.concurrent.CountDownLatch;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -65,6 +73,7 @@ class GlueServiceTest {
private GlueService glueService;
private GlueSchemaRegistryService schemaRegistryService;
private KmsService kmsService;
private StorageBackend<String, Database> databaseStore;
private StorageBackend<String, Table> tableStore;
private StorageBackend<String, Table> tableVersionStore;
@@ -76,6 +85,7 @@ class GlueServiceTest {
RegionResolver regionResolver = new RegionResolver(REGION, ACCOUNT_ID);
StorageFactory storageFactory = new InMemoryStorageFactory();
schemaRegistryService = new GlueSchemaRegistryService(storageFactory, regionResolver);
kmsService = new KmsService(storageFactory, regionResolver);
databaseStore = new InMemoryStorage<>();
tableStore = new InMemoryStorage<>();
tableVersionStore = new InMemoryStorage<>();
@@ -94,7 +104,10 @@ class GlueServiceTest {
new InMemoryStorage<String, Crawler>(),
new InMemoryStorage<String, Classifier>(),
new InMemoryStorage<String, Connection>(),
schemaRegistryService, regionResolver, new ResourceGroupsTaggingService(null));
new InMemoryStorage<String, GluePolicy>(),
new InMemoryStorage<String, DataCatalogEncryptionSettings>(),
schemaRegistryService, regionResolver, new ResourceGroupsTaggingService(null),
kmsService);
glueService.createDatabase(new Database("db1"));
}
@@ -1528,7 +1541,7 @@ class GlueServiceTest {
redefinition.setName("orders");
redefinition.setConnectionType("JDBC");
redefinition.setConnectionProperties(Map.of("JDBC_CONNECTION_URL", "jdbc:postgresql://db2.internal:5432/orders"));
glueService.updateConnection("orders", redefinition);
glueService.updateConnection("orders", redefinition, REGION);
Connection updated = glueService.getConnection("orders", false);
assertEquals("jdbc:postgresql://db2.internal:5432/orders", updated.getConnectionProperties().get("JDBC_CONNECTION_URL"));
@@ -1553,7 +1566,7 @@ class GlueServiceTest {
assertEquals("AlreadyExistsException", duplicate.getErrorCode());
AwsException missing = assertThrows(AwsException.class,
() -> glueService.updateConnection("absent", jdbcConnection("absent")));
() -> glueService.updateConnection("absent", jdbcConnection("absent"), REGION));
assertEquals("EntityNotFoundException", missing.getErrorCode());
AwsException missingDelete = assertThrows(AwsException.class,
@@ -1720,6 +1733,176 @@ class GlueServiceTest {
assertThrows(AwsException.class, () -> glueService.getTags(arn, REGION)).getErrorCode());
}
// ---- Catalog resource policy and encryption settings -----------------------------------
private static final String POLICY_V1 = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::111122223333:root\"},\"Action\":\"glue:GetTable\",\"Resource\":\"*\"}]}";
private static final String POLICY_V2 = POLICY_V1.replace("glue:GetTable", "glue:GetTables");
@Test
void resourcePolicyFollowsTheTerraformSequenceOfConditions() {
assertEquals("EntityNotFoundException",
assertThrows(AwsException.class, () -> glueService.getResourcePolicy()).getErrorCode());
assertTrue(glueService.getResourcePolicies(null, null).items().isEmpty());
// Create: NOT_EXIST must pass on an empty catalog.
String hash = glueService.putResourcePolicy(POLICY_V1, null, "NOT_EXIST", null);
GluePolicy stored = glueService.getResourcePolicy();
assertEquals(POLICY_V1, stored.getPolicyInJson());
assertEquals(hash, stored.getPolicyHash());
assertNotNull(stored.getCreateTime());
assertEquals(stored.getCreateTime(), stored.getUpdateTime());
assertEquals(1, glueService.getResourcePolicies(null, null).items().size());
// A second create must fail, an update must pass and keep CreateTime.
assertEquals("ConditionCheckFailureException",
assertThrows(AwsException.class,
() -> glueService.putResourcePolicy(POLICY_V2, null, "NOT_EXIST", null)).getErrorCode());
String hash2 = glueService.putResourcePolicy(POLICY_V2, hash, "MUST_EXIST", "TRUE");
GluePolicy updated = glueService.getResourcePolicy();
assertEquals(POLICY_V2, updated.getPolicyInJson());
assertNotEquals(hash, hash2);
assertEquals(stored.getCreateTime(), updated.getCreateTime());
assertFalse(updated.getUpdateTime().isBefore(stored.getUpdateTime()));
// A stale hash is refused on put and on delete; the right one is accepted.
assertEquals("ConditionCheckFailureException",
assertThrows(AwsException.class,
() -> glueService.putResourcePolicy(POLICY_V1, hash, "NONE", null)).getErrorCode());
assertEquals("ConditionCheckFailureException",
assertThrows(AwsException.class, () -> glueService.deleteResourcePolicy(hash)).getErrorCode());
glueService.deleteResourcePolicy(hash2);
assertEquals("EntityNotFoundException",
assertThrows(AwsException.class, () -> glueService.deleteResourcePolicy(null)).getErrorCode());
assertEquals("ConditionCheckFailureException",
assertThrows(AwsException.class,
() -> glueService.putResourcePolicy(POLICY_V1, null, "MUST_EXIST", null)).getErrorCode());
}
@Test
void resourcePolicyInputIsValidated() {
assertEquals("InvalidInputException",
assertThrows(AwsException.class, () -> glueService.putResourcePolicy(null, null, null, null)).getErrorCode());
assertEquals("InvalidInputException",
assertThrows(AwsException.class, () -> glueService.putResourcePolicy("{", null, null, null)).getErrorCode());
assertEquals("InvalidInputException",
assertThrows(AwsException.class, () -> glueService.putResourcePolicy("[]", null, null, null)).getErrorCode());
assertEquals("InvalidInputException",
assertThrows(AwsException.class, () -> glueService.putResourcePolicy(POLICY_V1, null, "MAYBE", null)).getErrorCode());
assertEquals("InvalidInputException",
assertThrows(AwsException.class, () -> glueService.putResourcePolicy(POLICY_V1, null, null, "yes")).getErrorCode());
// The same document always hashes the same, so a client can compare hashes across reads.
assertEquals(glueService.putResourcePolicy(POLICY_V1, null, null, null),
glueService.putResourcePolicy(POLICY_V1, null, null, null));
}
@Test
void encryptionSettingsDefaultToOffAndAreReplacedByPut() {
DataCatalogEncryptionSettings defaults = glueService.getDataCatalogEncryptionSettings();
assertEquals("DISABLED", defaults.getEncryptionAtRest().getCatalogEncryptionMode());
assertEquals(false, defaults.getConnectionPasswordEncryption().getReturnConnectionPasswordEncrypted());
assertNull(defaults.getConnectionPasswordEncryption().getAwsKmsKeyId());
DataCatalogEncryptionSettings put = new DataCatalogEncryptionSettings();
EncryptionAtRest atRest = new EncryptionAtRest();
atRest.setCatalogEncryptionMode("SSE-KMS");
atRest.setSseAwsKmsKeyId("alias/catalog");
put.setEncryptionAtRest(atRest);
glueService.putDataCatalogEncryptionSettings(put);
DataCatalogEncryptionSettings read = glueService.getDataCatalogEncryptionSettings();
assertEquals("SSE-KMS", read.getEncryptionAtRest().getCatalogEncryptionMode());
assertEquals("alias/catalog", read.getEncryptionAtRest().getSseAwsKmsKeyId());
// The block left out of the put is reported with its default, not dropped.
assertEquals(false, read.getConnectionPasswordEncryption().getReturnConnectionPasswordEncrypted());
EncryptionAtRest badMode = new EncryptionAtRest();
badMode.setCatalogEncryptionMode("AES");
DataCatalogEncryptionSettings invalid = new DataCatalogEncryptionSettings();
invalid.setEncryptionAtRest(badMode);
assertEquals("InvalidInputException",
assertThrows(AwsException.class, () -> glueService.putDataCatalogEncryptionSettings(invalid)).getErrorCode());
DataCatalogEncryptionSettings missingFlag = new DataCatalogEncryptionSettings();
missingFlag.setConnectionPasswordEncryption(new ConnectionPasswordEncryption());
assertEquals("InvalidInputException",
assertThrows(AwsException.class, () -> glueService.putDataCatalogEncryptionSettings(missingFlag)).getErrorCode());
assertEquals("InvalidInputException",
assertThrows(AwsException.class, () -> glueService.putDataCatalogEncryptionSettings(null)).getErrorCode());
}
private void enableConnectionPasswordEncryption(String keyId) {
ConnectionPasswordEncryption passwords = new ConnectionPasswordEncryption();
passwords.setReturnConnectionPasswordEncrypted(true);
passwords.setAwsKmsKeyId(keyId);
DataCatalogEncryptionSettings settings = new DataCatalogEncryptionSettings();
settings.setConnectionPasswordEncryption(passwords);
glueService.putDataCatalogEncryptionSettings(settings);
}
@Test
void connectionPasswordsAreStoredEncryptedOnceTheCatalogSettingIsOn() {
String keyId = kmsService.createKey("glue connection passwords", REGION).getKeyId();
enableConnectionPasswordEncryption(keyId);
ConnectionInput jdbc = jdbcConnection("enc-jdbc");
glueService.createConnection(jdbc, null, REGION);
Connection stored = glueService.getConnection("enc-jdbc", false);
assertNull(stored.getConnectionProperties().get("PASSWORD"));
String encrypted = stored.getConnectionProperties().get("ENCRYPTED_PASSWORD");
assertNotNull(encrypted);
assertEquals("s3cret", new String(
kmsService.decrypt(Base64.getDecoder().decode(encrypted), REGION), StandardCharsets.UTF_8));
assertEquals("app", stored.getConnectionProperties().get("USERNAME"));
// HidePassword removes the encrypted form as it removes the plaintext one.
assertNull(glueService.getConnection("enc-jdbc", true).getConnectionProperties().get("ENCRYPTED_PASSWORD"));
ConnectionInput kafka = jdbcConnection("enc-kafka");
kafka.setConnectionType("KAFKA");
Map<String, String> properties = new LinkedHashMap<>();
properties.put("KAFKA_BOOTSTRAP_SERVERS", "broker:9092");
properties.put("KAFKA_SASL_MECHANISM", "SCRAM-SHA-512");
properties.put("KAFKA_SASL_SCRAM_USERNAME", "svc");
properties.put("KAFKA_SASL_SCRAM_PASSWORD", "scram-pw");
properties.put("KAFKA_CLIENT_KEYSTORE_PASSWORD", "ks-pw");
kafka.setConnectionProperties(properties);
glueService.createConnection(kafka, null, REGION);
Map<String, String> kafkaStored = glueService.getConnection("enc-kafka", false).getConnectionProperties();
assertNull(kafkaStored.get("KAFKA_SASL_SCRAM_PASSWORD"));
assertNull(kafkaStored.get("KAFKA_CLIENT_KEYSTORE_PASSWORD"));
assertNotNull(kafkaStored.get("ENCRYPTED_KAFKA_SASL_SCRAM_PASSWORD"));
assertNotNull(kafkaStored.get("ENCRYPTED_KAFKA_CLIENT_KEYSTORE_PASSWORD"));
assertEquals("svc", kafkaStored.get("KAFKA_SASL_SCRAM_USERNAME"));
// An update goes through the same path.
ConnectionInput redefinition = jdbcConnection("enc-jdbc");
redefinition.setConnectionProperties(Map.of("JDBC_CONNECTION_URL", "jdbc:x://h/d", "PASSWORD", "changed"));
glueService.updateConnection("enc-jdbc", redefinition, REGION);
Map<String, String> afterUpdate = glueService.getConnection("enc-jdbc", false).getConnectionProperties();
assertNull(afterUpdate.get("PASSWORD"));
assertEquals("changed", new String(kmsService.decrypt(
Base64.getDecoder().decode(afterUpdate.get("ENCRYPTED_PASSWORD")), REGION), StandardCharsets.UTF_8));
}
@Test
void connectionPasswordEncryptionFailuresAreGlueEncryptionExceptions() {
enableConnectionPasswordEncryption("arn:aws:kms:us-east-1:000000000000:key/00000000-0000-0000-0000-000000000000");
AwsException unknownKey = assertThrows(AwsException.class,
() -> glueService.createConnection(jdbcConnection("enc-bad-key"), null, REGION));
assertEquals("GlueEncryptionException", unknownKey.getErrorCode());
assertEquals("EntityNotFoundException",
assertThrows(AwsException.class, () -> glueService.getConnection("enc-bad-key", false)).getErrorCode());
enableConnectionPasswordEncryption(null);
assertEquals("GlueEncryptionException",
assertThrows(AwsException.class,
() -> glueService.createConnection(jdbcConnection("enc-no-key"), null, REGION)).getErrorCode());
// A connection without any password property is unaffected by the setting.
ConnectionInput network = jdbcConnection("enc-network");
network.setConnectionType("NETWORK");
network.setConnectionProperties(Map.of());
assertEquals("READY", glueService.createConnection(network, null, REGION));
}
private static List<String> names(GlueService.Page<Table> page) {
return page.items().stream().map(Table::getName).toList();
}