Skip to content

Commit b9cb17a

Browse files
jamiebrindJamie Brind
andauthored
Preserve device credentials in registrar on settings validation errors (#1496)
* Preserve device credentials on registrar settings errors and stop writing key_bytes to auth_key Previously, when LocalDevice.validateMetadata() threw a ValidationError (e.g. from validatePointTypes), LocalDevice.initialize() exited before configure() and Registrar.initializeDevices() returned early, skipping loadCredentials(). Registrar then pushed UPDATE with empty credentials alongside metadata.key_bytes (the base64 PKCS#8 private key), and ZanzaraIotAccessProvider.toDeviceMap() wrote metadata.key_bytes into auth_key when credentials were empty—corrupting broker public keys. - Call configure() before validateMetadata() in LocalDevice.initialize() - Remove early return in Registrar.initializeDevices() so loadCredentials() still runs after settings validation errors - Remove metadata.key_bytes fallback into auth_key in ZanzaraIotAccessProvider.toDeviceMap() - Add unit tests in RegistrarTest and ZanzaraIotAccessProviderTest * Extract isLocalKind() helper in LocalDevice --------- Co-authored-by: Jamie Brind <brind@google.com>
1 parent 6e75fc6 commit b9cb17a

5 files changed

Lines changed: 66 additions & 5 deletions

File tree

‎udmis/src/main/java/com/google/bos/udmi/service/access/ZanzaraIotAccessProvider.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,6 @@ private Map<String, String> toDeviceMap(CloudModel cloudModel, String createdAt)
523523
requireNull(cloudModel.metadata_str, "unexpected metadata_str content");
524524
properties.put(METADATA_STR_KEY, stringifyTerse(cloudModel.metadata));
525525
properties.put(GUID_PROPERTY, extractDeviceGuid(cloudModel.metadata));
526-
ifNotNullThen(ifNotNullGet(cloudModel.metadata, metadata -> metadata.get("key_bytes")),
527-
keyBytes -> properties.put(AUTH_KEY_PROPERTY, keyBytes));
528526
properties.put(BLOCKED_PROPERTY, booleanString(cloudModel.blocked));
529527
ifNotNullThen(cloudModel.num_id, id -> properties.put(NUM_ID_PROPERTY, id));
530528
ifNotNullThen(cloudModel.auth_type,

‎udmis/src/test/java/com/google/bos/udmi/service/access/ZanzaraIotAccessProviderTest.java‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.jupiter.api.Assertions.assertEquals;
55
import static org.junit.jupiter.api.Assertions.assertFalse;
66
import static org.junit.jupiter.api.Assertions.assertNotNull;
7+
import static org.junit.jupiter.api.Assertions.assertNull;
78
import static org.junit.jupiter.api.Assertions.assertThrows;
89
import static org.junit.jupiter.api.Assertions.assertTrue;
910
import static org.mockito.ArgumentMatchers.any;
@@ -408,7 +409,32 @@ void testDeviceGuidPersistedOnCreateUpdateAndModify() {
408409
clearGuidModify.operation = ModelOperation.MODIFY;
409410
clearGuidModify.metadata = Map.of("udmi_metadata", "{\"system\":{}}");
410411
provider.modelDevice(TEST_REGISTRY, TEST_DEVICE, clearGuidModify, null);
411-
org.junit.jupiter.api.Assertions.assertNull(store.get("r/test-reg/d/test-dev:guid"));
412+
assertNull(store.get("r/test-reg/d/test-dev:guid"));
413+
}
414+
415+
@Test
416+
void testMetadataKeyBytesDoesNotOverwriteAuthKey() {
417+
String existingPublicKey = "-----BEGIN PUBLIC KEY-----\nTEST_PUB_KEY\n-----END PUBLIC KEY-----";
418+
store.put("r/test-reg/d/test-dev:auth_key", existingPublicKey);
419+
store.put("r/test-reg/d/test-dev:auth_type", "RS256");
420+
421+
CloudModel updateModel = new CloudModel();
422+
updateModel.operation = ModelOperation.UPDATE;
423+
updateModel.metadata = Map.of("key_bytes", "BASE64_PRIVATE_KEY_PKCS8_BYTES");
424+
updateModel.credentials = List.of();
425+
426+
provider.modelDevice(TEST_REGISTRY, TEST_DEVICE, updateModel, null);
427+
428+
assertEquals(existingPublicKey, store.get("r/test-reg/d/test-dev:auth_key"));
429+
430+
CloudModel createModel = new CloudModel();
431+
createModel.operation = ModelOperation.CREATE;
432+
createModel.metadata = Map.of("key_bytes", "BASE64_PRIVATE_KEY_PKCS8_BYTES");
433+
createModel.credentials = List.of();
434+
435+
provider.modelDevice(TEST_REGISTRY, "other-dev", createModel, null);
436+
437+
assertNull(store.get("r/test-reg/d/other-dev:auth_key"));
412438
}
413439

414440
@Test

‎validator/src/main/java/com/google/daq/mqtt/registrar/LocalDevice.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ class LocalDevice implements SiteDevice {
236236
public void initialize() {
237237
prepareOutDir();
238238
populatePointUrls();
239-
ifTrueThen(deviceKind == DeviceKind.LOCAL && metadata != null, this::validateMetadata);
240239
configure();
240+
ifTrueThen(isLocalKind() && metadata != null, this::validateMetadata);
241241
}
242242

243243
private void populatePointUrls() {
@@ -676,6 +676,10 @@ private boolean isExtraKind() {
676676
return deviceKind == DeviceKind.EXTRA;
677677
}
678678

679+
private boolean isLocalKind() {
680+
return deviceKind == DeviceKind.LOCAL;
681+
}
682+
679683
String getGatewayId() {
680684
GatewayModel gatewayModel = isExtraKind() ? cloudModel.gateway : metadata.gateway;
681685
return ifNotNullGet(gatewayModel, model -> model.gateway_id);

‎validator/src/main/java/com/google/daq/mqtt/registrar/Registrar.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,6 @@ private void initializeDevices(Map<String, LocalDevice> localDevices) {
16441644
localDevice.initialize();
16451645
} catch (Exception e) {
16461646
localDevice.captureError(ExceptionCategory.settings, e);
1647-
return;
16481647
}
16491648
try {
16501649
localDevice.loadCredentials();

‎validator/src/test/java/com/google/daq/mqtt/registrar/RegistrarTest.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.jetbrains.annotations.NotNull;
4444
import org.junit.Test;
4545
import udmi.schema.CloudModel;
46+
import udmi.schema.Credential.Key_format;
4647
import udmi.schema.ExecutionConfiguration;
4748
import udmi.schema.FamilyLocalnetModel;
4849
import udmi.schema.LocalnetModel;
@@ -524,6 +525,39 @@ public void localnetWithoutFamiliesTest() {
524525
}
525526
}
526527

528+
@Test
529+
public void credentialsPreservedOnSettingsErrorTest() {
530+
Registrar registrar = getRegistrar(ImmutableList.of());
531+
registrar.execute(() -> {
532+
Map<String, LocalDevice> localDevices = registrar.getWorkingDevices();
533+
Metadata metadata = localDevices.get(GATEWAY_ID).getMetadata();
534+
PointPointsetModel blankUnitsPoint = new PointPointsetModel();
535+
blankUnitsPoint.units = "";
536+
metadata.pointset.points.put("bad_blank_units_point", blankUnitsPoint);
537+
});
538+
539+
LocalDevice gateway = registrar.getWorkingDevices().get(GATEWAY_ID);
540+
assertTrue("Gateway should record settings error for blank units",
541+
gateway.hasCategory(ExceptionCategory.settings));
542+
assertEquals("Gateway settings should still load public key credentials",
543+
1, gateway.getSettings().credentials.size());
544+
assertEquals("Gateway credential format should be ES256",
545+
Key_format.ES_256, gateway.getSettings().credentials.get(0).key_format);
546+
547+
List<CloudModel> pushedModels = registrar.getMockActions().stream()
548+
.filter(action -> GATEWAY_ID.equals(action.deviceId)
549+
&& (action.action == UPDATE_DEVICE_ACTION || action.action == CREATE_DEVICE_ACTION))
550+
.map(action -> (CloudModel) action.data)
551+
.toList();
552+
assertEquals("Gateway should be pushed to IoT provider once", 1, pushedModels.size());
553+
assertNotNull("Pushed CloudModel credentials must not be null",
554+
pushedModels.get(0).credentials);
555+
assertEquals("Pushed CloudModel must retain public key credentials",
556+
1, pushedModels.get(0).credentials.size());
557+
assertEquals("Pushed CloudModel credential format should be ES256",
558+
Key_format.ES_256, pushedModels.get(0).credentials.get(0).key_format);
559+
}
560+
527561
private Boolean isNotBlocking(MockAction action) {
528562
return !TRUE.equals(((CloudModel) action.data).blocked);
529563
}

0 commit comments

Comments
 (0)