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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright 2017 LambdaStack All rights reserved.
//
// 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.
//

#![allow(unused_variables)]
use aws::common::params::{Params, ServiceParams};
use aws::common::xmlutil::*;
use aws::common::common::*;
use aws::s3::writeparse::*;
use aws::s3::object::*;
use aws::s3::bucket::*;
use aws::s3::grant::*;
use aws::s3::header::*;

pub type ObjectCannedACL = String;

pub type Permission = String;

/// Canned ACL for S3
//#[derive(Debug)]
#[derive(Debug, RustcDecodable, RustcEncodable)]
pub enum CannedAcl {
    Private,
    PublicRead,
    PublicReadWrite,
    AuthenticatedRead,
    BucketOwnerRead,
    BucketOwnerFullControl,
}

/// Parse `GetBucketAclRequest` from XML
pub struct GetBucketAclRequestParser;

/// Write `GetBucketAclRequest` contents to a `SignedRequest`
pub struct GetBucketAclRequestWriter;

/// Parse `PutObjectAclRequest` from XML
pub struct PutObjectAclRequestParser;

// Parse `GetBucketAclOutput` from XML
// pub struct GetBucketAclOutputParser;
//
// Write `GetBucketAclOutput` contents to a `SignedRequest`
// pub struct GetBucketAclOutputWriter;
//

/// Write `PutObjectAclRequest` contents to a `SignedRequest`
pub struct PutObjectAclRequestWriter;

/// Parse `AccessControlPolicy` from XML
pub struct AccessControlPolicyParser;

/// Write `AccessControlPolicy` contents to a `SignedRequest`
pub struct AccessControlPolicyWriter;

/// Parse `AccessControlList` from XML
pub struct AccessControlListParser;

/// Write `AccessControlList` contents to a `SignedRequest`
pub struct AccessControlListWriter;

/// Parse `ObjectCannedACL` from XML
pub struct ObjectCannedACLParser;

/// Write `ObjectCannedACL` contents to a `SignedRequest`
pub struct ObjectCannedACLWriter;

/// Parse `Permission` from XML
pub struct PermissionParser;

/// Write `Permission` contents to a `SignedRequest`
pub struct PermissionWriter;

/// Parse `GetObjectAclRequest` from XML
pub struct GetObjectAclRequestParser;

/// Write `GetBucketAclRequest` to XML
pub struct GetObjectAclRequestWriter;

/// Parse `GetObjectAclOutput` from XML
pub struct GetObjectAclOutputParser;

/// Write `GetObjectAclOutput` contents to a `SignedRequest`
pub struct GetObjectAclOutputWriter;

/// Parse `PutObjectAclOutput` from XML
pub struct PutObjectAclOutputParser;

/// Write `PutObjectAclOutput` contents to a `SignedRequest`
pub struct PutObjectAclOutputWriter;

//#[derive(Debug, Default)]
#[derive(Debug, Default, RustcDecodable, RustcEncodable)]
pub struct AccessControlPolicy {
    pub owner: Owner,
    pub acl: AccessControlList,
}

//#[derive(Debug, Default)]
#[derive(Debug, Default, RustcDecodable, RustcEncodable)]
pub struct AccessControlList {
    pub grants: Grants,
}

// NOTE: May remove this one...
//
// #[derive(Debug, Default)]
// pub struct GetBucketAclOutput {
// pub owner: Owner,
// pub acl: AccessControlList,
// pub grants: Grants,
// }
//


//#[derive(Debug, Default)]
#[derive(Debug, Default, RustcDecodable, RustcEncodable)]
pub struct GetBucketAclRequest {
    pub bucket: BucketName,
}

//#[derive(Debug, Default)]
#[derive(Debug, Default, RustcDecodable, RustcEncodable)]
pub struct PutBucketAclRequest {
    /// Allows grantee the read, write, read ACP, and write ACP permissions on the
    /// bucket.
    pub grant_full_control: Option<GrantFullControl>,
    /// Allows grantee to write the ACL for the applicable bucket.
    pub grant_write_acp: Option<GrantWriteACP>,
    pub content_md5: Option<ContentMD5>,
    pub bucket: BucketName,
    /// The canned ACL to apply to the bucket.
    pub acl: Option<CannedAcl>,
    pub access_control_policy: Option<AccessControlPolicy>,
    /// Allows grantee to create, overwrite, and delete any object in the bucket.
    pub grant_write: Option<GrantWrite>,
    /// Allows grantee to list the objects in the bucket.
    pub grant_read: Option<GrantRead>,
    /// Allows grantee to read the bucket ACL.
    pub grant_read_acp: Option<GrantReadACP>,
}

//#[derive(Debug, Default)]
#[derive(Debug, Default, RustcDecodable, RustcEncodable)]
pub struct GetObjectAclRequest {
    /// VersionId used to reference a specific version of the object.
    pub version_id: Option<ObjectVersionId>,
    pub bucket: BucketName,
    pub request_payer: Option<RequestPayer>,
    pub key: ObjectKey,
}

//#[derive(Debug, Default)]
#[derive(Debug, Default, RustcDecodable, RustcEncodable)]
pub struct PutObjectAclRequest {
    /// Allows grantee the read, write, read ACP, and write ACP permissions on the
    /// bucket.
    pub grant_full_control: Option<GrantFullControl>,
    /// Allows grantee to write the ACL for the applicable bucket.
    pub grant_write_acp: Option<GrantWriteACP>,
    pub key: ObjectKey,
    pub request_payer: Option<RequestPayer>,
    pub content_md5: Option<ContentMD5>,
    pub bucket: BucketName,
    /// The canned ACL to apply to the object.
    pub acl: Option<CannedAcl>,
    pub access_control_policy: Option<AccessControlPolicy>,
    /// Allows grantee to create, overwrite, and delete any object in the bucket.
    pub grant_write: Option<GrantWrite>,
    /// Allows grantee to list the objects in the bucket.
    pub grant_read: Option<GrantRead>,
    /// Allows grantee to read the bucket ACL.
    pub grant_read_acp: Option<GrantReadACP>,
}

//#[derive(Debug, Default)]
#[derive(Debug, Default, RustcDecodable, RustcEncodable)]
pub struct GetObjectAclOutput {
    pub owner: Owner,
    /// A list of grants.
    pub grants: Grants,
    pub request_charged: RequestCharged,
}

//#[derive(Debug, Default)]
#[derive(Debug, Default, RustcDecodable, RustcEncodable)]
pub struct PutObjectAclOutput {
    pub request_charged: RequestCharged,
}

// Impls below...

impl PutObjectAclRequestParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<PutObjectAclRequest, XmlParseError> {
        try!(start_element(tag_name, stack));
        let mut obj = PutObjectAclRequest::default();
        loop {
            let current_name = try!(peek_at_name(stack));
            if current_name == "x-amz-grant-full-control" {
                obj.grant_full_control = Some(try!(GrantFullControlParser::parse_xml("x-amz-grant-full-control",
                                                                                     stack)));
                continue;
            }
            if current_name == "x-amz-grant-write-acp" {
                obj.grant_write_acp = Some(try!(GrantWriteACPParser::parse_xml("x-amz-grant-write-acp", stack)));
                continue;
            }
            if current_name == "Key" {
                obj.key = try!(ObjectKeyParser::parse_xml("Key", stack));
                continue;
            }
            if current_name == "x-amz-request-payer" {
                obj.request_payer = Some(try!(RequestPayerParser::parse_xml("x-amz-request-payer", stack)));
                continue;
            }
            if current_name == "Content-MD5" {
                obj.content_md5 = Some(try!(ContentMD5Parser::parse_xml("Content-MD5", stack)));
                continue;
            }
            if current_name == "Bucket" {
                obj.bucket = try!(BucketNameParser::parse_xml("Bucket", stack));
                continue;
            }
            if current_name == "AccessControlPolicy" {
                obj.access_control_policy = Some(try!(AccessControlPolicyParser::parse_xml("AccessControlPolicy",
                                                                                           stack)));
                continue;
            }
            if current_name == "x-amz-grant-write" {
                obj.grant_write = Some(try!(GrantWriteParser::parse_xml("x-amz-grant-write", stack)));
                continue;
            }
            if current_name == "x-amz-grant-read" {
                obj.grant_read = Some(try!(GrantReadParser::parse_xml("x-amz-grant-read", stack)));
                continue;
            }
            if current_name == "x-amz-grant-read-acp" {
                obj.grant_read_acp = Some(try!(GrantReadACPParser::parse_xml("x-amz-grant-read-acp", stack)));
                continue;
            }
            break;
        }
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}

impl PutObjectAclRequestWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &PutObjectAclRequest) {
        let mut prefix = name.to_string();
        if prefix != "" {
            prefix.push_str(".");
        }
        if let Some(ref obj) = obj.grant_full_control {
            GrantFullControlWriter::write_params(params,
                                                 &(prefix.to_string() + "x-amz-grant-full-control"),
                                                 obj);
        }
        if let Some(ref obj) = obj.grant_write_acp {
            GrantWriteACPWriter::write_params(params, &(prefix.to_string() + "x-amz-grant-write-acp"), obj);
        }
        ObjectKeyWriter::write_params(params, &(prefix.to_string() + "Key"), &obj.key);
        if let Some(ref obj) = obj.request_payer {
            RequestPayerWriter::write_params(params, &(prefix.to_string() + "x-amz-request-payer"), obj);
        }
        if let Some(ref obj) = obj.content_md5 {
            ContentMD5Writer::write_params(params, &(prefix.to_string() + "Content-MD5"), obj);
        }
        BucketNameWriter::write_params(params, &(prefix.to_string() + "Bucket"), &obj.bucket);
        if let Some(ref obj) = obj.access_control_policy {
            AccessControlPolicyWriter::write_params(params, &(prefix.to_string() + "AccessControlPolicy"), obj);
        }
        if let Some(ref obj) = obj.grant_write {
            GrantWriteWriter::write_params(params, &(prefix.to_string() + "x-amz-grant-write"), obj);
        }
        if let Some(ref obj) = obj.grant_read {
            GrantReadWriter::write_params(params, &(prefix.to_string() + "x-amz-grant-read"), obj);
        }
        if let Some(ref obj) = obj.grant_read_acp {
            GrantReadACPWriter::write_params(params, &(prefix.to_string() + "x-amz-grant-read-acp"), obj);
        }
    }
}

impl GetBucketAclRequestParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<GetBucketAclRequest, XmlParseError> {
        try!(start_element(tag_name, stack));
        let mut obj = GetBucketAclRequest::default();
        loop {
            let current_name = try!(peek_at_name(stack));
            if current_name == "Bucket" {
                obj.bucket = try!(BucketNameParser::parse_xml("Bucket", stack));
                continue;
            }
            break;
        }
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}

impl GetBucketAclRequestWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &GetBucketAclRequest) {
        let mut prefix = name.to_string();
        if prefix != "" {
            prefix.push_str(".");
        }
        BucketNameWriter::write_params(params, &(prefix.to_string() + "Bucket"), &obj.bucket);
    }
}
// impl GetBucketAclOutputParser {
// pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<GetBucketAclOutput, XmlParseError> {
// try!(start_element(tag_name, stack));
// let mut obj = GetBucketAclOutput::default();
// loop {
// let current_name = try!(peek_at_name(stack));
// if current_name == "Owner" {
// obj.owner = try!(OwnerParser::parse_xml("Owner", stack));
// continue;
// }
// if current_name == "Grant" {
// obj.grants = try!(GrantsParser::parse_xml("Grant", stack));
// continue;
// }
// break;
// }
// try!(end_element(tag_name, stack));
// Ok(obj)
// }
// }
//
// impl GetBucketAclOutputWriter {
// pub fn write_params(params: &mut Params, name: &str, obj: &GetBucketAclOutput) {
// let mut prefix = name.to_string();
// if prefix != "" { prefix.push_str("."); }
// OwnerWriter::write_params(params, &(prefix.to_string() + "Owner"), &obj.owner);
// GrantsWriter::write_params(params, &(prefix.to_string() + "Grant"), &obj.grants);
// }
// }
//

impl AccessControlPolicyParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<AccessControlPolicy, XmlParseError> {
        try!(start_element(tag_name, stack));
        let mut obj = AccessControlPolicy::default();
        loop {
            let current_name = try!(peek_at_name(stack));
            if current_name == "Owner" {
                obj.owner = try!(OwnerParser::parse_xml("Owner", stack));
                continue;
            }
            if current_name == "AccessControlList" {
                obj.acl = try!(AccessControlListParser::parse_xml("AccessControlList", stack));
                continue;
            }
            break;
        }
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}

impl AccessControlPolicyWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &AccessControlPolicy) {
        let mut prefix = name.to_string();
        if prefix != "" {
            prefix.push_str(".");
        }
        OwnerWriter::write_params(params, &(prefix.to_string() + "Owner"), &obj.owner);
        AccessControlListWriter::write_params(params,
                                              &(prefix.to_string() + "AccessControlList"),
                                              &obj.acl);
    }
}

impl AccessControlListParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<AccessControlList, XmlParseError> {
        try!(start_element(tag_name, stack));
        let mut obj = AccessControlList::default();
        loop {
            let current_name = try!(peek_at_name(stack));
            if current_name == "Grant" {
                obj.grants = try!(GrantsParser::parse_xml("Grant", stack));
                continue;
            }
            break;
        }
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}

impl AccessControlListWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &AccessControlList) {
        let mut prefix = name.to_string();
        if prefix != "" {
            prefix.push_str(".");
        }
        GrantsWriter::write_params(params, &(prefix.to_string() + "Grant"), &obj.grants);
    }
}

impl ObjectCannedACLParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<ObjectCannedACL, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}

impl ObjectCannedACLWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &ObjectCannedACL) {
        params.put(name, obj);
    }
}

impl PermissionParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<Permission, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}

impl PermissionWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &Permission) {
        params.put(name, obj);
    }
}

/// Maps canned acl to AWS format.
pub fn canned_acl_in_aws_format(canned_acl: &CannedAcl) -> String {
    match *canned_acl {
        CannedAcl::Private => "private".to_string(),
        CannedAcl::PublicRead => "public-read".to_string(),
        CannedAcl::PublicReadWrite => "public-read-write".to_string(),
        CannedAcl::AuthenticatedRead => "authenticated-read".to_string(),
        CannedAcl::BucketOwnerRead => "bucket-owner-read".to_string(),
        CannedAcl::BucketOwnerFullControl => "bucket-owner-full-control".to_string(),
    }
}

impl GetObjectAclRequestParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<GetObjectAclRequest, XmlParseError> {
        try!(start_element(tag_name, stack));
        let mut obj = GetObjectAclRequest::default();
        loop {
            let current_name = try!(peek_at_name(stack));
            if current_name == "versionId" {
                obj.version_id = Some(try!(ObjectVersionIdParser::parse_xml("versionId", stack)));
                continue;
            }
            if current_name == "Bucket" {
                obj.bucket = try!(BucketNameParser::parse_xml("Bucket", stack));
                continue;
            }
            if current_name == "x-amz-request-payer" {
                obj.request_payer = Some(try!(RequestPayerParser::parse_xml("x-amz-request-payer", stack)));
                continue;
            }
            if current_name == "Key" {
                obj.key = try!(ObjectKeyParser::parse_xml("Key", stack));
                continue;
            }
            break;
        }
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}

impl GetObjectAclRequestWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &GetObjectAclRequest) {
        let mut prefix = name.to_string();
        if prefix != "" { prefix.push_str("."); }
        if let Some(ref obj) = obj.version_id {
            ObjectVersionIdWriter::write_params(params, &(prefix.to_string() + "versionId"), obj);
        }
        BucketNameWriter::write_params(params, &(prefix.to_string() + "Bucket"), &obj.bucket);
        if let Some(ref obj) = obj.request_payer {
            RequestPayerWriter::write_params(params, &(prefix.to_string() + "x-amz-request-payer"), obj);
        }
        ObjectKeyWriter::write_params(params, &(prefix.to_string() + "Key"), &obj.key);
    }
}

impl GetObjectAclOutputParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<GetObjectAclOutput, XmlParseError> {
        try!(start_element(tag_name, stack));
        let mut obj = GetObjectAclOutput::default();
        loop {
            let current_name = try!(peek_at_name(stack));
            if current_name == "Owner" {
                obj.owner = try!(OwnerParser::parse_xml("Owner", stack));
                continue;
            }
            if current_name == "Grant" {
                obj.grants = try!(GrantsParser::parse_xml("Grant", stack));
                continue;
            }
            if current_name == "x-amz-request-charged" {
                obj.request_charged = try!(RequestChargedParser::parse_xml("x-amz-request-charged", stack));
                continue;
            }
            break;
        }
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}

impl GetObjectAclOutputWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &GetObjectAclOutput) {
        let mut prefix = name.to_string();
        if prefix != "" { prefix.push_str("."); }
        OwnerWriter::write_params(params, &(prefix.to_string() + "Owner"), &obj.owner);
        GrantsWriter::write_params(params, &(prefix.to_string() + "Grant"), &obj.grants);
        RequestChargedWriter::write_params(params, &(prefix.to_string() + "x-amz-request-charged"), &obj.request_charged);
    }
}

impl PutObjectAclOutputParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<PutObjectAclOutput, XmlParseError> {
        try!(start_element(tag_name, stack));
        let mut obj = PutObjectAclOutput::default();
        loop {
            let current_name = try!(peek_at_name(stack));
            if current_name == "x-amz-request-charged" {
                obj.request_charged = try!(RequestChargedParser::parse_xml("x-amz-request-charged", stack));
                continue;
            }
            break;
        }
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}

impl PutObjectAclOutputWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &PutObjectAclOutput) {
        let mut prefix = name.to_string();
        if prefix != "" { prefix.push_str("."); }
        RequestChargedWriter::write_params(params, &(prefix.to_string() + "x-amz-request-charged"), &obj.request_charged);
    }
}