/
rpRegistrationPolicy.ts
198 lines (182 loc) · 6.9 KB
/
rpRegistrationPolicy.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { HttpOperationResponse } from "../httpOperationResponse";
import * as utils from "../util/utils";
import { WebResourceLike } from "../webResource";
import {
BaseRequestPolicy,
RequestPolicy,
RequestPolicyFactory,
RequestPolicyOptions
} from "./requestPolicy";
export function rpRegistrationPolicy(retryTimeout = 30): RequestPolicyFactory {
return {
create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
return new RPRegistrationPolicy(nextPolicy, options, retryTimeout);
}
};
}
export class RPRegistrationPolicy extends BaseRequestPolicy {
constructor(
nextPolicy: RequestPolicy,
options: RequestPolicyOptions,
readonly _retryTimeout = 30
) {
super(nextPolicy, options);
}
public sendRequest(request: WebResourceLike): Promise<HttpOperationResponse> {
return this._nextPolicy
.sendRequest(request.clone())
.then((response) => registerIfNeeded(this, request, response));
}
}
function registerIfNeeded(
policy: RPRegistrationPolicy,
request: WebResourceLike,
response: HttpOperationResponse
): Promise<HttpOperationResponse> {
if (response.status === 409) {
const rpName = checkRPNotRegisteredError(response.bodyAsText as string);
if (rpName) {
const urlPrefix = extractSubscriptionUrl(request.url);
return (
registerRP(policy, urlPrefix, rpName, request)
// Autoregistration of ${provider} failed for some reason. We will not return this error
// instead will return the initial response with 409 status code back to the user.
// do nothing here as we are returning the original response at the end of this method.
.catch(() => false)
.then((registrationStatus) => {
if (registrationStatus) {
// Retry the original request. We have to change the x-ms-client-request-id
// otherwise Azure endpoint will return the initial 409 (cached) response.
request.headers.set("x-ms-client-request-id", utils.generateUuid());
return policy._nextPolicy.sendRequest(request.clone());
}
return response;
})
);
}
}
return Promise.resolve(response);
}
/**
* Reuses the headers of the original request and url (if specified).
* @param originalRequest - The original request
* @param reuseUrlToo - Should the url from the original request be reused as well. Default false.
* @returns A new request object with desired headers.
*/
function getRequestEssentials(
originalRequest: WebResourceLike,
reuseUrlToo = false
): WebResourceLike {
const reqOptions: WebResourceLike = originalRequest.clone();
if (reuseUrlToo) {
reqOptions.url = originalRequest.url;
}
// We have to change the x-ms-client-request-id otherwise Azure endpoint
// will return the initial 409 (cached) response.
reqOptions.headers.set("x-ms-client-request-id", utils.generateUuid());
// Set content-type to application/json
reqOptions.headers.set("Content-Type", "application/json; charset=utf-8");
return reqOptions;
}
/**
* Validates the error code and message associated with 409 response status code. If it matches to that of
* RP not registered then it returns the name of the RP else returns undefined.
* @param body - The response body received after making the original request.
* @returns The name of the RP if condition is satisfied else undefined.
*/
function checkRPNotRegisteredError(body: string): string {
let result, responseBody;
if (body) {
try {
responseBody = JSON.parse(body);
} catch (err) {
// do nothing;
}
if (
responseBody &&
responseBody.error &&
responseBody.error.message &&
responseBody.error.code &&
responseBody.error.code === "MissingSubscriptionRegistration"
) {
const matchRes = responseBody.error.message.match(/.*'(.*)'/i);
if (matchRes) {
result = matchRes.pop();
}
}
}
return result;
}
/**
* Extracts the first part of the URL, just after subscription:
* https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/
* @param url - The original request url
* @returns The url prefix as explained above.
*/
function extractSubscriptionUrl(url: string): string {
let result;
const matchRes = url.match(/.*\/subscriptions\/[a-f0-9-]+\//gi);
if (matchRes && matchRes[0]) {
result = matchRes[0];
} else {
throw new Error(`Unable to extract subscriptionId from the given url - ${url}.`);
}
return result;
}
/**
* Registers the given provider.
* @param policy - The RPRegistrationPolicy this function is being called against.
* @param urlPrefix - https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/
* @param provider - The provider name to be registered.
* @param originalRequest - The original request sent by the user that returned a 409 response
* with a message that the provider is not registered.
* @param callback - The callback that handles the RP registration
*/
function registerRP(
policy: RPRegistrationPolicy,
urlPrefix: string,
provider: string,
originalRequest: WebResourceLike
): Promise<boolean> {
const postUrl = `${urlPrefix}providers/${provider}/register?api-version=2016-02-01`;
const getUrl = `${urlPrefix}providers/${provider}?api-version=2016-02-01`;
const reqOptions = getRequestEssentials(originalRequest);
reqOptions.method = "POST";
reqOptions.url = postUrl;
return policy._nextPolicy.sendRequest(reqOptions).then((response) => {
if (response.status !== 200) {
throw new Error(`Autoregistration of ${provider} failed. Please try registering manually.`);
}
return getRegistrationStatus(policy, getUrl, originalRequest);
});
}
/**
* Polls the registration status of the provider that was registered. Polling happens at an interval of 30 seconds.
* Polling will happen till the registrationState property of the response body is "Registered".
* @param policy - The RPRegistrationPolicy this function is being called against.
* @param url - The request url for polling
* @param originalRequest - The original request sent by the user that returned a 409 response
* with a message that the provider is not registered.
* @returns True if RP Registration is successful.
*/
function getRegistrationStatus(
policy: RPRegistrationPolicy,
url: string,
originalRequest: WebResourceLike
): Promise<boolean> {
const reqOptions: any = getRequestEssentials(originalRequest);
reqOptions.url = url;
reqOptions.method = "GET";
return policy._nextPolicy.sendRequest(reqOptions).then((res) => {
const obj = res.parsedBody as any;
if (res.parsedBody && obj.registrationState && obj.registrationState === "Registered") {
return true;
} else {
return utils
.delay(policy._retryTimeout * 1000)
.then(() => getRegistrationStatus(policy, url, originalRequest));
}
});
}