From df1d2c9ab58e217dd802ad1a459b72ede0d99cf6 Mon Sep 17 00:00:00 2001 From: Kristijan Mitrovic Date: Tue, 8 Sep 2020 01:40:40 +0200 Subject: [PATCH] Fix AccountPolicy sync --- .../NotificationHandler_sync_account.java | 48 ++++++++----------- .../account/StandardSyncAccountService.java | 5 +- .../account/SyncAccountNotification.java | 22 ++------- 3 files changed, 27 insertions(+), 48 deletions(-) diff --git a/bubble-server/src/main/java/bubble/notify/NotificationHandler_sync_account.java b/bubble-server/src/main/java/bubble/notify/NotificationHandler_sync_account.java index b48048d1..4b352f5a 100644 --- a/bubble-server/src/main/java/bubble/notify/NotificationHandler_sync_account.java +++ b/bubble-server/src/main/java/bubble/notify/NotificationHandler_sync_account.java @@ -7,15 +7,13 @@ package bubble.notify; import bubble.dao.account.AccountDAO; import bubble.dao.account.AccountPolicyDAO; import bubble.dao.cloud.BubbleNodeDAO; -import bubble.model.account.Account; -import bubble.model.account.AccountPolicy; import bubble.model.cloud.AnsibleInstallType; import bubble.model.cloud.notify.ReceivedNotification; import bubble.service.account.SyncAccountNotification; -import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import static org.cobbzilla.util.daemon.ZillaRuntime.empty; import static org.cobbzilla.util.json.JsonUtil.json; import static org.cobbzilla.wizard.server.RestServerBase.reportError; @@ -46,32 +44,26 @@ public class NotificationHandler_sync_account extends ReceivedNotificationHandle return; } - notification.getHashedPassword().ifPresent(hashedPassword -> syncAccountPassword(localAccount, hashedPassword)); - notification.getAccountPolicy().ifPresent(policy -> syncAccountPolicy(localAccount.getUuid(), policy)); - } - - private void syncAccountPassword(@NonNull final Account localAccount, - @NonNull final String incomingHashedPassword) { - localAccount.getHashedPassword().setHashedPassword(incomingHashedPassword); - - // if we are a node, set skipSync so we don't get caught in an infinite loop - // (the node would notify the sage, which would notify the node, ad infinitum) - localAccount.setSkipSync(configuration.getThisNetwork().getInstallType() == AnsibleInstallType.node); - - // update password, if we are a sage, this will notify all networks of password change - accountDAO.update(localAccount); - } - - private void syncAccountPolicy(@NonNull final String accountUuid, @NonNull final AccountPolicy incomingPolicy) { - final var localPolicy = accountPolicyDAO.findSingleByAccount(accountUuid); - if (localPolicy == null) { - reportError("sync_account: local AccountPolicy not found for account: " + accountUuid); - return; + final var incomingHashedPassword = notification.getUpdatedHashedPassword(); + if (!empty(incomingHashedPassword)) { + localAccount.getHashedPassword().setHashedPassword(incomingHashedPassword); + // if we are a node, set skipSync so we don't get caught in an infinite loop + // (the node would notify the sage, which would notify the node, ad infinitum) + localAccount.setSkipSync(configuration.getThisNetwork().getInstallType() == AnsibleInstallType.node); + // update password, if we are a sage, this will notify all networks of password change + accountDAO.update(localAccount); } - localPolicy.update(incomingPolicy); - localPolicy.setSkipSync(configuration.getThisNetwork().getInstallType() == AnsibleInstallType.node); - accountPolicyDAO.update(localPolicy); + final var incomingPolicy = notification.getUpdatedPolicy(); + if (!empty(incomingPolicy)) { + final var localPolicy = accountPolicyDAO.findSingleByAccount(localAccount.getUuid()); + if (localPolicy == null) { + reportError("sync_account: local AccountPolicy not found for account: " + localAccount.getUuid()); + return; + } + localPolicy.update(incomingPolicy); + localPolicy.setSkipSync(configuration.getThisNetwork().getInstallType() == AnsibleInstallType.node); + accountPolicyDAO.update(localPolicy); + } } - } diff --git a/bubble-server/src/main/java/bubble/service/account/StandardSyncAccountService.java b/bubble-server/src/main/java/bubble/service/account/StandardSyncAccountService.java index bd845e99..5af6369c 100644 --- a/bubble-server/src/main/java/bubble/service/account/StandardSyncAccountService.java +++ b/bubble-server/src/main/java/bubble/service/account/StandardSyncAccountService.java @@ -31,11 +31,12 @@ public class StandardSyncAccountService implements SyncAccountService { @Autowired private BubbleConfiguration configuration; public void syncAccount(@NonNull final Account account) { - sync(account, new SyncAccountNotification(account)); + sync(account, new SyncAccountNotification(account.getUuid(), account.getHashedPassword().getHashedPassword(), + null)); } public void syncPolicy(@NonNull final Account account, @NonNull final AccountPolicy policy) { - sync(account, new SyncAccountNotification(account.getUuid(), policy)); + sync(account, new SyncAccountNotification(account.getUuid(), null, policy)); } private void sync(@NonNull final Account account, @NonNull final SyncAccountNotification notification) { diff --git a/bubble-server/src/main/java/bubble/service/account/SyncAccountNotification.java b/bubble-server/src/main/java/bubble/service/account/SyncAccountNotification.java index 22760909..b4fa9270 100644 --- a/bubble-server/src/main/java/bubble/service/account/SyncAccountNotification.java +++ b/bubble-server/src/main/java/bubble/service/account/SyncAccountNotification.java @@ -4,30 +4,16 @@ */ package bubble.service.account; -import bubble.model.account.Account; import bubble.model.account.AccountPolicy; +import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; -import lombok.NonNull; import lombok.Setter; import lombok.experimental.Accessors; -import java.util.Optional; - -@NoArgsConstructor @Accessors(chain=true) +@AllArgsConstructor @NoArgsConstructor @Accessors(chain=true) public class SyncAccountNotification { - @Getter @Setter private String accountUuid; - @Getter @Setter private Optional hashedPassword = Optional.empty(); - @Getter @Setter private Optional accountPolicy = Optional.empty(); - - public SyncAccountNotification(@NonNull final Account account) { - this.accountUuid = account.getUuid(); - this.hashedPassword = Optional.of(account.getHashedPassword().getHashedPassword()); - } - - public SyncAccountNotification(@NonNull final String accountUuid, @NonNull final AccountPolicy policy) { - this.accountUuid = accountUuid; - this.accountPolicy = Optional.of(policy); - } + @Getter @Setter private String updatedHashedPassword; + @Getter @Setter private AccountPolicy updatedPolicy; }