From d5e3a7718ee01634be72d0f9a9d69f5b4f9a98d5 Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Fri, 6 May 2022 20:30:04 +0300 Subject: [PATCH] Registry: suport pulling public images (#50) --- Sources/tart/Credentials.swift | 12 ++---------- Sources/tart/OCI/Registry.swift | 11 ++++++----- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/Sources/tart/Credentials.swift b/Sources/tart/Credentials.swift index fea8d3f..9897a0f 100644 --- a/Sources/tart/Credentials.swift +++ b/Sources/tart/Credentials.swift @@ -1,15 +1,7 @@ import Foundation class Credentials { - static func retrieve(host: String) throws -> (String, String) { - do { - return try retrieveKeychain(host: host) - } catch RegistryError.AuthFailed { - return try retrieveStdin() - } - } - - static func retrieveKeychain(host: String) throws -> (String, String) { + static func retrieveKeychain(host: String) throws -> (String, String)? { let query: [String: Any] = [kSecClass as String: kSecClassInternetPassword, kSecAttrProtocol as String: kSecAttrProtocolHTTPS, kSecAttrServer as String: host, @@ -24,7 +16,7 @@ class Credentials { if status != errSecSuccess { if status == errSecItemNotFound { - throw RegistryError.AuthFailed(why: "Keychain item not found") + return nil } throw RegistryError.AuthFailed(why: "Keychain returned unsuccessful status \(status)") diff --git a/Sources/tart/OCI/Registry.swift b/Sources/tart/OCI/Registry.swift index 670244d..8a7ea90 100644 --- a/Sources/tart/OCI/Registry.swift +++ b/Sources/tart/OCI/Registry.swift @@ -192,11 +192,12 @@ class Registry { } } - let (user, password) = try Credentials.retrieve(host: baseURL.host!) - let encodedCredentials = "\(user):\(password)".data(using: .utf8)?.base64EncodedString() - let headers = [ - "Authorization": "Basic \(encodedCredentials!)" - ] + var headers: Dictionary = Dictionary() + + if let (user, password) = try Credentials.retrieveKeychain(host: baseURL.host!) { + let encodedCredentials = "\(user):\(password)".data(using: .utf8)?.base64EncodedString() + headers["Authorization"] = "Basic \(encodedCredentials!)" + } let (responseData, response) = try await rawRequest("GET", authenticateURL, headers: headers) if response.statusCode != 200 {