-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #167
- Loading branch information
Showing
5 changed files
with
98 additions
and
26 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
63 changes: 63 additions & 0 deletions
63
Sources/tart/Credentials/HelperProgramCredentialsProvider.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,63 @@ | ||
import Foundation | ||
|
||
class HelperProgramCredentialsProvider: CredentialsProvider { | ||
func retrieve(host: String) throws -> (String, String)? { | ||
let dockerConfigURL = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".docker").appendingPathComponent("config.json") | ||
if !FileManager.default.fileExists(atPath: dockerConfigURL.path) { | ||
return nil | ||
} | ||
let config = try JSONDecoder().decode(DockerConfig.self, from: Data(contentsOf: dockerConfigURL)) | ||
|
||
if let helperProgram = config.credHelpers[host] { | ||
return try executeHelper(binaryName: "docker-credential-\(helperProgram)", host: host) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
private func executeHelper(binaryName: String, host: String) throws -> (String, String)? { | ||
guard let executableURL = resolveBinaryPath(binaryName) else { | ||
throw CredentialsProviderError.Failed(message: "\(binaryName) not found in PATH") | ||
} | ||
|
||
let process = Process.init() | ||
process.executableURL = executableURL | ||
process.arguments = ["get"] | ||
|
||
let outPipe = Pipe() | ||
let inPipe = Pipe() | ||
|
||
process.standardOutput = outPipe | ||
process.standardError = outPipe | ||
process.standardInput = inPipe | ||
|
||
process.launch() | ||
|
||
inPipe.fileHandleForWriting.write("\(host)\n".data(using: .utf8)!) | ||
inPipe.fileHandleForWriting.closeFile() | ||
|
||
process.waitUntilExit() | ||
|
||
if !(process.terminationReason == .exit && process.terminationStatus == 0) { | ||
throw CredentialsProviderError.Failed(message: "Docker helper failed!") | ||
} | ||
|
||
let getOutput = try JSONDecoder().decode( | ||
DockerGetOutput.self, from: outPipe.fileHandleForReading.readDataToEndOfFile() | ||
) | ||
return (getOutput.Username, getOutput.Secret) | ||
} | ||
|
||
func store(host: String, user: String, password: String) throws { | ||
throw CredentialsProviderError.Failed(message: "Docker helpers don't support storing!") | ||
} | ||
} | ||
|
||
struct DockerConfig: Codable { | ||
var credHelpers: Dictionary<String, String> = Dictionary() | ||
} | ||
|
||
struct DockerGetOutput: Codable { | ||
var Username: String | ||
var Secret: String | ||
} |
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