Skip to content
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
21 changes: 17 additions & 4 deletions cloud_accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,22 @@
exit;
}

$cloud_saved = $cloud_accounts->save($cloud_parsed->account, $cloud_user);
$cloud_cleanup_failed = false;
$cloud_saved = $cloud_accounts->save($cloud_parsed->account, $cloud_user);
$cloud_raw = trim((string) get_nfilter_request_var('credential'));

if ($cloud_saved > 0 && $cloud_parsed->account->credentialSource !== \Somethingwithproof\Cloud\CredentialSource::Vault) {
// A credential kept in the vault would come back, unprompted, if the
// account were switched back to it with the box left empty.
if ($cloud_credentials->hasVaultCredential($cloud_saved)) {
$cloud_credentials->forget($cloud_saved);
$cloud_accounts->recordCredentialChange($cloud_saved, $cloud_user);
// The delete skips an account a concurrent save has moved
// back to the vault, so only a credential that is gone is
// recorded as removed.
if (!$cloud_credentials->forgetUnlessVault($cloud_saved)) {
$cloud_cleanup_failed = true;
} elseif (!$cloud_credentials->hasVaultCredential($cloud_saved)) {
$cloud_accounts->recordCredentialChange($cloud_saved, $cloud_user);
}
}
} elseif ($cloud_saved > 0 && $cloud_raw !== '') {
try {
Expand All @@ -125,7 +132,13 @@

unset($cloud_raw);

if ($cloud_saved > 0) {
if ($cloud_saved > 0 && $cloud_cleanup_failed) {
raise_message(
'cloud_saved',
__('The account was saved, but the stored credential could not be removed.', 'cloud'),
MESSAGE_LEVEL_ERROR
);
} elseif ($cloud_saved > 0) {
raise_message('cloud_saved', __('The account was saved.', 'cloud'), MESSAGE_LEVEL_INFO);
} else {
raise_message('cloud_saved', __('The account could not be saved.', 'cloud'), MESSAGE_LEVEL_ERROR);
Expand Down
17 changes: 15 additions & 2 deletions lib/CredentialStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,21 @@ public function storeVaultCredential(int $accountId, Secret $secret): void {
}
}

public function forget(int $accountId): void {
$this->secrets->delete($accountId, SecretRepository::CREDENTIAL);
/**
* @return bool whether the delete ran; false means the credential may remain
*/
public function forget(int $accountId): bool {
return $this->secrets->delete($accountId, SecretRepository::CREDENTIAL);
}

/**
* Drops the vault credential of an account that has moved to another
* source. An account that is on the vault again keeps it.
*
* @return bool whether the delete ran; false means the credential may remain
*/
public function forgetUnlessVault(int $accountId): bool {
return $this->secrets->deleteUnlessVault($accountId, SecretRepository::CREDENTIAL);
}

/**
Expand Down
23 changes: 20 additions & 3 deletions lib/SecretRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,29 @@ public function has(int $accountId, string $purpose): bool {
) > 0;
}

public function delete(int $accountId, string $purpose): void {
db_execute_prepared(
public function delete(int $accountId, string $purpose): bool {
return db_execute_prepared(
'DELETE FROM plugin_cloud_secret WHERE account_id = ? AND purpose = ?',
[$accountId, $purpose],
true,
$this->connection
);
) !== false;
}

/**
* Deletes the row only while the account does not keep its credential in
* the vault. The check and the delete are one statement, so a concurrent
* save that switches the account to the vault cannot lose its credential
* to a cleanup that began before the switch.
*/
public function deleteUnlessVault(int $accountId, string $purpose): bool {
return db_execute_prepared(
'DELETE s FROM plugin_cloud_secret s
INNER JOIN plugin_cloud_account a ON a.id = s.account_id
WHERE s.account_id = ? AND s.purpose = ? AND a.credential_source <> ?',
[$accountId, $purpose, CredentialSource::Vault->value],
true,
$this->connection
) !== false;
}
}
Binary file modified locales/LC_MESSAGES/ar-SA.mo
Binary file not shown.
Binary file modified locales/LC_MESSAGES/es-419.mo
Binary file not shown.
Binary file modified locales/LC_MESSAGES/es-ES.mo
Binary file not shown.
Binary file modified locales/LC_MESSAGES/hi-IN.mo
Binary file not shown.
Binary file modified locales/LC_MESSAGES/zh-CN.mo
Binary file not shown.
4 changes: 4 additions & 0 deletions locales/po/ar-SA.po
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ msgstr "الاسم: يستخدم حساب آخر هذا الاسم بالفعل.
msgid "The account was saved, but the credential was not stored: %s"
msgstr "حُفظ الحساب، لكن لم تُخزّن بيانات الاعتماد: %s"

#: cloud_accounts.php
msgid "The account was saved, but the stored credential could not be removed."
msgstr "تم حفظ الحساب، لكن تعذر حذف بيانات الاعتماد المخزنة."

#: cloud_accounts.php
msgid "The account was saved."
msgstr "حُفظ الحساب."
Expand Down
4 changes: 4 additions & 0 deletions locales/po/cacti.pot
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ msgstr ""
msgid "The account was saved, but the credential was not stored: %s"
msgstr ""

#: cloud_accounts.php
msgid "The account was saved, but the stored credential could not be removed."
msgstr ""

#: cloud_accounts.php
msgid "The account was saved."
msgstr ""
Expand Down
4 changes: 4 additions & 0 deletions locales/po/es-419.po
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ msgstr "Nombre: otra cuenta ya utiliza este nombre."
msgid "The account was saved, but the credential was not stored: %s"
msgstr "La cuenta se ha guardado, pero no se ha almacenado la credencial: %s"

#: cloud_accounts.php
msgid "The account was saved, but the stored credential could not be removed."
msgstr "La cuenta se guardó, pero no se pudo eliminar la credencial almacenada."

#: cloud_accounts.php
msgid "The account was saved."
msgstr "La cuenta se ha guardado."
Expand Down
4 changes: 4 additions & 0 deletions locales/po/es-ES.po
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ msgstr "Nombre: otra cuenta ya utiliza este nombre."
msgid "The account was saved, but the credential was not stored: %s"
msgstr "La cuenta se ha guardado, pero no se ha almacenado la credencial: %s"

#: cloud_accounts.php
msgid "The account was saved, but the stored credential could not be removed."
msgstr "La cuenta se ha guardado, pero no se ha podido eliminar la credencial almacenada."

#: cloud_accounts.php
msgid "The account was saved."
msgstr "La cuenta se ha guardado."
Expand Down
4 changes: 4 additions & 0 deletions locales/po/hi-IN.po
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ msgstr "नाम: कोई अन्य खाता पहले से इ
msgid "The account was saved, but the credential was not stored: %s"
msgstr "खाता सहेजा गया, लेकिन क्रेडेंशियल संग्रहीत नहीं हुआ: %s"

#: cloud_accounts.php
msgid "The account was saved, but the stored credential could not be removed."
msgstr "खाता सहेज लिया गया, लेकिन संग्रहीत क्रेडेंशियल हटाया नहीं जा सका।"

#: cloud_accounts.php
msgid "The account was saved."
msgstr "खाता सहेज दिया गया।"
Expand Down
4 changes: 4 additions & 0 deletions locales/po/zh-CN.po
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ msgstr "名称:其他账户已使用此名称。"
msgid "The account was saved, but the credential was not stored: %s"
msgstr "账户已保存,但凭据未存储:%s"

#: cloud_accounts.php
msgid "The account was saved, but the stored credential could not be removed."
msgstr "账户已保存,但无法删除已存储的凭据。"

#: cloud_accounts.php
msgid "The account was saved."
msgstr "账户已保存。"
Expand Down
31 changes: 31 additions & 0 deletions tests/Unit/VaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,34 @@ function cloud_refusal(callable $read): string {
->and(VaultEntry::fromRow(['nonce' => 12] + $row))->toBeNull()
->and(VaultEntry::fromRow([]))->toBeNull();
});

it('reports whether the credential delete ran', function () {
$store = new CredentialStore(new SecretRepository(false, new FixedClock(1)), '/nonexistent', $this->dir);

$GLOBALS['__cloud_db_exec'] = [];
$GLOBALS['__cloud_db_fail'] = [];

expect($store->forget(7))->toBeTrue()
->and($store->forgetUnlessVault(7))->toBeTrue();

$GLOBALS['__cloud_db_fail'] = ['DELETE FROM plugin_cloud_secret', 'DELETE s FROM plugin_cloud_secret'];

expect($store->forget(7))->toBeFalse()
->and($store->forgetUnlessVault(7))->toBeFalse();

$GLOBALS['__cloud_db_fail'] = [];
});

it('deletes a credential only while its account is off the vault, in one statement', function () {
$store = new CredentialStore(new SecretRepository(false, new FixedClock(1)), '/nonexistent', $this->dir);

$GLOBALS['__cloud_db_exec'] = [];
$store->forgetUnlessVault(7);

$statement = $GLOBALS['__cloud_db_exec'][0];

expect($GLOBALS['__cloud_db_exec'])->toHaveCount(1)
->and($statement['sql'])->toContain('INNER JOIN plugin_cloud_account')
->and($statement['sql'])->toContain('a.credential_source <> ?')
->and($statement['params'])->toBe([7, 'credential', 'vault']);
});