@@ -28,6 +28,11 @@ import {
2828 User ,
2929} from './config_types.js' ;
3030import { ExecAuth } from './exec_auth.js' ;
31+ import {
32+ ExecCredentialPluginPolicy ,
33+ parseKubercExecCredentialPluginPolicy ,
34+ validateExecCredentialPluginPolicy ,
35+ } from './exec_credential_policy.js' ;
3136import { FileAuth } from './file_auth.js' ;
3237import { GoogleCloudPlatformAuth } from './gcp_auth.js' ;
3338import {
@@ -135,10 +140,12 @@ function createSocksConnector(proxyUrl: string, tlsOptions: tls.ConnectionOption
135140export interface ApiType { }
136141
137142export class KubeConfig implements SecurityAuthentication {
143+ private execAuth : ExecAuth = new ExecAuth ( ) ;
144+
138145 private authenticators : Authenticator [ ] = [
139146 new AzureAuth ( ) ,
140147 new GoogleCloudPlatformAuth ( ) ,
141- new ExecAuth ( ) ,
148+ this . execAuth ,
142149 new FileAuth ( ) ,
143150 new OpenIDConnectAuth ( ) ,
144151 ] ;
@@ -158,6 +165,59 @@ export class KubeConfig implements SecurityAuthentication {
158165 this . custom_authenticators . push ( authenticator ) ;
159166 }
160167
168+ /**
169+ * Sets the application policy for kubeconfig `exec` credential plugins.
170+ * The semantics match the `credentialPluginPolicy` and `credentialPluginAllowlist`
171+ * fields of a kubectl kuberc file, see https://kubernetes.io/docs/reference/kubectl/kuberc/
172+ * The policy is checked before every plugin execution, including token refreshes.
173+ * @param policy the policy, undefined restores the default `AllowAll` behavior.
174+ * @throws Error if the policy is misconfigured.
175+ */
176+ public setExecCredentialPluginPolicy ( policy ?: ExecCredentialPluginPolicy ) : void {
177+ validateExecCredentialPluginPolicy ( policy ) ;
178+ this . execAuth . setPolicy ( policy ) ;
179+ }
180+
181+ /**
182+ * Loads the exec credential plugin policy from a kubectl kuberc file and applies it
183+ * with `setExecCredentialPluginPolicy`.
184+ *
185+ * If `file` is not given, the file is located the same way kubectl does: the `KUBERC`
186+ * environment variable if set, otherwise `$HOME/.kube/kuberc`. Setting `KUBERC=off` or
187+ * `KUBECTL_KUBERC=false` disables loading, in which case the current policy is unchanged.
188+ * A missing default kuberc file is not an error, a missing explicitly specified file is.
189+ * @param file optional path to the kuberc file.
190+ * @returns the loaded policy, or undefined if no kuberc file was loaded.
191+ */
192+ public loadExecCredentialPluginPolicyFromKuberc (
193+ file ?: string ,
194+ platform : string = process . platform ,
195+ ) : ExecCredentialPluginPolicy | undefined {
196+ let kubercFile = file ;
197+ let required = true ;
198+ if ( ! kubercFile ) {
199+ if ( process . env . KUBECTL_KUBERC === 'false' || process . env . KUBERC === 'off' ) {
200+ return undefined ;
201+ }
202+ if ( process . env . KUBERC ) {
203+ kubercFile = process . env . KUBERC ;
204+ } else {
205+ const home = findHomeDir ( platform ) ;
206+ if ( ! home ) {
207+ return undefined ;
208+ }
209+ kubercFile = path . join ( home , '.kube' , 'kuberc' ) ;
210+ required = false ;
211+ }
212+ }
213+ if ( ! required && ! fileExists ( kubercFile ) ) {
214+ return undefined ;
215+ }
216+ const policy = parseKubercExecCredentialPluginPolicy ( fs . readFileSync ( kubercFile ) . toString ( 'utf-8' ) ) ;
217+ this . setExecCredentialPluginPolicy ( policy ) ;
218+ return policy ;
219+ }
220+
161221 /**
162222 * The list of all known clusters
163223 */
0 commit comments