Skip to content

Commit

Permalink
feat: load service account token from a different path
Browse files Browse the repository at this point in the history
  • Loading branch information
omerlh committed Jan 30, 2024
1 parent d21542c commit c423a05
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ export class KubeConfig {
const clusterName = 'inCluster';
const userName = 'inClusterUser';
const contextName = 'inClusterContext';
const tokenFile = process.env.TOKEN_FILE_PATH ? process.env.TOKEN_FILE_PATH : `${pathPrefix}${Config.SERVICEACCOUNT_TOKEN_PATH}`
const tokenFile = process.env.TOKEN_FILE_PATH ? process.env.TOKEN_FILE_PATH : `${pathPrefix}${Config.SERVICEACCOUNT_TOKEN_PATH}`;
const caFile = process.env.KUBERNETES_CA_FILE_PATH ? process.env.KUBERNETES_CA_FILE_PATH : `${pathPrefix}${Config.SERVICEACCOUNT_CA_PATH}`;

let scheme = 'https';
if (port === '80' || port === '8080' || port === '8001') {
Expand All @@ -227,7 +228,7 @@ export class KubeConfig {
this.clusters = [
{
name: clusterName,
caFile: `${pathPrefix}${Config.SERVICEACCOUNT_CA_PATH}`,
caFile,
server: `${scheme}://${serverHost}:${port}`,
skipTLSVerify: false,
},
Expand Down
17 changes: 16 additions & 1 deletion src/config_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,28 @@ describe('KubeConfig', () => {

describe('loadFromCluster', () => {
let originalTokenPath: string | undefined;
let originalCaFilePath: string | undefined;

before(() => {
originalTokenPath = process.env['TOKEN_FILE_PATH'];
originalCaFilePath = process.env['KUBERNETES_CA_FILE_PATH']

delete process.env['TOKEN_FILE_PATH']
delete process.env['KUBERNETES_CA_FILE_PATH']
})

after(() => {

delete process.env['TOKEN_FILE_PATH']
delete process.env['KUBERNETES_CA_FILE_PATH']

if (originalTokenPath){
process.env['TOKEN_FILE_PATH'] = originalTokenPath
}

if (originalCaFilePath) {
process.env['KUBERNETES_CA_FILE_PATH'] = originalCaFilePath
}
})

it('should load from default env vars', () => {
Expand Down Expand Up @@ -198,11 +211,13 @@ describe('KubeConfig', () => {
it('should support custom token file path', () => {
const kc = new KubeConfig();
process.env['TOKEN_FILE_PATH'] = '/etc/tokenFile'
process.env['KUBERNETES_CA_FILE_PATH'] = '/etc/ca.crt'

const cluster = {
name: 'inCluster',
server: 'https://undefined:undefined',
skipTLSVerify: false,
caFile: '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt'
caFile: '/etc/ca.crt'
} as Cluster;

const user = {
Expand Down

0 comments on commit c423a05

Please sign in to comment.