@@ -33,135 +33,135 @@ import Foundation
3333import Logging
3434import NIOSSL
3535
36- extension AuthInfo {
37-
38- public func authentication( logger: Logger ? ) -> KubernetesClientAuthentication ? {
39- if let username = username, let password = password {
40- return . basicAuth( username: username, password: password)
41- }
42-
43- if let token = token {
44- return . bearer( token: token)
45- }
46-
47- if let tokenFile = tokenFile {
48- return . tokenFile( source: CachedFileTokenSource ( path: tokenFile) )
49- }
50-
51- do {
52- if let clientCertificateFile = clientCertificate, let clientKeyFile = clientKey {
53- let clientCertificate = try NIOSSLCertificate (
54- file: clientCertificateFile,
55- format: . pem
56- )
57- let clientKey = try NIOSSLPrivateKey (
58- file: clientKeyFile,
59- format: . pem
60- )
61- return . x509(
62- clientCertificate: clientCertificate,
63- clientKey: clientKey
64- )
65- }
66-
67- if let clientCertificateData = clientCertificateData, let clientKeyData = clientKeyData {
68- let clientCertificate = try NIOSSLCertificate (
69- bytes: [ UInt8] ( clientCertificateData) ,
70- format: . pem
71- )
72- let clientKey = try NIOSSLPrivateKey (
73- bytes: [ UInt8] ( clientKeyData) ,
74- format: . pem
75- )
76- return . x509(
77- clientCertificate: clientCertificate,
78- clientKey: clientKey
79- )
80- }
81- } catch {
82- logger? . warning (
83- " Error initializing authentication from client certificate: \( error) "
84- )
85- }
86-
87- #if os(Linux) || os(macOS)
88- do {
89- if let exec {
90- let outputData = try run (
91- command: exec. command,
92- arguments: exec. args
93- )
94-
95- let decoder = JSONDecoder ( )
96- decoder. dateDecodingStrategy = . iso8601
97- let credential = try decoder. decode (
98- ExecCredential . self,
99- from: outputData
100- )
101-
102- return . bearer( token: credential. status. token)
103- }
104- } catch {
105- logger? . warning (
106- " Error initializing authentication from exec \( error) "
107- )
108- }
109- #endif
110-
111- return nil
112- }
36+ public extension AuthInfo {
37+
38+ func authentication( logger: Logger ? ) -> KubernetesClientAuthentication ? {
39+ if let username = username, let password = password {
40+ return . basicAuth( username: username, password: password)
41+ }
42+
43+ if let token = token {
44+ return . bearer( token: token)
45+ }
46+
47+ if let tokenFile = tokenFile {
48+ return . tokenFile( source: CachedFileTokenSource ( path: tokenFile) )
49+ }
50+
51+ do {
52+ if let clientCertificateFile = clientCertificate, let clientKeyFile = clientKey {
53+ let clientCertificate = try NIOSSLCertificate (
54+ file: clientCertificateFile,
55+ format: . pem
56+ )
57+ let clientKey = try NIOSSLPrivateKey (
58+ file: clientKeyFile,
59+ format: . pem
60+ )
61+ return . x509(
62+ clientCertificate: clientCertificate,
63+ clientKey: clientKey
64+ )
65+ }
66+
67+ if let clientCertificateData = clientCertificateData, let clientKeyData = clientKeyData {
68+ let clientCertificate = try NIOSSLCertificate (
69+ bytes: [ UInt8] ( clientCertificateData) ,
70+ format: . pem
71+ )
72+ let clientKey = try NIOSSLPrivateKey (
73+ bytes: [ UInt8] ( clientKeyData) ,
74+ format: . pem
75+ )
76+ return . x509(
77+ clientCertificate: clientCertificate,
78+ clientKey: clientKey
79+ )
80+ }
81+ } catch {
82+ logger? . warning (
83+ " Error initializing authentication from client certificate: \( error) "
84+ )
85+ }
86+
87+ #if os(Linux) || os(macOS)
88+ do {
89+ if let exec {
90+ let outputData = try run (
91+ command: exec. command,
92+ arguments: exec. args
93+ )
94+
95+ let decoder = JSONDecoder ( )
96+ decoder. dateDecodingStrategy = . iso8601
97+ let credential = try decoder. decode (
98+ ExecCredential . self,
99+ from: outputData
100+ )
101+
102+ return . bearer( token: credential. status. token)
103+ }
104+ } catch {
105+ logger? . warning (
106+ " Error initializing authentication from exec \( error) "
107+ )
108+ }
109+ #endif
110+
111+ return nil
112+ }
113113}
114114
115115// MARK: - ExecCredential
116116
117- // It seems that AWS doesn't implement properly the model for client.authentication.k8s.io/v1beta1
118- // Acordingly with the doc https://kubernetes.io/docs/reference/config-api/client-authentication.v1beta1/
119- // ExecCredential.Spec.interactive is required as long as the ones in the Status object.
117+ /// It seems that AWS doesn't implement properly the model for client.authentication.k8s.io/v1beta1
118+ /// Acordingly with the doc https://kubernetes.io/docs/reference/config-api/client-authentication.v1beta1/
119+ /// ExecCredential.Spec.interactive is required as long as the ones in the Status object.
120120public struct ExecCredential : Codable {
121- let apiVersion : String
122- let kind : String
123- let spec : Spec
124- let status : Status
121+ let apiVersion : String
122+ let kind : String
123+ let spec : Spec
124+ let status : Status
125125}
126126
127- extension ExecCredential {
127+ public extension ExecCredential {
128128
129- public struct Spec : Codable {
130- let cluster : Cluster ?
131- let interactive : Bool ?
132- }
129+ struct Spec : Codable {
130+ let cluster : Cluster ?
131+ let interactive : Bool ?
132+ }
133133
134- public struct Status : Codable {
135- let expirationTimestamp : Date
136- let token : String
137- let clientCertificateData : String ?
138- let clientKeyData : String ?
139- }
134+ struct Status : Codable {
135+ let expirationTimestamp : Date
136+ let token : String
137+ let clientCertificateData : String ?
138+ let clientKeyData : String ?
139+ }
140140}
141141
142142#if os(Linux) || os(macOS)
143- internal func run( command: String , arguments: [ String ] ? = nil ) throws -> Data {
144- func run( _ command: String , _ arguments: [ String ] ? ) throws -> Data {
145- let task = Process ( )
146- task. executableURL = URL ( fileURLWithPath: command)
147- arguments. flatMap { task. arguments = $0 }
148-
149- let pipe = Pipe ( )
150- task. standardOutput = pipe
151-
152- try task. run ( )
153-
154- return pipe. fileHandleForReading. availableData
155- }
156-
157- func resolve( command: String ) throws -> String {
158- try String (
159- decoding:
160- run ( " /usr/bin/which " , [ " \( command) " ] ) ,
161- as: UTF8 . self
162- ) . trimmingCharacters ( in: . whitespacesAndNewlines)
163- }
164-
165- return try run ( resolve ( command: command) , arguments)
166- }
143+ internal func run( command: String , arguments: [ String ] ? = nil ) throws -> Data {
144+ func run( _ command: String , _ arguments: [ String ] ? ) throws -> Data {
145+ let task = Process ( )
146+ task. executableURL = URL ( fileURLWithPath: command)
147+ arguments. flatMap { task. arguments = $0 }
148+
149+ let pipe = Pipe ( )
150+ task. standardOutput = pipe
151+
152+ try task. run ( )
153+
154+ return pipe. fileHandleForReading. availableData
155+ }
156+
157+ func resolve( command: String ) throws -> String {
158+ try String (
159+ decoding:
160+ run ( " /usr/bin/which " , [ " \( command) " ] ) ,
161+ as: UTF8 . self
162+ ) . trimmingCharacters ( in: . whitespacesAndNewlines)
163+ }
164+
165+ return try run ( resolve ( command: command) , arguments)
166+ }
167167#endif
0 commit comments