Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(authentication): support for exec kube config #4

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
66 changes: 66 additions & 0 deletions Sources/SwiftkubeClient/Config/KubernetesClientConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,72 @@ private extension AuthInfo {
} catch {
logger?.warning("Error initializing authentication from client certificate: \(error)")
}

#if os(Linux) || os(macOS)
do {
if let exec {
let outputData = try run(command: exec.command, arguments: exec.args)

let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let credential = try decoder.decode(ExecCredential.self, from: outputData)

return .bearer(token: credential.status.token)
}
} catch {
logger?.warning("Error initializing authentication from exec \(error)")
}
#endif
return nil
}
}

// MARK: - ExecCredential

// It seems that AWS doesn't implement properly the model for client.authentication.k8s.io/v1beta1
// Acordingly with the doc https://kubernetes.io/docs/reference/config-api/client-authentication.v1beta1/
// ExecCredential.Spec.interactive is required as long as the ones in the Status object.
internal struct ExecCredential: Decodable {
let apiVersion: String
let kind: String
let spec: Spec
let status: Status
}

internal extension ExecCredential {
struct Spec: Decodable {
let cluster: Cluster?
let interactive: Bool?
}

struct Status: Decodable {
let expirationTimestamp: Date
let token: String
let clientCertificateData: String?
let clientKeyData: String?
}
}

#if os(Linux) || os(macOS)
internal func run(command: String, arguments: [String]? = nil) throws -> Data {
func run(_ command: String, _ arguments: [String]?) throws -> Data {
let task = Process()
task.executableURL = URL(fileURLWithPath: command)
task.arguments = arguments

let pipe = Pipe()
task.standardOutput = pipe

try task.run()

return pipe.fileHandleForReading.availableData
}

func resolve(command: String) throws -> String {
try String(decoding:
run("/usr/bin/which", ["\(command)"]), as: UTF8.self).trimmingCharacters(in: .whitespacesAndNewlines)
}

return try run(resolve(command: command), arguments)
}
#endif