/
fabricMsi.ts
77 lines (68 loc) · 2.28 KB
/
fabricMsi.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { AccessToken, GetTokenOptions, RequestPrepareOptions } from "@azure/core-http";
import { MSI } from "./models";
import { credentialLogger } from "../../util/logging";
import { IdentityClient } from "../../client/identityClient";
import { msiGenericGetToken } from "./utils";
import { azureFabricVersion } from "./constants";
const logger = credentialLogger("ManagedIdentityCredential - Fabric MSI");
function expiresInParser(requestBody: any): number {
// Parses a string representation of the seconds since epoch into a number value
return Number(requestBody.expires_on);
}
function prepareRequestOptions(resource: string, clientId?: string): RequestPrepareOptions {
const queryParameters: any = {
resource,
"api-version": azureFabricVersion
};
if (clientId) {
queryParameters.client_id = clientId;
}
return {
url: process.env.IDENTITY_ENDPOINT,
method: "GET",
queryParameters,
headers: {
Accept: "application/json",
Secret: process.env.IDENTITY_HEADER
}
};
}
// This credential can be easily tested by deploying a container to Azure Service Fabric with the Dockerfile:
//
// FROM node:12
// RUN wget https://host.any/path/bash.sh
// CMD ["bash", "bash.sh"]
//
// Where the bash script contains:
//
// curl --insecure $IDENTITY_ENDPOINT'?api-version=2019-07-01-preview&resource=https://vault.azure.net/' -H "Secret: $IDENTITY_HEADER"
//
export const fabricMsi: MSI = {
async isAvailable(): Promise<boolean> {
const env = process.env;
return Boolean(env.IDENTITY_ENDPOINT && env.IDENTITY_HEADER && env.IDENTITY_SERVER_THUMBPRINT);
},
async getToken(
identityClient: IdentityClient,
resource: string,
clientId?: string,
getTokenOptions: GetTokenOptions = {}
): Promise<AccessToken | null> {
logger.info(
[
"Using the endpoint and the secret coming from the environment variables:",
`IDENTITY_ENDPOINT=${process.env.IDENTITY_ENDPOINT},`,
"IDENTITY_HEADER=[REDACTED] and",
"IDENTITY_SERVER_THUMBPRINT=[REDACTED]."
].join(" ")
);
return msiGenericGetToken(
identityClient,
prepareRequestOptions(resource, clientId),
expiresInParser,
getTokenOptions
);
}
};