-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Indentation, clean up, and batch migration to avoid inconsistent state
- Loading branch information
1 parent
3dfa43b
commit f5c01b1
Showing
7 changed files
with
87 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
AmplifyPlugins/Core/AWSPluginsCore/Keychain/KeychainStoreMigrator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import Amplify | ||
|
||
public struct KeychainStoreMigrator { | ||
let oldAttributes: KeychainStoreAttributes | ||
let newAttributes: KeychainStoreAttributes | ||
|
||
public init(oldService: String, newService: String, oldAccessGroup: String?, newAccessGroup: String?) { | ||
self.oldAttributes = KeychainStoreAttributes(service: oldService, accessGroup: oldAccessGroup) | ||
self.newAttributes = KeychainStoreAttributes(service: newService, accessGroup: newAccessGroup) | ||
} | ||
|
||
public func migrate() throws { | ||
log.verbose("[KeychainStoreMigrator] Starting to migrate items") | ||
|
||
var updateQuery = oldAttributes.defaultGetQuery() | ||
|
||
var updateAttributes = [String: Any]() | ||
updateAttributes[KeychainStore.Constants.AttributeService] = newAttributes.service | ||
updateAttributes[KeychainStore.Constants.AttributeAccessGroup] = newAttributes.accessGroup | ||
|
||
// Remove any current items to avoid duplicate item error | ||
try? KeychainStore(service: newAttributes.service, accessGroup: newAttributes.accessGroup)._removeAll() | ||
|
||
let updateStatus = SecItemUpdate(updateQuery as CFDictionary, updateAttributes as CFDictionary) | ||
switch updateStatus { | ||
case errSecSuccess: | ||
break | ||
case errSecItemNotFound: | ||
log.verbose("[KeychainStoreMigrator] No items to migrate, keychain under new access group is cleared") | ||
case errSecDuplicateItem: | ||
log.verbose("[KeychainStoreMigrator] Duplicate items found, could not migrate") | ||
return | ||
default: | ||
log.error("[KeychainStoreMigrator] Error of status=\(updateStatus) occurred when attempting to migrate items in keychain") | ||
throw KeychainStoreError.securityError(updateStatus) | ||
} | ||
|
||
log.verbose("[KeychainStoreMigrator] Successfully migrated items to new service and access group") | ||
} | ||
} | ||
|
||
extension KeychainStoreMigrator: DefaultLogger { | ||
public static var log: Logger { | ||
Amplify.Logging.logger(forNamespace: String(describing: self)) | ||
} | ||
|
||
public nonisolated var log: Logger { Self.log } | ||
} |