-
Notifications
You must be signed in to change notification settings - Fork 742
/
security-processor.ts
220 lines (196 loc) · 7.23 KB
/
security-processor.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { AADTokenSecurityScheme, AzureKeySecurityScheme, Security, SecurityScheme } from "@autorest/codemodel";
import { Session } from "@autorest/extension-base";
import * as oai3 from "@azure-tools/openapi";
import { dereference, ParameterLocation, Refable } from "@azure-tools/openapi";
import { Interpretations } from "./interpretations";
import { arrayify } from "./utils";
export enum KnownSecurityScheme {
AADToken = "AADToken",
AzureKey = "AzureKey",
Anonymous = "Anonymous",
}
const KnownSecuritySchemeList = Object.values(KnownSecurityScheme);
const ArmDefaultScope = "https://management.azure.com/.default";
const DefaultHeaderName = "Authorization";
export interface SecurityConfiguration {
azureArm: boolean;
security: KnownSecurityScheme[];
scopes: string[];
headerName: string;
}
/**
* Body processing functions
*/
export class SecurityProcessor {
private securityConfig!: SecurityConfiguration;
public constructor(private session: Session<oai3.Model>, private interpret: Interpretations) {}
public async init() {
this.securityConfig = await this.getSecurityConfig();
return this;
}
/**
* Process the security definition defined in OpenAPI
*/
public process(openApiModel: oai3.Model): Security {
const securityFromSpec = this.getSecurityFromOpenAPISpec(openApiModel);
const securityFromConfig = this.getSecurityFromConfig();
const securityFromArm = this.getSecurityForAzureArm();
if (securityFromSpec && (securityFromConfig || securityFromArm)) {
this.session.warning(
"OpenAPI spec has a security definition but autorest security config is defined. Security config from autorest will be used.",
["SecurityDefinedSpecAndConfig"],
);
}
if (securityFromArm && securityFromConfig) {
this.session.warning("OpenAPI spec has both 'security' and 'azure-arm' defined. Ignoring 'security'.", [
"SecurityAndAzureArmDefined",
]);
}
return securityFromArm ?? securityFromConfig ?? securityFromSpec ?? new Security(false);
}
private getSecurityForAzureArm(): Security | undefined {
if (!this.securityConfig.azureArm) {
return undefined;
}
return new Security(true, {
schemes: [
new AADTokenSecurityScheme({
scopes: this.securityConfig.scopes ?? [ArmDefaultScope],
}),
],
});
}
/**
* Build the security object from the autorest configuration
*/
private getSecurityFromConfig(): Security | undefined {
const schemeList = this.securityConfig.security.map((x) => this.getSecuritySchemeFromConfig(x));
if (schemeList.length === 0) {
return undefined;
}
const schemes = [];
let authenticationRequired = true;
for (const scheme of schemeList) {
if (scheme === undefined) {
authenticationRequired = false;
} else {
schemes.push(scheme);
}
}
return new Security(authenticationRequired, { schemes });
}
/**
* @param name Name of the security scheme
* @returns CodeModel security scheme with given name, undefined if this is anonymous security and throw an error if unknown.
*/
private getSecuritySchemeFromConfig(name: string): SecurityScheme | undefined {
switch (name) {
case KnownSecurityScheme.AADToken:
return new AADTokenSecurityScheme({
scopes: this.securityConfig.scopes,
});
case KnownSecurityScheme.AzureKey:
return new AzureKeySecurityScheme({
headerName: this.securityConfig.headerName,
});
case KnownSecurityScheme.Anonymous:
return undefined;
default:
throw new Error(`Unexpected security scheme '${name}'. Only known schemes are ${KnownSecuritySchemeList}`);
}
}
/**
* Build the security object from the OpenAPI spec.
*/
private getSecurityFromOpenAPISpec(openApiModel: oai3.Model): Security | undefined {
const oai3Schemes = Object.values(openApiModel.components?.securitySchemes ?? {});
const security = openApiModel.security;
if (!security || oai3Schemes.length === 0) {
return undefined;
}
const schemeMap = this.resolveOpenAPI3SecuritySchemes(oai3Schemes);
const schemes: SecurityScheme[] = [];
let authenticationRequired = true;
for (const oai3SecurityRequirement of security) {
const names = Object.keys(oai3SecurityRequirement);
if (names.length > 1) {
this.session.warning(
[
`Security defines multiple requirements at the same time which is not supported(${names.join(",")}).`,
`Did you meant to have multiple authentication options instead? Define each option seperately in your spec:`,
JSON.stringify(
names.map((x) => ({ [x]: oai3SecurityRequirement[x] })),
null,
2,
),
].join("\n"),
["MultipleSecurityLayerUnsupported"],
);
continue;
}
if (names.length === 0) {
authenticationRequired = false;
} else {
const name = names[0];
const scheme = schemeMap.get(name);
if (!scheme) {
throw new Error(`Couldn't find a scheme defined in the securitySchemes with name: ${name}`);
}
const processedScheme = this.processSecurityScheme(name, oai3SecurityRequirement[name], scheme);
if (processedScheme !== undefined) {
schemes.push(processedScheme);
} else {
this.session.warning(
`Security scheme ${name} is unknown and will not be processed. Only supported types are ${KnownSecuritySchemeList}`,
["UnkownSecurityScheme"],
);
}
}
}
return new Security(authenticationRequired, {
schemes,
});
}
private resolveOpenAPI3SecuritySchemes(
oai3Schemes: Refable<oai3.SecurityScheme>[],
): Map<string, oai3.SecurityScheme> {
const map = new Map<string, oai3.SecurityScheme>();
for (const value of oai3Schemes) {
const scheme = dereference(this.session.model, value);
const name = this.interpret.getName("", scheme.instance);
map.set(name, scheme.instance);
}
return map;
}
private processSecurityScheme(
name: string,
scopes: string[],
scheme: oai3.SecurityScheme,
): SecurityScheme | undefined {
if (name === KnownSecurityScheme.AADToken) {
return new AADTokenSecurityScheme({
scopes: scopes,
});
} else if (name === KnownSecurityScheme.AzureKey) {
if (scheme.type !== oai3.SecurityType.ApiKey) {
throw new Error(`AzureKey security scheme should be of type 'apiKey' but was '${scheme.type}'`);
}
if (scheme.in !== ParameterLocation.Header) {
throw new Error(`AzureKey security scheme should be of in 'header' but was '${scheme.in}'`);
}
return new AzureKeySecurityScheme({
headerName: scheme.name,
});
} else {
return undefined;
}
}
private async getSecurityConfig(): Promise<SecurityConfiguration> {
return {
azureArm: await this.session.getValue("azure-arm", false),
security: arrayify(await this.session.getValue("security", [])),
scopes: arrayify(await this.session.getValue("security-scopes", [ArmDefaultScope])),
headerName: await this.session.getValue("security-header-name", DefaultHeaderName),
};
}
}