13
13
package io .kubernetes .client .util ;
14
14
15
15
import io .kubernetes .client .ApiClient ;
16
+ import io .kubernetes .client .util .credentials .AccessTokenCredentialProvider ;
17
+ import io .kubernetes .client .util .credentials .UsernamePasswordCredentialProvider ;
16
18
import java .io .ByteArrayInputStream ;
17
19
import okio .ByteString ;
18
20
import org .apache .log4j .Logger ;
@@ -41,26 +43,11 @@ public class Config {
41
43
public static final String ENV_SERVICE_PORT = "KUBERNETES_SERVICE_PORT" ;
42
44
// The last resort host to try
43
45
public static final String DEFAULT_FALLBACK_HOST = "http://localhost:8080" ;
44
- public static final Charset BASIC_AUTH_CHARSET = Charset .forName ("ISO-8859-1" );
45
46
46
47
private static final Logger log = Logger .getLogger (Config .class );
47
48
48
49
public static ApiClient fromCluster () throws IOException {
49
- String host = System .getenv (ENV_SERVICE_HOST );
50
- String port = System .getenv (ENV_SERVICE_PORT );
51
-
52
- FileInputStream caCert = new FileInputStream (SERVICEACCOUNT_CA_PATH );
53
- BufferedReader tokenReader = new BufferedReader (new FileReader (SERVICEACCOUNT_TOKEN_PATH ));
54
- StringBuilder builder = new StringBuilder ();
55
- for (String line = tokenReader .readLine (); line != null ; line = tokenReader .readLine ()) {
56
- builder .append (line );
57
- }
58
- ApiClient result = new ApiClient ();
59
- result .setBasePath ("https://" + host + ":" + port );
60
- result .setSslCaCert (caCert );
61
- result .setApiKey ("Bearer " + builder .toString ());
62
-
63
- return result ;
50
+ return ClientBuilder .fromCluster ().build ();
64
51
}
65
52
66
53
public static ApiClient fromUrl (String url ) {
@@ -78,95 +65,41 @@ public static ApiClient fromUserPassword(String url, String user, String passwor
78
65
}
79
66
80
67
public static ApiClient fromUserPassword (String url , String user , String password , boolean validateSSL ) {
81
- ApiClient client = fromUrl ( url , validateSSL );
82
- final String usernameAndPassword = user + ":" + password ;
83
- client . setApiKeyPrefix ( "Basic" );
84
- client . setApiKey ( ByteString . of ( usernameAndPassword . getBytes ( BASIC_AUTH_CHARSET )). base64 ());
85
- return client ;
68
+ return new ClientBuilder ()
69
+ . setBasePath ( url )
70
+ . setCredentialProvider ( new UsernamePasswordCredentialProvider ( user , password ))
71
+ . setVerifyingSsl ( validateSSL )
72
+ . build () ;
86
73
}
87
74
88
75
public static ApiClient fromToken (String url , String token ) {
89
76
return fromToken (url , token , true );
90
77
}
91
78
92
79
public static ApiClient fromToken (String url , String token , boolean validateSSL ) {
93
- ApiClient client = fromUrl (url , validateSSL );
94
- client .setApiKeyPrefix ("Bearer" );
95
- client .setApiKey (token );
96
- return client ;
80
+ return new ClientBuilder ()
81
+ .setBasePath (url )
82
+ .setCredentialProvider (new AccessTokenCredentialProvider (token ))
83
+ .setVerifyingSsl (validateSSL )
84
+ .build ();
97
85
}
98
86
99
87
public static ApiClient fromConfig (String fileName ) throws IOException {
100
88
return fromConfig (new FileReader (fileName ));
101
89
}
102
90
103
- public static ApiClient fromConfig (InputStream stream ) {
91
+ public static ApiClient fromConfig (InputStream stream ) throws IOException {
104
92
return fromConfig (new InputStreamReader (stream ));
105
93
}
106
94
107
- public static ApiClient fromConfig (Reader input ) {
95
+ public static ApiClient fromConfig (Reader input ) throws IOException {
108
96
return fromConfig (KubeConfig .loadKubeConfig (input ));
109
97
}
110
98
111
- public static ApiClient fromConfig (KubeConfig config ) {
112
- ApiClient client = new ApiClient ();
113
- String server = config .getServer ();
114
- if (!server .startsWith ("http://" ) && !server .startsWith ("https://" )) {
115
- if (server .indexOf (":443" ) != -1 ) {
116
- server = "https://" + server ;
117
- } else {
118
- server = "http://" + server ;
119
- }
120
- }
121
- client .setBasePath (server );
122
-
123
- try {
124
- KeyManager [] mgrs = SSLUtils .keyManagers (
125
- KubeConfig .getDataOrFile (config .getClientCertificateData (), config .getClientCertificateFile ()),
126
- KubeConfig .getDataOrFile (config .getClientKeyData (), config .getClientKeyFile ()),
127
- "RSA" , "" ,
128
- null , null );
129
- client .setKeyManagers (mgrs );
130
- } catch (Exception ex ) {
131
- log .error ("Failed to invoke build key managers" , ex );
132
- }
133
-
134
- if (config .verifySSL ()) {
135
- // It's silly to have to do it in this order, but each SSL setup
136
- // consumes the CA cert, so if we do this before the client certs
137
- // are injected the cert input stream is exhausted and things get
138
- // grumpy
139
- String caCert = config .getCertificateAuthorityData ();
140
- String caCertFile = config .getCertificateAuthorityFile ();
141
- if (caCert != null || caCertFile != null ) {
142
- try {
143
- client .setSslCaCert (new ByteArrayInputStream (KubeConfig .getDataOrFile (caCert , caCertFile )));
144
- } catch (IOException ex ) {
145
- log .error ("Failed to read CA Cert file" , ex );
146
- }
147
- }
148
- } else {
149
- client .setVerifyingSsl (false );
150
- }
151
-
152
- String token = config .getAccessToken ();
153
- if (token != null ) {
154
- // This is kind of a hack, except not, because I don't think we actually
155
- // want to use oauth here.
156
- client .setApiKey ("Bearer " + token );
157
- }
158
-
159
- String username = config .getUsername ();
160
- if (username != null ) {
161
- client .setUsername (username );
162
- }
163
-
164
- String password = config .getPassword ();
165
- if (password != null ) {
166
- client .setPassword (password );
167
- }
168
-
169
- return client ;
99
+ public static ApiClient fromConfig (KubeConfig config ) throws IOException {
100
+ return ClientBuilder
101
+ .fromKubeConfig (config )
102
+ .build ();
170
103
}
171
104
172
105
/**
@@ -181,23 +114,6 @@ public static ApiClient fromConfig(KubeConfig config) {
181
114
* @return The best APIClient given the previously described rules
182
115
*/
183
116
public static ApiClient defaultClient () throws IOException {
184
- String kubeConfig = System .getenv (ENV_KUBECONFIG );
185
- if (kubeConfig != null ) {
186
- return fromConfig (new FileReader (kubeConfig ));
187
- }
188
- File config = new File (
189
- new File (System .getenv (KubeConfig .ENV_HOME ),
190
- KubeConfig .KUBEDIR ),
191
- KubeConfig .KUBECONFIG );
192
- if (config .exists ()) {
193
- return fromConfig (new FileReader (config ));
194
- }
195
- File clusterCA = new File (SERVICEACCOUNT_CA_PATH );
196
- if (clusterCA .exists ()) {
197
- return fromCluster ();
198
- }
199
- ApiClient client = new ApiClient ();
200
- client .setBasePath (DEFAULT_FALLBACK_HOST );
201
- return client ;
117
+ return ClientBuilder .defaults ().build ();
202
118
}
203
119
}
0 commit comments