aws-greengrass / aws-greengrass-client-device-auth

Apache License 2.0
2 stars 3 forks source link

fix(uat): add connection state check to mosquitto client #383

Closed bgklika closed 1 year ago

bgklika commented 1 year ago

Issue #, if available: MQTT Client Control does not send shutdown agent request

Description of changes:

Why is this change necessary: Currently mosquitto client throw an exception when closeMqttConnection gRPC request is called on connection already closed from broker's side. Also behavior of gRPC requests for publish,subscribe,unsubscribe and shutdown when connection is closed by broker depends on mosquitto library which differ from other client. We need unify behavior of clients to simplify control and MQTT steps.

How was this change tested: Manually by run Control as application and local mosquitto broker. When client is connected terminate broker before first subscribe. Observe results in client and control logs:

Test results: Control:

[INFO ] 2023-07-28 23:57:45.102 [grpc-default-executor-0] GRPCDiscoveryServer - RegisterAgent: agentId mosquitto-agent
[INFO ] 2023-07-28 23:57:45.108 [grpc-default-executor-0] GRPCDiscoveryServer - DiscoveryClient: agentId mosquitto-agent address 127.0.0.1 port 44355
[INFO ] 2023-07-28 23:57:45.109 [grpc-default-executor-0] EngineControlImpl - Created new agent control for mosquitto-agent on 127.0.0.1:44355
[INFO ] 2023-07-28 23:57:45.109 [grpc-default-executor-0] ExampleControl - Agent mosquitto-agent is connected
[INFO ] 2023-07-28 23:57:45.110 [pool-2-thread-1] AgentTestScenario - Playing test scenario for agent id mosquitto-agent
[INFO ] 2023-07-28 23:57:48.111 [pool-2-thread-1] AgentTestScenario - Set CONNECT user property: region, US
[INFO ] 2023-07-28 23:57:48.111 [pool-2-thread-1] AgentTestScenario - Set CONNECT user property: type, JSON
[INFO ] 2023-07-28 23:57:48.112 [pool-2-thread-1] AgentTestScenario - Set CONNECT request response information true
[INFO ] 2023-07-28 23:57:48.197 [pool-2-thread-1] AgentControlImpl - Created connection with id 1 CONNACK 'sessionPresent: false
reasonCode: 0
receiveMaximum: 20
topicAliasMaximum: 10
'
[INFO ] 2023-07-28 23:57:48.197 [pool-2-thread-1] AgentControlImpl - createMqttConnection: MQTT connectionId 1 created
[INFO ] 2023-07-28 23:57:48.198 [pool-2-thread-1] AgentTestScenario - MQTT connection with id 1 is established
[INFO ] 2023-07-28 23:57:49.057 [grpc-default-executor-0] GRPCDiscoveryServer - OnMqttDisconnect: agentId mosquitto-agent connectionId 1 disconnect 'reasonCode: 7
' error ''
[INFO ] 2023-07-28 23:57:49.058 [grpc-default-executor-0] AgentTestScenario - MQTT disconnected on agentId mosquitto-agent connectionId 1 disconnect 'reasonCode: 7
' error ''
[INFO ] 2023-07-28 23:57:53.199 [pool-2-thread-1] AgentTestScenario - Set SUBSCRIBE user property: region, US
[INFO ] 2023-07-28 23:57:53.199 [pool-2-thread-1] AgentTestScenario - Set SUBSCRIBE user property: type, JSON
[INFO ] 2023-07-28 23:57:53.200 [pool-2-thread-1] AgentControlImpl - SubscribeMqtt: subscribe on connection 1
[ERROR] 2023-07-28 23:57:53.210 [pool-2-thread-1] AgentTestScenario - gRPC error code INTERNAL: description: MQTT client is not connected
io.grpc.StatusRuntimeException: INTERNAL: MQTT client is not connected
        at io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:271) ~[grpc-stub-1.53.0.jar:1.53.0]
        at io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:252) ~[grpc-stub-1.53.0.jar:1.53.0]
        at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:165) ~[grpc-stub-1.53.0.jar:1.53.0]
        at com.aws.greengrass.testing.mqtt.client.MqttClientControlGrpc$MqttClientControlBlockingStub.subscribeMqtt(MqttClientControlGrpc.java:511) ~[classes/:?]
        at com.aws.greengrass.testing.mqtt.client.control.implementation.AgentControlImpl.subscribeMqtt(AgentControlImpl.java:224) ~[classes/:?]
        at com.aws.greengrass.testing.mqtt.client.control.implementation.ConnectionControlImpl.subscribeMqtt(ConnectionControlImpl.java:123) ~[classes/:?]
        at com.aws.greengrass.testing.mqtt.client.control.AgentTestScenario.testSubscribe(AgentTestScenario.java:255) ~[classes/:?]
        at com.aws.greengrass.testing.mqtt.client.control.AgentTestScenario.run(AgentTestScenario.java:183) [classes/:?]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
        at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
        at java.lang.Thread.run(Thread.java:834) [?:?]
[INFO ] 2023-07-28 23:57:53.217 [pool-2-thread-1] AgentTestScenario - Set DISCONNECT user property: region, US
[INFO ] 2023-07-28 23:57:53.217 [pool-2-thread-1] AgentTestScenario - Set DISCONNECT user property: type, JSON
[INFO ] 2023-07-28 23:57:53.220 [pool-2-thread-1] AgentControlImpl - closeMqttConnection: MQTT connectionId 1 closed
[INFO ] 2023-07-28 23:57:53.220 [pool-2-thread-1] AgentControlImpl - sending shutdown request
[INFO ] 2023-07-28 23:57:53.222 [pool-2-thread-1] AgentControlImpl - shutdown request sent successfully
[INFO ] 2023-07-28 23:57:53.225 [grpc-default-executor-0] GRPCDiscoveryServer - UnregisterAgent: agentId mosquitto-agent reason Agent shutdown by OTF request 'That's it.'
[INFO ] 2023-07-28 23:57:53.225 [grpc-default-executor-0] AgentControlImpl - shutting down channel with agent id mosquitto-agent
[INFO ] 2023-07-28 23:57:53.226 [grpc-default-executor-0] AgentControlImpl - channel terminated with agent id mosquitto-agent
[INFO ] 2023-07-28 23:57:53.226 [grpc-default-executor-0] ExampleControl - Agent mosquitto-agent is disconnected

Mosquitto client:

$ build/src/mosquitto-test-client mosquitto-agent
[DEBUG]: Initialize gRPC library
[DEBUG]: Making gPRC link with 127.0.0.1:47619 as agent_id 'mosquitto-agent'
[DEBUG]: Sending RegisterAgent request with agent_id mosquitto-agent
[DEBUG]: Local address is 127.0.0.1
[DEBUG]: GRPCControlServer created and listed on 127.0.0.1:44355
[DEBUG]: Sending DiscoveryAgent request agent_id 'mosquitto-agent' host:port 127.0.0.1:44355
[DEBUG]: gPRC link established with 127.0.0.1:47619 as agent_id 'mosquitto-agent'
[DEBUG]: Initialize Mosquitto MQTT library
[DEBUG]: Mosquitto library version 2.0.15
[DEBUG]: Handle gRPC requests
[DEBUG]: CreateMqttConnection client_id 'client' broker server:8883
[DEBUG]: Creating Mosquitto MQTT v5 connection for server:8883
[DEBUG]: Copied TX user property region:US
[DEBUG]: Copied TX user property type:JSON
[DEBUG]: Establishing Mosquitto MQTT v5 connection to server:8883 in 10 seconds
[DEBUG]: Copied TX request response information 1
[DEBUG]: Use provided TLS credentials
[DEBUG]: mosquitto: Client client sending CONNECT
[DEBUG]: mosquitto: Client client received CONNACK (0)
[DEBUG]: onConnect rc 0 flags 0 props 0x7f37f400bf10
[DEBUG]: Copied RX topic alias maximum 10
[DEBUG]: Copied RX receive maximum 20
[DEBUG]: Establishing MQTT connection to server:8883 completed with reason code 0
[DEBUG]: Connection registered with id 1
[DEBUG]: onDisconnect rc 7 props (nil)
[DEBUG]: Sending OnMqttDisconnect request agent_id 'mosquitto-agent' connection_id 1 error '(null)'
[DEBUG]: Subscription: filter test/topic QoS 0 noLocal 0 retainAsPublished 0 retainHandling 2
[DEBUG]: SubscribeMqtt connection_id 1
[ERROR]: SubscribeMqtt: exception during subscribing
[DEBUG]: CloseMqttConnection connection_id 1 reason 4
[DEBUG]: Destroy Mosquitto MQTT connection
[DEBUG]: ShutdownAgent with reason 'That's it.'
[DEBUG]: Shutdown gPRC link
[DEBUG]: Sending UnregisterAgent request agent_id 'mosquitto-agent' reason 'Agent shutdown by OTF request 'That's it.''
[DEBUG]: Shutdown MQTT library
[DEBUG]: Shutdown gRPC library
[DEBUG]: Execution done

Any additional information or context required to review the change:

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

github-actions[bot] commented 1 year ago

Unit Tests Coverage Report

File Coverage Lines Branches
All files 72% 79% 65% :white_check_mark:
com.aws.greengrass.clientdevices.auth.PermissionEvaluationUtils 78% 82% 74% :white_check_mark:
com.aws.greengrass.clientdevices.auth.PermissionEvaluationUtils$Operation 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.PermissionEvaluationUtils$Resource 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.CertificateManager 79% 90% 69% :white_check_mark:
com.aws.greengrass.clientdevices.auth.ClientDevicesAuthService 78% 90% 67% :white_check_mark:
com.aws.greengrass.clientdevices.auth.DeviceAuthClient 73% 83% 64% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.ClientCertificateGenerator 95% 90% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.CertificateHelper 74% 93% 54% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.CertificateHelper$ProviderType 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.CertificateStore 72% 85% 60% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.CertificateExpiryMonitor 77% 87% 67% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.ServerCertificateGenerator 93% 87% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.CertificateGenerator 70% 90% 50% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.CertificateStore$CAType 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.CertificateExpiryMonitor$CertRotationDecider 90% 100% 80% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.CertificatesConfig 100% 100% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.session.events.SessionCreationEvent$SessionCreationStatus 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.iot.usecases.VerifyIotCertificate 94% 88% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.iot.usecases.VerifyThingAttachedToCertificate 92% 96% 88% :white_check_mark:
com.aws.greengrass.clientdevices.auth.iot.usecases.CreateIoTThingSession 87% 90% 83% :white_check_mark:
com.aws.greengrass.clientdevices.auth.iot.usecases.VerifyCertificateValidityPeriod 88% 88% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.infra.ClientCertificateStore 100% 100% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.infra.BackgroundCertificateRefresh 83% 85% 82% :white_check_mark:
com.aws.greengrass.clientdevices.auth.iot.infra.ThingRegistry 92% 97% 88% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.usecases.ConfigureManagedCertificateAuthority 85% 85% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.usecases.ConfigureCustomCertificateAuthority 83% 83% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.usecases.RegisterCertificateAuthorityUseCase 65% 81% 50% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.MetricsConfiguration 83% 100% 67% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.AuthorizationPolicyStatement$Effect 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.GroupManager 89% 94% 83% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.ConfigurationFormatVersion 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.CAConfiguration 96% 100% 92% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.RuntimeConfiguration 84% 99% 70% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.SecurityConfiguration 80% 93% 67% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.CDAConfiguration 100% 100% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.GroupDefinition 75% 100% 50% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.ExpressionVisitor 84% 94% 75% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.GroupConfiguration 90% 95% 86% :white_check_mark:
com.aws.greengrass.clientdevices.auth.api.ServiceErrorEvent 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.api.ClientDevicesAuthServiceApi 90% 79% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.api.DomainEvents 100% 100% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.api.AuthorizeClientDeviceActionEvent$AuthorizationStatus 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.api.UseCases 71% 92% 50% :white_check_mark:
com.aws.greengrass.clientdevices.auth.api.DomainEvent 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.api.GetCertificateRequestOptions$CertificateType 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.session.attribute.StringLiteralAttribute 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.session.attribute.WildcardSuffixAttribute 88% 100% 75% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.events.CertificateSubscriptionEvent$SubscriptionStatus 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.events.CACertificateChainChanged 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.iot.Certificate$Status 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.iot.IotAuthClient$Default 56% 47% 64% :white_check_mark:
com.aws.greengrass.clientdevices.auth.iot.Thing 82% 86% 79% :white_check_mark:
com.aws.greengrass.clientdevices.auth.iot.Certificate 78% 89% 67% :white_check_mark:
com.aws.greengrass.clientdevices.auth.iot.GreengrassV2DataClientFactory 18% 18% 0% :x:
com.aws.greengrass.clientdevices.auth.iot.CertificateRegistry 95% 90% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.iot.Component 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.iot.events.VerifyClientDeviceIdentityEvent$VerificationStatus 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.infra.NetworkStateProvider$Default$1 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.infra.NetworkStateProvider$ConnectionState 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.infra.NetworkStateProvider$Default 75% 90% 60% :white_check_mark:
com.aws.greengrass.ipc.IPCUtils 83% 67% 100% :white_check_mark:
com.aws.greengrass.ipc.VerifyClientDeviceIdentityOperationHandler 60% 69% 50% :white_check_mark:
com.aws.greengrass.ipc.GetClientDeviceAuthTokenOperationHandler 86% 98% 75% :white_check_mark:
com.aws.greengrass.ipc.AuthorizeClientDeviceActionOperationHandler 79% 92% 67% :white_check_mark:
com.aws.greengrass.ipc.SubscribeToCertificateUpdatesOperationHandler 81% 88% 75% :white_check_mark:
com.aws.greengrass.clientdevices.auth.session.SessionConfig 92% 100% 83% :white_check_mark:
com.aws.greengrass.clientdevices.auth.session.SessionManager$1 100% 100% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.session.MqttSessionFactory 100% 100% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.session.SessionCreator 100% 100% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.session.SessionManager 88% 100% 75% :white_check_mark:
com.aws.greengrass.clientdevices.auth.session.SessionImpl 100% 100% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.session.SessionCreator$SessionFactorySingleton 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.session.MqttSessionFactory$MqttCredential 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.handlers.CACertificateChainChangedHandler 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.handlers.CAConfigurationChangedHandler 93% 87% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.handlers.CertificateRotationHandler 96% 91% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.certificate.handlers.SecurityConfigurationChangedHandler 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.metrics.handlers.SessionCreationEventHandler 88% 100% 75% :white_check_mark:
com.aws.greengrass.clientdevices.auth.metrics.handlers.MetricsConfigurationChangedHandler 70% 90% 50% :white_check_mark:
com.aws.greengrass.clientdevices.auth.metrics.handlers.AuthorizeClientDeviceActionsMetricHandler 88% 100% 75% :white_check_mark:
com.aws.greengrass.clientdevices.auth.metrics.handlers.VerifyClientDeviceIdentityEventHandler 88% 100% 75% :white_check_mark:
com.aws.greengrass.clientdevices.auth.metrics.handlers.CertificateSubscriptionEventHandler 83% 100% 67% :white_check_mark:
com.aws.greengrass.clientdevices.auth.metrics.handlers.ServiceErrorEventHandler 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.iot.dto.CertificateV1DTO$Status 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.connectivity.usecases.GetConnectivityInformationUseCase 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.connectivity.usecases.RecordConnectivityChangesUseCase 100% 100% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.util.ResizableLinkedBlockingQueue 90% 80% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.util.ParseIPAddress 90% 95% 84% :white_check_mark:
com.aws.greengrass.clientdevices.auth.metrics.ClientDeviceAuthMetrics 87% 98% 75% :white_check_mark:
com.aws.greengrass.clientdevices.auth.metrics.MetricsEmitter 100% 100% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.connectivity.CISShadowMonitor 63% 77% 50% :white_check_mark:
com.aws.greengrass.clientdevices.auth.connectivity.RecordConnectivityChangesResponse 100% 100% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.connectivity.HostAddress 67% 67% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.connectivity.RecordConnectivityChangesRequest 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.connectivity.ConnectivityInformation 100% 100% 100% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.parser.RuleExpressionConstants 100% 100% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.parser.TokenMgrError 22% 32% 12% :x:
com.aws.greengrass.clientdevices.auth.configuration.parser.RuleExpressionTokenManager 61% 65% 58% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.parser.ASTStart 33% 33% 0% :x:
com.aws.greengrass.clientdevices.auth.configuration.parser.ASTAnd 67% 67% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.parser.Token 58% 58% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.parser.RuleExpressionDefaultVisitor 0% 0% 0% :x:
com.aws.greengrass.clientdevices.auth.configuration.parser.ASTOr 67% 67% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.parser.SimpleCharStream 28% 31% 25% :x:
com.aws.greengrass.clientdevices.auth.configuration.parser.RuleExpressionTreeConstants 0% 0% 0% :x:
com.aws.greengrass.clientdevices.auth.configuration.parser.JJTRuleExpressionState 67% 65% 70% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.parser.ASTThing 67% 67% 0% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.parser.RuleExpression 63% 63% 62% :white_check_mark:
com.aws.greengrass.clientdevices.auth.configuration.parser.SimpleNode 27% 35% 19% :x:

Minimum allowed coverage is 50%

Generated by :monkey: cobertura-action against 37364869a14eea2c169e015a1f8d55f8f9de5d3b