Skip to content

Commit b7f2478

Browse files
authored
Merge pull request kubernetes-client#328 from silasbw/auth0
Convert `Authenticator.applyAuthentication` to return a `Promise<void>`
2 parents 674b976 + 4c88a95 commit b7f2478

File tree

7 files changed

+62
-52
lines changed

7 files changed

+62
-52
lines changed

src/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ export interface Authenticator {
77
isAuthProvider(user: User): boolean;
88
// TODO: Deprecate this and roll it into applyAuthentication
99
getToken(user: User): string | null;
10-
applyAuthentication(user: User, opts: request.Options | https.RequestOptions);
10+
applyAuthentication(user: User, opts: request.Options | https.RequestOptions): Promise<void>;
1111
}

src/cloud_auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class CloudAuth implements Authenticator {
3434
return 'Bearer ' + config['access-token'];
3535
}
3636

37-
public applyAuthentication(user: User, opts: request.Options | https.RequestOptions) {
37+
public async applyAuthentication(user: User, opts: request.Options | https.RequestOptions) {
3838
// pass
3939
}
4040

src/config.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,21 @@ export class KubeConfig {
108108
this.makePathsAbsolute(rootDirectory);
109109
}
110110

111-
public applytoHTTPSOptions(opts: https.RequestOptions) {
111+
public async applytoHTTPSOptions(opts: https.RequestOptions) {
112112
const user = this.getCurrentUser();
113113

114-
this.applyOptions(opts);
114+
await this.applyOptions(opts);
115115

116116
if (user && user.username) {
117117
opts.auth = `${user.username}:${user.password}`;
118118
}
119119
}
120120

121-
public applyToRequest(opts: request.Options) {
121+
public async applyToRequest(opts: request.Options) {
122122
const cluster = this.getCurrentCluster();
123123
const user = this.getCurrentUser();
124124

125-
this.applyOptions(opts);
125+
await this.applyOptions(opts);
126126

127127
if (cluster && cluster.skipTLSVerify) {
128128
opts.strictSSL = false;
@@ -341,7 +341,7 @@ export class KubeConfig {
341341
}
342342
}
343343

344-
private applyAuthorizationHeader(opts: request.Options | https.RequestOptions) {
344+
private async applyAuthorizationHeader(opts: request.Options | https.RequestOptions) {
345345
const user = this.getCurrentUser();
346346
if (!user) {
347347
return;
@@ -353,7 +353,7 @@ export class KubeConfig {
353353
let token: string | null = null;
354354
if (authenticator) {
355355
token = authenticator.getToken(user);
356-
authenticator.applyAuthentication(user, opts);
356+
await authenticator.applyAuthentication(user, opts);
357357
}
358358

359359
if (user.token) {
@@ -368,9 +368,9 @@ export class KubeConfig {
368368
}
369369
}
370370

371-
private applyOptions(opts: request.Options | https.RequestOptions) {
371+
private async applyOptions(opts: request.Options | https.RequestOptions) {
372372
this.applyHTTPSOptions(opts);
373-
this.applyAuthorizationHeader(opts);
373+
await this.applyAuthorizationHeader(opts);
374374
}
375375
}
376376

src/config_test.ts

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,15 @@ describe('KubeConfig', () => {
211211
rejectUnauthorized: false,
212212
});
213213
});
214-
it('should apply password', () => {
214+
it('should apply password', async () => {
215215
const kc = new KubeConfig();
216216
kc.loadFromFile(kcFileName);
217217
kc.setCurrentContext('passwd');
218218

219219
const opts: requestlib.Options = {
220220
url: 'https://company.com',
221221
};
222-
kc.applyToRequest(opts);
222+
await kc.applyToRequest(opts);
223223
expect(opts).to.deep.equal({
224224
ca: new Buffer('CADATA2', 'utf-8'),
225225
auth: {
@@ -514,18 +514,18 @@ describe('KubeConfig', () => {
514514
});
515515

516516
describe('auth options', () => {
517-
it('should populate basic-auth for https', () => {
517+
it('should populate basic-auth for https', async () => {
518518
const config = new KubeConfig();
519519
const user = 'user';
520520
const passwd = 'password';
521521

522522
config.loadFromClusterAndUser({} as Cluster, { username: user, password: passwd } as User);
523523
const opts = {} as https.RequestOptions;
524-
config.applytoHTTPSOptions(opts);
524+
await config.applytoHTTPSOptions(opts);
525525

526526
expect(opts.auth).to.equal(`${user}:${passwd}`);
527527
});
528-
it('should populate options for request', () => {
528+
it('should populate options for request', async () => {
529529
const config = new KubeConfig();
530530
const user = 'user';
531531
const passwd = 'password';
@@ -541,7 +541,7 @@ describe('KubeConfig', () => {
541541
);
542542
const opts = {} as requestlib.Options;
543543

544-
config.applyToRequest(opts);
544+
await config.applyToRequest(opts);
545545

546546
/* tslint:disable no-unused-expression*/
547547
expect(opts.auth).to.not.be.undefined;
@@ -551,17 +551,17 @@ describe('KubeConfig', () => {
551551
}
552552
expect(opts.strictSSL).to.equal(false);
553553
});
554-
it('should not populate strict ssl', () => {
554+
it('should not populate strict ssl', async () => {
555555
const config = new KubeConfig();
556556

557557
config.loadFromClusterAndUser({ skipTLSVerify: false } as Cluster, {} as User);
558558
const opts = {} as requestlib.Options;
559559

560-
config.applyToRequest(opts);
560+
await config.applyToRequest(opts);
561561

562562
expect(opts.strictSSL).to.equal(undefined);
563563
});
564-
it('should populate from token', () => {
564+
it('should populate from token', async () => {
565565
const config = new KubeConfig();
566566
const token = 'token';
567567
config.loadFromClusterAndUser(
@@ -572,13 +572,13 @@ describe('KubeConfig', () => {
572572
);
573573
const opts = {} as requestlib.Options;
574574

575-
config.applyToRequest(opts);
575+
await config.applyToRequest(opts);
576576
expect(opts.headers).to.not.be.undefined;
577577
if (opts.headers) {
578578
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
579579
}
580580
});
581-
it('should populate from auth provider', () => {
581+
it('should populate from auth provider', async () => {
582582
const config = new KubeConfig();
583583
const token = 'token';
584584
config.loadFromClusterAndUser(
@@ -595,18 +595,18 @@ describe('KubeConfig', () => {
595595
);
596596
const opts = {} as requestlib.Options;
597597

598-
config.applyToRequest(opts);
598+
await config.applyToRequest(opts);
599599
expect(opts.headers).to.not.be.undefined;
600600
if (opts.headers) {
601601
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
602602
}
603603
opts.headers = [];
604604
opts.headers.Host = 'foo.com';
605-
config.applyToRequest(opts);
605+
await config.applyToRequest(opts);
606606
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
607607
});
608608

609-
it('should populate from auth provider without expirty', () => {
609+
it('should populate from auth provider without expirty', async () => {
610610
const config = new KubeConfig();
611611
const token = 'token';
612612
config.loadFromClusterAndUser(
@@ -622,14 +622,14 @@ describe('KubeConfig', () => {
622622
);
623623
const opts = {} as requestlib.Options;
624624

625-
config.applyToRequest(opts);
625+
await config.applyToRequest(opts);
626626
expect(opts.headers).to.not.be.undefined;
627627
if (opts.headers) {
628628
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
629629
}
630630
});
631631

632-
it('should populate rejectUnauthorized=false when skipTLSVerify is set', () => {
632+
it('should populate rejectUnauthorized=false when skipTLSVerify is set', async () => {
633633
const config = new KubeConfig();
634634
const token = 'token';
635635
config.loadFromClusterAndUser(
@@ -645,11 +645,11 @@ describe('KubeConfig', () => {
645645
);
646646
const opts = {} as requestlib.Options;
647647

648-
config.applyToRequest(opts);
648+
await config.applyToRequest(opts);
649649
expect(opts.rejectUnauthorized).to.equal(false);
650650
});
651651

652-
it('should not set rejectUnauthorized if skipTLSVerify is not set', () => {
652+
it('should not set rejectUnauthorized if skipTLSVerify is not set', async () => {
653653
// This test is just making 100% sure we validate certs unless we explictly set
654654
// skipTLSVerify = true
655655
const config = new KubeConfig();
@@ -667,7 +667,7 @@ describe('KubeConfig', () => {
667667
);
668668
const opts = {} as requestlib.Options;
669669

670-
config.applyToRequest(opts);
670+
await config.applyToRequest(opts);
671671
expect(opts.rejectUnauthorized).to.equal(undefined);
672672
});
673673

@@ -686,7 +686,7 @@ describe('KubeConfig', () => {
686686
);
687687
const opts = {} as requestlib.Options;
688688

689-
expect(() => config.applyToRequest(opts)).to.throw('Token is expired!');
689+
return expect(config.applyToRequest(opts)).to.eventually.be.rejectedWith('Token is expired!');
690690
});
691691

692692
it('should throw with bad command', () => {
@@ -705,10 +705,12 @@ describe('KubeConfig', () => {
705705
} as User,
706706
);
707707
const opts = {} as requestlib.Options;
708-
expect(() => config.applyToRequest(opts)).to.throw(/Failed to refresh token/);
708+
return expect(config.applyToRequest(opts)).to.eventually.be.rejectedWith(
709+
/Failed to refresh token/,
710+
);
709711
});
710712

711-
it('should exec with expired token', () => {
713+
it('should exec with expired token', async () => {
712714
const config = new KubeConfig();
713715
const token = 'token';
714716
const responseStr = `{"token":{"accessToken":"${token}"}}`;
@@ -728,13 +730,13 @@ describe('KubeConfig', () => {
728730
} as User,
729731
);
730732
const opts = {} as requestlib.Options;
731-
config.applyToRequest(opts);
733+
await config.applyToRequest(opts);
732734
expect(opts.headers).to.not.be.undefined;
733735
if (opts.headers) {
734736
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
735737
}
736738
});
737-
it('should exec without access-token', () => {
739+
it('should exec without access-token', async () => {
738740
const config = new KubeConfig();
739741
const token = 'token';
740742
const responseStr = `{"token":{"accessToken":"${token}"}}`;
@@ -753,13 +755,13 @@ describe('KubeConfig', () => {
753755
} as User,
754756
);
755757
const opts = {} as requestlib.Options;
756-
config.applyToRequest(opts);
758+
await config.applyToRequest(opts);
757759
expect(opts.headers).to.not.be.undefined;
758760
if (opts.headers) {
759761
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
760762
}
761763
});
762-
it('should exec without access-token', () => {
764+
it('should exec without access-token', async () => {
763765
const config = new KubeConfig();
764766
const token = 'token';
765767
const responseStr = `{"token":{"accessToken":"${token}"}}`;
@@ -778,13 +780,13 @@ describe('KubeConfig', () => {
778780
} as User,
779781
);
780782
const opts = {} as requestlib.Options;
781-
config.applyToRequest(opts);
783+
await config.applyToRequest(opts);
782784
expect(opts.headers).to.not.be.undefined;
783785
if (opts.headers) {
784786
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
785787
}
786788
});
787-
it('should exec with exec auth and env vars', () => {
789+
it('should exec with exec auth and env vars', async () => {
788790
const config = new KubeConfig();
789791
const token = 'token';
790792
const responseStr = `{"status": { "token": "${token}" }}`;
@@ -810,13 +812,13 @@ describe('KubeConfig', () => {
810812
);
811813
// TODO: inject the exec command here and validate env vars?
812814
const opts = {} as requestlib.Options;
813-
config.applyToRequest(opts);
815+
await config.applyToRequest(opts);
814816
expect(opts.headers).to.not.be.undefined;
815817
if (opts.headers) {
816818
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
817819
}
818820
});
819-
it('should exec with exec auth', () => {
821+
it('should exec with exec auth', async () => {
820822
const config = new KubeConfig();
821823
const token = 'token';
822824
const responseStr = `{
@@ -842,13 +844,13 @@ describe('KubeConfig', () => {
842844
);
843845
// TODO: inject the exec command here?
844846
const opts = {} as requestlib.Options;
845-
config.applyToRequest(opts);
847+
await config.applyToRequest(opts);
846848
expect(opts.headers).to.not.be.undefined;
847849
if (opts.headers) {
848850
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
849851
}
850852
});
851-
it('should exec with exec auth (other location)', () => {
853+
it('should exec with exec auth (other location)', async () => {
852854
const config = new KubeConfig();
853855
const token = 'token';
854856
const responseStr = `{
@@ -869,7 +871,7 @@ describe('KubeConfig', () => {
869871
);
870872
// TODO: inject the exec command here?
871873
const opts = {} as requestlib.Options;
872-
config.applyToRequest(opts);
874+
await config.applyToRequest(opts);
873875
expect(opts.headers).to.not.be.undefined;
874876
if (opts.headers) {
875877
expect(opts.headers.Authorization).to.equal(`Bearer ${token}`);
@@ -889,7 +891,7 @@ describe('KubeConfig', () => {
889891
} as User,
890892
);
891893
const opts = {} as requestlib.Options;
892-
expect(() => config.applyToRequest(opts)).to.throw(
894+
return expect(config.applyToRequest(opts)).to.eventually.be.rejectedWith(
893895
'No command was specified for exec authProvider!',
894896
);
895897
});
@@ -1083,9 +1085,9 @@ describe('KubeConfig', () => {
10831085
expect(emptyConfig.getCurrentContext()).to.be.undefined;
10841086
});
10851087

1086-
it('should apply to request', () => {
1088+
it('should apply to request', async () => {
10871089
const opts = {} as requestlib.Options;
1088-
emptyConfig.applyToRequest(opts);
1090+
await emptyConfig.applyToRequest(opts);
10891091
});
10901092
});
10911093

src/proto-client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ export class ProtoClient {
1414
hostname: u.hostname,
1515
protocol: u.protocol,
1616
};
17-
this.config.applytoHTTPSOptions(options);
17+
await this.config.applytoHTTPSOptions(options);
1818
const req = http.request(options);
1919

20-
const result = new Promise<any>((resolve, reject) => {
20+
const result = await new Promise<any>((resolve, reject) => {
2121
let data = '';
2222
req.on('data', (chunk) => {
2323
data = data + chunk;

src/web-socket-handler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export class WebSocketHandler implements WebSocketInterface {
137137
* @param binaryHandler Callback for binary data over the web socket.
138138
* Returns true if the connection should be kept alive, false to disconnect.
139139
*/
140-
public connect(
140+
public async connect(
141141
path: string,
142142
textHandler: ((text: string) => boolean) | null,
143143
binaryHandler: ((stream: number, buff: Buffer) => boolean) | null,
@@ -154,9 +154,9 @@ export class WebSocketHandler implements WebSocketInterface {
154154

155155
const opts: WebSocket.ClientOptions = {};
156156

157-
this.config.applytoHTTPSOptions(opts);
157+
await this.config.applytoHTTPSOptions(opts);
158158

159-
return new Promise((resolve, reject) => {
159+
return await new Promise<WebSocket>((resolve, reject) => {
160160
const client = this.socketFactory
161161
? this.socketFactory(uri, opts)
162162
: new WebSocket(uri, protocols, opts);

0 commit comments

Comments
 (0)