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

Renew Certificate After Expiration #57

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions AstarteDeviceSDKCSharp/AstartePairingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ public AstartePairingHandler(string pairingUrl, string astarteRealm, string devi

}

public void Init()
public async Task Init()
{
ReloadTransports();
await ReloadTransports();
}

private void ReloadTransports()
private async Task ReloadTransports()
{
_transports = _AstartePairingService.ReloadTransports(_credentialSecret,
_cryptoStore, _deviceId).Result;
_transports = await _AstartePairingService.ReloadTransports(_credentialSecret,
_cryptoStore, _deviceId);
}

public List<AstarteTransport> GetTransports()
Expand Down
6 changes: 2 additions & 4 deletions AstarteDeviceSDKCSharp/Crypto/AstarteCryptoStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,8 @@ public string GenerateCSR(string commonName)

public MqttClientOptionsBuilderTlsParameters GetMqttClientOptionsBuilderTlsParameters()
{
if (_parametersFactory == null)
{
_parametersFactory = new AstarteMutualTLSParametersFactory(this);
}
_parametersFactory = new AstarteMutualTLSParametersFactory(this);

return _parametersFactory.Get();
}

Expand Down
14 changes: 10 additions & 4 deletions AstarteDeviceSDKCSharp/Device/AstarteDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class AstarteDevice : IAstarteTransportEventListener
private IAstarteMessageListener? _astarteMessagelistener;
private IAstartePropertyStorage astartePropertyStorage;
private AstarteFailedMessageStorage _astarteFailedMessageStorage;
private bool _initialized;
private bool _initialized = false;
private const string _cryptoSubDir = "crypto";
private bool _alwaysReconnect = false;
private bool _explicitDisconnectionRequest;
Expand Down Expand Up @@ -117,9 +117,9 @@ public AstarteDevice(
_astarteFailedMessageStorage = new(fullCryptoDirPath);
}

private void Init()
private async Task Init()
{
_pairingHandler.Init();
await _pairingHandler.Init();

// Get and configure the first available transport
SetFirstTransportFromPairingHandler();
Expand Down Expand Up @@ -221,10 +221,16 @@ public void SetAlwaysReconnect(bool alwaysReconnect)
public async Task Connect()
{

if (!_pairingHandler.IsCertificateAvailable())
{
await _pairingHandler.RequestNewCertificate();
_initialized = false;
}

harlem88 marked this conversation as resolved.
Show resolved Hide resolved
if (!_initialized)
{

Init();
await Init();
_initialized = true;
}

Expand Down
18 changes: 14 additions & 4 deletions AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttV1Transport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,23 @@ private async Task DoSendMqttMessage(string topic, byte[] payload, int qos)
.WithRetainFlag(false)
.Build();

MqttClientPublishResult result = await _client.PublishAsync(applicationMessage);
try
{

MqttClientPublishResult result = await _client.PublishAsync(applicationMessage);

if (result.ReasonCode != MqttClientPublishReasonCode.Success)
{
throw new AstarteTransportException
($"Error publishing on MQTT. Code: {result.ReasonCode}");
}

if (result.ReasonCode != MqttClientPublishReasonCode.Success)
}
catch (Exception)
{
throw new AstarteTransportException
($"Error publishing on MQTT. Code: {result.ReasonCode}");
_astarteTransportEventListener?.OnTransportDisconnected();
harlem88 marked this conversation as resolved.
Show resolved Hide resolved
}

}

public override async Task SendIntrospection()
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add option to ignore SSL errors in the AstarteDevice constructor.
- Add a method to remove interface from device.

### Fixed
- Fix Connect method to handle certificate expiration.

## [0.5.4] - 2023-06-23
### Added
- Expose an interface path from Astarte aggregate datastream event.
Expand Down
Loading