Skip to content

Commit 909fbc0

Browse files
Merge pull request #2 from scality/feature/update-upstream
Update with master/upstream
2 parents 61d8543 + 135e339 commit 909fbc0

15 files changed

+384
-78
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ examples/package-lock.json
1212
kubernetes-client-node-*.tgz
1313
**/*.swp
1414
.idea/
15+
docs/*
16+
.DS_Store

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ npm install
119119
npm run generate
120120
```
121121

122+
## Documentation
123+
124+
Documentation is generated via typedoc:
125+
126+
```
127+
npm run docs
128+
```
129+
130+
To view the generated documentation, open `docs/index.html`
131+
122132
## Formatting
123133

124134
Run `npm run format` or install an editor plugin like https://github.com/prettier/prettier-vscode and https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig

package-lock.json

Lines changed: 135 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"generate": "./generate-client.sh",
2626
"watch": "tsc --watch",
2727
"test": "nyc mocha",
28-
"prepare": "npm run build"
28+
"prepare": "npm run build",
29+
"docs": "typedoc src/gen/api"
2930
},
3031
"nyc": {
3132
"include": [
@@ -90,6 +91,7 @@
9091
"ts-mockito": "^2.3.1",
9192
"ts-node": "^8.2.0",
9293
"tslint": "^5.17.0",
94+
"typedoc": "^0.15.0",
9395
"typescript": "^3.1.3"
9496
},
9597
"bugs": {

src/auth.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@ import { User } from './config_types';
55

66
export interface Authenticator {
77
isAuthProvider(user: User): boolean;
8-
// TODO: Deprecate this and roll it into applyAuthentication
9-
getToken(user: User): string | null;
108
applyAuthentication(user: User, opts: request.Options | https.RequestOptions): Promise<void>;
119
}

src/cloud_auth.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,19 @@ export class CloudAuth implements Authenticator {
2626
return user.authProvider.name === 'azure' || user.authProvider.name === 'gcp';
2727
}
2828

29-
public getToken(user: User): string | null {
29+
public async applyAuthentication(user: User, opts: request.Options | https.RequestOptions) {
30+
const token = this.getToken(user);
31+
if (token) {
32+
opts.headers!.Authorization = `Bearer ${token}`;
33+
}
34+
}
35+
36+
private getToken(user: User): string | null {
3037
const config = user.authProvider.config;
3138
if (this.isExpired(config)) {
3239
this.updateAccessToken(config);
3340
}
34-
return 'Bearer ' + config['access-token'];
35-
}
36-
37-
public async applyAuthentication(user: User, opts: request.Options | https.RequestOptions) {
38-
// pass
41+
return config['access-token'];
3942
}
4043

4144
private isExpired(config: Config) {

src/config.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Authenticator } from './auth';
1212
import { CloudAuth } from './cloud_auth';
1313
import { Cluster, Context, newClusters, newContexts, newUsers, User } from './config_types';
1414
import { ExecAuth } from './exec_auth';
15+
import { FileAuth } from './file_auth';
1516
import { OpenIDConnectAuth } from './oidc_auth';
1617

1718
// fs.existsSync was removed in node 10
@@ -28,6 +29,7 @@ export class KubeConfig {
2829
private static authenticators: Authenticator[] = [
2930
new CloudAuth(),
3031
new ExecAuth(),
32+
new FileAuth(),
3133
new OpenIDConnectAuth(),
3234
];
3335

@@ -190,7 +192,12 @@ export class KubeConfig {
190192
this.users = [
191193
{
192194
name: userName,
193-
token: fs.readFileSync(`${pathPrefix}${Config.SERVICEACCOUNT_TOKEN_PATH}`).toString(),
195+
authProvider: {
196+
name: 'tokenFile',
197+
config: {
198+
tokenFile: `${pathPrefix}${Config.SERVICEACCOUNT_TOKEN_PATH}`,
199+
},
200+
},
194201
},
195202
];
196203
this.contexts = [
@@ -350,21 +357,15 @@ export class KubeConfig {
350357
return elt.isAuthProvider(user);
351358
});
352359

353-
let token: string | null = null;
360+
if (!opts.headers) {
361+
opts.headers = [];
362+
}
354363
if (authenticator) {
355-
token = authenticator.getToken(user);
356364
await authenticator.applyAuthentication(user, opts);
357365
}
358366

359367
if (user.token) {
360-
token = 'Bearer ' + user.token;
361-
}
362-
363-
if (token) {
364-
if (!opts.headers) {
365-
opts.headers = [];
366-
}
367-
opts.headers.Authorization = token;
368+
opts.headers.Authorization = `Bearer ${user.token}`;
368369
}
369370
}
370371

src/config_test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ describe('KubeConfig', () => {
205205
kc.applytoHTTPSOptions(opts);
206206

207207
expect(opts).to.deep.equal({
208+
headers: [],
208209
ca: new Buffer('CADATA2', 'utf-8'),
209210
cert: new Buffer('USER2_CADATA', 'utf-8'),
210211
key: new Buffer('USER2_CKDATA', 'utf-8'),
@@ -221,6 +222,7 @@ describe('KubeConfig', () => {
221222
};
222223
await kc.applyToRequest(opts);
223224
expect(opts).to.deep.equal({
225+
headers: [],
224226
ca: new Buffer('CADATA2', 'utf-8'),
225227
auth: {
226228
username: 'foo',
@@ -1000,7 +1002,9 @@ describe('KubeConfig', () => {
10001002
const user = kc.getCurrentUser();
10011003
expect(user).to.not.be.null;
10021004
if (user) {
1003-
expect(user.token).to.equal(token);
1005+
expect(user.authProvider.config.tokenFile).to.equal(
1006+
'/var/run/secrets/kubernetes.io/serviceaccount/token',
1007+
);
10041008
}
10051009
});
10061010

0 commit comments

Comments
 (0)