-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ksAwsBase.pas
393 lines (340 loc) · 14.8 KB
/
ksAwsBase.pas
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
{*******************************************************************************
* *
* ksAwsConst - Amazon Web Service Base Classes *
* *
* https://github.com/gmurt/ksAws *
* *
* Copyright 2020 Graham Murt *
* *
* email: [email protected] *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
*******************************************************************************}
unit ksAwsBase;
interface
uses Classes, ksAwsHttpIntf, Xml.xmldom, Xml.XMLIntf, Xml.XMLDoc;
type
TksAwsRegion = (awsEuCentral1, awsEuNorth1, awsEuSouth1, awsEuWest1, awsEuWest2, awsEuWest3, awsUsEast1, awsUsEast2, awsUsWest1, awsUsWest2);
TksAwsBaseService = class(TInterfacedObject)
private
FAccessKey: string;
FSecretKey: string;
FRegion: TksAwsRegion;
function GetRegionStr: string;
function RegionToStr(ARegion: TksAwsRegion): string;
procedure GetHeaders(ARequestTime: TDateTime; AHost: string; APayload: TStream; AHeaders: TStrings);
function GetUrl(AHost, APath: string; AParams: TStrings): string;
protected
function GetApiVersion: string; virtual;
function GetHost: string; virtual;
function GetHostSubdomain: string; virtual;
function GetPayload(AStr: string): string;
function GenerateUrl(ASubDomain, APath: string): string;
function GetServiceName: string; virtual; abstract;
function GenerateCanonicalRequest(AVerb, AHost, URI: string; APayload: TStream; AHeaders, AQueryValues: TStrings): string; overload;
//function ExecuteHttpNew(AVerb, AAction, AHost, APath: string; AExtraHeaders, AQueryParams: TStrings; AContent: string; const AUseRegion: Boolean = True; const AResponseStream: TStream = nil): IksAwsHttpResponse; overload;
//function ExecuteHttp(AVerb, AAction, AHost, APath: string; AContent: string): IksAwsHttpResponse; overload;
function ExecuteHttp(AVerb, AAction, AHost, APath: string; AExtraHeaders, AQueryParams: TStrings; AContent: string; const AUseRegion: Boolean = True; const AResponseStream: TStream = nil): IksAwsHttpResponse; overload;
function ExecuteHttp(AVerb, AAction, AHost, APath: string; AExtraHeaders, AQueryParams: TStrings; AContent: TStream; const AUseRegion: Boolean = True; const AResponseStream: TStream = nil): IksAwsHttpResponse; overload;
//function ExecuteHttp(AVerb, AAction, AHost, APath, APayload: string; AExtraHeaders, AQueryParams: TStrings; const AUseRegion: Boolean = True; const AResponseStream: TStream = nil): IksAwsHttpResponse; overload;
function ExecuteHttpXml(AVerb, AAction, AHost, APath, APayload: string; AExtraHeaders, AQueryParams: TStrings; const AUseRegion: Boolean = True; const AResponseStream: TStream = nil): IXmlDocument;
public
constructor Create(AAccessKey, ASecretKey: string; ARegion: TksAwsRegion);
property AccessKey: string read FAccessKey;
property SecretKey: string read FSecretKey;
property Region: TksAwsRegion read FRegion;
property RegionStr: string read GetRegionStr;
property ServiceName: string read GetServiceName;
property Host: string read GetHost;
end;
function StringToRegion(ARegionStr: string): TksAwsRegion;
implementation
{$I include.inc}
uses ksAwsConst, ksAwsHash, SysUtils, DateUtils, IdGlobal;
function StringToRegion(ARegionStr: string): TksAwsRegion;
begin
ARegionStr := Trim(ARegionStr.ToLower);
if ARegionStr = C_RGN_EU_CENTRAL_1 then Result := awsEuCentral1;
if ARegionStr = C_RGN_EU_NORTH_1 then Result := awsEuNorth1;
if ARegionStr = C_RGN_EU_SOUTH_1 then Result := awsEuSouth1;
if ARegionStr = C_RGN_EU_WEST_1 then Result := awsEuWest1;
if ARegionStr = C_RGN_EU_WEST_2 then Result := awsEuWest2;
if ARegionStr = C_RGN_EU_WEST_3 then Result := awsEuWest3;
if ARegionStr = C_RGN_US_EAST_1 then Result := awsUsEast1;
if ARegionStr = C_RGN_US_EAST_2 then Result := awsUsEast2;
if ARegionStr = C_RGN_US_WEST_1 then Result := awsUsWest1;
if ARegionStr = C_RGN_US_WEST_2 then Result := awsUsWest2;
end;
{ TksAwsBaseService }
constructor TksAwsBaseService.Create(AAccessKey, ASecretKey: string; ARegion: TksAwsRegion);
begin
inherited Create;
FAccessKey := AAccessKey;
FSecretKey := ASecretKey;
FRegion := ARegion;
end;
function TksAwsBaseService.GetUrl(AHost, APath: string; AParams: TStrings): string;
var
ICount: integer;
begin
Result := '';
if AParams <> nil then
begin
for ICount := 0 to AParams.Count-1 do
begin
if ICount = 0 then
Result := Result + '?'
else
Result := Result + '&';
Result := Result + AParams.Names[ICount]+'='+ParamEncode(AParams.ValueFromIndex[ICount]); end;
end;
Result := C_PROTOCOL+'://'+AHost+APath+Result;
end;
function TksAwsBaseService.RegionToStr(ARegion: TksAwsRegion): string;
begin
case ARegion of
awsEuCentral1: Result := C_RGN_EU_CENTRAL_1;
awsEuNorth1 : Result := C_RGN_EU_NORTH_1;
awsEuSouth1 : Result := C_RGN_EU_SOUTH_1;
awsEuWest1 : Result := C_RGN_EU_WEST_1;
awsEuWest2 : Result := C_RGN_EU_WEST_2;
awsEuWest3 : Result := C_RGN_EU_WEST_3;
awsUsEast1 : Result := C_RGN_US_EAST_1;
awsUsEast2 : Result := C_RGN_US_EAST_2;
awsUsWest1 : Result := C_RGN_US_WEST_1;
awsUsWest2 : Result := C_RGN_US_WEST_2;
end;
end;
function TksAwsBaseService.GenerateUrl(ASubDomain, APath: string): string;
begin
Result := C_PROTOCOL+'://';
if ASubDomain <> '' then Result := Result + UrlEncode(ASubDomain);// TNetEncoding.URL.Encode(ASubDomain)+'.';
Result := Result + ServiceName+'.'+RegionStr+'.'+C_AMAZON_DOMAIN+'/';
if APath <> '' then
Result := Result + UrlEncode(APath);
end;
function TksAwsBaseService.GenerateCanonicalRequest(AVerb, AHost, URI: string; APayload: TStream; AHeaders, AQueryValues: TStrings): string;
var
ICount:
integer;
AHash: string;
begin
AHash := GetHashSHA256Hex(APayload);
Result := UrlEncode(AVerb) +C_LF;
Result := Result + UrlEncode(URI) +C_LF;
if AQueryValues <> nil then
begin
(AQueryValues as TStringList).Sort;
for ICount := 0 to AQueryValues.Count-1 do
begin
Result := Result + AQueryValues.Names[ICount]+'='+ParamEncode(AQueryValues.ValueFromIndex[ICount]);
if ICount < AQueryValues.Count-1 then Result := Result + '&';
end;
end;
Result := Result + C_LF;;
for ICount := 0 to AHeaders.Count-1 do
Result := Result + AHeaders.Names[ICount]+':'+AHeaders.ValueFromIndex[ICount]+C_LF;
Result := Result + C_LF;
for ICount := 0 to AHeaders.Count-1 do
begin
Result := Result + AHeaders.Names[ICount];
if ICount < AHeaders.Count-1 then
Result := Result + ';';
end;
Result := Result + C_LF;
Result := Result + AHash;
end;
function TksAwsBaseService.GetApiVersion: string;
begin
//
end;
procedure TksAwsBaseService.GetHeaders(ARequestTime: TDateTime; AHost: string; APayload: TStream; AHeaders: TStrings);
begin
AHeaders.Values['host'] := Trim(UrlEncode(AHost));
AHeaders.Values['x-amz-content-sha256'] := GetHashSHA256Hex(APayload);
AHeaders.Values['x-amz-date'] := FormatDateTime(C_AMZ_DATE_FORMAT, ARequestTime, TFormatSettings.Create('en-US'));
(AHeaders as TStringList).Sort;
end;
function TksAwsBaseService.GetHost: string;
begin
Result := Format('%s.%s.%s', [GetHostSubdomain, RegionStr, C_AMAZON_DOMAIN]);
end;
function TksAwsBaseService.GetHostSubdomain: string;
begin
Result := GetServiceName;
end;
function TksAwsBaseService.GetPayload(AStr: string): string;
begin
Result := AStr;
Result := StringReplace(AStr, '%REGION%', GetRegionStr, [rfReplaceAll]);
end;
function TksAwsBaseService.GetRegionStr: string;
begin
Result := RegionToStr(FRegion);
end;
function TksAwsBaseService.ExecuteHttpXml(AVerb, AAction, AHost, APath, APayload: string; AExtraHeaders, AQueryParams: TStrings; const AUseRegion: Boolean = True; const AResponseStream: TStream = nil): IXmlDocument;
var
AResponse: IksAwsHttpResponse;
begin
Result := TXMLDocument.Create(nil);
AResponse := ExecuteHttp(AVerb, AAction, AHost, APath, AExtraHeaders, AQueryParams, APayload, AUseRegion, AResponseStream);
Result.LoadFromXML(AResponse.ContentAsString);
end;
{
function TksAwsBaseService.ExecuteHttpNew(AVerb, AAction, AHost, APath: string; AExtraHeaders, AQueryParams: TStrings; AContent: string; const AUseRegion: Boolean = True; const AResponseStream: TStream = nil): IksAwsHttpResponse;
var
ARegion: string;
AHash: string;
AHeaders: TStrings;
AParams: TStrings;
begin
ARegion := RegionStr;
if AUseRegion = False then
ARegion := RegionToStr(awsUsEast1);
AHeaders := TStringList.Create;
AParams := TStringList.Create;
try
AHash := GetHashSHA256Hex(AContent);
finally
AHeaders.Free;
AParams.Free;
end;
end; }
function TksAwsBaseService.ExecuteHttp(AVerb,
AAction,
AHost,
APath: string;
AExtraHeaders,
AQueryParams: TStrings;
AContent: string;
const AUseRegion: Boolean = True;
const AResponseStream: TStream = nil): IksAwsHttpResponse;
var
AStream: TStringStream;
begin
AStream := TStringStream.Create(AContent, TEncoding.UTF8);
try
Result := ExecuteHttp(AVerb,
AAction,
AHost,
APath,
AExtraHeaders,
AQueryParams,
AStream,
AUseRegion,
AResponseStream);
finally
AStream.Free;
end;
end;
function TksAwsBaseService.ExecuteHttp(AVerb, AAction, AHost, APath: string; AExtraHeaders, AQueryParams: TStrings; AContent: TStream; const AUseRegion: Boolean = True; const AResponseStream: TStream = nil): IksAwsHttpResponse;
var
AHttp: IksAwsHttp;
AHeaders: TStrings;
ACanonical: string;
AStringToSign: string;
ASignature: string;
AAmzDate: string;
AAuthHeader: string;
ANowUtc: TDateTime;
AShortDate: string;
AHash: string;
AUrl: string;
ADelimitedHeaders: string;
ARegion: string;
ICount: integer;
AParams: TStrings;
begin
ARegion := RegionStr;
if AUseRegion = False then
ARegion := RegionToStr(awsUsEast1);
AHeaders := TStringList.Create;
AParams := TStringList.Create;
try
if AQueryParams <> nil then
AParams.AddStrings(AQueryParams);
AParams.Values['Action'] := AAction;
AParams.Values['Version'] := GetApiVersion;
if Pos('/', APath) <> 1 then
APath := '/'+APath;
AHash := GetHashSHA256Hex(AContent);
AContent.Position := 0;
if AExtraHeaders <> nil then
AHeaders.AddStrings(AExtraHeaders);
ANowUtc := TTimeZone.Local.ToUniversalTime(Now);
GetHeaders(ANowUtc, AHost, AContent, AHeaders);
ADelimitedHeaders := '';
for ICount := 0 to AHeaders.Count-1 do
begin
ADelimitedHeaders := LowerCase(ADelimitedHeaders+AHeaders.Names[ICount]);
if ICount < AHeaders.Count-1 then
ADelimitedHeaders := ADelimitedHeaders + ';';
end;
AAmzDate := FormatDateTime(C_AMZ_DATE_FORMAT, ANowUtc, TFormatSettings.Create('en-US'));
AShortDate := FormatDateTime(C_SHORT_DATE_FORMAT, ANowUtc, TFormatSettings.Create('en-US'));
ACanonical := GenerateCanonicalRequest(AVerb, AHost, APath, AContent, AHeaders, AParams);
AStringToSign := C_HASH_ALGORITHM +C_LF +
AAmzDate +C_LF+
FormatDateTime(C_SHORT_DATE_FORMAT, ANowUtc) +'/'+ ARegion +'/'+ GetServiceName +'/aws4_request' +C_LF+
GetHashSHA256Hex(ACanonical);
ASignature := GenerateSignature(ANowUtc, AStringToSign, FSecretKey, ARegion, GetServiceName);
AAuthHeader := C_HASH_ALGORITHM+' Credential='+FAccessKey+'/'+
FormatDateTime(C_SHORT_DATE_FORMAT, ANowUtc)+'/'+
ARegion+'/'+
GetServiceName+'/'+
'aws4_request,SignedHeaders='+ADelimitedHeaders+',Signature='+ASignature;
AHeaders.Values['Authorization'] := AAuthHeader;
AUrl := GetUrl(AHost, APath, AParams);
AHttp := CreateAwsHttp;
if AVerb = C_HEAD then Result := AHttp.Head(AUrl, AHeaders);
if AVerb = C_GET then Result := AHttp.Get(AUrl, AHeaders, AResponseStream);
if AVerb = C_PUT then Result := AHttp.Put(AUrl, AContent, AHeaders, AResponseStream);
if AVerb = C_POST then Result := AHttp.Post(AUrl, AContent, AHeaders, AResponseStream);
if AVerb = C_DELETE then Result := AHttp.Delete(AUrl, AHeaders, AResponseStream);
finally
AHeaders.Free;
AParams.Free;
end;
end;
{
function TksAwsBaseService.ExecuteHttp(AVerb, AAction, AHost, APath, APayload: string; AExtraHeaders, AQueryParams: TStrings; const AUseRegion: Boolean = True; const AResponseStream: TStream = nil): IksAwsHttpResponse;
var
AContent: TStringStream;
begin
AContent := TStringStream.Create(APayload);
try
Result := ExecuteHttp(AVerb, AAction, AHost, APath, AExtraHeaders, AQueryParams, AContent, AUseRegion, AResponseStream);
finally
AContent.Free;
end;
end; }{
function TksAwsBaseService.ExecuteHttp(AVerb, AAction, AHost, APath, AContent: string): IksAwsHttpResponse;
var
AStrings: TStrings;
AStream: TMemoryStream;
begin
AStrings := TStringList.Create;
AStream := TMemoryStream.Create;
try
AStrings.Text := Trim(AContent);
AStrings.SaveToStream(AStream);
AStream.Position := 0;
Result := ExecuteHttp(AVerb, AAction, AHost, APath, nil, nil, AStream);
finally
AStream.Free;
AStrings.Free;
end;
end;
}
end.