Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#3608] Verify that auto-provisioned device ids match device id pattern #3641

Merged

Conversation

harism
Copy link
Contributor

@harism harism commented Jun 2, 2024

This is initial implementation idea I had and thought it should cover all the paths device is created to prevent creating any deivce with id which does not conform to the device id pattern.

I think I need to look at integration tests also but need to do that bit later.

@harism harism requested a review from kaniyan as a code owner June 2, 2024 08:45
@harism harism force-pushed the auto_provision_device_id_checks branch 11 times, most recently from 9b85c1d to c76d2ad Compare June 2, 2024 17:33
@harism harism force-pushed the auto_provision_device_id_checks branch from c76d2ad to c3ce2da Compare June 13, 2024 03:43
@harism harism requested a review from sophokles73 as a code owner June 13, 2024 03:43
@harism
Copy link
Contributor Author

harism commented Jun 13, 2024

I added integration test for assertRegistration to verify that assertion fails with invalid device id. But do you consider this is enough for integration tests and rely on it that all adapters do call assertRegistration and this will cover all adapter uses where using via gateway it has been possible to have invalid device id?

If not I will continue writing per adapter integration tests in a similar fashion too.

@@ -276,6 +284,15 @@ public final Future<OperationResult<Id>> createDevice(
Objects.requireNonNull(span);

final String deviceIdValue = deviceId.orElseGet(() -> generateDeviceId(tenantId));
final Matcher matcher = Objects.requireNonNull(serviceConfig.getDeviceIdPattern()).matcher(deviceIdValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getDeviceIdPattern() will never return null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return Future.failedFuture(new ClientErrorException(
tenantId,
HttpURLConnection.HTTP_BAD_REQUEST,
"invalid device id \"" + deviceIdValue + "\""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not use string concatenation like that. Can you replace this with e.g.

"invalid device ID: %s".formatted(deviceIdValue);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment on lines 73 to 75
final ServiceConfigProperties serviceConfig = mock(ServiceConfigProperties.class);
when(serviceConfig.getDeviceIdPattern())
.thenReturn(Pattern.compile(RegistryManagementConstants.DEFAULT_REGEX_DEVICE_ID));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to mock ServiceConfigProperties. Simply instantiate it and set the pattern as required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment on lines 59 to 61
@Inject
MongoDbBasedHttpServiceConfigOptions mongoDbBasedHttpServiceConfigOptions;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that these options are needed in multiple places, I'd rather have a corresponding producer in org.eclipse.hono.deviceregistry.mongodb.app.ConfigPropertiesProducer and adapt HttpServerFactory accordingly. You can then add a corresponding parameter to this class' deviceManagementService() method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you can do the same for the JDBC registry correspondingly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I get back to this a bit later on better time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, hope I understood it correctly what you were requesting to change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You did :-)

@harism harism force-pushed the auto_provision_device_id_checks branch 6 times, most recently from 018ffd6 to a6bfae7 Compare June 23, 2024 07:51
Copy link
Contributor

@sophokles73 sophokles73 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

thanks for fixing 👍

@sophokles73 sophokles73 merged commit 778a8e3 into eclipse-hono:master Jun 24, 2024
7 checks passed
@harism harism deleted the auto_provision_device_id_checks branch August 16, 2024 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Device auto-provision and auto-create may use invalid device id
2 participants