Repository navigation
Expand file tree
/
Copy pathroot.zig
More file actions
180 lines (162 loc) · 6.14 KB
/
Copy pathroot.zig
File metadata and controls
180 lines (162 loc) · 6.14 KB
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
//! The guide's examples, compiled and run with the module's own test suite.
//! A guide page cannot show code that does not build.
const std = @import("std");
const testing = std.testing;
const sigv4 = @import("sigv4");
test "the request the signature covers" {
var headers = [_]sigv4.canonical.Header{
.{ .name = "host", .value = "example.amazonaws.com" },
.{ .name = "x-amz-date", .value = "20150830T123600Z" },
};
const request: sigv4.canonical.Request = .{
.method = "GET",
.path = "/",
.headers = &headers,
.payload_hash = sigv4.empty_payload_hash,
};
var buffer: [1024]u8 = undefined;
const canonical = try sigv4.canonical.writeRequest(&buffer, request);
try testing.expectEqualStrings(
"GET\n/\n\nhost:example.amazonaws.com\nx-amz-date:20150830T123600Z\n\n" ++
"host;x-amz-date\n" ++ sigv4.empty_payload_hash,
canonical,
);
}
test "the path mode decides the canonical URI" {
var service: [256]u8 = undefined;
var unnormalized: [256]u8 = undefined;
var s3: [256]u8 = undefined;
// A wire path arrives already escaped. A service escapes it again.
try testing.expectEqualStrings(
"/a%2525b",
try sigv4.canonical.writeUri(&service, "/a%25b", .service),
);
// S3 signs the wire path as it is.
try testing.expectEqualStrings(
"/a%25b",
try sigv4.canonical.writeUri(&s3, "/a%25b", .s3),
);
// Only the service mode removes dot segments.
try testing.expectEqualStrings(
"/b",
try sigv4.canonical.writeUri(&service, "/a/../b", .service),
);
try testing.expectEqualStrings(
"/a/../b",
try sigv4.canonical.writeUri(&unnormalized, "/a/../b", .unnormalized),
);
}
test "the scope takes its date from the timestamp" {
const scope = try sigv4.signing.Scope.init("20150830T123600Z", "us-east-1", "iam");
var buffer: [128]u8 = undefined;
try testing.expectEqualStrings(
"20150830/us-east-1/iam/aws4_request",
try sigv4.signing.writeScope(&buffer, scope),
);
try testing.expectError(
error.InvalidTimestamp,
sigv4.signing.Scope.init("2015-08-30T12:36:00Z", "us-east-1", "iam"),
);
}
test "the signing key is the documented vector" {
const scope = try sigv4.signing.Scope.init("20150830T123600Z", "us-east-1", "iam");
const key = sigv4.signing.signingKey(
"wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
scope,
);
try testing.expectEqualStrings(
"c4afb1cc5771d871763a393e44b703571b55cc28424d1a5e86da6ed3c154a4b9",
&std.fmt.bytesToHex(key, .lower),
);
const mac = sigv4.signing.signature(key, "the string to sign");
try testing.expectEqual(@as(usize, 32), mac.len);
}
test "write the Authorization field" {
var headers = [_]sigv4.canonical.Header{
.{ .name = "host", .value = "example.amazonaws.com" },
.{ .name = "x-amz-content-sha256", .value = sigv4.empty_payload_hash },
.{ .name = "x-amz-date", .value = "20260826T000000Z" },
};
var field: [1024]u8 = undefined;
var creq: [2048]u8 = undefined;
const value = try sigv4.signing.writeAuthorization(&field, &creq, .{
.method = "GET",
.path = "/",
.path_mode = .s3,
.headers = &headers,
.payload_hash = sigv4.empty_payload_hash,
}, .{
.access_key = "AKIDEXAMPLE",
.secret_key = "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
.region = "us-east-1",
.service = "s3",
.timestamp = "20260826T000000Z",
});
try testing.expectEqualStrings(
"AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20260826/us-east-1/s3/aws4_request," ++
" SignedHeaders=host;x-amz-content-sha256;x-amz-date," ++
" Signature=42e3431ea83ebfba8aa2488a2fa9a07afae23a0fc658721ef9b2f105a0e9ab34",
value,
);
}
test "a request the signature cannot cover is refused" {
var without_host = [_]sigv4.canonical.Header{
.{ .name = "x-amz-date", .value = "20260826T000000Z" },
};
var buffer: [512]u8 = undefined;
try testing.expectError(error.MissingHost, sigv4.canonical.writeRequest(&buffer, .{
.method = "GET",
.path = "/",
.headers = &without_host,
.payload_hash = sigv4.empty_payload_hash,
}));
var stale = [_]sigv4.canonical.Header{
.{ .name = "host", .value = "example.amazonaws.com" },
.{ .name = "x-amz-date", .value = "20260826T000001Z" },
};
var field: [512]u8 = undefined;
var creq: [1024]u8 = undefined;
try testing.expectError(
error.TimestampMismatch,
sigv4.signing.writeAuthorization(&field, &creq, .{
.method = "GET",
.path = "/",
.headers = &stale,
.payload_hash = sigv4.empty_payload_hash,
}, .{
.access_key = "AKIDEXAMPLE",
.secret_key = "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
.region = "us-east-1",
.service = "service",
.timestamp = "20260826T000000Z",
}),
);
}
test "write the query string of a presigned URL" {
var headers = [_]sigv4.canonical.Header{
.{ .name = "host", .value = "example.amazonaws.com" },
};
var buffer: [1024]u8 = undefined;
const query = try sigv4.signing.presign(&buffer, .{
.method = "GET",
.path = "/",
.headers = &headers,
.payload_hash = sigv4.empty_payload_hash,
}, .{
.access_key = "AKIDEXAMPLE",
.secret_key = "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
.region = "us-east-1",
.service = "service",
.timestamp = "20150830T123600Z",
}, .{ .expires_seconds = 3600 });
// The AWS signing suite's get-vanilla query fixture, byte for byte.
try testing.expectEqualStrings(
"X-Amz-Algorithm=AWS4-HMAC-SHA256" ++
"&X-Amz-Credential=AKIDEXAMPLE%2F20150830%2Fus-east-1%2Fservice%2Faws4_request" ++
"&X-Amz-Date=20150830T123600Z" ++
"&X-Amz-SignedHeaders=host" ++
"&X-Amz-Expires=3600" ++
"&X-Amz-Signature=e93c787ed7f371d5c6b165c1b38ede9550f4dce4144713e844b25b7192d3865d",
query,
);
}