Skip to content

Commit 35932fa

Browse files
committed
config: support username impersonation
This commit adds support for username impersonation. This does not implement group, UID, or extra impersonation. Refs: #2355
1 parent ecd208c commit 35932fa

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,11 @@ export class KubeConfig implements SecurityAuthentication {
582582
if (key) {
583583
opts.key = key;
584584
}
585+
586+
if (user.impersonateUser != null) {
587+
opts.headers ??= {};
588+
opts.headers['Impersonate-User'] = user.impersonateUser;
589+
}
585590
}
586591

587592
private async applyAuthorizationHeader(

src/config_test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,4 +1787,25 @@ describe('KubeConfig', () => {
17871787
strictEqual(opts.headers!.Authorization, 'Bearer test-token');
17881788
});
17891789
});
1790+
1791+
describe('Impersonation', () => {
1792+
it('injects Impersonate-User header', async () => {
1793+
const kc = new KubeConfig();
1794+
const cluster: Cluster = {
1795+
name: 'test-cluster',
1796+
server: 'https://localhost:6443',
1797+
skipTLSVerify: false,
1798+
};
1799+
const user: User = {
1800+
name: 'test-user',
1801+
authProvider: 'custom',
1802+
impersonateUser: 'impersonate-user',
1803+
};
1804+
1805+
kc.loadFromClusterAndUser(cluster, user);
1806+
const opts: RequestOptions = {};
1807+
await kc.applyToHTTPSOptions(opts);
1808+
strictEqual(opts.headers!['Impersonate-User'], 'impersonate-user');
1809+
});
1810+
});
17901811
});

src/config_types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export interface User {
9797
readonly token?: string;
9898
readonly username?: string;
9999
readonly password?: string;
100+
readonly impersonateUser?: string;
100101
}
101102

102103
export function newUsers(a: any, opts?: Partial<ConfigOptions>): User[] {
@@ -122,6 +123,7 @@ export function exportUser(user: User): any {
122123
token: user.token,
123124
password: user.password,
124125
username: user.username,
126+
as: user.impersonateUser,
125127
},
126128
};
127129
}
@@ -143,6 +145,7 @@ function userIterator(onInvalidEntry: ActionOnInvalid): (elt: any, i: number, li
143145
token: findToken(elt.user),
144146
password: elt.user ? elt.user.password : null,
145147
username: elt.user ? elt.user.username : null,
148+
impersonateUser: elt.as ? elt.as : null,
146149
};
147150
} catch (err) {
148151
switch (onInvalidEntry) {

0 commit comments

Comments
 (0)